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 1c0504e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 12 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 @@ -117,7 +118,9 @@ public Object getObjectValue(SqlFunctionProperties sqlFunctionProperties, Block
if (block.isNull(position)) {
return null;
}
return new UUID(block.getLong(position, 0), block.getLong(position, SIZE_OF_LONG)).toString();
return new UUID(
reverseBytes(block.getLong(position, 0)),
reverseBytes(block.getLong(position, SIZE_OF_LONG))).toString();
}

@Override
Expand Down Expand Up @@ -161,8 +164,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 +174,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 @@ -75,7 +75,7 @@ public void testVarbinaryToUUIDCast()
public void testUUIDToVarbinaryCast()
{
assertFunction("CAST(UUID '00000000-0000-0000-0000-000000000000' AS VARBINARY)", VARBINARY, new SqlVarbinary(base16().decode("00000000000000000000000000000000")));
assertFunction("CAST(UUID '6b5f5b65-67e4-43b0-8ee3-586cd49f58a0' AS VARBINARY)", VARBINARY, new SqlVarbinary(base16().decode("B043E467655B5F6BA0589FD46C58E38E")));
assertFunction("CAST(UUID '6b5f5b65-67e4-43b0-8ee3-586cd49f58a0' AS VARBINARY)", VARBINARY, new SqlVarbinary(base16().decode("6B5F5B6567E443B08EE3586CD49F58A0")));
}

@Test
Expand Down 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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static com.facebook.presto.common.type.UuidType.UUID;
import static com.facebook.presto.type.UuidOperators.castFromVarcharToUuid;
import static io.airlift.slice.SizeOf.SIZE_OF_LONG;
import static io.airlift.slice.Slices.utf8Slice;
import static org.testng.Assert.assertEquals;

Expand All @@ -46,7 +47,7 @@ public static Block createTestBlock()
protected Object getGreaterValue(Object value)
{
Slice slice = (Slice) value;
return Slices.wrappedLongArray(slice.getLong(0), slice.getLong(1) + 1);
return Slices.wrappedLongArray(slice.getLong(0), slice.getLong(SIZE_OF_LONG) + 1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import static com.fasterxml.jackson.databind.SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS;
import static io.airlift.slice.Slices.utf8Slice;
import static io.airlift.slice.Slices.wrappedLongArray;
import static java.lang.Long.reverseBytes;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;

Expand Down Expand Up @@ -196,6 +197,8 @@ private ReadMapping uuidColumnMapping()

private static Slice uuidSlice(UUID uuid)
{
return wrappedLongArray(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits());
return wrappedLongArray(
reverseBytes(uuid.getMostSignificantBits()),
reverseBytes(uuid.getLeastSignificantBits()));
}
}

0 comments on commit 1c0504e

Please sign in to comment.