Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions be/src/exprs/function/function_timezone_hour_minute.cpp
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();

Copy link
Copy Markdown
Contributor

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 zone retains a zone key, and timezone_hour/timezone_minute extract 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) returns 8/0 here 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.

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
2 changes: 2 additions & 0 deletions be/src/exprs/function/simple_function_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ void register_function_uuid_transforms(SimpleFunctionFactory& factory);
void register_function_grouping(SimpleFunctionFactory& factory);
void register_function_datetime_floor_ceil(SimpleFunctionFactory& factory);
void register_function_convert_tz(SimpleFunctionFactory& factory);
void register_function_timezone_hour_minute(SimpleFunctionFactory& factory);
void register_function_least_greast(SimpleFunctionFactory& factory);
void register_function_fake(SimpleFunctionFactory& factory);
void register_function_array(SimpleFunctionFactory& factory);
Expand Down Expand Up @@ -332,6 +333,7 @@ class SimpleFunctionFactory {
register_function_grouping(instance);
register_function_datetime_floor_ceil(instance);
register_function_convert_tz(instance);
register_function_timezone_hour_minute(instance);
register_function_least_greast(instance);
register_function_fake(instance);
register_function_encryption(instance);
Expand Down
4 changes: 3 additions & 1 deletion be/src/exprs/vectorized_fn_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,9 @@ bool VectorizedFnCall::can_push_down_to_index() const {

bool VectorizedFnCall::is_deterministic() const {
static const std::set<std::string> NON_DETERMINISTIC_FUNCTIONS = {
"random", "rand", "random_bytes", "uuid", "uuid_numeric"};
"random", "rand", "random_bytes", "uuid", "uuid_numeric",
// timezone_hour/timezone_minute depend on the session time_zone.
"timezone_hour", "timezone_minute"};
return !NON_DETERMINISTIC_FUNCTIONS.contains(_function_name) && VExpr::is_deterministic();
}

Expand Down
190 changes: 190 additions & 0 deletions be/test/exprs/function/function_timezone_hour_minute_test.cpp
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
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,8 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.TimeFormat;
import org.apache.doris.nereids.trees.expressions.functions.scalar.TimeToSec;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Timestamp;
import org.apache.doris.nereids.trees.expressions.functions.scalar.TimezoneHour;
import org.apache.doris.nereids.trees.expressions.functions.scalar.TimezoneMinute;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ToBase64;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ToBase64Binary;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ToBinary;
Expand Down Expand Up @@ -718,6 +720,8 @@ public class BuiltinScalarFunctions implements FunctionHelper {
scalar(Conv.class, "conv"),
scalar(ConvertTo.class, "convert_to"),
scalar(ConvertTz.class, "convert_tz"),
scalar(TimezoneHour.class, "timezone_hour"),
scalar(TimezoneMinute.class, "timezone_minute"),
scalar(Cos.class, "cos"),
scalar(Csc.class, "csc"),
scalar(Cosh.class, "cosh"),
Expand Down
Loading
Loading