From 55740c7e151e2b6161990c934a01e87546ebb3ce Mon Sep 17 00:00:00 2001 From: kche0169 Date: Mon, 27 Jan 2025 11:48:43 +0800 Subject: [PATCH 1/7] add current date, current time and current timestamp and their unit tests(Support passing time zone) --- src/function/builtin_functions.cpp | 6 + src/function/scalar/current_date.cpp | 67 ++++++++++ src/function/scalar/current_date.cppm | 27 ++++ src/function/scalar/current_time.cpp | 66 ++++++++++ src/function/scalar/current_time.cppm | 27 ++++ src/function/scalar/current_timestamp.cpp | 65 ++++++++++ src/function/scalar/current_timestamp.cppm | 27 ++++ .../scalar/current_date_functions.cpp | 114 +++++++++++++++++ .../scalar/current_time_functions.cpp | 115 ++++++++++++++++++ .../scalar/current_timestamp_functions.cpp | 115 ++++++++++++++++++ 10 files changed, 629 insertions(+) create mode 100644 src/function/scalar/current_date.cpp create mode 100644 src/function/scalar/current_date.cppm create mode 100644 src/function/scalar/current_time.cpp create mode 100644 src/function/scalar/current_time.cppm create mode 100644 src/function/scalar/current_timestamp.cpp create mode 100644 src/function/scalar/current_timestamp.cppm create mode 100644 src/unit_test/function/scalar/current_date_functions.cpp create mode 100644 src/unit_test/function/scalar/current_time_functions.cpp create mode 100644 src/unit_test/function/scalar/current_timestamp_functions.cpp diff --git a/src/function/builtin_functions.cpp b/src/function/builtin_functions.cpp index e5fa0154a7..645053e2ef 100644 --- a/src/function/builtin_functions.cpp +++ b/src/function/builtin_functions.cpp @@ -48,6 +48,9 @@ import like; import minus; import modulo; import multiply; +import current_date; +import current_time; +import current_timestamp; import century; import year; import month; @@ -166,6 +169,9 @@ void BuiltinFunctions::RegisterScalarFunction() { RegisterPositionFunction(catalog_ptr_); // date and time functions + RegisterCurrentDateFunction(catalog_ptr_); + RegisterCurrentTimeFunction(catalog_ptr_); + RegisterCurrentTimestampFunction(catalog_ptr_); RegisterCenturyFunction(catalog_ptr_); RegisterYearFunction(catalog_ptr_); RegisterMonthFunction(catalog_ptr_); diff --git a/src/function/scalar/current_date.cpp b/src/function/scalar/current_date.cpp new file mode 100644 index 0000000000..cec463d018 --- /dev/null +++ b/src/function/scalar/current_date.cpp @@ -0,0 +1,67 @@ +// Copyright(C) 2025 InfiniFlow, Inc. All rights reserved. +// +// Licensed 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 +// +// https://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. +module; +#include +module current_date; +import stl; +import catalog; +import status; +import logical_type; +import infinity_exception; +import scalar_function; +import scalar_function_set; +import third_party; +import internal_types; +import data_type; +import column_vector; + +namespace infinity { +using namespace std::chrono; +struct CurrentDateFunction { + template + static inline void Run(TA &left, TB &result) { + Status status = Status::NotSupport("Not implemented"); + RecoverableError(status); + return; + } + +}; + +template <> +inline void CurrentDateFunction::Run(VarcharT &left, DateT &result) { + // auto zone = std::chrono::locate_zone(left); + auto now = system_clock::now(); + auto sys_days = std::chrono::floor(now); + // std::chrono::zoned_time zt{zone, now}; + // auto tp = zt.get_local_time(); + // auto sys_days = std::chrono::floor(tp); + // std::chrono::year_month_day ymd = std::chrono::year_month_day(sys_days); + result.value = sys_days.time_since_epoch().count(); +} + +void RegisterCurrentDateFunction(const UniquePtr &catalog_ptr) { + String func_name = "currentdate"; + + SharedPtr function_set_ptr = MakeShared(func_name); + + ScalarFunction current_date_function(func_name, + {DataType(LogicalType::kVarchar)}, + DataType(LogicalType::kDate), + &ScalarFunction::UnaryFunction); + function_set_ptr->AddFunction(current_date_function); + + Catalog::AddFunctionSet(catalog_ptr.get(), function_set_ptr); +} + +} // namespace infinity \ No newline at end of file diff --git a/src/function/scalar/current_date.cppm b/src/function/scalar/current_date.cppm new file mode 100644 index 0000000000..1a348918a4 --- /dev/null +++ b/src/function/scalar/current_date.cppm @@ -0,0 +1,27 @@ +// Copyright(C) 2025 InfiniFlow, Inc. All rights reserved. +// +// Licensed 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 +// +// https://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. + +module; + +export module current_date; + +import stl; + +namespace infinity { + +class Catalog; + +export void RegisterCurrentDateFunction(const UniquePtr &catalog_ptr); + +} // namespace infinity \ No newline at end of file diff --git a/src/function/scalar/current_time.cpp b/src/function/scalar/current_time.cpp new file mode 100644 index 0000000000..82baad2e74 --- /dev/null +++ b/src/function/scalar/current_time.cpp @@ -0,0 +1,66 @@ +// Copyright(C) 2025 InfiniFlow, Inc. All rights reserved. +// +// Licensed 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 +// +// https://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. +module; +#include +module current_time; +import stl; +import catalog; +import status; +import logical_type; +import infinity_exception; +import scalar_function; +import scalar_function_set; +import third_party; +import internal_types; +import data_type; +import column_vector; + +namespace infinity { +using namespace std::chrono; +struct CurrentTimeFunction { + template + static inline void Run(TA &left, TB &result) { + Status status = Status::NotSupport("Not implemented"); + RecoverableError(status); + } + +}; + +// template <> +// inline void CurrentTimeFunction::Run(VarcharT &left, DateT &result) { +// auto zone = locate_zone(left); +// auto now = system_clock::now(); +// // auto sys_days = std::chrono::floor(now); +// std::chrono::zoned_time zt{zone, now}; +// auto tp = zt.get_local_time(); +// auto sys_days = std::chrono::floor(tp); +// std::chrono::year_month_day ymd = std::chrono::year_month_day(sys_days); +// DateT::OuterYMD2Date(ymd, result); +// } + +void RegisterCurrentTimeFunction(const UniquePtr &catalog_ptr) { + String func_name = "currenttime"; + + SharedPtr function_set_ptr = MakeShared(func_name); + + ScalarFunction current_time_function(func_name, + {DataType(LogicalType::kVarchar)}, + {DataType(LogicalType::kTime)}, + &ScalarFunction::UnaryFunction); + function_set_ptr->AddFunction(current_time_function); + + Catalog::AddFunctionSet(catalog_ptr.get(), function_set_ptr); +} + +} // namespace infinity \ No newline at end of file diff --git a/src/function/scalar/current_time.cppm b/src/function/scalar/current_time.cppm new file mode 100644 index 0000000000..0ea71c29bf --- /dev/null +++ b/src/function/scalar/current_time.cppm @@ -0,0 +1,27 @@ +// Copyright(C) 2025 InfiniFlow, Inc. All rights reserved. +// +// Licensed 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 +// +// https://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. + +module; + +export module current_time; + +import stl; + +namespace infinity { + +class Catalog; + +export void RegisterCurrentTimeFunction(const UniquePtr &catalog_ptr); + +} // namespace infinity \ No newline at end of file diff --git a/src/function/scalar/current_timestamp.cpp b/src/function/scalar/current_timestamp.cpp new file mode 100644 index 0000000000..2b60eb6d08 --- /dev/null +++ b/src/function/scalar/current_timestamp.cpp @@ -0,0 +1,65 @@ +// Copyright(C) 2025 InfiniFlow, Inc. All rights reserved. +// +// Licensed 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 +// +// https://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. +module; +module current_timestamp; +import stl; +import catalog; +import status; +import logical_type; +import infinity_exception; +import scalar_function; +import scalar_function_set; +import third_party; +import internal_types; +import data_type; +import column_vector; + +namespace infinity { +using namespace std::chrono; +struct CurrentTimestampFunction { + template + static inline void Run(TA &left, TB &result) { + Status status = Status::NotSupport("Not implemented"); + RecoverableError(status); + } + +}; + +// template <> +// inline void CurrentDateFunction::Run(VarcharT &left, DateT &result) { +// auto zone = locate_zone(left); +// auto now = system_clock::now(); +// // auto sys_days = std::chrono::floor(now); +// std::chrono::zoned_time zt{zone, now}; +// auto tp = zt.get_local_time(); +// auto sys_days = std::chrono::floor(tp); +// std::chrono::year_month_day ymd = std::chrono::year_month_day(sys_days); +// DateT::OuterYMD2Date(ymd, result); +// } + +void RegisterCurrentTimestampFunction(const UniquePtr &catalog_ptr) { + String func_name = "currenttimestamp"; + + SharedPtr function_set_ptr = MakeShared(func_name); + + ScalarFunction current_timestamp_function(func_name, + {DataType(LogicalType::kVarchar)}, + DataType(LogicalType::kDate), + &ScalarFunction::UnaryFunction); + function_set_ptr->AddFunction(current_timestamp_function); + + Catalog::AddFunctionSet(catalog_ptr.get(), function_set_ptr); +} + +} // namespace infinity \ No newline at end of file diff --git a/src/function/scalar/current_timestamp.cppm b/src/function/scalar/current_timestamp.cppm new file mode 100644 index 0000000000..11aef9add1 --- /dev/null +++ b/src/function/scalar/current_timestamp.cppm @@ -0,0 +1,27 @@ +// Copyright(C) 2025 InfiniFlow, Inc. All rights reserved. +// +// Licensed 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 +// +// https://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. + +module; + +export module current_timestamp; + +import stl; + +namespace infinity { + +class Catalog; + +export void RegisterCurrentTimestampFunction(const UniquePtr &catalog_ptr); + +} // namespace infinity \ No newline at end of file diff --git a/src/unit_test/function/scalar/current_date_functions.cpp b/src/unit_test/function/scalar/current_date_functions.cpp new file mode 100644 index 0000000000..683b52ae43 --- /dev/null +++ b/src/unit_test/function/scalar/current_date_functions.cpp @@ -0,0 +1,114 @@ +// Copyright(C) 2025 InfiniFlow, Inc. All rights reserved. +// +// Licensed 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 +// +// https://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 "gtest/gtest.h" + +import stl; +import base_test; +import infinity_exception; +import infinity_context; + +import catalog; +import logger; + +import default_values; +import value; + +import base_expression; +import column_expression; +import column_vector; +import data_block; + +import function_set; +import function; + +import global_resource_usage; + +import data_type; +import internal_types; +import logical_type; + +import scalar_function; +import scalar_function_set; + +import current_date; +import third_party; + +using namespace infinity; + +class CurrentDateFunctionsTest : public BaseTestParamStr {}; + +INSTANTIATE_TEST_SUITE_P(TestWithDifferentParams, CurrentDateFunctionsTest, ::testing::Values(BaseTestParamStr::NULL_CONFIG_PATH)); + +TEST_P(CurrentDateFunctionsTest, current_date_func) { + using namespace infinity; + + UniquePtr catalog_ptr = MakeUnique(); + + RegisterCurrentDateFunction(catalog_ptr); + + String op = "currentdate"; + + SharedPtr function_set = Catalog::GetFunctionSetByName(catalog_ptr.get(), op); + EXPECT_EQ(function_set->type_, FunctionType::kScalar); + SharedPtr scalar_function_set = std::static_pointer_cast(function_set); + + { + Vector> inputs; + + DataType data_type1(LogicalType::kVarchar); + SharedPtr result_type = MakeShared(LogicalType::kDate); + SharedPtr col1_expr_ptr = MakeShared(data_type1, "t1", 1, "c1", 0, 0); + // SharedPtr col2_expr_ptr = MakeShared(data_type2, "t1", 1, "c2", 1, 0); + + inputs.emplace_back(col1_expr_ptr); + + ScalarFunction func = scalar_function_set->GetMostMatchFunction(inputs); + EXPECT_STREQ("currentdate(Varchar)->Date", func.ToString().c_str()); + + Vector> column_types; + column_types.emplace_back(MakeShared(data_type1)); + // column_types.emplace_back(MakeShared(data_type2)); + + SizeT row_count = DEFAULT_VECTOR_SIZE; + + DataBlock data_block; + data_block.Init(column_types); + + for (SizeT i = 0; i < row_count; ++i) { + data_block.AppendValue(0, Value::MakeVarchar("Asia/Shanghai")); + // data_block.AppendValue(1, Value::MakeBigInt(static_cast(i))); + } + data_block.Finalize(); + + for (SizeT i = 0; i < row_count; ++i) { + Value v1 = data_block.GetValue(0, i); + // Value v2 = data_block.GetValue(1, i); + EXPECT_EQ(v1.type_.type(), LogicalType::kVarchar); + // EXPECT_EQ(v2.type_.type(), LogicalType::kBigInt); + // EXPECT_FLOAT_EQ(v1.value_.float32, static_cast(i)); + // EXPECT_EQ(v2.value_.big_int, static_cast(i)); + } + + SharedPtr result = MakeShared(result_type); + result->Initialize(); + func.function_(data_block, result); + + for (SizeT i = 0; i < row_count; ++i) { + Value v = result->GetValue(i); + EXPECT_EQ(v.type_.type(), LogicalType::kDate); + } + } +} diff --git a/src/unit_test/function/scalar/current_time_functions.cpp b/src/unit_test/function/scalar/current_time_functions.cpp new file mode 100644 index 0000000000..b535c74e85 --- /dev/null +++ b/src/unit_test/function/scalar/current_time_functions.cpp @@ -0,0 +1,115 @@ +// Copyright(C) 2025 InfiniFlow, Inc. All rights reserved. +// +// Licensed 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 +// +// https://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 "gtest/gtest.h" + +import stl; +import base_test; +import infinity_exception; +import infinity_context; + +import catalog; +import logger; + +import default_values; +import value; + +import base_expression; +import column_expression; +import column_vector; +import data_block; + +import function_set; +import function; + +import global_resource_usage; + +import data_type; +import internal_types; +import logical_type; + +import scalar_function; +import scalar_function_set; + +import current_time; +import third_party; + +using namespace infinity; + +class CurrentTimeFunctionsTest : public BaseTestParamStr {}; + +INSTANTIATE_TEST_SUITE_P(TestWithDifferentParams, CurrentTimeFunctionsTest, ::testing::Values(BaseTestParamStr::NULL_CONFIG_PATH)); + +TEST_P(CurrentTimeFunctionsTest, current_time_func) { + using namespace infinity; + + UniquePtr catalog_ptr = MakeUnique(); + + RegisterCurrentTimeFunction(catalog_ptr); + + String op = "currenttime"; + + SharedPtr function_set = Catalog::GetFunctionSetByName(catalog_ptr.get(), op); + EXPECT_EQ(function_set->type_, FunctionType::kScalar); + SharedPtr scalar_function_set = std::static_pointer_cast(function_set); + + { + Vector> inputs; + + DataType data_type1(LogicalType::kVarchar); + // DataType data_type2(LogicalType::kBigInt); + SharedPtr result_type = MakeShared(LogicalType::kTime); + SharedPtr col1_expr_ptr = MakeShared(data_type1, "t1", 1, "c1", 0, 0); + + inputs.emplace_back(col1_expr_ptr); + // inputs.emplace_back(col2_expr_ptr); + + ScalarFunction func = scalar_function_set->GetMostMatchFunction(inputs); + EXPECT_STREQ("currenttime(Varchar)->Time", func.ToString().c_str()); + + Vector> column_types; + column_types.emplace_back(MakeShared(data_type1)); + // column_types.emplace_back(MakeShared(data_type2)); + + SizeT row_count = DEFAULT_VECTOR_SIZE; + + DataBlock data_block; + data_block.Init(column_types); + + for (SizeT i = 0; i < row_count; ++i) { + data_block.AppendValue(0, Value::MakeVarchar("Asia/Shanghai")); + } + data_block.Finalize(); + + for (SizeT i = 0; i < row_count; ++i) { + Value v1 = data_block.GetValue(0, i); + // Value v2 = data_block.GetValue(1, i); + EXPECT_EQ(v1.type_.type(), LogicalType::kVarchar); + // EXPECT_EQ(v2.type_.type(), LogicalType::kBigInt); + // EXPECT_FLOAT_EQ(v1.value_.float32, static_cast(i)); + // EXPECT_EQ(v2.value_.big_int, static_cast(i)); + } + + SharedPtr result = MakeShared(result_type); + result->Initialize(); + func.function_(data_block, result); + + for (SizeT i = 0; i < row_count; ++i) { + Value v = result->GetValue(i); + EXPECT_EQ(v.type_.type(), LogicalType::kTime); + } + } + +} diff --git a/src/unit_test/function/scalar/current_timestamp_functions.cpp b/src/unit_test/function/scalar/current_timestamp_functions.cpp new file mode 100644 index 0000000000..af60e6a503 --- /dev/null +++ b/src/unit_test/function/scalar/current_timestamp_functions.cpp @@ -0,0 +1,115 @@ +// Copyright(C) 2025 InfiniFlow, Inc. All rights reserved. +// +// Licensed 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 +// +// https://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 "gtest/gtest.h" + +import stl; +import base_test; +import infinity_exception; +import infinity_context; + +import catalog; +import logger; + +import default_values; +import value; + +import base_expression; +import column_expression; +import column_vector; +import data_block; + +import function_set; +import function; + +import global_resource_usage; + +import data_type; +import internal_types; +import logical_type; + +import scalar_function; +import scalar_function_set; + +import current_timestamp; +import third_party; + +using namespace infinity; + +class CurrentTimestampFunctionsTest : public BaseTestParamStr {}; + +INSTANTIATE_TEST_SUITE_P(TestWithDifferentParams, CurrentTimestampFunctionsTest, ::testing::Values(BaseTestParamStr::NULL_CONFIG_PATH)); + +TEST_P(CurrentTimestampFunctionsTest, current_timestamp_func) { + using namespace infinity; + + UniquePtr catalog_ptr = MakeUnique(); + + RegisterCurrentTimestampFunction(catalog_ptr); + + String op = "currenttimestamp"; + + SharedPtr function_set = Catalog::GetFunctionSetByName(catalog_ptr.get(), op); + EXPECT_EQ(function_set->type_, FunctionType::kScalar); + SharedPtr scalar_function_set = std::static_pointer_cast(function_set); + + { + Vector> inputs; + + DataType data_type1(LogicalType::kVarchar); + // DataType data_type2(LogicalType::kBigInt); + SharedPtr result_type = MakeShared(LogicalType::kTimestamp); + SharedPtr col1_expr_ptr = MakeShared(data_type1, "t1", 1, "c1", 0, 0); + // SharedPtr col2_expr_ptr = MakeShared(data_type2, "t1", 1, "c2", 1, 0); + + inputs.emplace_back(col1_expr_ptr); + ScalarFunction func = scalar_function_set->GetMostMatchFunction(inputs); + EXPECT_STREQ("currenttimestamp(Varchar)->Timestamp", func.ToString().c_str()); + + Vector> column_types; + column_types.emplace_back(MakeShared(data_type1)); + // column_types.emplace_back(MakeShared(data_type2)); + + SizeT row_count = DEFAULT_VECTOR_SIZE; + + DataBlock data_block; + data_block.Init(column_types); + + for (SizeT i = 0; i < row_count; ++i) { + data_block.AppendValue(0, Value::MakeVarchar("Asia/Shanghai")); + // data_block.AppendValue(1, Value::MakeBigInt(static_cast(i))); + } + data_block.Finalize(); + + for (SizeT i = 0; i < row_count; ++i) { + Value v1 = data_block.GetValue(0, i); + Value v2 = data_block.GetValue(1, i); + EXPECT_EQ(v1.type_.type(), LogicalType::kVarchar); + // EXPECT_EQ(v2.type_.type(), LogicalType::kBigInt); + // EXPECT_FLOAT_EQ(v1.value_.float32, static_cast(i)); + // EXPECT_EQ(v2.value_.big_int, static_cast(i)); + } + + SharedPtr result = MakeShared(result_type); + result->Initialize(); + func.function_(data_block, result); + + for (SizeT i = 0; i < row_count; ++i) { + Value v = result->GetValue(i); + EXPECT_EQ(v.type_.type(), LogicalType::kTimestamp); + } + } + +} From b0ed62492cd9d3d4e88cf6d6fe27f00945d766e2 Mon Sep 17 00:00:00 2001 From: kche0169 Date: Mon, 27 Jan 2025 15:11:22 +0800 Subject: [PATCH 2/7] update current time and its unit test --- src/function/scalar/current_date.cpp | 5 ----- src/function/scalar/current_time.cpp | 20 ++++++++----------- .../scalar/current_time_functions.cpp | 7 ------- 3 files changed, 8 insertions(+), 24 deletions(-) diff --git a/src/function/scalar/current_date.cpp b/src/function/scalar/current_date.cpp index cec463d018..31405238ab 100644 --- a/src/function/scalar/current_date.cpp +++ b/src/function/scalar/current_date.cpp @@ -40,13 +40,8 @@ struct CurrentDateFunction { template <> inline void CurrentDateFunction::Run(VarcharT &left, DateT &result) { - // auto zone = std::chrono::locate_zone(left); auto now = system_clock::now(); auto sys_days = std::chrono::floor(now); - // std::chrono::zoned_time zt{zone, now}; - // auto tp = zt.get_local_time(); - // auto sys_days = std::chrono::floor(tp); - // std::chrono::year_month_day ymd = std::chrono::year_month_day(sys_days); result.value = sys_days.time_since_epoch().count(); } diff --git a/src/function/scalar/current_time.cpp b/src/function/scalar/current_time.cpp index 82baad2e74..21b4e8b831 100644 --- a/src/function/scalar/current_time.cpp +++ b/src/function/scalar/current_time.cpp @@ -37,17 +37,13 @@ struct CurrentTimeFunction { }; -// template <> -// inline void CurrentTimeFunction::Run(VarcharT &left, DateT &result) { -// auto zone = locate_zone(left); -// auto now = system_clock::now(); -// // auto sys_days = std::chrono::floor(now); -// std::chrono::zoned_time zt{zone, now}; -// auto tp = zt.get_local_time(); -// auto sys_days = std::chrono::floor(tp); -// std::chrono::year_month_day ymd = std::chrono::year_month_day(sys_days); -// DateT::OuterYMD2Date(ymd, result); -// } +template <> +inline void CurrentTimeFunction::Run(VarcharT &left, TimeT &result) { + auto now = system_clock::now(); + auto sys_days = std::chrono::floor(now); + auto sys_secs = std::chrono::floor(now); + result.value = static_cast(sys_secs.time_since_epoch().count() - sys_days.time_since_epoch().count()); +} void RegisterCurrentTimeFunction(const UniquePtr &catalog_ptr) { String func_name = "currenttime"; @@ -56,7 +52,7 @@ void RegisterCurrentTimeFunction(const UniquePtr &catalog_ptr) { ScalarFunction current_time_function(func_name, {DataType(LogicalType::kVarchar)}, - {DataType(LogicalType::kTime)}, + DataType(LogicalType::kTime), &ScalarFunction::UnaryFunction); function_set_ptr->AddFunction(current_time_function); diff --git a/src/unit_test/function/scalar/current_time_functions.cpp b/src/unit_test/function/scalar/current_time_functions.cpp index b535c74e85..f5c717e47c 100644 --- a/src/unit_test/function/scalar/current_time_functions.cpp +++ b/src/unit_test/function/scalar/current_time_functions.cpp @@ -69,19 +69,16 @@ TEST_P(CurrentTimeFunctionsTest, current_time_func) { Vector> inputs; DataType data_type1(LogicalType::kVarchar); - // DataType data_type2(LogicalType::kBigInt); SharedPtr result_type = MakeShared(LogicalType::kTime); SharedPtr col1_expr_ptr = MakeShared(data_type1, "t1", 1, "c1", 0, 0); inputs.emplace_back(col1_expr_ptr); - // inputs.emplace_back(col2_expr_ptr); ScalarFunction func = scalar_function_set->GetMostMatchFunction(inputs); EXPECT_STREQ("currenttime(Varchar)->Time", func.ToString().c_str()); Vector> column_types; column_types.emplace_back(MakeShared(data_type1)); - // column_types.emplace_back(MakeShared(data_type2)); SizeT row_count = DEFAULT_VECTOR_SIZE; @@ -95,11 +92,7 @@ TEST_P(CurrentTimeFunctionsTest, current_time_func) { for (SizeT i = 0; i < row_count; ++i) { Value v1 = data_block.GetValue(0, i); - // Value v2 = data_block.GetValue(1, i); EXPECT_EQ(v1.type_.type(), LogicalType::kVarchar); - // EXPECT_EQ(v2.type_.type(), LogicalType::kBigInt); - // EXPECT_FLOAT_EQ(v1.value_.float32, static_cast(i)); - // EXPECT_EQ(v2.value_.big_int, static_cast(i)); } SharedPtr result = MakeShared(result_type); From 2a832738ae2985757b19327cab4f7c897205987d Mon Sep 17 00:00:00 2001 From: kche0169 Date: Mon, 27 Jan 2025 15:13:19 +0800 Subject: [PATCH 3/7] remove comments in current date unit test --- src/unit_test/function/scalar/current_date_functions.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/unit_test/function/scalar/current_date_functions.cpp b/src/unit_test/function/scalar/current_date_functions.cpp index 683b52ae43..7ddea74bd3 100644 --- a/src/unit_test/function/scalar/current_date_functions.cpp +++ b/src/unit_test/function/scalar/current_date_functions.cpp @@ -71,7 +71,6 @@ TEST_P(CurrentDateFunctionsTest, current_date_func) { DataType data_type1(LogicalType::kVarchar); SharedPtr result_type = MakeShared(LogicalType::kDate); SharedPtr col1_expr_ptr = MakeShared(data_type1, "t1", 1, "c1", 0, 0); - // SharedPtr col2_expr_ptr = MakeShared(data_type2, "t1", 1, "c2", 1, 0); inputs.emplace_back(col1_expr_ptr); @@ -80,7 +79,6 @@ TEST_P(CurrentDateFunctionsTest, current_date_func) { Vector> column_types; column_types.emplace_back(MakeShared(data_type1)); - // column_types.emplace_back(MakeShared(data_type2)); SizeT row_count = DEFAULT_VECTOR_SIZE; @@ -89,17 +87,12 @@ TEST_P(CurrentDateFunctionsTest, current_date_func) { for (SizeT i = 0; i < row_count; ++i) { data_block.AppendValue(0, Value::MakeVarchar("Asia/Shanghai")); - // data_block.AppendValue(1, Value::MakeBigInt(static_cast(i))); } data_block.Finalize(); for (SizeT i = 0; i < row_count; ++i) { Value v1 = data_block.GetValue(0, i); - // Value v2 = data_block.GetValue(1, i); EXPECT_EQ(v1.type_.type(), LogicalType::kVarchar); - // EXPECT_EQ(v2.type_.type(), LogicalType::kBigInt); - // EXPECT_FLOAT_EQ(v1.value_.float32, static_cast(i)); - // EXPECT_EQ(v2.value_.big_int, static_cast(i)); } SharedPtr result = MakeShared(result_type); From fee7a18f3f9515503fb9089516bf0db0c045239e Mon Sep 17 00:00:00 2001 From: kche0169 Date: Mon, 27 Jan 2025 15:52:37 +0800 Subject: [PATCH 4/7] update current timestamp --- src/function/scalar/current_timestamp.cpp | 23 +++++++++---------- .../scalar/current_timestamp_functions.cpp | 10 +------- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/src/function/scalar/current_timestamp.cpp b/src/function/scalar/current_timestamp.cpp index 2b60eb6d08..83cf24e4d6 100644 --- a/src/function/scalar/current_timestamp.cpp +++ b/src/function/scalar/current_timestamp.cpp @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. module; +#include module current_timestamp; import stl; import catalog; @@ -36,17 +37,15 @@ struct CurrentTimestampFunction { }; -// template <> -// inline void CurrentDateFunction::Run(VarcharT &left, DateT &result) { -// auto zone = locate_zone(left); -// auto now = system_clock::now(); -// // auto sys_days = std::chrono::floor(now); -// std::chrono::zoned_time zt{zone, now}; -// auto tp = zt.get_local_time(); -// auto sys_days = std::chrono::floor(tp); -// std::chrono::year_month_day ymd = std::chrono::year_month_day(sys_days); -// DateT::OuterYMD2Date(ymd, result); -// } +template <> +inline void CurrentTimestampFunction::Run(VarcharT &left, TimestampT &result) { + auto now = system_clock::now(); + auto sys_days = std::chrono::floor(now); + auto sys_secs = std::chrono::floor(now); + result.time.value = static_cast(sys_secs.time_since_epoch().count() - sys_days.time_since_epoch().count()); + result.date.value = static_cast(sys_days.time_since_epoch().count()); + // result(static_cast(sys_days.time_since_epoch().count(), static_cast(sys_secs.time_since_epoch().count() - sys_days.time_since_epoch().count()))); +} void RegisterCurrentTimestampFunction(const UniquePtr &catalog_ptr) { String func_name = "currenttimestamp"; @@ -55,7 +54,7 @@ void RegisterCurrentTimestampFunction(const UniquePtr &catalog_ptr) { ScalarFunction current_timestamp_function(func_name, {DataType(LogicalType::kVarchar)}, - DataType(LogicalType::kDate), + DataType(LogicalType::kTimestamp), &ScalarFunction::UnaryFunction); function_set_ptr->AddFunction(current_timestamp_function); diff --git a/src/unit_test/function/scalar/current_timestamp_functions.cpp b/src/unit_test/function/scalar/current_timestamp_functions.cpp index af60e6a503..53afcfd001 100644 --- a/src/unit_test/function/scalar/current_timestamp_functions.cpp +++ b/src/unit_test/function/scalar/current_timestamp_functions.cpp @@ -69,10 +69,8 @@ TEST_P(CurrentTimestampFunctionsTest, current_timestamp_func) { Vector> inputs; DataType data_type1(LogicalType::kVarchar); - // DataType data_type2(LogicalType::kBigInt); SharedPtr result_type = MakeShared(LogicalType::kTimestamp); SharedPtr col1_expr_ptr = MakeShared(data_type1, "t1", 1, "c1", 0, 0); - // SharedPtr col2_expr_ptr = MakeShared(data_type2, "t1", 1, "c2", 1, 0); inputs.emplace_back(col1_expr_ptr); ScalarFunction func = scalar_function_set->GetMostMatchFunction(inputs); @@ -80,7 +78,6 @@ TEST_P(CurrentTimestampFunctionsTest, current_timestamp_func) { Vector> column_types; column_types.emplace_back(MakeShared(data_type1)); - // column_types.emplace_back(MakeShared(data_type2)); SizeT row_count = DEFAULT_VECTOR_SIZE; @@ -89,17 +86,12 @@ TEST_P(CurrentTimestampFunctionsTest, current_timestamp_func) { for (SizeT i = 0; i < row_count; ++i) { data_block.AppendValue(0, Value::MakeVarchar("Asia/Shanghai")); - // data_block.AppendValue(1, Value::MakeBigInt(static_cast(i))); } data_block.Finalize(); for (SizeT i = 0; i < row_count; ++i) { Value v1 = data_block.GetValue(0, i); - Value v2 = data_block.GetValue(1, i); EXPECT_EQ(v1.type_.type(), LogicalType::kVarchar); - // EXPECT_EQ(v2.type_.type(), LogicalType::kBigInt); - // EXPECT_FLOAT_EQ(v1.value_.float32, static_cast(i)); - // EXPECT_EQ(v2.value_.big_int, static_cast(i)); } SharedPtr result = MakeShared(result_type); @@ -112,4 +104,4 @@ TEST_P(CurrentTimestampFunctionsTest, current_timestamp_func) { } } -} +} \ No newline at end of file From b1fad403194d276004dcbbb50d52ff8b0d569eb4 Mon Sep 17 00:00:00 2001 From: kche0169 Date: Mon, 27 Jan 2025 17:00:58 +0800 Subject: [PATCH 5/7] support changing time zone(This will not be the final version, the remaining changes will be updated in other pull request.) --- src/function/scalar/current_date.cpp | 22 ++++++++++++++++++---- src/function/scalar/current_time.cpp | 15 +++++++++++++++ src/function/scalar/current_timestamp.cpp | 17 +++++++++++++++-- 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/function/scalar/current_date.cpp b/src/function/scalar/current_date.cpp index 31405238ab..7c26753107 100644 --- a/src/function/scalar/current_date.cpp +++ b/src/function/scalar/current_date.cpp @@ -35,14 +35,28 @@ struct CurrentDateFunction { RecoverableError(status); return; } - + static inline void TimeZoneConvertHelper(VarcharT &left) { + const char* tzValue = std::getenv("TZ"); + const std::string str = left.ToString(); + const char* newTZ = str.c_str(); + if ( tzValue == newTZ) { + return; + } + if (setenv("TZ", newTZ, 1) != 0) { + const char* newTZ = "Asia/Shanghai"; + setenv("TZ", newTZ, 1); + } + tzset(); + return; + } }; template <> inline void CurrentDateFunction::Run(VarcharT &left, DateT &result) { - auto now = system_clock::now(); - auto sys_days = std::chrono::floor(now); - result.value = sys_days.time_since_epoch().count(); + TimeZoneConvertHelper(left); + auto now = system_clock::now(); + auto sys_days = std::chrono::floor(now); + result.value = sys_days.time_since_epoch().count(); } void RegisterCurrentDateFunction(const UniquePtr &catalog_ptr) { diff --git a/src/function/scalar/current_time.cpp b/src/function/scalar/current_time.cpp index 21b4e8b831..ee216d0b67 100644 --- a/src/function/scalar/current_time.cpp +++ b/src/function/scalar/current_time.cpp @@ -34,11 +34,26 @@ struct CurrentTimeFunction { Status status = Status::NotSupport("Not implemented"); RecoverableError(status); } + static inline void TimeZoneConvertHelper(VarcharT &left) { + const char* tzValue = std::getenv("TZ"); + const std::string str = left.ToString(); + const char* newTZ = str.c_str(); + if ( tzValue == newTZ) { + return; + } + if (setenv("TZ", newTZ, 1) != 0) { + const char* newTZ = "Asia/Shanghai"; + setenv("TZ", newTZ, 1); + } + tzset(); + return; + } }; template <> inline void CurrentTimeFunction::Run(VarcharT &left, TimeT &result) { + TimeZoneConvertHelper(left); auto now = system_clock::now(); auto sys_days = std::chrono::floor(now); auto sys_secs = std::chrono::floor(now); diff --git a/src/function/scalar/current_timestamp.cpp b/src/function/scalar/current_timestamp.cpp index 83cf24e4d6..7e02316502 100644 --- a/src/function/scalar/current_timestamp.cpp +++ b/src/function/scalar/current_timestamp.cpp @@ -34,17 +34,30 @@ struct CurrentTimestampFunction { Status status = Status::NotSupport("Not implemented"); RecoverableError(status); } - + static inline void TimeZoneConvertHelper(VarcharT &left) { + const char* tzValue = std::getenv("TZ"); + const std::string str = left.ToString(); + const char* newTZ = str.c_str(); + if ( tzValue == newTZ) { + return; + } + if (setenv("TZ", newTZ, 1) != 0) { + const char* newTZ = "Asia/Shanghai"; + setenv("TZ", newTZ, 1); + } + tzset(); + return; + } }; template <> inline void CurrentTimestampFunction::Run(VarcharT &left, TimestampT &result) { + TimeZoneConvertHelper(left); auto now = system_clock::now(); auto sys_days = std::chrono::floor(now); auto sys_secs = std::chrono::floor(now); result.time.value = static_cast(sys_secs.time_since_epoch().count() - sys_days.time_since_epoch().count()); result.date.value = static_cast(sys_days.time_since_epoch().count()); - // result(static_cast(sys_days.time_since_epoch().count(), static_cast(sys_secs.time_since_epoch().count() - sys_days.time_since_epoch().count()))); } void RegisterCurrentTimestampFunction(const UniquePtr &catalog_ptr) { From 01eb95ad3216a2c82b63407ee5f53bc43b003bd1 Mon Sep 17 00:00:00 2001 From: kche0169 Date: Mon, 27 Jan 2025 17:45:34 +0800 Subject: [PATCH 6/7] add reset time zone helper --- src/function/scalar/current_date.cpp | 11 +++++++++++ src/function/scalar/current_time.cpp | 12 +++++++++++- src/function/scalar/current_timestamp.cpp | 12 ++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/function/scalar/current_date.cpp b/src/function/scalar/current_date.cpp index 7c26753107..cc36fcb299 100644 --- a/src/function/scalar/current_date.cpp +++ b/src/function/scalar/current_date.cpp @@ -49,6 +49,16 @@ struct CurrentDateFunction { tzset(); return; } + static inline void TimeZoneResetHelper() { + const char* tzValue = std::getenv("TZ"); + if (tzValue == "Asia/Shanghai") { + return; + } + const char* newTZ = "Asia/Shanghai"; + setenv("TZ", newTZ, 1); + tzset(); + return; + } }; template <> @@ -57,6 +67,7 @@ inline void CurrentDateFunction::Run(VarcharT &left, DateT &result) { auto now = system_clock::now(); auto sys_days = std::chrono::floor(now); result.value = sys_days.time_since_epoch().count(); + TimeZoneResetHelper(); } void RegisterCurrentDateFunction(const UniquePtr &catalog_ptr) { diff --git a/src/function/scalar/current_time.cpp b/src/function/scalar/current_time.cpp index ee216d0b67..a4c70ea145 100644 --- a/src/function/scalar/current_time.cpp +++ b/src/function/scalar/current_time.cpp @@ -48,7 +48,16 @@ struct CurrentTimeFunction { tzset(); return; } - + static inline void TimeZoneResetHelper() { + const char* tzValue = std::getenv("TZ"); + if (tzValue == "Asia/Shanghai") { + return; + } + const char* newTZ = "Asia/Shanghai"; + setenv("TZ", newTZ, 1); + tzset(); + return; + } }; template <> @@ -58,6 +67,7 @@ inline void CurrentTimeFunction::Run(VarcharT &left, TimeT &result) { auto sys_days = std::chrono::floor(now); auto sys_secs = std::chrono::floor(now); result.value = static_cast(sys_secs.time_since_epoch().count() - sys_days.time_since_epoch().count()); + TimeZoneResetHelper(); } void RegisterCurrentTimeFunction(const UniquePtr &catalog_ptr) { diff --git a/src/function/scalar/current_timestamp.cpp b/src/function/scalar/current_timestamp.cpp index 7e02316502..593e942977 100644 --- a/src/function/scalar/current_timestamp.cpp +++ b/src/function/scalar/current_timestamp.cpp @@ -48,6 +48,17 @@ struct CurrentTimestampFunction { tzset(); return; } + + static inline void TimeZoneResetHelper() { + const char* tzValue = std::getenv("TZ"); + if (tzValue == "Asia/Shanghai") { + return; + } + const char* newTZ = "Asia/Shanghai"; + setenv("TZ", newTZ, 1); + tzset(); + return; + } }; template <> @@ -58,6 +69,7 @@ inline void CurrentTimestampFunction::Run(VarcharT &left, TimestampT &result) { auto sys_secs = std::chrono::floor(now); result.time.value = static_cast(sys_secs.time_since_epoch().count() - sys_days.time_since_epoch().count()); result.date.value = static_cast(sys_days.time_since_epoch().count()); + TimeZoneResetHelper(); } void RegisterCurrentTimestampFunction(const UniquePtr &catalog_ptr) { From f6f6ae3b5fbc4c852a16bf3a488fdc1ffbe3c5b0 Mon Sep 17 00:00:00 2001 From: kche0169 Date: Mon, 27 Jan 2025 18:09:21 +0800 Subject: [PATCH 7/7] update reset helper --- src/function/scalar/current_date.cpp | 8 ++++---- src/function/scalar/current_time.cpp | 7 ++++--- src/function/scalar/current_timestamp.cpp | 6 +++--- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/function/scalar/current_date.cpp b/src/function/scalar/current_date.cpp index cc36fcb299..697f1f733b 100644 --- a/src/function/scalar/current_date.cpp +++ b/src/function/scalar/current_date.cpp @@ -29,6 +29,7 @@ import column_vector; namespace infinity { using namespace std::chrono; struct CurrentDateFunction { + const char* defaultTZ = "Asia/Shanghai"; template static inline void Run(TA &left, TB &result) { Status status = Status::NotSupport("Not implemented"); @@ -43,7 +44,7 @@ struct CurrentDateFunction { return; } if (setenv("TZ", newTZ, 1) != 0) { - const char* newTZ = "Asia/Shanghai"; + const char* newTZ = CurrentDateFunction().defaultTZ; setenv("TZ", newTZ, 1); } tzset(); @@ -51,11 +52,10 @@ struct CurrentDateFunction { } static inline void TimeZoneResetHelper() { const char* tzValue = std::getenv("TZ"); - if (tzValue == "Asia/Shanghai") { + if (tzValue == CurrentDateFunction().defaultTZ) { return; } - const char* newTZ = "Asia/Shanghai"; - setenv("TZ", newTZ, 1); + setenv("TZ", CurrentDateFunction().defaultTZ, 1); tzset(); return; } diff --git a/src/function/scalar/current_time.cpp b/src/function/scalar/current_time.cpp index a4c70ea145..93b673f7c7 100644 --- a/src/function/scalar/current_time.cpp +++ b/src/function/scalar/current_time.cpp @@ -29,6 +29,7 @@ import column_vector; namespace infinity { using namespace std::chrono; struct CurrentTimeFunction { + const char* defaultTZ = "Asia/Shanghai"; template static inline void Run(TA &left, TB &result) { Status status = Status::NotSupport("Not implemented"); @@ -48,13 +49,13 @@ struct CurrentTimeFunction { tzset(); return; } + static inline void TimeZoneResetHelper() { const char* tzValue = std::getenv("TZ"); - if (tzValue == "Asia/Shanghai") { + if (tzValue == CurrentTimeFunction().defaultTZ) { return; } - const char* newTZ = "Asia/Shanghai"; - setenv("TZ", newTZ, 1); + setenv("TZ", CurrentTimeFunction().defaultTZ, 1); tzset(); return; } diff --git a/src/function/scalar/current_timestamp.cpp b/src/function/scalar/current_timestamp.cpp index 593e942977..85d07d06bd 100644 --- a/src/function/scalar/current_timestamp.cpp +++ b/src/function/scalar/current_timestamp.cpp @@ -29,6 +29,7 @@ import column_vector; namespace infinity { using namespace std::chrono; struct CurrentTimestampFunction { + const char* defaultTZ = "Asia/Shanghai"; template static inline void Run(TA &left, TB &result) { Status status = Status::NotSupport("Not implemented"); @@ -51,11 +52,10 @@ struct CurrentTimestampFunction { static inline void TimeZoneResetHelper() { const char* tzValue = std::getenv("TZ"); - if (tzValue == "Asia/Shanghai") { + if (tzValue == CurrentTimestampFunction().defaultTZ) { return; } - const char* newTZ = "Asia/Shanghai"; - setenv("TZ", newTZ, 1); + setenv("TZ", CurrentTimestampFunction().defaultTZ, 1); tzset(); return; }