aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/udfs/common/datetime2/datetime_udf.cpp
diff options
context:
space:
mode:
authorimunkin <imunkin@yandex-team.com>2024-11-26 19:46:40 +0300
committerimunkin <imunkin@yandex-team.com>2024-11-26 19:56:06 +0300
commit00deefc167044535c1a7adb6b825f46e47c1d643 (patch)
treef87a85464212d2ebb04853373baa45cd0ec6ea58 /yql/essentials/udfs/common/datetime2/datetime_udf.cpp
parent4b252d894a5c829dca409068912a233b6e275d56 (diff)
downloadydb-00deefc167044535c1a7adb6b825f46e47c1d643.tar.gz
YQL-18303: Introduce From* converters overloads for Timestamp64
commit_hash:9e4f59aa752831d906745606e53e3f36a370a0a4
Diffstat (limited to 'yql/essentials/udfs/common/datetime2/datetime_udf.cpp')
-rw-r--r--yql/essentials/udfs/common/datetime2/datetime_udf.cpp76
1 files changed, 40 insertions, 36 deletions
diff --git a/yql/essentials/udfs/common/datetime2/datetime_udf.cpp b/yql/essentials/udfs/common/datetime2/datetime_udf.cpp
index be41a0b8ed..3798557b91 100644
--- a/yql/essentials/udfs/common/datetime2/datetime_udf.cpp
+++ b/yql/essentials/udfs/common/datetime2/datetime_udf.cpp
@@ -548,14 +548,19 @@ TValue DoAddYears(const TValue& date, i64 years, const NUdf::IDateBuilder& build
return false;
}
- inline bool ValidateDatetime(ui32 datetime) {
- return datetime < MAX_DATETIME;
- }
+ template<typename TType>
+ inline bool Validate(typename TDataType<TType>::TLayout arg);
- inline bool ValidateTimestamp(ui64 timestamp) {
+ template<>
+ inline bool Validate<TTimestamp>(ui64 timestamp) {
return timestamp < MAX_TIMESTAMP;
}
+ template<>
+ inline bool Validate<TTimestamp64>(i64 timestamp) {
+ return timestamp >= MIN_TIMESTAMP64 && timestamp <= MAX_TIMESTAMP64;
+ }
+
inline bool ValidateInterval(i64 interval) {
return interval > -i64(MAX_TIMESTAMP) && interval < i64(MAX_TIMESTAMP);
}
@@ -1268,44 +1273,39 @@ TValue DoAddYears(const TValue& date, i64 years, const NUdf::IDateBuilder& build
// From*
- BEGIN_SIMPLE_STRICT_ARROW_UDF(TFromSeconds, TOptional<TTimestamp>(TAutoMap<ui32>)) {
- Y_UNUSED(valueBuilder);
- auto res = args[0].Get<ui32>();
- if (!ValidateDatetime(res)) {
- return TUnboxedValuePod();
- }
- return TUnboxedValuePod((ui64)(res * 1000000ull));
+ template<typename TInput, typename TOutput, i64 UsecMultiplier>
+ inline TUnboxedValuePod TFromConverter(TInput arg) {
+ using TLayout = TDataType<TOutput>::TLayout;
+ const TLayout usec = TLayout(arg) * UsecMultiplier;
+ return Validate<TOutput>(usec) ? TUnboxedValuePod(usec) : TUnboxedValuePod();
}
- using TFromSecondsKernel = TUnaryUnsafeFixedSizeFilterKernel<ui32, ui64,
- [] (ui32 seconds) { return std::make_pair(ui64(seconds * 1000000ull), ValidateDatetime(seconds)); }>;
- END_SIMPLE_ARROW_UDF(TFromSeconds, TFromSecondsKernel::Do);
- BEGIN_SIMPLE_STRICT_ARROW_UDF(TFromMilliseconds, TOptional<TTimestamp>(TAutoMap<ui64>)) {
- Y_UNUSED(valueBuilder);
- auto res = args[0].Get<ui64>();
- if (res >= MAX_TIMESTAMP / 1000u) {
- return TUnboxedValuePod();
- }
- return TUnboxedValuePod(res * 1000u);
- }
+ template<typename TInput, typename TOutput, i64 UsecMultiplier>
+ using TFromConverterKernel = TUnaryUnsafeFixedSizeFilterKernel<TInput,
+ typename TDataType<TOutput>::TLayout, [] (TInput arg) {
+ using TLayout = TDataType<TOutput>::TLayout;
+ const TLayout usec = TLayout(arg) * UsecMultiplier;
+ return std::make_pair(usec, Validate<TOutput>(usec));
+ }>;
- using TFromMillisecondsKernel = TUnaryUnsafeFixedSizeFilterKernel<ui64, ui64,
- [] (ui64 milliseconds) { return std::make_pair(ui64(milliseconds * 1000u), milliseconds < MAX_TIMESTAMP / 1000u); }>;
- END_SIMPLE_ARROW_UDF(TFromMilliseconds, TFromMillisecondsKernel::Do);
- BEGIN_SIMPLE_STRICT_ARROW_UDF(TFromMicroseconds, TOptional<TTimestamp>(TAutoMap<ui64>)) {
- Y_UNUSED(valueBuilder);
- auto res = args[0].Get<ui64>();
- if (!ValidateTimestamp(res)) {
- return TUnboxedValuePod();
- }
- return TUnboxedValuePod(res);
- }
+#define DATETIME_FROM_CONVERTER_UDF(name, retType, argType, usecMultiplier) \
+ BEGIN_SIMPLE_STRICT_ARROW_UDF(T##name, TOptional<retType>(TAutoMap<argType>)) { \
+ Y_UNUSED(valueBuilder); \
+ return TFromConverter<argType, retType, usecMultiplier>(args[0].Get<argType>()); \
+ } \
+ \
+ END_SIMPLE_ARROW_UDF(T##name, (TFromConverterKernel<argType, retType, usecMultiplier>::Do))
+
+ DATETIME_FROM_CONVERTER_UDF(FromSeconds, TTimestamp, ui32, UsecondsInSecond);
+ DATETIME_FROM_CONVERTER_UDF(FromMilliseconds, TTimestamp, ui64, UsecondsInMilliseconds);
+ DATETIME_FROM_CONVERTER_UDF(FromMicroseconds, TTimestamp, ui64, 1);
+
+ DATETIME_FROM_CONVERTER_UDF(FromSeconds64, TTimestamp64, i64, UsecondsInSecond);
+ DATETIME_FROM_CONVERTER_UDF(FromMilliseconds64, TTimestamp64, i64, UsecondsInMilliseconds);
+ DATETIME_FROM_CONVERTER_UDF(FromMicroseconds64, TTimestamp64, i64, 1);
- using TFromMicrosecondsKernel = TUnaryUnsafeFixedSizeFilterKernel<ui64, ui64,
- [] (ui64 timestamp) { return std::make_pair(timestamp, ValidateTimestamp(timestamp)); }>;
- END_SIMPLE_ARROW_UDF(TFromMicroseconds, TFromMicrosecondsKernel::Do);
template <typename TInput, i64 Multiplier>
using TIntervalFromKernel = TUnaryUnsafeFixedSizeFilterKernel<TInput, i64,
@@ -2426,6 +2426,10 @@ TValue DoAddYears(const TValue& date, i64 years, const NUdf::IDateBuilder& build
TFromMilliseconds,
TFromMicroseconds,
+ TFromSeconds64,
+ TFromMilliseconds64,
+ TFromMicroseconds64,
+
TIntervalFromDays,
TIntervalFromHours,
TIntervalFromMinutes,