From 25ec8d532a1c39b9562d2a87f0b3b338e852400c Mon Sep 17 00:00:00 2001 From: Ankita Victor Date: Tue, 26 Mar 2024 11:10:26 +0530 Subject: [PATCH] Add Spark function current_timestamp --- velox/docs/functions/spark/datetime.rst | 8 ++++++++ velox/functions/sparksql/Register.cpp | 3 ++- .../sparksql/tests/DateTimeFunctionsTest.cpp | 15 ++++++++------- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/velox/docs/functions/spark/datetime.rst b/velox/docs/functions/spark/datetime.rst index d7ed56e5fc51..b18900f39b1d 100644 --- a/velox/docs/functions/spark/datetime.rst +++ b/velox/docs/functions/spark/datetime.rst @@ -23,6 +23,10 @@ These functions support TIMESTAMP and DATE input types. SELECT add_months('2015-01-30', -2); -- '2014-11-30' SELECT add_months('2015-03-31', -1); -- '2015-02-28' +.. spark:function:: current_timestamp() -> timestamp + + Returns the current timestamp at the start of query evaluation as a timestamp. If session timezone is set then returns timestamp in that timezone. + .. spark:function:: date_add(start_date, num_days) -> date Returns the date that is ``num_days`` after ``start_date``. According to the inputs, @@ -215,6 +219,10 @@ These functions support TIMESTAMP and DATE input types. SELECT next_day('2015-07-23', "tu"); -- '2015-07-28' SELECT next_day('2015-07-23', "we"); -- '2015-07-29' +.. spark:function:: now() -> timestamp + + Alias for ``current_timestamp() -> timestamp``. + .. spark:function:: second(timestamp) -> integer Returns the seconds of ``timestamp``.:: diff --git a/velox/functions/sparksql/Register.cpp b/velox/functions/sparksql/Register.cpp index 35e673e5ca8b..6c2898cf7681 100644 --- a/velox/functions/sparksql/Register.cpp +++ b/velox/functions/sparksql/Register.cpp @@ -284,7 +284,8 @@ void registerFunctions(const std::string& prefix) { registerFunction( {prefix + "from_utc_timestamp"}); - registerFunction({prefix + "current_timestamp"}); + registerFunction( + {prefix + "current_timestamp", prefix + "now"}); registerFunction({prefix + "unix_date"}); diff --git a/velox/functions/sparksql/tests/DateTimeFunctionsTest.cpp b/velox/functions/sparksql/tests/DateTimeFunctionsTest.cpp index 73391566b32d..ad2e10891976 100644 --- a/velox/functions/sparksql/tests/DateTimeFunctionsTest.cpp +++ b/velox/functions/sparksql/tests/DateTimeFunctionsTest.cpp @@ -42,16 +42,17 @@ class DateTimeFunctionsTest : public SparkFunctionBaseTest { static constexpr int8_t kMinTinyint = std::numeric_limits::min(); static constexpr int8_t kMaxTinyint = std::numeric_limits::max(); -int64_t getCurrentTimestamp(const std::optional& timeZone) { + int64_t getCurrentTimestamp(const std::optional& timeZone) { auto now = std::chrono::system_clock::now(); - auto tp = timeZone.has_value() - ? date::make_zoned( - timeZone.value(), now).get_local_time().time_since_epoch() - : now.time_since_epoch(); - auto since_epoch = std::chrono::duration_cast(tp); + auto tp = timeZone.has_value() ? date::make_zoned(timeZone.value(), now) + .get_local_time() + .time_since_epoch() + : now.time_since_epoch(); + auto since_epoch = + std::chrono::duration_cast(tp); LOG(INFO) << since_epoch.count(); return since_epoch.count(); -} + } protected: void setQueryTimeZone(const std::string& timeZone) {