Skip to content

Commit

Permalink
Add Spark function current_timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
acvictor committed Mar 26, 2024
1 parent 2d23b7b commit 25ec8d5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
8 changes: 8 additions & 0 deletions velox/docs/functions/spark/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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``.::
Expand Down
3 changes: 2 additions & 1 deletion velox/functions/sparksql/Register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ void registerFunctions(const std::string& prefix) {
registerFunction<FromUtcTimestampFunction, Timestamp, Timestamp, Varchar>(
{prefix + "from_utc_timestamp"});

registerFunction<CurrentTimestampFunction, Timestamp>({prefix + "current_timestamp"});
registerFunction<CurrentTimestampFunction, Timestamp>(
{prefix + "current_timestamp", prefix + "now"});

registerFunction<UnixDateFunction, int32_t, Date>({prefix + "unix_date"});

Expand Down
15 changes: 8 additions & 7 deletions velox/functions/sparksql/tests/DateTimeFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@ class DateTimeFunctionsTest : public SparkFunctionBaseTest {
static constexpr int8_t kMinTinyint = std::numeric_limits<int8_t>::min();
static constexpr int8_t kMaxTinyint = std::numeric_limits<int8_t>::max();

int64_t getCurrentTimestamp(const std::optional<std::string>& timeZone) {
int64_t getCurrentTimestamp(const std::optional<std::string>& 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<std::chrono::microseconds>(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<std::chrono::microseconds>(tp);
LOG(INFO) << since_epoch.count();
return since_epoch.count();
}
}

protected:
void setQueryTimeZone(const std::string& timeZone) {
Expand Down

0 comments on commit 25ec8d5

Please sign in to comment.