diff --git a/velox/docs/functions/spark/datetime.rst b/velox/docs/functions/spark/datetime.rst index d7ed56e5fc51..97aa769e6a24 100644 --- a/velox/docs/functions/spark/datetime.rst +++ b/velox/docs/functions/spark/datetime.rst @@ -244,6 +244,11 @@ These functions support TIMESTAMP and DATE input types. SELECT unix_date('1970-01-02'); -- '1' SELECT unix_date('1969-12-31'); -- '-1' +.. spark:function:: unix_seconds(timestamp) -> bigint + Returns the number of seconds since 1970-01-01 00:00:00 UTC.:: + + SELECT unix_micros('1970-01-01 00:00:01'); -- 1 + .. spark:function:: unix_timestamp() -> integer Returns the current UNIX timestamp in seconds. diff --git a/velox/functions/sparksql/DateTimeFunctions.h b/velox/functions/sparksql/DateTimeFunctions.h index 0ba287089e4a..caa924e96c92 100644 --- a/velox/functions/sparksql/DateTimeFunctions.h +++ b/velox/functions/sparksql/DateTimeFunctions.h @@ -731,4 +731,15 @@ struct MakeYMIntervalFunction { result = totalMonths; } }; + +template +struct UnixSecondsFunction { + VELOX_DEFINE_FUNCTION_TYPES(T); + + FOLLY_ALWAYS_INLINE void call( + int64_t& result, + const arg_type& timestamp) { + result = timestamp.getSeconds(); + } +}; } // namespace facebook::velox::functions::sparksql diff --git a/velox/functions/sparksql/Register.cpp b/velox/functions/sparksql/Register.cpp index 72a8ca527568..351bdfad36b4 100644 --- a/velox/functions/sparksql/Register.cpp +++ b/velox/functions/sparksql/Register.cpp @@ -325,6 +325,9 @@ void registerFunctions(const std::string& prefix) { registerFunction({prefix + "unix_date"}); + registerFunction( + {prefix + "unix_seconds"}); + registerFunction({prefix + "unix_timestamp"}); registerFunction( diff --git a/velox/functions/sparksql/tests/DateTimeFunctionsTest.cpp b/velox/functions/sparksql/tests/DateTimeFunctionsTest.cpp index 383f8bd48d64..a176374589e4 100644 --- a/velox/functions/sparksql/tests/DateTimeFunctionsTest.cpp +++ b/velox/functions/sparksql/tests/DateTimeFunctionsTest.cpp @@ -968,5 +968,17 @@ TEST_F(DateTimeFunctionsTest, makeYMInterval) { fromYear(-178956971), "Integer overflow in make_ym_interval(-178956971)"); } +TEST_F(DateTimeFunctionsTest, unixSeconds) { + const auto unixSeconds = [&](const StringView time) { + return evaluateOnce( + "unix_seconds(c0)", util::fromTimestampString(time)); + }; + EXPECT_EQ(unixSeconds("1970-01-01 00:00:01"), 1); + EXPECT_EQ(unixSeconds("1970-01-01 00:00:00.000127"), 0); + EXPECT_EQ(unixSeconds("1969-12-31 23:59:59.999872"), -1); + EXPECT_EQ(unixSeconds("1970-01-01 00:35:47.483647"), 2147); + EXPECT_EQ(unixSeconds("1971-01-01 00:00:01.483647"), 31536001); +} + } // namespace } // namespace facebook::velox::functions::sparksql::test