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 @@ -45,6 +45,7 @@
import org.apache.parquet.column.values.dictionary.DictionaryValuesWriter.PlainFloatDictionaryValuesWriter;
import org.apache.parquet.column.values.dictionary.DictionaryValuesWriter.PlainIntegerDictionaryValuesWriter;
import org.apache.parquet.column.values.dictionary.DictionaryValuesWriter.PlainLongDictionaryValuesWriter;
import org.apache.parquet.column.values.dictionary.PlainValuesDictionary.PlainBinaryDictionary;
import org.apache.parquet.column.values.dictionary.PlainValuesDictionary.PlainBooleanDictionary;
import org.apache.parquet.column.values.fallback.FallbackValuesWriter;
import org.apache.parquet.column.values.plain.BinaryPlainValuesReader;
Expand Down Expand Up @@ -803,6 +804,17 @@ public void testZeroValues() throws IOException {
}
}

@Test
public void testDictionaryPageCopyDoesNotAliasSourceBytes() throws IOException {
byte[] source = {1, 0, 0, 0, 'a'};
DictionaryPage copied = new DictionaryPage(BytesInput.from(source), 1, PLAIN).copy();

source[4] = 'b';

PlainBinaryDictionary dictionary = new PlainBinaryDictionary(copied);
assertThat(dictionary.decodeToBinary(0).toStringUsingUTF8()).isEqualTo("a");
}

@Test
public void testBooleanDictionary() throws IOException {
// Create a dictionary page with boolean values (false, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -690,18 +690,9 @@ public ByteBuffer toByteBuffer() throws IOException {
return java.nio.ByteBuffer.wrap(in, offset, length);
}

/**
* Zero-copy override: returns the backing array directly when fully used,
* skipping the base-class BAOS allocation + copy on every decompressor call.
* Returning the mutable array is safe — the base class already exposes a
* mutable {@code BAOS.getBuf()}.

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.

I think Returning the mutable array is safe is the key. The array was mutable before, but it was a mutable copy of the original array.

*/
@SuppressWarnings("deprecation")
@Override
public byte[] toByteArray() {
if (offset == 0 && length == in.length) {
return in;
}
return Arrays.copyOfRange(in, offset, offset + length);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,16 @@ public void testFromByteArray(ByteBufferAllocator innerAllocator) throws IOExcep

@ParameterizedTest(name = "{0}")
@MethodSource("parameters")
public void testFromByteArrayToByteArrayZeroCopy(ByteBufferAllocator innerAllocator) throws IOException {
public void testFromByteArrayToByteArrayCopiesFullArray(ByteBufferAllocator innerAllocator) throws IOException {
initAllocator(innerAllocator);
// Full array (offset=0, length=array.length): toByteArray() returns the backing array directly
byte[] data = new byte[1000];
RANDOM.nextBytes(data);
BytesInput bi = BytesInput.from(data, 0, data.length);
byte[] result = bi.toByteArray();
assertThat(result)
.as("toByteArray() should return the backing array when offset=0 and length=full")
.isSameAs(data);
.as("toByteArray() should materialize a copy when offset=0 and length=full")
.isEqualTo(data)
.isNotSameAs(data);
}

@ParameterizedTest(name = "{0}")
Expand Down