Skip to content

Commit 41f7359

Browse files
authored
GH-3317: Fix bytes written by VariantBuilder.appendFloat (#3334)
1 parent 7e7a658 commit 41f7359

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

parquet-variant/src/main/java/org/apache/parquet/variant/VariantBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ public void appendFloat(float f) {
358358
onAppend();
359359
checkCapacity(1 /* header size */ + 4);
360360
writeBuffer[writePos] = VariantUtil.HEADER_FLOAT;
361-
VariantUtil.writeLong(writeBuffer, writePos + 1, Float.floatToIntBits(f), 8);
361+
VariantUtil.writeLong(writeBuffer, writePos + 1, Float.floatToIntBits(f), 4);
362362
writePos += 5;
363363
}
364364

parquet-variant/src/test/java/org/apache/parquet/variant/TestVariantScalarBuilder.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.apache.parquet.variant;
2020

21+
import java.lang.reflect.Field;
2122
import java.math.BigDecimal;
2223
import java.nio.ByteBuffer;
2324
import java.nio.ByteOrder;
@@ -181,6 +182,43 @@ public void testFloatBuilder() {
181182
});
182183
}
183184

185+
@Test
186+
public void testFloatBuilderDoesNotWriteTooManyBytes() throws Exception {
187+
VariantBuilder vb = new VariantBuilder();
188+
189+
Field writeBufferField = VariantBuilder.class.getDeclaredField("writeBuffer");
190+
writeBufferField.setAccessible(true);
191+
Field writePosField = VariantBuilder.class.getDeclaredField("writePos");
192+
writePosField.setAccessible(true);
193+
194+
byte[] buffer = (byte[]) writeBufferField.get(vb);
195+
for (int i = 0; i < 20; i++) {
196+
buffer[i] = (byte) 0xFF;
197+
}
198+
199+
float testFloat = 1.23456f;
200+
vb.appendFloat(testFloat);
201+
202+
int writePos = (Integer) writePosField.get(vb);
203+
Assert.assertEquals("writePos should be exactly 5 after appendFloat", 5, writePos);
204+
205+
int modifiedBytes = 0;
206+
for (int i = 0; i < 10; i++) {
207+
if (buffer[i] != (byte) 0xFF) {
208+
modifiedBytes++;
209+
}
210+
}
211+
Assert.assertEquals("appendFloat should write exactly 5 bytes (1 header + 4 data)", 5, modifiedBytes);
212+
213+
for (int i = 5; i < 10; i++) {
214+
Assert.assertEquals(
215+
"Byte at position " + i + " should not be modified by appendFloat", (byte) 0xFF, buffer[i]);
216+
}
217+
218+
Variant variant = vb.build();
219+
Assert.assertEquals("Float value should be preserved correctly", testFloat, variant.getFloat(), 0.0f);
220+
}
221+
184222
@Test
185223
public void testDoubleBuilder() {
186224
Arrays.asList(Double.MIN_VALUE, 0d, -0d, Double.MAX_VALUE).forEach(d -> {

0 commit comments

Comments
 (0)