From ebd35c1122eafc08590f9523bc26a9311b9f3ec3 Mon Sep 17 00:00:00 2001 From: Hongze Zhang Date: Fri, 25 Jun 2021 09:48:10 +0800 Subject: [PATCH] Add Gandiva functions for datetime (#23) --- cpp/src/gandiva/function_registry_datetime.cc | 25 ++++++++++++ cpp/src/gandiva/precompiled/time.cc | 40 +++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/cpp/src/gandiva/function_registry_datetime.cc b/cpp/src/gandiva/function_registry_datetime.cc index a5617c109515f..a5bc662530a19 100644 --- a/cpp/src/gandiva/function_registry_datetime.cc +++ b/cpp/src/gandiva/function_registry_datetime.cc @@ -148,6 +148,31 @@ std::vector GetDateTimeFunctionRegistry() { NativeFunction("castDATE", {}, DataTypeVector{timestamp()}, date32(), kResultNullIfNull, "castDATE32_timestamp"), + + NativeFunction("unix_date", {}, DataTypeVector{date32()}, int32(), + kResultNullIfNull, "unix_date_date32"), + + NativeFunction("unix_seconds", {}, DataTypeVector{timestampusutc()}, int64(), + kResultNullIfNull, "unix_seconds_timestampusutc"), + + NativeFunction("unix_millis", {}, DataTypeVector{timestampusutc()}, int64(), + kResultNullIfNull, "unix_millis_timestampusutc"), + + NativeFunction("unix_micros", {}, DataTypeVector{timestampusutc()}, int64(), + kResultNullIfNull, "unix_micros_timestampusutc"), + + NativeFunction("seconds_to_timestamp", {}, DataTypeVector{int64()}, timestampusutc(), + kResultNullIfNull, "seconds_to_timestamp_int64"), + + NativeFunction("millis_to_timestamp", {}, DataTypeVector{int64()}, timestampusutc(), + kResultNullIfNull, "millis_to_timestamp_int64"), + + NativeFunction("micros_to_timestamp", {}, DataTypeVector{int64()}, timestampusutc(), + kResultNullIfNull, "micros_to_timestamp_int64"), + + NativeFunction("date_diff", {}, DataTypeVector{date32(), date32()}, int32(), + kResultNullIfNull, "micros_to_timestamp_date32_date32"), + DATE_TYPES(LAST_DAY_SAFE_NULL_IF_NULL, last_day, {}); return date_time_fn_registry_; diff --git a/cpp/src/gandiva/precompiled/time.cc b/cpp/src/gandiva/precompiled/time.cc index b9efb820836d5..8ce7d40f903c8 100644 --- a/cpp/src/gandiva/precompiled/time.cc +++ b/cpp/src/gandiva/precompiled/time.cc @@ -965,4 +965,44 @@ CAST_NULLABLE_INTERVAL_DAY(int64) CAST_NULLABLE_INTERVAL_YEAR(int32) CAST_NULLABLE_INTERVAL_YEAR(int64) +FORCE_INLINE +gdv_int32 unix_date_date32(gdv_date32 in) { + return in; +} + +FORCE_INLINE +gdv_int64 unix_seconds_timestampusutc(gdv_timestamp in) { + return in / 1000000; +} + +FORCE_INLINE +gdv_int64 unix_millis_timestampusutc(gdv_timestamp in) { + return in / 1000; +} + +FORCE_INLINE +gdv_int64 unix_micros_timestampusutc(gdv_timestamp in) { + return in; +} + +FORCE_INLINE +gdv_timestamp seconds_to_timestamp_int64(gdv_int64 in) { + return in * 1000000; +} + +FORCE_INLINE +gdv_timestamp millis_to_timestamp_int64(gdv_int64 in) { + return in * 1000; +} + +FORCE_INLINE +gdv_timestamp micros_to_timestamp_int64(gdv_int64 in) { + return in; +} + +FORCE_INLINE +gdv_int32 micros_to_timestamp_date32_date32(gdv_date32 left, gdv_date32 right) { + return left - right; +} + } // extern "C"