Skip to content

Commit 0132bb2

Browse files
committed
feat(iceberg): Add timestamp_ns function
1 parent bbe1d1d commit 0132bb2

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

velox/functions/iceberg/DateTimeFunctions.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ struct HoursFunction {
8787
}
8888
};
8989

90+
// timestamp_ns(input) -> nanoseconds from 1970-01-01 00:00:00.000000000
91+
// Input is timestamp.
92+
template <typename TExec>
93+
struct TimestampNsFunction {
94+
VELOX_DEFINE_FUNCTION_TYPES(TExec);
95+
96+
FOLLY_ALWAYS_INLINE void call(
97+
int64_t& result,
98+
const arg_type<Timestamp>& timestamp) {
99+
result = epochNanos(timestamp);
100+
}
101+
};
102+
90103
void registerDateTimeFunctions(const std::string& prefix) {
91104
registerFunction<YearsFunction, int32_t, Timestamp>({prefix + "years"});
92105
registerFunction<YearsFunction, int32_t, Date>({prefix + "years"});
@@ -95,6 +108,8 @@ void registerDateTimeFunctions(const std::string& prefix) {
95108
registerFunction<DaysFunction, Date, Timestamp>({prefix + "days"});
96109
registerFunction<DaysFunction, Date, Date>({prefix + "days"});
97110
registerFunction<HoursFunction, int32_t, Timestamp>({prefix + "hours"});
111+
registerFunction<TimestampNsFunction, int64_t, Timestamp>(
112+
{prefix + "timestamp_ns"});
98113
}
99114

100115
} // namespace facebook::velox::functions::iceberg

velox/functions/iceberg/DateTimeUtil.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,10 @@ FOLLY_ALWAYS_INLINE int32_t epochHour(Timestamp ts) {
5454
return (seconds >= 0) ? seconds / 3'600 : ((seconds + 1) / 3'600) - 1;
5555
}
5656

57+
/// Extract nanoseconds from 1970-01-01 00:00:00.000000000.
58+
/// Stores nanoseconds from 1970-01-01 00:00:00.000000000.
59+
FOLLY_ALWAYS_INLINE int64_t epochNanos(Timestamp ts) {
60+
return ts.getSeconds() * 1'000'000'000L + ts.getNanos();
61+
}
62+
5763
} // namespace facebook::velox::functions::iceberg

velox/functions/iceberg/tests/DateTimeFunctionsTest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,25 @@ TEST_F(DateTimeFunctionsTest, hours) {
8484
EXPECT_EQ(-1, hours("1969-12-31 23:59:58.999999"));
8585
}
8686

87+
TEST_F(DateTimeFunctionsTest, timestampNs) {
88+
const auto timestampNs = [&](const std::string& ts) {
89+
return evaluateOnce<int64_t>(
90+
"timestamp_ns(c0)", std::make_optional<Timestamp>(parseTimestamp(ts)));
91+
};
92+
93+
// 1970-01-01 00:00:00.000000000 should be 0
94+
EXPECT_EQ(0, timestampNs("1970-01-01 00:00:00.000000"));
95+
96+
// 2017-12-01 10:12:55.038194 is 1512123175038194000 nanoseconds from
97+
// 1970-01-01
98+
EXPECT_EQ(1512123175038194000L, timestampNs("2017-12-01 10:12:55.038194"));
99+
100+
// 1970-01-01 00:00:01.000001 is 1000001000 nanoseconds from 1970-01-01
101+
EXPECT_EQ(1000001000L, timestampNs("1970-01-01 00:00:01.000001"));
102+
103+
// Timestamp before 1970 should be negative
104+
EXPECT_EQ(-1000000000L, timestampNs("1969-12-31 23:59:59.000000"));
105+
}
106+
87107
} // namespace
88108
} // namespace facebook::velox::functions::iceberg

0 commit comments

Comments
 (0)