Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.parquet;

import org.apache.parquet.VersionParser.ParsedVersion;
import org.apache.parquet.column.Encoding;

public class CorruptDeltaByteArrays {
private static final Log LOG = Log.getLog(CorruptStatistics.class);

private static final SemanticVersion PARQUET_246_FIXED_VERSION =
new SemanticVersion(1, 8, 0);

public static boolean requiresSequentialReads(ParsedVersion version, Encoding encoding) {
if (encoding != Encoding.DELTA_BYTE_ARRAY) {
return false;
}

if (version == null) {
return true;
}

if (!"parquet-mr".equals(version.application)) {
// assume other applications don't have this bug
return false;
}

if (!version.hasSemanticVersion()) {
LOG.warn("Requiring sequential reads because created_by did not " +
"contain a valid version (see PARQUET-246): " + version.version);
return true;
}

return requiresSequentialReads(version.getSemanticVersion(), encoding);
}

public static boolean requiresSequentialReads(SemanticVersion semver, Encoding encoding) {
if (encoding != Encoding.DELTA_BYTE_ARRAY) {
return false;
}

if (semver == null) {
return true;
}

if (semver.compareTo(PARQUET_246_FIXED_VERSION) < 0) {
LOG.info("Requiring sequential reads because this file was created " +
"prior to " + PARQUET_246_FIXED_VERSION + ". See PARQUET-246" );
return true;
}

// this file was created after the fix
return false;
}

public static boolean requiresSequentialReads(String createdBy, Encoding encoding) {
if (encoding != Encoding.DELTA_BYTE_ARRAY) {
return false;
}

if (Strings.isNullOrEmpty(createdBy)) {
LOG.info("Requiring sequential reads because file version is empty. " +
"See PARQUET-246");
return true;
}

try {
return requiresSequentialReads(VersionParser.parse(createdBy), encoding);

} catch (RuntimeException e) {
warnParseError(createdBy, e);
return true;
} catch (VersionParser.VersionParseException e) {
warnParseError(createdBy, e);
return true;
}
}

private static void warnParseError(String createdBy, Throwable e) {
LOG.warn("Requiring sequential reads because created_by could not be " +
"parsed (see PARQUET-246): " + createdBy, e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ public static boolean shouldIgnoreStatistics(String createdBy, PrimitiveTypeName
return false;
}

if (Strings.isNullOrEmpty(version.semver)) {
if (Strings.isNullOrEmpty(version.version)) {
LOG.warn("Ignoring statistics because created_by did not contain a semver (see PARQUET-251): " + createdBy);
return true;
}

SemanticVersion semver = SemanticVersion.parse(version.semver);
SemanticVersion semver = SemanticVersion.parse(version.version);

if (semver.compareTo(PARQUET_251_FIXED_VERSION) < 0) {
LOG.info("Ignoring statistics because this file was created prior to "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package org.apache.parquet.column.impl;

import org.apache.parquet.VersionParser;
import org.apache.parquet.VersionParser.ParsedVersion;
import org.apache.parquet.VersionParser.VersionParseException;
import org.apache.parquet.column.ColumnDescriptor;
import org.apache.parquet.column.ColumnReadStore;
import org.apache.parquet.column.ColumnReader;
Expand All @@ -43,17 +46,30 @@ public class ColumnReadStoreImpl implements ColumnReadStore {
private final PageReadStore pageReadStore;
private final GroupConverter recordConverter;
private final MessageType schema;
private final ParsedVersion writerVersion;

/**
* @param pageReadStore uderlying page storage
* @param pageReadStore underlying page storage
* @param recordConverter the user provided converter to materialize records
* @param schema the schema we are reading
*/
public ColumnReadStoreImpl(PageReadStore pageReadStore, GroupConverter recordConverter, MessageType schema) {
public ColumnReadStoreImpl(PageReadStore pageReadStore,
GroupConverter recordConverter,
MessageType schema, String createdBy) {
super();
this.pageReadStore = pageReadStore;
this.recordConverter = recordConverter;
this.schema = schema;

ParsedVersion version;
try {
version = VersionParser.parse(createdBy);
} catch (RuntimeException e) {
version = null;
} catch (VersionParseException e) {
version = null;
}
this.writerVersion = version;
}

@Override
Expand All @@ -63,7 +79,7 @@ public ColumnReader getColumnReader(ColumnDescriptor path) {

private ColumnReaderImpl newMemColumnReader(ColumnDescriptor path, PageReader pageReader) {
PrimitiveConverter converter = getPrimitiveConverter(path);
return new ColumnReaderImpl(path, pageReader, converter);
return new ColumnReaderImpl(path, pageReader, converter, writerVersion);
}

private PrimitiveConverter getPrimitiveConverter(ColumnDescriptor path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;

import org.apache.parquet.CorruptDeltaByteArrays;
import org.apache.parquet.Log;
import org.apache.parquet.VersionParser.ParsedVersion;
import org.apache.parquet.bytes.BytesInput;
import org.apache.parquet.bytes.BytesUtils;
import org.apache.parquet.column.ColumnDescriptor;
Expand All @@ -40,6 +42,7 @@
import org.apache.parquet.column.page.DataPageV2;
import org.apache.parquet.column.page.DictionaryPage;
import org.apache.parquet.column.page.PageReader;
import org.apache.parquet.column.values.RequiresPreviousReader;
import org.apache.parquet.column.values.ValuesReader;
import org.apache.parquet.column.values.rle.RunLengthBitPackingHybridDecoder;
import org.apache.parquet.io.ParquetDecodingException;
Expand Down Expand Up @@ -130,6 +133,7 @@ public double getDouble() {
}
}

private final ParsedVersion writerVersion;
private final ColumnDescriptor path;
private final long totalValueCount;
private final PageReader pageReader;
Expand All @@ -138,6 +142,7 @@ public double getDouble() {
private IntIterator repetitionLevelColumn;
private IntIterator definitionLevelColumn;
protected ValuesReader dataColumn;
private Encoding currentEncoding;

private int repetitionLevel;
private int definitionLevel;
Expand Down Expand Up @@ -327,10 +332,11 @@ void writeValue() {
* @param path the descriptor for the corresponding column
* @param pageReader the underlying store to read from
*/
public ColumnReaderImpl(ColumnDescriptor path, PageReader pageReader, PrimitiveConverter converter) {
public ColumnReaderImpl(ColumnDescriptor path, PageReader pageReader, PrimitiveConverter converter, ParsedVersion writerVersion) {
this.path = checkNotNull(path, "path");
this.pageReader = checkNotNull(pageReader, "pageReader");
this.converter = checkNotNull(converter, "converter");
this.writerVersion = writerVersion;
DictionaryPage dictionaryPage = pageReader.readDictionaryPage();
if (dictionaryPage != null) {
try {
Expand Down Expand Up @@ -459,10 +465,28 @@ public void readValue() {
valueRead = true;
}
} catch (RuntimeException e) {
if (CorruptDeltaByteArrays.requiresSequentialReads(writerVersion, currentEncoding) &&
e instanceof ArrayIndexOutOfBoundsException) {
// this is probably PARQUET-246, which may happen if reading data with
// MR because this can't be detected without reading all footers
throw new ParquetDecodingException("Read failure possibly due to " +
"PARQUET-246: try setting parquet.split.files to false",
new ParquetDecodingException(
format("Can't read value in column %s at value %d out of %d, " +
"%d out of %d in currentPage. repetition level: " +
"%d, definition level: %d",
path, readValues, totalValueCount,
readValues - (endOfPageValueCount - pageValueCount),
pageValueCount, repetitionLevel, definitionLevel),
e));
}
throw new ParquetDecodingException(
format(
"Can't read value in column %s at value %d out of %d, %d out of %d in currentPage. repetition level: %d, definition level: %d",
path, readValues, totalValueCount, readValues - (endOfPageValueCount - pageValueCount), pageValueCount, repetitionLevel, definitionLevel),
format("Can't read value in column %s at value %d out of %d, " +
"%d out of %d in currentPage. repetition level: " +
"%d, definition level: %d",
path, readValues, totalValueCount,
readValues - (endOfPageValueCount - pageValueCount),
pageValueCount, repetitionLevel, definitionLevel),
e);
}
}
Expand Down Expand Up @@ -525,8 +549,12 @@ public Void visit(DataPageV2 dataPageV2) {
}

private void initDataReader(Encoding dataEncoding, byte[] bytes, int offset, int valueCount) {
ValuesReader previousReader = this.dataColumn;

this.currentEncoding = dataEncoding;
this.pageValueCount = valueCount;
this.endOfPageValueCount = readValues + pageValueCount;

if (dataEncoding.usesDictionary()) {
if (dictionary == null) {
throw new ParquetDecodingException(
Expand All @@ -546,6 +574,12 @@ private void initDataReader(Encoding dataEncoding, byte[] bytes, int offset, int
} catch (IOException e) {
throw new ParquetDecodingException("could not read page in col " + path, e);
}

if (CorruptDeltaByteArrays.requiresSequentialReads(writerVersion, dataEncoding) &&
previousReader != null && previousReader instanceof RequiresPreviousReader) {
// previous reader can only be set if reading sequentially
((RequiresPreviousReader) dataColumn).setPreviousReader(previousReader);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a better way than doing the type casting here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want this to be a normal part of the API, so using an interface like this to carry the method is a good practice. This also allows the reader to expose just the one method and not expose its internal state.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline. If we wanna make it general, we should make encoding to provide a SequentialReader rather than type casting the Reader.
If we wanna make it not that generic, we should not let the "requiresSequentialReads" be part of the normal Encoding's API

}
}

private void readPageV1(DataPageV1 page) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.parquet.column.values;

public interface RequiresPreviousReader {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this interface/generalization is actually needed.
The actual problem is the DeltaByteArrayReader needs to reset the 'previous' member to fix the bug.
It needs the previous char array, the previous value, not the previous reader. This interface gives an impression the subclass could hold a reference of the previous reader.

How about:
remove this interface,
create a setPreviousValue method in DeltaByteArrayReader.
and call:
setPreviousValue(deltaByteArrayReader.previous)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous is internal state. This method avoids exposing that state to others, allowing access to all private members in the recipient ValuesReader.

void setPreviousReader(ValuesReader reader);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.IOException;

import org.apache.parquet.column.values.RequiresPreviousReader;
import org.apache.parquet.column.values.ValuesReader;
import org.apache.parquet.column.values.delta.DeltaBinaryPackingValuesReader;
import org.apache.parquet.column.values.deltalengthbytearray.DeltaLengthByteArrayValuesReader;
Expand All @@ -31,7 +32,7 @@
* @author Aniket Mokashi
*
*/
public class DeltaByteArrayReader extends ValuesReader {
public class DeltaByteArrayReader extends ValuesReader implements RequiresPreviousReader {
private ValuesReader prefixLengthReader;
private ValuesReader suffixReader;

Expand Down Expand Up @@ -63,7 +64,13 @@ 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.

// We have to do this to materialize the output
if(prefixLength != 0) {
byte[] out = new byte[length];
Expand All @@ -75,4 +82,18 @@ public Binary readBytes() {
}
return previous;
}

/**
* 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 recover the previous page's last value to use it (if needed) to
* read the first value.
*/
public void setPreviousReader(ValuesReader reader) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the fix is actually setPreviousValue, not setPreviousReader

if (reader != null) {
this.previous = ((DeltaByteArrayReader) reader).previous;
}
}
}
Loading