From a5e71e2169aee05586a72482f984516f8d3e2c5e Mon Sep 17 00:00:00 2001 From: zclllyybb Date: Wed, 19 Jul 2023 01:09:48 +0800 Subject: [PATCH 1/2] [feature](function) support time_to_sec (#21722) mysql >select sec_to_time(time_to_sec(cast('16:32:18' as time))); +----------------------------------------------------+ | sec_to_time(time_to_sec(CAST('16:32:18' AS TIME))) | +----------------------------------------------------+ | 16:32:18 | +----------------------------------------------------+ 1 row in set (0.53 sec) mysql [test]>select sec_to_time(59538); +--------------------+ | sec_to_time(59538) | +--------------------+ | 16:32:18 | +--------------------+ 1 row in set (0.03 sec) --- .../function_date_or_datetime_computation.cpp | 2 + .../function_date_or_datetime_computation.h | 45 ++++++++++------- .../date-time-functions/sec_to_time.md | 48 +++++++++++++++++++ docs/sidebars.json | 1 + .../date-time-functions/sec_to_time.md | 48 +++++++++++++++++++ gensrc/script/doris_builtins_functions.py | 1 + 6 files changed, 128 insertions(+), 17 deletions(-) create mode 100644 docs/en/docs/sql-manual/sql-functions/date-time-functions/sec_to_time.md create mode 100644 docs/zh-CN/docs/sql-manual/sql-functions/date-time-functions/sec_to_time.md diff --git a/be/src/vec/functions/function_date_or_datetime_computation.cpp b/be/src/vec/functions/function_date_or_datetime_computation.cpp index 1f2de986a1edc8..8034d34c195e05 100644 --- a/be/src/vec/functions/function_date_or_datetime_computation.cpp +++ b/be/src/vec/functions/function_date_or_datetime_computation.cpp @@ -123,6 +123,7 @@ using FunctionCurTime = FunctionCurrentDateOrDateTime>; using FunctionUtcTimeStamp = FunctionCurrentDateOrDateTime; using FunctionTimeToSec = FunctionCurrentDateOrDateTime; +using FunctionSecToTime = FunctionCurrentDateOrDateTime; /// @TEMPORARY: for be_exec_version=2 using FunctionToWeekTwoArgsOld = @@ -177,6 +178,7 @@ void register_function_date_time_computation(SimpleFunctionFactory& factory) { factory.register_function(); factory.register_function(); factory.register_function(); + factory.register_function(); // alias factory.register_alias("days_add", "date_add"); diff --git a/be/src/vec/functions/function_date_or_datetime_computation.h b/be/src/vec/functions/function_date_or_datetime_computation.h index e31d8f64c62362..c53eefd36cc3ea 100644 --- a/be/src/vec/functions/function_date_or_datetime_computation.h +++ b/be/src/vec/functions/function_date_or_datetime_computation.h @@ -1032,24 +1032,35 @@ struct TimeToSecImpl { static constexpr auto name = "time_to_sec"; static Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments, size_t result, size_t input_rows_count) { - auto res_col = ColumnVector::create(); - const auto& [argument_column, arg_is_const] = - unpack_if_const(block.get_by_position(arguments[0]).column); - const auto& column_data = assert_cast(*argument_column); - if (arg_is_const) { - double time = column_data.get_element(0); - res_col->insert_value(static_cast(time)); - block.replace_by_position(result, - ColumnConst::create(std::move(res_col), input_rows_count)); - } else { - auto& res_data = res_col->get_data(); - res_data.resize(input_rows_count); - for (int i = 0; i < input_rows_count; ++i) { - double time = column_data.get_element(i); - res_data[i] = static_cast(time); - } - block.replace_by_position(result, std::move(res_col)); + auto res_col = ColumnInt32::create(input_rows_count); + const auto& arg_col = block.get_by_position(arguments[0]).column; + const auto& column_data = assert_cast(*arg_col); + + auto& res_data = res_col->get_data(); + for (int i = 0; i < input_rows_count; ++i) { + res_data[i] = static_cast(column_data.get_element(i)); + } + block.replace_by_position(result, std::move(res_col)); + + return Status::OK(); + } +}; + +struct SecToTimeImpl { + using ReturnType = DataTypeTime; + static constexpr auto name = "sec_to_time"; + static Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments, + size_t result, size_t input_rows_count) { + const auto& arg_col = block.get_by_position(arguments[0]).column; + const auto& column_data = assert_cast(*arg_col); + + auto res_col = ColumnFloat64::create(input_rows_count); + auto& res_data = res_col->get_data(); + for (int i = 0; i < input_rows_count; ++i) { + res_data[i] = static_cast(column_data.get_element(i)); } + + block.replace_by_position(result, std::move(res_col)); return Status::OK(); } }; diff --git a/docs/en/docs/sql-manual/sql-functions/date-time-functions/sec_to_time.md b/docs/en/docs/sql-manual/sql-functions/date-time-functions/sec_to_time.md new file mode 100644 index 00000000000000..554c79ba0f443f --- /dev/null +++ b/docs/en/docs/sql-manual/sql-functions/date-time-functions/sec_to_time.md @@ -0,0 +1,48 @@ +--- +{ + "title": "sec_to_time", + "language": "en" +} +--- + + + +## sec_to_time +### description +#### Syntax + +`TIME sec_to_time(INT timestamp)` + +The parameter is a timestamp of type INT, and the function returns a time of type TIME. + +### example + +``` +mysql >select sec_to_time(time_to_sec(cast('16:32:18' as time))); ++----------------------------------------------------+ +| sec_to_time(time_to_sec(CAST('16:32:18' AS TIME))) | ++----------------------------------------------------+ +| 16:32:18 | ++----------------------------------------------------+ +1 row in set (0.53 sec) +``` + +### keywords + SEC_TO_TIME diff --git a/docs/sidebars.json b/docs/sidebars.json index 26fc6051421231..52757c8f8d5933 100644 --- a/docs/sidebars.json +++ b/docs/sidebars.json @@ -346,6 +346,7 @@ "sql-manual/sql-functions/date-time-functions/to_date", "sql-manual/sql-functions/date-time-functions/to_days", "sql-manual/sql-functions/date-time-functions/time_to_sec", + "sql-manual/sql-functions/date-time-functions/sec_to_time", "sql-manual/sql-functions/date-time-functions/extract", "sql-manual/sql-functions/date-time-functions/makedate", "sql-manual/sql-functions/date-time-functions/str_to_date", diff --git a/docs/zh-CN/docs/sql-manual/sql-functions/date-time-functions/sec_to_time.md b/docs/zh-CN/docs/sql-manual/sql-functions/date-time-functions/sec_to_time.md new file mode 100644 index 00000000000000..a80b4fcabf682f --- /dev/null +++ b/docs/zh-CN/docs/sql-manual/sql-functions/date-time-functions/sec_to_time.md @@ -0,0 +1,48 @@ +--- +{ + "title": "sec_to_time", + "language": "zh-CN" +} +--- + + + +## sec_to_time +### description +#### Syntax + +`TIME sec_to_time(INT timestamp)` + +参数为INT类型时间戳,函数返回TIME类型时间。 + +### example + +``` +mysql >select sec_to_time(time_to_sec(cast('16:32:18' as time))); ++----------------------------------------------------+ +| sec_to_time(time_to_sec(CAST('16:32:18' AS TIME))) | ++----------------------------------------------------+ +| 16:32:18 | ++----------------------------------------------------+ +1 row in set (0.53 sec) +``` + +### keywords + SEC_TO_TIME diff --git a/gensrc/script/doris_builtins_functions.py b/gensrc/script/doris_builtins_functions.py index 5903259e5f31f4..8b0df0f76c084a 100644 --- a/gensrc/script/doris_builtins_functions.py +++ b/gensrc/script/doris_builtins_functions.py @@ -996,6 +996,7 @@ [['to_days'], 'INT', ['DATEV2'], ''], [['time_to_sec'], 'INT', ['TIME'], ''], + [['sec_to_time'], 'TIME', ['INT'], ''], [['year'], 'SMALLINT', ['DATETIMEV2'], ''], [['month'], 'TINYINT', ['DATETIMEV2'], ''], From 831bc04dc0b191c50b60a31517b40b07fc031e57 Mon Sep 17 00:00:00 2001 From: Mryange <59914473+Mryange@users.noreply.github.com> Date: Mon, 7 Aug 2023 17:33:24 +0800 Subject: [PATCH 2/2] [fix](time) fix error in time_to_sec --- .../function_date_or_datetime_computation.h | 3 +- .../data/correctness/test_time_function.out | 13 ++++++ .../correctness/test_time_function.groovy | 42 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 regression-test/data/correctness/test_time_function.out create mode 100644 regression-test/suites/correctness/test_time_function.groovy diff --git a/be/src/vec/functions/function_date_or_datetime_computation.h b/be/src/vec/functions/function_date_or_datetime_computation.h index c53eefd36cc3ea..3272419d6d3b5c 100644 --- a/be/src/vec/functions/function_date_or_datetime_computation.h +++ b/be/src/vec/functions/function_date_or_datetime_computation.h @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -1038,7 +1039,7 @@ struct TimeToSecImpl { auto& res_data = res_col->get_data(); for (int i = 0; i < input_rows_count; ++i) { - res_data[i] = static_cast(column_data.get_element(i)); + res_data[i] = static_cast(column_data.get_element(i)) / (1000 * 1000); } block.replace_by_position(result, std::move(res_col)); diff --git a/regression-test/data/correctness/test_time_function.out b/regression-test/data/correctness/test_time_function.out new file mode 100644 index 00000000000000..dcdc6da37a3af1 --- /dev/null +++ b/regression-test/data/correctness/test_time_function.out @@ -0,0 +1,13 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !select1 -- +16:32:18 + +-- !select2 -- +16:32:18 + +-- !select3 -- +16:32:18 + +-- !select4 -- +16:32:18 + diff --git a/regression-test/suites/correctness/test_time_function.groovy b/regression-test/suites/correctness/test_time_function.groovy new file mode 100644 index 00000000000000..77a4f9be8ae74a --- /dev/null +++ b/regression-test/suites/correctness/test_time_function.groovy @@ -0,0 +1,42 @@ +// 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. + +suite("test_time_function") { + sql """ + set enable_nereids_planner=true,enable_fold_constant_by_be = false + """ + qt_select1 """ + select sec_to_time(time_to_sec(cast('16:32:18' as time))); + """ + qt_select2 """ + select sec_to_time(59538); + """ + + sql """ + set enable_nereids_planner=false + """ + + qt_select3 """ + select sec_to_time(time_to_sec(cast('16:32:18' as time))); + """ + qt_select4 """ + select sec_to_time(59538); + """ + + + +} \ No newline at end of file