Skip to content

Commit 2302437

Browse files
han-yan01meta-codesync[bot]
authored andcommitted
feat: Add TIME support to xxHash64 function (facebookincubator#15229)
Summary: Pull Request resolved: facebookincubator#15229 - Added `XxHash64TimeFunction` in `DateTimeFunctions.h` that hashes the TIME value (int64_t milliseconds since midnight) using XXH64 - Registered the function in `DateTimeFunctionsRegistration.cpp` Reviewed By: kgpai Differential Revision: D84962427 fbshipit-source-id: d3de6ee8aebc41ee71cd3acbac8a0c8d01805f39
1 parent 2e8bb31 commit 2302437

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

velox/functions/prestosql/DateTimeFunctions.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,6 +1941,21 @@ struct XxHash64TimestampFunction {
19411941
}
19421942
};
19431943

1944+
/// xxhash64(Time) → bigint
1945+
/// Return a xxhash64 of input Time
1946+
template <typename T>
1947+
struct XxHash64TimeFunction {
1948+
VELOX_DEFINE_FUNCTION_TYPES(T);
1949+
1950+
FOLLY_ALWAYS_INLINE
1951+
void call(out_type<int64_t>& result, const arg_type<Time>& input) {
1952+
// TIME is represented as milliseconds since midnight
1953+
// Convert to big-endian to match Presto's xxhash64 behavior
1954+
auto bigEndianValue = folly::Endian::big(input);
1955+
result = XXH64(&bigEndianValue, sizeof(bigEndianValue), 0);
1956+
}
1957+
};
1958+
19441959
template <typename T>
19451960
struct ParseDurationFunction {
19461961
VELOX_DEFINE_FUNCTION_TYPES(T);

velox/functions/prestosql/registration/DateTimeFunctionsRegistration.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ void registerSimpleFunctions(const std::string& prefix) {
325325
{prefix + "xxhash64_internal"});
326326
registerFunction<XxHash64TimestampFunction, int64_t, Timestamp>(
327327
{prefix + "xxhash64_internal"});
328+
registerFunction<XxHash64TimeFunction, int64_t, Time>(
329+
{prefix + "xxhash64_internal"});
328330

329331
registerFunction<ParseDurationFunction, IntervalDayTime, Varchar>(
330332
{prefix + "parse_duration"});

velox/functions/prestosql/tests/DateTimeFunctionsTest.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6269,6 +6269,44 @@ TEST_F(DateTimeFunctionsTest, xxHash64FunctionTimestamp) {
62696269
7585368295023641328, xxhash64(parseTimestamp("1900-01-01 00:00:00.000")));
62706270
}
62716271

6272+
TEST_F(DateTimeFunctionsTest, xxHash64FunctionTime) {
6273+
const auto xxhash64 = [&](std::optional<int64_t> time) {
6274+
return evaluateOnce<int64_t>("xxhash64_internal(c0)", TIME(), time);
6275+
};
6276+
6277+
// Test NULL handling
6278+
EXPECT_EQ(std::nullopt, xxhash64(std::nullopt));
6279+
6280+
// Test determinism - same input should give same output
6281+
auto result1 = xxhash64(43200000);
6282+
auto result2 = xxhash64(43200000);
6283+
EXPECT_EQ(result1, result2);
6284+
6285+
// Test that different inputs give different outputs
6286+
auto hash0 = xxhash64(0);
6287+
auto hash1 = xxhash64(1);
6288+
auto hashNoon = xxhash64(43200000);
6289+
auto hashEnd = xxhash64(86399999);
6290+
6291+
EXPECT_NE(hash0, hash1);
6292+
EXPECT_NE(hash0, hashNoon);
6293+
EXPECT_NE(hash0, hashEnd);
6294+
EXPECT_NE(hashNoon, hashEnd);
6295+
6296+
// Test boundary values don't crash
6297+
EXPECT_TRUE(xxhash64(0).has_value());
6298+
EXPECT_TRUE(xxhash64(86399999).has_value());
6299+
6300+
// Test known hash values validated against Presto xxhash64
6301+
// Query: SELECT from_big_endian_64(xxhash64(to_big_endian_64(value)))
6302+
EXPECT_EQ(3803688792395291579, xxhash64(0)); // Midnight
6303+
EXPECT_EQ(-6980583299780818982, xxhash64(1)); // 1 millisecond after midnight
6304+
EXPECT_EQ(7848233046982034517, xxhash64(43200000)); // Noon (12:00:00.000)
6305+
EXPECT_EQ(
6306+
5892092673475229733, xxhash64(86399999)); // End of day (23:59:59.999)
6307+
EXPECT_EQ(-3599997350390034763, xxhash64(1234)); // Arbitrary value
6308+
}
6309+
62726310
TEST_F(DateTimeFunctionsTest, localtime) {
62736311
const auto localtime = [&](int64_t sessionStartTime,
62746312
const std::optional<std::string>& timeZone) {

0 commit comments

Comments
 (0)