diff --git a/parquet-column/src/main/java/org/apache/parquet/io/RecordReaderImplementation.java b/parquet-column/src/main/java/org/apache/parquet/io/RecordReaderImplementation.java index 46c4b714a2..a20f0922ef 100644 --- a/parquet-column/src/main/java/org/apache/parquet/io/RecordReaderImplementation.java +++ b/parquet-column/src/main/java/org/apache/parquet/io/RecordReaderImplementation.java @@ -240,6 +240,8 @@ public State getNextState(int nextR) { private final RecordMaterializer recordMaterializer; private State[] states; + // The states of the repeated columns, which must be at repetition level 0 when a record starts + private State[] repeatedStates; private ColumnReader[] columnReaders; private boolean shouldSkipCurrentRecord = false; @@ -338,6 +340,9 @@ public RecordReaderImplementation( } states[i].definitionLevelToDepth = definitionLevelToDepth; } + repeatedStates = Arrays.stream(states) + .filter(state -> state.maxRepetitionLevel > 0) + .toArray(State[]::new); for (int i = 0; i < leaves.length; i++) { State state = states[i]; int[] nextStateIds = nextColumnIdxForRepLevel[i]; @@ -407,6 +412,16 @@ private RecordConsumer wrap(RecordConsumer recordConsumer) { */ @Override public T read() { + for (State state : repeatedStates) { + int r = state.column.getCurrentRepetitionLevel(); + if (r != 0) { + throw new ParquetDecodingException("Invalid repetition level " + r + + " at the start of a record in column " + + Arrays.toString( + state.primitiveColumnIO.getColumnDescriptor().getPath()) + + ": a record must start with repetition level 0"); + } + } int currentLevel = 0; recordRootConverter.start(); State currentState = states[0]; diff --git a/parquet-column/src/test/java/org/apache/parquet/io/TestColumnIO.java b/parquet-column/src/test/java/org/apache/parquet/io/TestColumnIO.java index 508e58267b..5a042a696f 100644 --- a/parquet-column/src/test/java/org/apache/parquet/io/TestColumnIO.java +++ b/parquet-column/src/test/java/org/apache/parquet/io/TestColumnIO.java @@ -32,17 +32,24 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assumptions.assumeThat; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; +import org.apache.parquet.bytes.BytesInput; +import org.apache.parquet.bytes.HeapByteBufferAllocator; import org.apache.parquet.column.ColumnDescriptor; import org.apache.parquet.column.ColumnWriteStore; import org.apache.parquet.column.ColumnWriter; +import org.apache.parquet.column.Encoding; import org.apache.parquet.column.ParquetProperties; import org.apache.parquet.column.impl.ColumnWriteStoreV1; import org.apache.parquet.column.page.PageReadStore; import org.apache.parquet.column.page.mem.MemPageStore; +import org.apache.parquet.column.statistics.Statistics; +import org.apache.parquet.column.values.plain.PlainValuesWriter; +import org.apache.parquet.column.values.rle.RunLengthBitPackingHybridValuesWriter; import org.apache.parquet.example.data.Group; import org.apache.parquet.example.data.GroupFactory; import org.apache.parquet.example.data.GroupWriter; @@ -229,6 +236,41 @@ public void testReadUsingSchemaWithRequiredFieldThatWasOptional() { "The requested schema is not compatible with the file schema. incompatible types: required int32 e != optional int32 e"); } + @Test + public void testReadFailsOnRecordStartingWithNonZeroRepetitionLevel() throws IOException { + MessageType listSchema = MessageTypeParser.parseMessageType( + "message schema { required group x (LIST) { repeated group list { required int32 element; } } }"); + ColumnDescriptor column = listSchema.getColumns().get(0); + // Like parquet-testing's bad_data/ARROW-GH-45185.parquet: 5 records, 10 values with the repetition + // levels 1, 0, 1, 0, ..., so the first record starts with repetition level 1. The page is written + // directly, since the column writers don't write such pages. + RunLengthBitPackingHybridValuesWriter repetitionLevels = + new RunLengthBitPackingHybridValuesWriter(1, 64, 64, new HeapByteBufferAllocator()); + RunLengthBitPackingHybridValuesWriter definitionLevels = + new RunLengthBitPackingHybridValuesWriter(1, 64, 64, new HeapByteBufferAllocator()); + PlainValuesWriter values = new PlainValuesWriter(64, 64, new HeapByteBufferAllocator()); + for (int i = 0; i < 10; i++) { + repetitionLevels.writeInteger(i % 2 == 0 ? 1 : 0); + definitionLevels.writeInteger(1); + values.writeInteger(i); + } + MemPageStore store = new MemPageStore(5); + store.getPageWriter(column) + .writePage( + BytesInput.concat(repetitionLevels.getBytes(), definitionLevels.getBytes(), values.getBytes()), + 10, + 5, + Statistics.createStats(column.getPrimitiveType()), + Encoding.RLE, + Encoding.RLE, + Encoding.PLAIN); + + assertThatThrownBy(() -> readGroups(store, listSchema, listSchema, 5)) + .isInstanceOf(ParquetDecodingException.class) + .hasMessage("Invalid repetition level 1 at the start of a record in column [x, list, element]: " + + "a record must start with repetition level 0"); + } + @Test public void testReadUsingProjectedSchema() { MessageType orginalSchema = new MessageType(