Skip to content

Commit

Permalink
Add UUID comparison functions
Browse files Browse the repository at this point in the history
This adds UUID comparison functions that were previously unsupported.
Functions added are <, >, <=, >=. Equal was already supported. The
ordering is done lexicographically and conforms to IETF [RFC 4122].
This ordering also matches Presto Java after #[23847].

[RFC 4122]: https://datatracker.ietf.org/doc/html/rfc4122.html
[23847]: prestodb/presto#23847
  • Loading branch information
BryanCutler committed Nov 7, 2024
1 parent aba7ba8 commit 0d9abca
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
25 changes: 25 additions & 0 deletions velox/functions/prestosql/UuidFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,31 @@

#include "velox/functions/Macros.h"
#include "velox/functions/Registerer.h"
#include "velox/functions/prestosql/Comparisons.h"
#include "velox/functions/prestosql/types/UuidType.h"

namespace facebook::velox::functions {

#define VELOX_GEN_BINARY_EXPR_UUID(Name, uuidCompExpr) \
template <typename T> \
struct Name##Uuid { \
VELOX_DEFINE_FUNCTION_TYPES(T); \
\
FOLLY_ALWAYS_INLINE void call( \
bool& result, \
const arg_type<Uuid>& lhs, \
const arg_type<Uuid>& rhs) { \
result = (uuidCompExpr); \
} \
};

VELOX_GEN_BINARY_EXPR_UUID(LtFunction, (uint128_t) lhs < (uint128_t) rhs);
VELOX_GEN_BINARY_EXPR_UUID(GtFunction, (uint128_t) lhs > (uint128_t) rhs);
VELOX_GEN_BINARY_EXPR_UUID(LteFunction, (uint128_t) lhs <= (uint128_t) rhs);
VELOX_GEN_BINARY_EXPR_UUID(GteFunction, (uint128_t) lhs >= (uint128_t) rhs);

#undef VELOX_GEN_BINARY_EXPR_UUID

template <typename T>
struct UuidFunction {
VELOX_DEFINE_FUNCTION_TYPES(T);
Expand All @@ -42,6 +63,10 @@ struct UuidFunction {
inline void registerUuidFunctions(const std::string& prefix) {
registerUuidType();
registerFunction<UuidFunction, Uuid>({prefix + "uuid"});
registerFunction<LtFunctionUuid, bool, Uuid, Uuid>({prefix + "lt"});
registerFunction<GtFunctionUuid, bool, Uuid, Uuid>({prefix + "gt"});
registerFunction<LteFunctionUuid, bool, Uuid, Uuid>({prefix + "lte"});
registerFunction<GteFunctionUuid, bool, Uuid, Uuid>({prefix + "gte"});
}

} // namespace facebook::velox::functions
24 changes: 24 additions & 0 deletions velox/functions/prestosql/tests/UuidFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,29 @@ TEST_F(UuidFunctionsTest, unsupportedCast) {
evaluate("cast(123 as uuid())", input), "Cannot cast BIGINT to UUID.");
}

TEST_F(UuidFunctionsTest, comparisons) {
const auto uuidEval = [&](const std::optional<std::string>& lhs, const std::string& operation, const std::optional<std::string>& rhs) {
return evaluateOnce<bool>(fmt::format("cast(c0 as uuid) {} cast(c1 as uuid)", operation), lhs, rhs);
};

ASSERT_EQ(uuidEval("33355449-2c7d-43d7-967a-f53cd23215ad", "<", "ffffffff-ffff-ffff-ffff-ffffffffffff"), true);
ASSERT_EQ(uuidEval("33355449-2c7d-43d7-967a-f53cd23215ad", "<", "00000000-0000-0000-0000-000000000000"), false);
ASSERT_EQ(uuidEval("f768f36d-4f09-4da7-a298-3564d8f3c986", ">", "00000000-0000-0000-0000-000000000000"), true);
ASSERT_EQ(uuidEval("f768f36d-4f09-4da7-a298-3564d8f3c986", ">", "ffffffff-ffff-ffff-ffff-ffffffffffff"), false);

ASSERT_EQ(uuidEval("33355449-2c7d-43d7-967a-f53cd23215ad", "<=", "33355449-2c7d-43d7-967a-f53cd23215ad"), true);
ASSERT_EQ(uuidEval("33355449-2c7d-43d7-967a-f53cd23215ad", "<=", "ffffffff-ffff-ffff-ffff-ffffffffffff"), true);
ASSERT_EQ(uuidEval("33355449-2c7d-43d7-967a-f53cd23215ad", ">=", "33355449-2c7d-43d7-967a-f53cd23215ad"), true);
ASSERT_EQ(uuidEval("ffffffff-ffff-ffff-ffff-ffffffffffff", ">=", "33355449-2c7d-43d7-967a-f53cd23215ad"), true);

ASSERT_EQ(uuidEval("f768f36d-4f09-4da7-a298-3564d8f3c986", "==", "f768f36d-4f09-4da7-a298-3564d8f3c986"), true);
ASSERT_EQ(uuidEval("eed9f812-4b0c-472f-8a10-4ae7bff79a47", "!=", "f768f36d-4f09-4da7-a298-3564d8f3c986"), true);

ASSERT_EQ(uuidEval("11000000-0000-0022-0000-000000000000", "<", "22000000-0000-0011-0000-000000000000"), true);
ASSERT_EQ(uuidEval("00000000-0000-0000-2200-000000000011", ">", "00000000-0000-0000-1100-000000000022"), true);
ASSERT_EQ(uuidEval("00000000-0000-0000-0000-000000000011", ">", "22000000-0000-0000-0000-000000000000"), false);
ASSERT_EQ(uuidEval("11000000-0000-0000-0000-000000000000", "<", "00000000-0000-0000-0000-000000000022"), false);
}

} // namespace
} // namespace facebook::velox::functions::prestosql

0 comments on commit 0d9abca

Please sign in to comment.