Skip to content

Commit

Permalink
use big-endian representation for uuids
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacBlanco committed Oct 16, 2024
1 parent 9fa4aee commit 347c794
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 347c794

Please sign in to comment.