Skip to content

Commit

Permalink
Force use BIG_ENDIAN order for UUIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
alex268 committed Oct 11, 2024
1 parent 90681c1 commit 87c44d9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tech.ydb.table.values.proto;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.time.Duration;
import java.time.Instant;
Expand Down Expand Up @@ -832,7 +833,7 @@ private static final class Uuid extends PrimitiveValue {
}

Uuid(byte[] value) {
ByteBuffer buf = ByteBuffer.wrap(value);
ByteBuffer buf = ByteBuffer.wrap(value).order(ByteOrder.BIG_ENDIAN);
this.high = buf.getLong();
this.low = buf.getLong();
}
Expand Down Expand Up @@ -916,7 +917,7 @@ public long getUuidLow() {

@Override
public byte[] getUuidAsBytes() {
ByteBuffer buf = ByteBuffer.allocate(16);
ByteBuffer buf = ByteBuffer.allocate(16).order(ByteOrder.BIG_ENDIAN);
buf.putLong(high);
buf.putLong(low);
return buf.array();
Expand Down
29 changes: 22 additions & 7 deletions table/src/test/java/tech/ydb/table/integration/ValuesReadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ public void nullReadTest() {
@SuppressWarnings("deprecation")
public void uuidReadTest() {
DataQueryResult result = CTX.supplyResult(
s -> s.executeDataQuery(
"SELECT CAST('123e4567-e89b-12d3-a456-426614174000' AS UUID) AS p1",
s -> s.executeDataQuery("SELECT "
+ "CAST('123e4567-e89b-12d3-a456-426614174000' AS UUID) AS p1,"
+ "CAST('2d9e498b-b746-9cfb-084d-de4e1cb4736e' AS UUID) AS p2;",
TxControl.serializableRw()
)
).join().getValue();
Expand All @@ -83,10 +84,24 @@ public void uuidReadTest() {

Assert.assertEquals(UUID.fromString("123e4567-e89b-12d3-a456-426614174000"), p1.getUuid());

PrimitiveValue v = p1.getValue().asOptional().get().asData();
Assert.assertEquals("123e4567-e89b-12d3-a456-426614174000", v.getUuidString());
Assert.assertEquals(0x12d3e89b123e4567L, v.getUuidLow());
Assert.assertEquals(0x00401714664256a4L, v.getUuidHigh());
Assert.assertArrayEquals(BaseEncoding.base16().decode("00401714664256A412D3E89B123E4567"), v.getUuidAsBytes());
PrimitiveValue v1 = p1.getValue().asOptional().get().asData();
Assert.assertEquals("123e4567-e89b-12d3-a456-426614174000", v1.getUuidString());
Assert.assertEquals(0x12d3e89b123e4567L, v1.getUuidLow());
Assert.assertEquals(0x00401714664256a4L, v1.getUuidHigh());
Assert.assertArrayEquals(BaseEncoding.base16().decode("00401714664256A412D3E89B123E4567"), v1.getUuidAsBytes());

ValueReader p2 = rs.getColumn("p2");
Assert.assertNotNull(p2);

Assert.assertSame(Type.Kind.OPTIONAL, p2.getType().getKind());
Assert.assertSame(PrimitiveType.Uuid, p2.getType().unwrapOptional());

Assert.assertEquals(UUID.fromString("2d9e498b-b746-9cfb-084d-de4e1cb4736e"), p2.getUuid());

PrimitiveValue v2 = p2.getValue().asOptional().get().asData();
Assert.assertEquals("2d9e498b-b746-9cfb-084d-de4e1cb4736e", v2.getUuidString());
Assert.assertEquals(0x9cfbb7462d9e498bL, v2.getUuidLow());
Assert.assertEquals(0x6e73b41c4ede4d08L, v2.getUuidHigh());
Assert.assertArrayEquals(BaseEncoding.base16().decode("6E73B41C4EDE4D089CFBB7462D9E498B"), v2.getUuidAsBytes());
}
}

0 comments on commit 87c44d9

Please sign in to comment.