Skip to content

Commit 0b677ad

Browse files
committed
test: encode bound ByteBuffers via Conversions.toByteBuffer
1 parent 370dae7 commit 0b677ad

1 file changed

Lines changed: 12 additions & 13 deletions

File tree

sdks/java/io/iceberg/src/test/java/org/apache/beam/sdk/io/iceberg/SerializableDataFileTest.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import org.apache.iceberg.FileFormat;
3737
import org.apache.iceberg.Metrics;
3838
import org.apache.iceberg.PartitionSpec;
39+
import org.apache.iceberg.types.Conversions;
40+
import org.apache.iceberg.types.Types;
3941
import org.junit.Test;
4042

4143
/**
@@ -93,21 +95,18 @@ public void testFieldsInEqualsMethodInSyncWithGetterFields() {
9395
*/
9496
@Test
9597
public void testBoundByteBufferIsCopiedByLimitNotBackingArrayLength() {
96-
// Reproduce the shape iceberg-parquet produces in the wild: a ByteBuffer
97-
// whose backing array is larger than [position, limit), with trailing
98-
// 0x00 bytes. iceberg-parquet hits this because the JDK UTF-8 encoder
99-
// over-allocates; here we build it explicitly so the test doesn't depend
100-
// on encoder internals.
98+
// Encode bounds the same way iceberg-parquet does in the wild — via
99+
// Conversions.toByteBuffer(STRING, value). For UTF-8 strings of 10+
100+
// characters the underlying JDK CharsetEncoder over-allocates by ~10%
101+
// and flips, producing a ByteBuffer with capacity > limit.
101102
int columnId = 3;
102-
byte[] expectedLower = "lower_bound_str".getBytes(StandardCharsets.UTF_8);
103-
byte[] expectedUpper = "upper_bound_str".getBytes(StandardCharsets.UTF_8);
103+
String lowerValue = "lower_bound_str";
104+
String upperValue = "upper_bound_str";
105+
byte[] expectedLower = lowerValue.getBytes(StandardCharsets.UTF_8);
106+
byte[] expectedUpper = upperValue.getBytes(StandardCharsets.UTF_8);
104107

105-
ByteBuffer lower = ByteBuffer.allocate(expectedLower.length + 1);
106-
lower.put(expectedLower);
107-
lower.flip();
108-
ByteBuffer upper = ByteBuffer.allocate(expectedUpper.length + 1);
109-
upper.put(expectedUpper);
110-
upper.flip();
108+
ByteBuffer lower = Conversions.toByteBuffer(Types.StringType.get(), lowerValue);
109+
ByteBuffer upper = Conversions.toByteBuffer(Types.StringType.get(), upperValue);
111110

112111
Map<Integer, ByteBuffer> lowerBounds = new HashMap<>();
113112
lowerBounds.put(columnId, lower);

0 commit comments

Comments
 (0)