From 347c79413af0856c4d547b2d62a4d2e673864bcb Mon Sep 17 00:00:00 2001 From: Zac Blanco Date: Wed, 16 Oct 2024 11:26:37 -0700 Subject: [PATCH] use big-endian representation for uuids --- .../facebook/presto/common/type/UuidType.java | 9 +- .../presto/type/TestUuidOperators.java | 89 ++++++++++++++++++- 2 files changed, 90 insertions(+), 8 deletions(-) diff --git a/presto-common/src/main/java/com/facebook/presto/common/type/UuidType.java b/presto-common/src/main/java/com/facebook/presto/common/type/UuidType.java index 06aa22ee0683a..2685dd2cbf364 100644 --- a/presto-common/src/main/java/com/facebook/presto/common/type/UuidType.java +++ b/presto-common/src/main/java/com/facebook/presto/common/type/UuidType.java @@ -28,6 +28,7 @@ import static com.facebook.presto.common.type.TypeSignature.parseTypeSignature; import static io.airlift.slice.SizeOf.SIZE_OF_LONG; import static io.airlift.slice.Slices.wrappedLongArray; +import static java.lang.Long.reverseBytes; import static java.lang.String.format; public class UuidType @@ -161,8 +162,8 @@ public final Slice getSlice(Block block, int position) public static Slice javaUuidToPrestoUuid(UUID uuid) { return wrappedLongArray( - uuid.getMostSignificantBits(), - uuid.getLeastSignificantBits()); + reverseBytes(uuid.getMostSignificantBits()), + reverseBytes(uuid.getLeastSignificantBits())); } public static UUID prestoUuidToJavaUuid(Slice uuid) @@ -171,7 +172,7 @@ public static UUID prestoUuidToJavaUuid(Slice uuid) throw new IllegalStateException(format("Expected value to be exactly %d bytes but was %d", INT128_BYTES, uuid.length())); } return new UUID( - uuid.getLong(0), - uuid.getLong(SIZE_OF_LONG)); + reverseBytes(uuid.getLong(0)), + reverseBytes(uuid.getLong(SIZE_OF_LONG))); } } diff --git a/presto-main/src/test/java/com/facebook/presto/type/TestUuidOperators.java b/presto-main/src/test/java/com/facebook/presto/type/TestUuidOperators.java index 8a2e85ee2d0d3..a0c8890422b4a 100644 --- a/presto-main/src/test/java/com/facebook/presto/type/TestUuidOperators.java +++ b/presto-main/src/test/java/com/facebook/presto/type/TestUuidOperators.java @@ -105,11 +105,92 @@ public void testNotEquals() @Test public void testOrderOperators() { - assertFunction("CAST('12151fd2-7586-11e9-8f9e-2a86e4085a58' AS UUID) < CAST('12151fd2-7586-11e9-8f9e-2a86e4085a59' AS UUID)", BOOLEAN, true); - assertFunction("CAST('12151fd2-7586-11e9-8f9e-2a86e4085a59' AS UUID) < CAST('12151fd2-7586-11e9-8f9e-2a86e4085a58' AS UUID)", BOOLEAN, false); + // LT + // equality + assertFunction("CAST('00000000-0000-0000-0000-000000000000' AS UUID) < CAST('00000000-0000-0000-0000-000000000000' AS UUID)", BOOLEAN, false); + // check bytes in LSB + assertFunction("CAST('00000000-0000-0000-0000-000000000000' AS UUID) < CAST('00000000-0000-0000-0000-000000000001' AS UUID)", BOOLEAN, true); + assertFunction("CAST('00000000-0000-0000-0000-000000000001' AS UUID) < CAST('00000000-0000-0000-0000-000000000000' AS UUID)", BOOLEAN, false); + assertFunction("CAST('00000000-0000-0000-ff00-000000000001' AS UUID) < CAST('00000000-0000-0000-0000-000000000000' AS UUID)", BOOLEAN, false); + assertFunction("CAST('00000000-0000-0000-0000-000000000001' AS UUID) < CAST('00000000-0000-0000-ff00-000000000000' AS UUID)", BOOLEAN, true); - assertFunction("UUID '12151fd2-7586-11e9-8f9e-2a86e4085a52' BETWEEN UUID '12151fd2-7586-11e9-8f9e-2a86e4085a50' AND UUID '12151fd2-7586-11e9-8f9e-2a86e4085a59'", BOOLEAN, true); - assertFunction("UUID '12151fd2-7586-11e9-8f9e-2a86e4085a52' BETWEEN UUID '12151fd2-7586-11e9-8f9e-2a86e4085a54' AND UUID '12151fd2-7586-11e9-8f9e-2a86e4085a59'", BOOLEAN, false); + // check bytes in MSB + assertFunction("CAST('00000000-0000-0001-0000-000000000000' AS UUID) < CAST('00000000-0000-0000-0000-000000000000' AS UUID)", BOOLEAN, false); + assertFunction("CAST('00000000-0000-0000-0000-000000000000' AS UUID) < CAST('00000000-0000-0001-0000-000000000000' AS UUID)", BOOLEAN, true); + assertFunction("CAST('ff000000-0000-0001-0000-000000000000' AS UUID) < CAST('00000000-0000-0001-0000-000000000000' AS UUID)", BOOLEAN, false); + assertFunction("CAST('00000000-0000-0001-0000-000000000000' AS UUID) < CAST('ff000000-0000-0001-0000-000000000000' AS UUID)", BOOLEAN, true); + + // check MSB side takes precedent over LSB side + assertFunction("CAST('00000000-0000-0001-0000-000000000000' AS UUID) < CAST('00000000-0000-0000-0000-000000000001' AS UUID)", BOOLEAN, false); + assertFunction("CAST('00000000-0000-0000-0000-000000000001' AS UUID) < CAST('00000000-0000-0001-0000-000000000000' AS UUID)", BOOLEAN, true); + assertFunction("CAST('ff000000-0000-0000-0000-000000000001' AS UUID) < CAST('00000000-0000-0001-0000-000000000000' AS UUID)", BOOLEAN, false); + assertFunction("CAST('00000000-0000-0000-0000-000000000001' AS UUID) < CAST('ff000000-0000-0000-0000-000000000000' AS UUID)", BOOLEAN, true); + + // ======================== + // LTE + // equality + assertFunction("CAST('00000000-0000-0000-0000-000000000000' AS UUID) <= CAST('00000000-0000-0000-0000-000000000000' AS UUID)", BOOLEAN, true); + // check bytes in LSB + assertFunction("CAST('00000000-0000-0000-0000-000000000000' AS UUID) <= CAST('00000000-0000-0000-0000-000000000001' AS UUID)", BOOLEAN, true); + assertFunction("CAST('00000000-0000-0000-0000-000000000001' AS UUID) <= CAST('00000000-0000-0000-0000-000000000000' AS UUID)", BOOLEAN, false); + assertFunction("CAST('00000000-0000-0000-ff00-000000000001' AS UUID) <= CAST('00000000-0000-0000-0000-000000000000' AS UUID)", BOOLEAN, false); + assertFunction("CAST('00000000-0000-0000-0000-000000000001' AS UUID) <= CAST('00000000-0000-0000-ff00-000000000000' AS UUID)", BOOLEAN, true); + + // check bytes in MSB + assertFunction("CAST('00000000-0000-0001-0000-000000000000' AS UUID) <= CAST('00000000-0000-0000-0000-000000000000' AS UUID)", BOOLEAN, false); + assertFunction("CAST('00000000-0000-0000-0000-000000000000' AS UUID) <= CAST('00000000-0000-0001-0000-000000000000' AS UUID)", BOOLEAN, true); + assertFunction("CAST('ff000000-0000-0001-0000-000000000000' AS UUID) <= CAST('00000000-0000-0001-0000-000000000000' AS UUID)", BOOLEAN, false); + assertFunction("CAST('00000000-0000-0001-0000-000000000000' AS UUID) <= CAST('ff000000-0000-0001-0000-000000000000' AS UUID)", BOOLEAN, true); + + // check MSB side takes precedent over LSB side + assertFunction("CAST('00000000-0000-0001-0000-000000000000' AS UUID) <= CAST('00000000-0000-0000-0000-000000000001' AS UUID)", BOOLEAN, false); + assertFunction("CAST('00000000-0000-0000-0000-000000000001' AS UUID) <= CAST('00000000-0000-0001-0000-000000000000' AS UUID)", BOOLEAN, true); + assertFunction("CAST('ff000000-0000-0000-0000-000000000001' AS UUID) <= CAST('00000000-0000-0001-0000-000000000000' AS UUID)", BOOLEAN, false); + assertFunction("CAST('00000000-0000-0000-0000-000000000001' AS UUID) <= CAST('ff000000-0000-0000-0000-000000000000' AS UUID)", BOOLEAN, true); + + // ======================== + // GT + // equality + assertFunction("CAST('00000000-0000-0000-0000-000000000000' AS UUID) > CAST('00000000-0000-0000-0000-000000000000' AS UUID)", BOOLEAN, false); + // check bytes in LSB + assertFunction("CAST('00000000-0000-0000-0000-000000000000' AS UUID) > CAST('00000000-0000-0000-0000-000000000001' AS UUID)", BOOLEAN, false); + assertFunction("CAST('00000000-0000-0000-0000-000000000001' AS UUID) > CAST('00000000-0000-0000-0000-000000000000' AS UUID)", BOOLEAN, true); + assertFunction("CAST('00000000-0000-0000-ff00-000000000001' AS UUID) > CAST('00000000-0000-0000-0000-000000000000' AS UUID)", BOOLEAN, true); + assertFunction("CAST('00000000-0000-0000-0000-000000000001' AS UUID) > CAST('00000000-0000-0000-ff00-000000000000' AS UUID)", BOOLEAN, false); + + // check bytes in MSB + assertFunction("CAST('00000000-0000-0001-0000-000000000000' AS UUID) > CAST('00000000-0000-0000-0000-000000000000' AS UUID)", BOOLEAN, true); + assertFunction("CAST('00000000-0000-0000-0000-000000000000' AS UUID) > CAST('00000000-0000-0001-0000-000000000000' AS UUID)", BOOLEAN, false); + assertFunction("CAST('ff000000-0000-0001-0000-000000000000' AS UUID) > CAST('00000000-0000-0001-0000-000000000000' AS UUID)", BOOLEAN, true); + assertFunction("CAST('00000000-0000-0001-0000-000000000000' AS UUID) > CAST('ff000000-0000-0001-0000-000000000000' AS UUID)", BOOLEAN, false); + + // check MSB side takes precedent over LSB side + assertFunction("CAST('00000000-0000-0001-0000-000000000000' AS UUID) > CAST('00000000-0000-0000-0000-000000000001' AS UUID)", BOOLEAN, true); + assertFunction("CAST('00000000-0000-0000-0000-000000000001' AS UUID) > CAST('00000000-0000-0001-0000-000000000000' AS UUID)", BOOLEAN, false); + assertFunction("CAST('ff000000-0000-0000-0000-000000000001' AS UUID) > CAST('00000000-0000-0001-0000-000000000000' AS UUID)", BOOLEAN, true); + assertFunction("CAST('00000000-0000-0000-0000-000000000001' AS UUID) > CAST('ff000000-0000-0000-0000-000000000000' AS UUID)", BOOLEAN, false); + + // ======================== + // GTE + // equality + assertFunction("CAST('00000000-0000-0000-0000-000000000000' AS UUID) >= CAST('00000000-0000-0000-0000-000000000000' AS UUID)", BOOLEAN, true); + // check bytes in LSB + assertFunction("CAST('00000000-0000-0000-0000-000000000000' AS UUID) >= CAST('00000000-0000-0000-0000-000000000001' AS UUID)", BOOLEAN, false); + assertFunction("CAST('00000000-0000-0000-0000-000000000001' AS UUID) >= CAST('00000000-0000-0000-0000-000000000000' AS UUID)", BOOLEAN, true); + assertFunction("CAST('00000000-0000-0000-ff00-000000000001' AS UUID) >= CAST('00000000-0000-0000-0000-000000000000' AS UUID)", BOOLEAN, true); + assertFunction("CAST('00000000-0000-0000-0000-000000000001' AS UUID) >= CAST('00000000-0000-0000-ff00-000000000000' AS UUID)", BOOLEAN, false); + + // check bytes in MSB + assertFunction("CAST('00000000-0000-0001-0000-000000000000' AS UUID) >= CAST('00000000-0000-0000-0000-000000000000' AS UUID)", BOOLEAN, true); + assertFunction("CAST('00000000-0000-0000-0000-000000000000' AS UUID) >= CAST('00000000-0000-0001-0000-000000000000' AS UUID)", BOOLEAN, false); + assertFunction("CAST('ff000000-0000-0001-0000-000000000000' AS UUID) >= CAST('00000000-0000-0001-0000-000000000000' AS UUID)", BOOLEAN, true); + assertFunction("CAST('00000000-0000-0001-0000-000000000000' AS UUID) >= CAST('ff000000-0000-0001-0000-000000000000' AS UUID)", BOOLEAN, false); + + // check MSB side takes precedent over LSB side + assertFunction("CAST('00000000-0000-0001-0000-000000000000' AS UUID) >= CAST('00000000-0000-0000-0000-000000000001' AS UUID)", BOOLEAN, true); + assertFunction("CAST('00000000-0000-0000-0000-000000000001' AS UUID) >= CAST('00000000-0000-0001-0000-000000000000' AS UUID)", BOOLEAN, false); + assertFunction("CAST('ff000000-0000-0000-0000-000000000001' AS UUID) >= CAST('00000000-0000-0001-0000-000000000000' AS UUID)", BOOLEAN, true); + assertFunction("CAST('00000000-0000-0000-0000-000000000001' AS UUID) >= CAST('ff000000-0000-0000-0000-000000000000' AS UUID)", BOOLEAN, false); } @Test