From 2e0c5e84c391b99cc4852d4a7908eae36e1620a5 Mon Sep 17 00:00:00 2001 From: NopeCharon <115794254+NopeCharon@users.noreply.github.com> Date: Mon, 26 Oct 2026 15:24:27 +0530 Subject: [PATCH] GH-2270: Accept any GenericFixed in fixed logical type conversions ParentValueContainer.getConversionContainer cast fixed values to GenericData.Fixed before handing them to Conversion#fromFixed. When reading with a SpecificData model, the fixed converters instantiate the generated class for a named fixed schema, which extends SpecificFixed and is not a GenericData.Fixed. Reading a specific record with a logical type on such a fixed, e.g. a decimal compiled with enableDecimalLogicalType, therefore failed with: ClassCastException: class ...Decimal9Fixed cannot be cast to class org.apache.avro.generic.GenericData$Fixed Cast to GenericFixed, the type Conversion#fromFixed accepts and the interface both implementations share. Add a generated test record with a decimal on a named fixed type, compile test schemas with enableDecimalLogicalType so its SpecificData model carries the decimal conversion, and round-trip it through AvroParquetWriter and AvroParquetReader with and without compatibility mode. Both cases failed with the exception above before this change. Signed-off-by: NopeCharon <115794254+NopeCharon@users.noreply.github.com> --- parquet-avro/pom.xml | 3 ++ .../parquet/avro/ParentValueContainer.java | 4 +- parquet-avro/src/test/avro/decimalFixed.avsc | 19 ++++++++++ .../parquet/avro/TestSpecificReadWrite.java | 38 +++++++++++++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 parquet-avro/src/test/avro/decimalFixed.avsc diff --git a/parquet-avro/pom.xml b/parquet-avro/pom.xml index 2db3e55c2d..c453eaff9a 100644 --- a/parquet-avro/pom.xml +++ b/parquet-avro/pom.xml @@ -154,6 +154,9 @@ schema + + true + diff --git a/parquet-avro/src/main/java/org/apache/parquet/avro/ParentValueContainer.java b/parquet-avro/src/main/java/org/apache/parquet/avro/ParentValueContainer.java index f7f5125749..ec521a652e 100644 --- a/parquet-avro/src/main/java/org/apache/parquet/avro/ParentValueContainer.java +++ b/parquet-avro/src/main/java/org/apache/parquet/avro/ParentValueContainer.java @@ -24,8 +24,8 @@ import org.apache.avro.Conversion; import org.apache.avro.LogicalType; import org.apache.avro.Schema; -import org.apache.avro.generic.GenericData; import org.apache.avro.generic.GenericEnumSymbol; +import org.apache.avro.generic.GenericFixed; import org.apache.avro.generic.IndexedRecord; abstract class ParentValueContainer { @@ -185,7 +185,7 @@ public void add(Object value) { return new ParentValueContainer() { @Override public void add(Object value) { - parent.add(conversion.fromFixed((GenericData.Fixed) value, schema, logicalType)); + parent.add(conversion.fromFixed((GenericFixed) value, schema, logicalType)); } }; case RECORD: diff --git a/parquet-avro/src/test/avro/decimalFixed.avsc b/parquet-avro/src/test/avro/decimalFixed.avsc new file mode 100644 index 0000000000..7e501e1d1c --- /dev/null +++ b/parquet-avro/src/test/avro/decimalFixed.avsc @@ -0,0 +1,19 @@ +{ + "name": "DecimalFixedTest", + "namespace": "org.apache.parquet.avro", + "doc": "Record for testing a decimal logical type on a named fixed type", + "type": "record", + "fields": [ + { + "name": "amount", + "type": { + "name": "Decimal9Fixed", + "type": "fixed", + "size": 4, + "logicalType": "decimal", + "precision": 9, + "scale": 2 + } + } + ] +} diff --git a/parquet-avro/src/test/java/org/apache/parquet/avro/TestSpecificReadWrite.java b/parquet-avro/src/test/java/org/apache/parquet/avro/TestSpecificReadWrite.java index 91aa29d36e..97953dc731 100644 --- a/parquet-avro/src/test/java/org/apache/parquet/avro/TestSpecificReadWrite.java +++ b/parquet-avro/src/test/java/org/apache/parquet/avro/TestSpecificReadWrite.java @@ -27,6 +27,7 @@ import com.google.common.collect.ImmutableList; import java.io.File; import java.io.IOException; +import java.math.BigDecimal; import java.time.Instant; import java.time.LocalDate; import java.time.LocalTime; @@ -339,6 +340,43 @@ public void testParsesSpecificDataModel(boolean compat) throws IOException { assertThat(output).containsExactlyElementsOf(records); } + @ParameterizedTest + @ValueSource(booleans = {false, true}) + public void testReadSpecificDecimalFixed(boolean compat) throws IOException { + Configuration testConf = testConf(compat); + // The decimal is stored in the generated Decimal9Fixed class, which is a SpecificFixed and not a + // GenericData.Fixed, so the conversion must accept any GenericFixed + List records = IntStream.range(0, 25) + .mapToObj(i -> DecimalFixedTest.newBuilder() + .setAmount(BigDecimal.valueOf(i * 1000 - 12345, 2)) + .build()) + .collect(Collectors.toList()); + + File tmp = File.createTempFile(getClass().getSimpleName(), ".tmp"); + tmp.deleteOnExit(); + tmp.delete(); + Path path = new Path(tmp.getPath()); + + try (ParquetWriter writer = AvroParquetWriter.builder(path) + .withSchema(DecimalFixedTest.getClassSchema()) + .withConf(new Configuration(false)) + .withCompressionCodec(CompressionCodecName.UNCOMPRESSED) + .build()) { + for (DecimalFixedTest record : records) { + writer.write(record); + } + } + + final List output = new ArrayList<>(); + try (ParquetReader reader = new AvroParquetReader<>(testConf, path)) { + for (DecimalFixedTest record = reader.read(); record != null; record = reader.read()) { + output.add(record); + } + } + + assertThat(output).containsExactlyElementsOf(records); + } + private Path writeCarsToParquetFile(int num, CompressionCodecName compression, boolean enableDictionary) throws IOException { return writeCarsToParquetFile(num, compression, enableDictionary, DEFAULT_BLOCK_SIZE, DEFAULT_PAGE_SIZE);