diff --git a/parquet-column/src/main/java/org/apache/parquet/column/Encoding.java b/parquet-column/src/main/java/org/apache/parquet/column/Encoding.java index 9770044728..d13bc1d4d0 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/Encoding.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/Encoding.java @@ -48,6 +48,7 @@ import org.apache.parquet.column.values.plain.PlainValuesReader.LongPlainValuesReader; import org.apache.parquet.column.values.rle.RunLengthBitPackingHybridValuesReader; import org.apache.parquet.io.ParquetDecodingException; +import org.apache.parquet.io.api.Binary; /** * encoding of the data @@ -197,6 +198,59 @@ public ValuesReader getValuesReader(ColumnDescriptor descriptor, } return new DeltaByteArrayReader(); } + + /** + * There was a bug (PARQUET-246) in which DeltaByteArrayWriter's reset() method did not + * clear the previous value state that it tracks internally. This resulted in the first + * value of all pages (except for the first page) to be a delta from the last value of the + * previous page. In order to read corrupted files written with this bug, when reading a + * new page we need to pass the previous page's last value to the DeltaByteArrayReader so + * it can use it (if needed) to read the first value. + * + * The DeltaByteArrayReader can determine from the data it is reading whether this bug was + * in effect when the data was written. So it is safe to always pass along the previous + * page's last value to a new reader, and let the reader decide for itself whether or not + * to use it. + */ + @Override + public CrossPageStateRepairer newCrossPageStateRepairer() { + return new CrossPageStateRepairer() { + + // here we track the last value of the last page to be written + // with delta byte array encoding. (there could, in theory, be + // pages in between that were written with a different encoding, + // such as dictionary encoding, but we only care about the last + // delta byte array encoded value to be read. + private Binary previousDeltaByteArrayPagesLastValue = null; + + @Override + public void onBeginReadNewPage(ValuesReader previousReader, ValuesReader newReader) { + if (previousReader == null) { + // this is the first page, so this page is not + // corrupted even if written with the buggy version of parquet + return; + } + + if (previousReader instanceof DeltaByteArrayReader) { + // we only want to track the previous values of delta byte array encoded + // pages, so we skip over pages that don't use this encoding. + + // TODO: should be getPrevious().copy() once the updates to Binary are merged + // as is, this is safe because previous is in DeltaByteArrayReader does not use + // a re-used byte array + previousDeltaByteArrayPagesLastValue = ((DeltaByteArrayReader) previousReader).getPrevious(); + } + + if (newReader instanceof DeltaByteArrayReader && previousDeltaByteArrayPagesLastValue != null) { + // the new page is a delta byte array encoded page, and we have previously seen a + // delta byte array encoded page, so we cary the previous value over. + // There may have been pages of different encodings between, but that's OK because + // we did not update previousDeltaByteArrayPagesLastValue when we saw them. + ((DeltaByteArrayReader) newReader).setPrevious(previousDeltaByteArrayPagesLastValue); + } + } + }; + } }, /** @@ -288,4 +342,26 @@ public ValuesReader getDictionaryBasedValuesReader(ColumnDescriptor descriptor, throw new UnsupportedOperationException(this.name() + " is not dictionary based"); } + public CrossPageStateRepairer newCrossPageStateRepairer() { + // default is to do nothing + return CrossPageStateRepairer.NONE; + } + + /** + * This defines a callback to be called every time a {@link ColumnReader} + * begins reading a new page, within the same row-group, allowing us to carry state over from + * one page to the next. This is used to fix a bug where state was not + * cleared between pages in the write path (PARQUET-246). + * see DELTA_BYTE_ARRAY#newCrossPageStateRepairer for more details. + */ + public static interface CrossPageStateRepairer { + + // default is to do nothing + public static final CrossPageStateRepairer NONE = new CrossPageStateRepairer() { + @Override + public void onBeginReadNewPage(ValuesReader previousReader, ValuesReader newReader) { } + }; + + void onBeginReadNewPage(ValuesReader previousReader, ValuesReader newReader); + } } diff --git a/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnReaderImpl.java b/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnReaderImpl.java index 2fa63a8f13..a20a15f8a2 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnReaderImpl.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnReaderImpl.java @@ -27,6 +27,8 @@ import java.io.ByteArrayInputStream; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import org.apache.parquet.Log; import org.apache.parquet.bytes.BytesInput; @@ -35,6 +37,7 @@ import org.apache.parquet.column.ColumnReader; import org.apache.parquet.column.Dictionary; import org.apache.parquet.column.Encoding; +import org.apache.parquet.column.Encoding.CrossPageStateRepairer; import org.apache.parquet.column.page.DataPage; import org.apache.parquet.column.page.DataPageV1; import org.apache.parquet.column.page.DataPageV2; @@ -135,6 +138,9 @@ public double getDouble() { private final PageReader pageReader; private final Dictionary dictionary; + private final Map crossPageStateRepairers = + new HashMap(); + private IntIterator repetitionLevelColumn; private IntIterator definitionLevelColumn; protected ValuesReader dataColumn; @@ -525,8 +531,11 @@ public Void visit(DataPageV2 dataPageV2) { } private void initDataReader(Encoding dataEncoding, byte[] bytes, int offset, int valueCount) { + ValuesReader previousReader = this.dataColumn; + this.pageValueCount = valueCount; this.endOfPageValueCount = readValues + pageValueCount; + if (dataEncoding.usesDictionary()) { if (dictionary == null) { throw new ParquetDecodingException( @@ -546,6 +555,20 @@ private void initDataReader(Encoding dataEncoding, byte[] bytes, int offset, int } catch (IOException e) { throw new ParquetDecodingException("could not read page in col " + path, e); } + + addRepairerIfAbsent(dataEncoding); + + for (CrossPageStateRepairer repairer : crossPageStateRepairers.values()) { + repairer.onBeginReadNewPage(previousReader, dataColumn); + } + } + + private void addRepairerIfAbsent(Encoding encoding) { + CrossPageStateRepairer repairer = crossPageStateRepairers.get(encoding); + if (repairer == null) { + repairer = encoding.newCrossPageStateRepairer(); + crossPageStateRepairers.put(encoding, repairer); + } } private void readPageV1(DataPageV1 page) { diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/deltastrings/DeltaByteArrayReader.java b/parquet-column/src/main/java/org/apache/parquet/column/values/deltastrings/DeltaByteArrayReader.java index de3df025c5..f3b326eb99 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/deltastrings/DeltaByteArrayReader.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/deltastrings/DeltaByteArrayReader.java @@ -63,7 +63,14 @@ public Binary readBytes() { // This does not copy bytes Binary suffix = suffixReader.readBytes(); int length = prefixLength + suffix.length(); - + + // NOTE: due to PARQUET-246, it is important that we + // respect prefixLength which was read from prefixLengthReader, + // even for the *first* value of a page. Even though the first + // value of the page should have an empty prefix, it may not + // because of PARQUET-246. See org.apache.parquet.column.Encoding.newCrossPageStateRepairer() + // for more details. + // We have to do this to materialize the output if(prefixLength != 0) { byte[] out = new byte[length]; @@ -75,4 +82,16 @@ public Binary readBytes() { } return previous; } + + // NOTE, this is exposed because of PARQUET-246 + // See org.apache.parquet.column.Encoding.newCrossPageStateRepairer() + public Binary getPrevious() { + return previous; + } + + // NOTE, this is exposed because of PARQUET-246 + // See org.apache.parquet.column.Encoding.newCrossPageStateRepairer() + public void setPrevious(Binary previous) { + this.previous = previous; + } }