Skip to content

Commit 4b8fd9d

Browse files
committed
feat(iceberg): Add timestamptz_ns function
1 parent 0132bb2 commit 4b8fd9d

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

velox/functions/iceberg/DateTimeFunctions.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ struct TimestampNsFunction {
100100
}
101101
};
102102

103+
// timestamptz_ns(input) -> nanoseconds from 1970-01-01 00:00:00.000000000 UTC
104+
// Input is timestamp with timezone.
105+
template <typename TExec>
106+
struct TimestamptzNsFunction {
107+
VELOX_DEFINE_FUNCTION_TYPES(TExec);
108+
109+
FOLLY_ALWAYS_INLINE void call(
110+
int64_t& result,
111+
const arg_type<Timestamp>& timestamp) {
112+
result = epochNanos(timestamp);
113+
}
114+
};
115+
103116
void registerDateTimeFunctions(const std::string& prefix) {
104117
registerFunction<YearsFunction, int32_t, Timestamp>({prefix + "years"});
105118
registerFunction<YearsFunction, int32_t, Date>({prefix + "years"});
@@ -110,6 +123,8 @@ void registerDateTimeFunctions(const std::string& prefix) {
110123
registerFunction<HoursFunction, int32_t, Timestamp>({prefix + "hours"});
111124
registerFunction<TimestampNsFunction, int64_t, Timestamp>(
112125
{prefix + "timestamp_ns"});
126+
registerFunction<TimestamptzNsFunction, int64_t, Timestamp>(
127+
{prefix + "timestamptz_ns"});
113128
}
114129

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

velox/functions/iceberg/tests/DateTimeFunctionsTest.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,26 @@ TEST_F(DateTimeFunctionsTest, timestampNs) {
104104
EXPECT_EQ(-1000000000L, timestampNs("1969-12-31 23:59:59.000000"));
105105
}
106106

107+
TEST_F(DateTimeFunctionsTest, timestamptzNs) {
108+
const auto timestamptzNs = [&](const std::string& ts) {
109+
return evaluateOnce<int64_t>(
110+
"timestamptz_ns(c0)",
111+
std::make_optional<Timestamp>(parseTimestamp(ts)));
112+
};
113+
114+
// 1970-01-01 00:00:00.000000000 UTC should be 0
115+
EXPECT_EQ(0, timestamptzNs("1970-01-01 00:00:00.000000"));
116+
117+
// 2017-12-01 10:12:55.038194 is 1512123175038194000 nanoseconds from
118+
// 1970-01-01 UTC
119+
EXPECT_EQ(1512123175038194000L, timestamptzNs("2017-12-01 10:12:55.038194"));
120+
121+
// 1970-01-01 00:00:01.000001 is 1000001000 nanoseconds from 1970-01-01 UTC
122+
EXPECT_EQ(1000001000L, timestamptzNs("1970-01-01 00:00:01.000001"));
123+
124+
// Timestamp before 1970 should be negative
125+
EXPECT_EQ(-1000000000L, timestamptzNs("1969-12-31 23:59:59.000000"));
126+
}
127+
107128
} // namespace
108129
} // namespace facebook::velox::functions::iceberg

0 commit comments

Comments
 (0)