-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[feature](function) Add Trino-compatible timezone_hour and timezone_m… #66860
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
Open
POSSIBLEwbwb
wants to merge
3
commits into
apache:master
Choose a base branch
from
POSSIBLEwbwb:timezone-hour-minute
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
41ddf2d
[feature](function) Add Trino-compatible timezone_hour and timezone_m…
POSSIBLEwbwb aee93c9
[fix](function) address review: session-timezone semantics, determini…
POSSIBLEwbwb 77bbf78
[fix](function) unwrap nullable before const in timezone_hour/timezon…
POSSIBLEwbwb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
be/src/exprs/function/function_timezone_hour_minute.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| #include <cctz/time_zone.h> | ||
|
|
||
| #include <cstdint> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <utility> | ||
|
|
||
| #include "common/status.h" | ||
| #include "core/assert_cast.h" | ||
| #include "core/block/block.h" | ||
| #include "core/block/column_numbers.h" | ||
| #include "core/column/column.h" | ||
| #include "core/column/column_const.h" | ||
| #include "core/column/column_nullable.h" | ||
| #include "core/column/column_vector.h" | ||
| #include "core/data_type/data_type.h" | ||
| #include "core/data_type/data_type_nullable.h" | ||
| #include "core/data_type/data_type_number.h" | ||
| #include "core/data_type/data_type_timestamptz.h" | ||
| #include "core/data_type/primitive_type.h" | ||
| #include "core/value/timestamptz_value.h" | ||
| #include "exprs/function_context.h" | ||
| #include "exprs/function/function.h" | ||
| #include "exprs/function/simple_function_factory.h" | ||
| #include "runtime/runtime_state.h" | ||
|
|
||
| namespace doris { | ||
|
|
||
| namespace { | ||
| constexpr int64_t SECONDS_PER_HOUR = 3600; | ||
| constexpr int64_t SECONDS_PER_MINUTE = 60; | ||
|
|
||
| // TIMESTAMPTZ values are stored as UTC instants without the input zone, so the | ||
| // offset extracted here is the offset of the session time zone at the instant. | ||
| // See TimestampTzValue for the storage design. | ||
| Status execute_timezone_offset_part(FunctionContext* context, Block& block, | ||
| const ColumnNumbers& arguments, uint32_t result, | ||
| size_t input_rows_count, bool extract_hour) { | ||
| ColumnPtr col = block.get_by_position(arguments[0]).column; | ||
| // Unwrap nullable and const wrappers in any nesting order so that | ||
| // ColumnNullable(ColumnConst(...)) and ColumnConst(ColumnNullable(...)) | ||
| // inputs both reach the plain ColumnTimeStampTz data below. | ||
| col = remove_nullable(col); | ||
| if (is_column_const(*col)) { | ||
| col = assert_cast<const ColumnConst&>(*col).convert_to_full_column(); | ||
| col = remove_nullable(col); | ||
| } | ||
| const auto* tz_column = assert_cast<const ColumnTimeStampTz*>(col.get()); | ||
| const auto& tz_data = tz_column->get_data(); | ||
|
|
||
| auto result_column = ColumnInt64::create(); | ||
| auto& result_data = result_column->get_data(); | ||
| result_data.resize(input_rows_count); | ||
|
|
||
| const cctz::time_zone& timezone = context->state()->timezone_obj(); | ||
| for (size_t i = 0; i < input_rows_count; ++i) { | ||
| int64_t offset = tz_data[i].utc_offset(timezone); | ||
| result_data[i] = extract_hour ? offset / SECONDS_PER_HOUR | ||
| : (offset % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE; | ||
| } | ||
|
|
||
| block.get_by_position(result).column = std::move(result_column); | ||
| return Status::OK(); | ||
| } | ||
| } // namespace | ||
|
|
||
| class FunctionTimezoneHour : public IFunction { | ||
| public: | ||
| static constexpr auto name = "timezone_hour"; | ||
|
|
||
| static FunctionPtr create() { return std::make_shared<FunctionTimezoneHour>(); } | ||
|
|
||
| String get_name() const override { return name; } | ||
|
|
||
| size_t get_number_of_arguments() const override { return 1; } | ||
|
|
||
| DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { | ||
| return std::make_shared<DataTypeInt64>(); | ||
| } | ||
|
|
||
| Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, | ||
| uint32_t result, size_t input_rows_count) const override { | ||
| return execute_timezone_offset_part(context, block, arguments, result, input_rows_count, | ||
| true); | ||
| } | ||
| }; | ||
|
|
||
| class FunctionTimezoneMinute : public IFunction { | ||
| public: | ||
| static constexpr auto name = "timezone_minute"; | ||
|
|
||
| static FunctionPtr create() { return std::make_shared<FunctionTimezoneMinute>(); } | ||
|
|
||
| String get_name() const override { return name; } | ||
|
|
||
| size_t get_number_of_arguments() const override { return 1; } | ||
|
|
||
| DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { | ||
| return std::make_shared<DataTypeInt64>(); | ||
| } | ||
|
|
||
| Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, | ||
| uint32_t result, size_t input_rows_count) const override { | ||
| return execute_timezone_offset_part(context, block, arguments, result, input_rows_count, | ||
| false); | ||
| } | ||
| }; | ||
|
|
||
| void register_function_timezone_hour_minute(SimpleFunctionFactory& factory) { | ||
| factory.register_function<FunctionTimezoneHour>(); | ||
| factory.register_function<FunctionTimezoneMinute>(); | ||
| } | ||
|
|
||
| } // namespace doris | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
be/test/exprs/function/function_timezone_hour_minute_test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| #include <cctz/time_zone.h> | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <chrono> | ||
| #include <memory> | ||
| #include <vector> | ||
|
|
||
| #include "core/assert_cast.h" | ||
| #include "core/block/block.h" | ||
| #include "core/block/column_numbers.h" | ||
| #include "core/column/column.h" | ||
| #include "core/column/column_nullable.h" | ||
| #include "core/column/column_vector.h" | ||
| #include "core/data_type/data_type.h" | ||
| #include "core/data_type/data_type_nullable.h" | ||
| #include "core/data_type/data_type_number.h" | ||
| #include "core/data_type/data_type_timestamptz.h" | ||
| #include "exprs/function/function.h" | ||
| #include "exprs/function/simple_function_factory.h" | ||
| #include "testutil/column_helper.h" | ||
| #include "testutil/datetime_ut_util.h" | ||
| #include "testutil/mock/mock_runtime_state.h" | ||
| #include "util/timezone_utils.h" | ||
|
|
||
| namespace doris { | ||
|
|
||
| class FunctionTimezoneHourMinuteTest : public testing::Test { | ||
| public: | ||
| void SetUp() override { | ||
| TimezoneUtils::load_offsets_to_cache(); | ||
| TimezoneUtils::load_timezones_to_cache(); | ||
| context._state = &_state; | ||
| arguments = {0}; | ||
| result = 1; | ||
| } | ||
|
|
||
| void set_session_timezone(const cctz::time_zone& tz) { _state._timezone_obj = tz; } | ||
|
|
||
| void check_result(const std::string& func_name, const Block& block, | ||
| const std::vector<int64_t>& expected) { | ||
| auto return_type = std::make_shared<DataTypeInt64>(); | ||
| FunctionBasePtr func = SimpleFunctionFactory::instance().get_function( | ||
| func_name, block.get_columns_with_type_and_name(), return_type); | ||
| ASSERT_NE(func, nullptr); | ||
| Block input_block = block; | ||
| input_block.insert({nullptr, return_type, "result"}); | ||
| auto st = func->execute(&context, input_block, arguments, result, input_block.rows()); | ||
| ASSERT_TRUE(st.ok()) << st.to_string(); | ||
| // Constant input may produce a const result column; materialize it | ||
| // before inspecting elements. | ||
| auto result_col = input_block.get_by_position(result).column->convert_to_full_column_if_const(); | ||
| const auto& col = assert_cast<const ColumnInt64&>(*result_col); | ||
| ASSERT_EQ(col.size(), expected.size()); | ||
| for (size_t i = 0; i < expected.size(); ++i) { | ||
| EXPECT_EQ(col.get_element(i), expected[i]) << "at row " << i; | ||
| } | ||
| } | ||
|
|
||
| MockRuntimeState _state; | ||
| FunctionContext context; | ||
| ColumnNumbers arguments; | ||
| uint32_t result; | ||
| }; | ||
|
|
||
| TEST_F(FunctionTimezoneHourMinuteTest, fixed_offset_shanghai) { | ||
| // Asia/Shanghai has a fixed UTC+08:00 offset without DST, so the offset | ||
| // part of the session timezone is the same for every instant. | ||
| set_session_timezone(cctz::fixed_time_zone(std::chrono::hours(8))); | ||
|
|
||
| auto block = ColumnHelper::create_block<DataTypeTimeStampTz>( | ||
| {make_timestamptz(2024, 1, 15, 12, 0, 0, 0), | ||
| make_timestamptz(2024, 7, 15, 12, 0, 0, 0)}); | ||
|
|
||
| check_result("timezone_hour", block, {8, 8}); | ||
| check_result("timezone_minute", block, {0, 0}); | ||
| } | ||
|
|
||
| TEST_F(FunctionTimezoneHourMinuteTest, dst_new_york) { | ||
| // America/New_York switches between EST (UTC-05:00) in winter and | ||
| // EDT (UTC-04:00) in summer, which is reflected in the returned offset. | ||
| cctz::time_zone tz; | ||
| ASSERT_TRUE(TimezoneUtils::find_cctz_time_zone("America/New_York", tz)); | ||
| set_session_timezone(tz); | ||
|
|
||
| auto winter_block = ColumnHelper::create_block<DataTypeTimeStampTz>( | ||
| {make_timestamptz(2024, 1, 15, 12, 0, 0, 0)}); | ||
| auto summer_block = ColumnHelper::create_block<DataTypeTimeStampTz>( | ||
| {make_timestamptz(2024, 7, 15, 12, 0, 0, 0)}); | ||
|
|
||
| check_result("timezone_hour", winter_block, {-5}); | ||
| check_result("timezone_minute", winter_block, {0}); | ||
| check_result("timezone_hour", summer_block, {-4}); | ||
| check_result("timezone_minute", summer_block, {0}); | ||
| } | ||
|
|
||
| TEST_F(FunctionTimezoneHourMinuteTest, fractional_offsets) { | ||
| // Trino returns truncated integer values for fractional offsets: | ||
| // timezone_hour(UTC-04:30) = -4 and timezone_minute(UTC-04:30) = -30. | ||
| set_session_timezone(cctz::fixed_time_zone(std::chrono::seconds(-4 * 3600 - 30 * 60))); | ||
| auto block = ColumnHelper::create_block<DataTypeTimeStampTz>( | ||
| {make_timestamptz(2024, 6, 20, 12, 0, 0, 0)}); | ||
| check_result("timezone_hour", block, {-4}); | ||
| check_result("timezone_minute", block, {-30}); | ||
|
|
||
| // Nepal Standard Time (UTC+05:45). | ||
| set_session_timezone(cctz::fixed_time_zone(std::chrono::seconds(5 * 3600 + 45 * 60))); | ||
| check_result("timezone_hour", block, {5}); | ||
| check_result("timezone_minute", block, {45}); | ||
| } | ||
|
|
||
| TEST_F(FunctionTimezoneHourMinuteTest, const_input) { | ||
| // TIMESTAMPTZ stores a UTC instant without the input zone; even when the | ||
| // value was produced by CAST with an explicit zone (here '2024-01-15 | ||
| // 12:00:00-04:30'), the extracted offset is the session zone's offset. | ||
| set_session_timezone(cctz::fixed_time_zone(std::chrono::hours(8))); | ||
|
|
||
| auto inner = ColumnTimeStampTz::create(); | ||
| inner->insert_value(make_timestamptz(2024, 1, 15, 16, 30, 0, 0)); | ||
| auto const_col = ColumnConst::create(std::move(inner), 3); | ||
| Block block; | ||
| block.insert({std::move(const_col), std::make_shared<DataTypeTimeStampTz>(), "arg"}); | ||
|
|
||
| check_result("timezone_hour", block, {8, 8, 8}); | ||
| check_result("timezone_minute", block, {0, 0, 0}); | ||
| } | ||
|
|
||
| TEST_F(FunctionTimezoneHourMinuteTest, session_zone_wins_over_input_zone) { | ||
| // The input instant is noon in UTC-04:30, i.e. 16:30 UTC. Trino would | ||
| // return -4/-30 from the input zone; Doris stores only the UTC instant | ||
| // and therefore returns the session zone offset (America/New_York in | ||
| // winter: -5/0). | ||
| cctz::time_zone tz; | ||
| ASSERT_TRUE(TimezoneUtils::find_cctz_time_zone("America/New_York", tz)); | ||
| set_session_timezone(tz); | ||
|
|
||
| auto block = ColumnHelper::create_block<DataTypeTimeStampTz>( | ||
| {make_timestamptz(2024, 1, 15, 16, 30, 0, 0)}); | ||
|
|
||
| check_result("timezone_hour", block, {-5}); | ||
| check_result("timezone_minute", block, {0}); | ||
| } | ||
|
|
||
| TEST_F(FunctionTimezoneHourMinuteTest, nullable_input) { | ||
| set_session_timezone(cctz::fixed_time_zone(std::chrono::hours(8))); | ||
|
|
||
| auto nested = ColumnTimeStampTz::create(); | ||
| nested->insert_value(make_timestamptz(2024, 1, 15, 12, 0, 0, 0)); | ||
| nested->insert_value(make_timestamptz(2024, 1, 15, 12, 0, 0, 0)); | ||
| auto null_map = ColumnUInt8::create(); | ||
| null_map->insert_value(0); | ||
| null_map->insert_value(1); | ||
| auto nullable_col = ColumnNullable::create(std::move(nested), std::move(null_map)); | ||
| Block block; | ||
| block.insert({std::move(nullable_col), make_nullable(std::make_shared<DataTypeTimeStampTz>()), | ||
| "arg"}); | ||
|
|
||
| auto return_type = make_nullable(std::make_shared<DataTypeInt64>()); | ||
| FunctionBasePtr func = SimpleFunctionFactory::instance().get_function( | ||
| "timezone_hour", block.get_columns_with_type_and_name(), return_type); | ||
| ASSERT_NE(func, nullptr); | ||
| block.insert({nullptr, return_type, "result"}); | ||
| auto st = func->execute(&context, block, arguments, result, block.rows()); | ||
| ASSERT_TRUE(st.ok()) << st.to_string(); | ||
|
|
||
| const auto& col = assert_cast<const ColumnNullable&>(*block.get_by_position(result).column); | ||
| const auto& data = assert_cast<const ColumnInt64&>(col.get_nested_column()); | ||
| ASSERT_EQ(col.size(), 2); | ||
| EXPECT_EQ(data.get_element(0), 8); | ||
| EXPECT_FALSE(col.is_null_at(0)); | ||
| EXPECT_TRUE(col.is_null_at(1)); | ||
| } | ||
|
|
||
| } // namespace doris |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
[P1] Extract the input value's zone, not the session zone
Trino's
timestamp with time zoneretains a zone key, andtimezone_hour/timezone_minuteextract that value's offset. Doris converts an explicit input zone to UTC and discards it, then this line substitutes the session zone. For example, with session+08:00,CAST('2024-01-15 12:00:00-04:30' AS TIMESTAMPTZ)returns8/0here instead of Trino's-4/-30. That silently breaks the advertised migration compatibility. Please resolve the contract by retaining/extracting the input zone (including serialization compatibility), or explicitly scope/rename the feature as session-offset extraction, and add an end-to-end case where the input and session zones differ.