-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add Time with Timezone to at_timezone function #15435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1902,6 +1902,38 @@ struct AtTimezoneFunction : public TimestampWithTimezoneSupport<T> { | |
| } | ||
| }; | ||
|
|
||
| template <typename T> | ||
| struct AtTimezoneTimeFunction : public TimestampWithTimezoneSupport<T> { | ||
| VELOX_DEFINE_FUNCTION_TYPES(T); | ||
|
|
||
| std::optional<int64_t> targetTimezoneID_; | ||
|
|
||
| FOLLY_ALWAYS_INLINE void initialize( | ||
| const std::vector<TypePtr>& /*inputTypes*/, | ||
| const core::QueryConfig& config, | ||
| const arg_type<TimeWithTimezone>* /*timeWithTz*/, | ||
| const arg_type<Varchar>* timezone) { | ||
| if (timezone) { | ||
| targetTimezoneID_ = tz::getTimeZoneID( | ||
| std::string_view(timezone->data(), timezone->size())); | ||
| } | ||
| } | ||
|
|
||
| FOLLY_ALWAYS_INLINE void call( | ||
| out_type<TimeWithTimezone>& result, | ||
| const arg_type<TimeWithTimezone>& timeWithTz, | ||
| const arg_type<Varchar>& timezone) { | ||
| const auto inputMs = unpackMillisUtc(*timeWithTz); | ||
| const auto targetTimezoneID = targetTimezoneID_.has_value() | ||
| ? targetTimezoneID_.value() | ||
| : tz::getTimeZoneID(std::string_view(timezone.data(), timezone.size())); | ||
|
|
||
| // Similar to timestamp version - only timezone ID changes, not the time | ||
| // value. | ||
| result = pack(inputMs, targetTimezoneID); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you need to call the time equivalent of time with timezone else you will create invalid time types. |
||
| } | ||
| }; | ||
|
|
||
| template <typename TExec> | ||
| struct ToMillisecondFunction { | ||
| VELOX_DEFINE_FUNCTION_TYPES(TExec); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| #include "velox/common/base/tests/GTestUtils.h" | ||
| #include "velox/external/tzdb/zoned_time.h" | ||
| #include "velox/functions/prestosql/tests/utils/FunctionBaseTest.h" | ||
| #include "velox/functions/prestosql/types/TimeWithTimezoneType.h" | ||
| #include "velox/functions/prestosql/types/TimestampWithTimeZoneType.h" | ||
| #include "velox/type/tz/TimeZoneMap.h" | ||
|
|
||
|
|
@@ -6135,6 +6136,123 @@ TEST_F(DateTimeFunctionsTest, atTimezoneTest) { | |
| EXPECT_EQ(at_timezone(std::nullopt, "Pacific/Fiji"), std::nullopt); | ||
| } | ||
|
|
||
| TEST_F(DateTimeFunctionsTest, atTimezoneTimeTest) { | ||
| const auto at_timezone_time = [&](std::optional<int64_t> timeWithTimezone, | ||
| std::optional<std::string> targetTimezone) { | ||
| return evaluateOnce<int64_t>( | ||
| "at_timezone(c0, c1)", | ||
| {TIME_WITH_TIME_ZONE(), VARCHAR()}, | ||
| timeWithTimezone, | ||
| targetTimezone); | ||
| }; | ||
|
|
||
| const int64_t millisTenOClockWarsawWinter = 9 * 60 * 60 * 1000; | ||
|
|
||
| EXPECT_EQ( | ||
| at_timezone_time( | ||
| pack(millisTenOClockWarsawWinter, tz::getTimeZoneID("Europe/Warsaw")), | ||
| "UTC"), | ||
| pack(millisTenOClockWarsawWinter, tz::getTimeZoneID("UTC"))); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isnt right, timewithtimezone supports bias encoding of offset minutes, not IANA timezone id's. |
||
|
|
||
| EXPECT_EQ( | ||
| at_timezone_time( | ||
| pack(millisTenOClockWarsawWinter, tz::getTimeZoneID("Europe/Warsaw")), | ||
| "+00:45"), | ||
| pack(millisTenOClockWarsawWinter, tz::getTimeZoneID("+00:45"))); | ||
|
|
||
| EXPECT_EQ( | ||
| at_timezone_time( | ||
| pack(millisTenOClockWarsawWinter, tz::getTimeZoneID("Europe/Warsaw")), | ||
| "America/New_York"), | ||
| pack(millisTenOClockWarsawWinter, tz::getTimeZoneID("America/New_York"))); | ||
|
|
||
| EXPECT_EQ( | ||
| at_timezone_time( | ||
| pack(millisTenOClockWarsawWinter, tz::getTimeZoneID("Europe/Warsaw")), | ||
| "Europe/Berlin"), | ||
| pack(millisTenOClockWarsawWinter, tz::getTimeZoneID("Europe/Berlin"))); | ||
|
|
||
| const int64_t millisTenOClockUTC = 10 * 60 * 60 * 1000; | ||
| EXPECT_EQ( | ||
| at_timezone_time( | ||
| pack(millisTenOClockUTC, tz::getTimeZoneID("UTC")), "UTC"), | ||
| pack(millisTenOClockUTC, tz::getTimeZoneID("UTC"))); | ||
|
|
||
| EXPECT_EQ( | ||
| at_timezone_time( | ||
| pack(millisTenOClockWarsawWinter, tz::getTimeZoneID("Europe/Warsaw")), | ||
| "Europe/Warsaw"), | ||
| pack(millisTenOClockWarsawWinter, tz::getTimeZoneID("Europe/Warsaw"))); | ||
|
|
||
| const int64_t millisTwoOClockWarsawWinter = 1 * 60 * 60 * 1000; | ||
| const int64_t millisTwentyOClockNewYork = 20 * 60 * 60 * 1000; | ||
| EXPECT_EQ( | ||
| at_timezone_time( | ||
| pack(millisTwoOClockWarsawWinter, tz::getTimeZoneID("Europe/Warsaw")), | ||
| "America/New_York"), | ||
| pack(millisTwentyOClockNewYork, tz::getTimeZoneID("America/New_York"))); | ||
|
|
||
| const int64_t millisTwentyTwoOClockNewYork = 22 * 60 * 60 * 1000; | ||
| const int64_t millisFourOClockWarsaw = 4 * 60 * 60 * 1000; | ||
| EXPECT_EQ( | ||
| at_timezone_time( | ||
| pack( | ||
| millisTwentyTwoOClockNewYork, | ||
| tz::getTimeZoneID("America/New_York")), | ||
| "Europe/Warsaw"), | ||
| pack(millisFourOClockWarsaw, tz::getTimeZoneID("Europe/Warsaw"))); | ||
|
|
||
| const int64_t millisMidnight = 0; | ||
| const int64_t millisTwentyThreeOClock = 23 * 60 * 60 * 1000; | ||
| EXPECT_EQ( | ||
| at_timezone_time( | ||
| pack(millisMidnight, tz::getTimeZoneID("+14:00")), "+13:00"), | ||
| pack(millisTwentyThreeOClock, tz::getTimeZoneID("+13:00"))); | ||
|
|
||
| const int64_t millisTwentyOClock = 20 * 60 * 60 * 1000; | ||
| EXPECT_EQ( | ||
| at_timezone_time( | ||
| pack(millisMidnight, tz::getTimeZoneID("+14:00")), "-14:00"), | ||
| pack(millisTwentyOClock, tz::getTimeZoneID("-14:00"))); | ||
|
|
||
| const int64_t millisMaxTime = | ||
| 23 * 60 * 60 * 1000 + 59 * 60 * 1000 + 59 * 1000 + 999; | ||
| const int64_t millisTwentyTwoFiftyNine = | ||
| 22 * 60 * 60 * 1000 + 59 * 60 * 1000 + 59 * 1000 + 999; | ||
| EXPECT_EQ( | ||
| at_timezone_time( | ||
| pack(millisMaxTime, tz::getTimeZoneID("+14:00")), "+13:00"), | ||
| pack(millisTwentyTwoFiftyNine, tz::getTimeZoneID("+13:00"))); | ||
|
|
||
| const int64_t millisNineteenFiftyNine = | ||
| 19 * 60 * 60 * 1000 + 59 * 60 * 1000 + 59 * 1000 + 999; | ||
| EXPECT_EQ( | ||
| at_timezone_time( | ||
| pack(millisMaxTime, tz::getTimeZoneID("+14:00")), "-14:00"), | ||
| pack(millisNineteenFiftyNine, tz::getTimeZoneID("-14:00"))); | ||
|
|
||
| const int64_t millisTenOClockKathmandu = 10 * 60 * 60 * 1000; | ||
| const int64_t millisFourFifteenUTC = 4 * 60 * 60 * 1000 + 15 * 60 * 1000; | ||
| EXPECT_EQ( | ||
| at_timezone_time( | ||
| pack(millisFourFifteenUTC, tz::getTimeZoneID("Asia/Kathmandu")), | ||
| "UTC"), | ||
| pack(millisFourFifteenUTC, tz::getTimeZoneID("UTC"))); | ||
|
|
||
| EXPECT_EQ( | ||
| at_timezone_time( | ||
| pack(millisTenOClockKathmandu, tz::getTimeZoneID("Asia/Kabul")), | ||
| "Asia/Kabul"), | ||
| pack(millisTenOClockKathmandu, tz::getTimeZoneID("Asia/Kabul"))); | ||
|
|
||
| EXPECT_EQ( | ||
| at_timezone_time( | ||
| pack(millisTenOClockUTC, tz::getTimeZoneID("UTC")), std::nullopt), | ||
| std::nullopt); | ||
|
|
||
| EXPECT_EQ(at_timezone_time(std::nullopt, "UTC"), std::nullopt); | ||
| } | ||
|
|
||
| TEST_F(DateTimeFunctionsTest, toMilliseconds) { | ||
| EXPECT_EQ( | ||
| 123, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This need only be an int16_t type..