Skip to content

Commit c86b436

Browse files
committed
Normalize the byte buffer when constructing BoundingBoxLiteral
1 parent 0e16afd commit c86b436

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

api/src/main/java/org/apache/iceberg/expressions/Literals.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.math.BigDecimal;
2323
import java.math.RoundingMode;
2424
import java.nio.ByteBuffer;
25+
import java.nio.ByteOrder;
2526
import java.time.Instant;
2627
import java.time.LocalDate;
2728
import java.time.LocalTime;
@@ -734,7 +735,8 @@ static class BoundingBoxLiteral implements Literal<ByteBuffer> {
734735
}
735736

736737
BoundingBoxLiteral(ByteBuffer value) {
737-
this.value = value;
738+
Preconditions.checkNotNull(value, "Bounding box buffer cannot be null");
739+
this.value = value.slice().order(ByteOrder.LITTLE_ENDIAN);
738740
}
739741

740742
@Override

api/src/test/java/org/apache/iceberg/expressions/TestExpressionUtil.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.assertj.core.api.Assertions.assertThat;
2222

2323
import java.nio.ByteBuffer;
24+
import java.nio.ByteOrder;
2425
import java.time.LocalDate;
2526
import java.time.OffsetDateTime;
2627
import java.time.ZoneOffset;
@@ -1393,6 +1394,22 @@ public void testSanitizeGeospatialPredicates(
13931394
.isEqualTo(expectedSanitizedString);
13941395
}
13951396

1397+
@Test
1398+
public void testBoundingBoxLiteralNormalizesBuffer() {
1399+
GeospatialBound min = GeospatialBound.createXY(1.0, 2.0);
1400+
GeospatialBound max = GeospatialBound.createXY(3.0, 4.0);
1401+
BoundingBox box = new BoundingBox(min, max);
1402+
ByteBuffer serialized = box.toByteBuffer();
1403+
1404+
ByteBuffer padded = ByteBuffer.allocate(serialized.remaining() + 1).order(ByteOrder.BIG_ENDIAN);
1405+
padded.put((byte) 0x0);
1406+
padded.put(serialized.duplicate());
1407+
padded.position(1);
1408+
Literal<ByteBuffer> literal = new Literals.BoundingBoxLiteral(padded);
1409+
1410+
assertThat(literal.toString()).isEqualTo(box.toString());
1411+
}
1412+
13961413
private void assertEquals(Expression expected, Expression actual) {
13971414
assertThat(expected).isInstanceOf(UnboundPredicate.class);
13981415
assertEquals((UnboundPredicate<?>) expected, (UnboundPredicate<?>) actual);

0 commit comments

Comments
 (0)