Skip to content
Open
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
Expand Up @@ -49,7 +49,7 @@ public void initFromPage(int valueCount, ByteBufferInputStream stream) throws IO

@Override
public Binary readBytes() {
int length = lengthReader.readInteger();
int length = readLength();
try {
return Binary.fromConstantByteBuffer(in.slice(length));
} catch (IOException e) {
Expand All @@ -64,14 +64,22 @@ public void skip() {

@Override
public void skip(int n) {
int length = 0;
long length = 0;
for (int i = 0; i < n; ++i) {
length += lengthReader.readInteger();
length += readLength();
}
try {
in.skipFully(length);
} catch (IOException e) {
throw new ParquetDecodingException("Failed to skip " + length + " bytes");
}
}

private int readLength() {
int length = lengthReader.readInteger();
if (length < 0) {
throw new ParquetDecodingException("Invalid DELTA_LENGTH_BYTE_ARRAY value length: " + length);
}
return length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@
package org.apache.parquet.column.values.deltalengthbytearray;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import org.apache.parquet.bytes.ByteBufferInputStream;
import org.apache.parquet.bytes.BytesInput;
import org.apache.parquet.bytes.DirectByteBufferAllocator;
import org.apache.parquet.column.values.Utils;
import org.apache.parquet.column.values.ValuesReader;
import org.apache.parquet.column.values.delta.DeltaBinaryPackingValuesReader;
import org.apache.parquet.column.values.delta.DeltaBinaryPackingValuesWriterForInteger;
import org.apache.parquet.io.ParquetDecodingException;
import org.apache.parquet.io.api.Binary;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -99,4 +106,49 @@ public void testLengths() throws IOException {
assertThat(bin[i]).isEqualTo(values[i].length());
}
}

@Test
public void testNegativeLength() throws IOException {
DeltaLengthByteArrayValuesReader reader = readerFor("abcd", 2, -2, 2);

assertThat(reader.readBytes()).isEqualTo(Binary.fromString("ab"));
assertThatThrownBy(reader::readBytes)
.isInstanceOf(ParquetDecodingException.class)
.hasMessage("Invalid DELTA_LENGTH_BYTE_ARRAY value length: -2");
}

@Test
public void testSkipNegativeLength() throws IOException {
DeltaLengthByteArrayValuesReader reader = readerFor("abcd", 2, -2, 2);

assertThatThrownBy(() -> reader.skip(3))
.isInstanceOf(ParquetDecodingException.class)
.hasMessage("Invalid DELTA_LENGTH_BYTE_ARRAY value length: -2");
}

@Test
public void testSkipLengthsOverflowingInt() throws IOException {
DeltaLengthByteArrayValuesReader reader = readerFor("abcd", Integer.MAX_VALUE, Integer.MAX_VALUE);

assertThatThrownBy(() -> reader.skip(2))
.isInstanceOf(ParquetDecodingException.class)
.hasMessage("Failed to skip 4294967294 bytes");
}

private static DeltaLengthByteArrayValuesReader readerFor(String data, int... lengths) throws IOException {
DeltaLengthByteArrayValuesReader reader = new DeltaLengthByteArrayValuesReader();
reader.initFromPage(lengths.length, pageWithLengths(data, lengths));
return reader;
}

private static ByteBufferInputStream pageWithLengths(String data, int... lengths) throws IOException {
DeltaBinaryPackingValuesWriterForInteger lengthWriter =
new DeltaBinaryPackingValuesWriterForInteger(64, 64, new DirectByteBufferAllocator());
for (int length : lengths) {
lengthWriter.writeInteger(length);
}
byte[] page = BytesInput.concat(lengthWriter.getBytes(), BytesInput.from(data.getBytes(StandardCharsets.UTF_8)))
.toByteArray();
return ByteBufferInputStream.wrap(ByteBuffer.wrap(page));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@
package org.apache.parquet.column.values.deltastrings;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import org.apache.parquet.bytes.ByteBufferInputStream;
import org.apache.parquet.bytes.BytesInput;
import org.apache.parquet.bytes.DirectByteBufferAllocator;
import org.apache.parquet.column.values.Utils;
import org.apache.parquet.column.values.ValuesReader;
import org.apache.parquet.column.values.delta.DeltaBinaryPackingValuesReader;
import org.apache.parquet.column.values.delta.DeltaBinaryPackingValuesWriterForInteger;
import org.apache.parquet.io.ParquetDecodingException;
import org.apache.parquet.io.api.Binary;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -150,4 +155,28 @@ public void testReusedBackingArrayRegression() throws Exception {
assertThat(decoded[1]).isEqualTo(Binary.fromString("parquet-111"));
assertThat(decoded[2]).isEqualTo(Binary.fromString("parquet-222"));
}

@Test
public void testNegativeSuffixLength() throws IOException {
DeltaBinaryPackingValuesWriterForInteger prefixLengths =
new DeltaBinaryPackingValuesWriterForInteger(64, 64, new DirectByteBufferAllocator());
DeltaBinaryPackingValuesWriterForInteger suffixLengths =
new DeltaBinaryPackingValuesWriterForInteger(64, 64, new DirectByteBufferAllocator());
for (int length : new int[] {2, -2, 2}) {
prefixLengths.writeInteger(0);
suffixLengths.writeInteger(length);
}
byte[] page = BytesInput.concat(
prefixLengths.getBytes(),
suffixLengths.getBytes(),
BytesInput.from("abcd".getBytes(StandardCharsets.UTF_8)))
.toByteArray();
DeltaByteArrayReader reader = new DeltaByteArrayReader();
reader.initFromPage(3, ByteBufferInputStream.wrap(ByteBuffer.wrap(page)));

assertThat(reader.readBytes()).isEqualTo(Binary.fromString("ab"));
assertThatThrownBy(reader::readBytes)
.isInstanceOf(ParquetDecodingException.class)
.hasMessage("Invalid DELTA_LENGTH_BYTE_ARRAY value length: -2");
}
}