Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ using FunctionCurTime = FunctionCurrentDateOrDateTime<CurrentTimeImpl<CurTimeFun
using FunctionCurrentTime = FunctionCurrentDateOrDateTime<CurrentTimeImpl<CurrentTimeFunctionName>>;
using FunctionUtcTimeStamp = FunctionCurrentDateOrDateTime<UtcTimestampImpl>;
using FunctionTimeToSec = FunctionCurrentDateOrDateTime<TimeToSecImpl>;
using FunctionSecToTime = FunctionCurrentDateOrDateTime<SecToTimeImpl>;

/// @TEMPORARY: for be_exec_version=2
using FunctionToWeekTwoArgsOld =
Expand Down Expand Up @@ -177,6 +178,7 @@ void register_function_date_time_computation(SimpleFunctionFactory& factory) {
factory.register_function<FunctionCurrentTime>();
factory.register_function<FunctionUtcTimeStamp>();
factory.register_function<FunctionTimeToSec>();
factory.register_function<FunctionSecToTime>();

// alias
factory.register_alias("days_add", "date_add");
Expand Down
46 changes: 29 additions & 17 deletions be/src/vec/functions/function_date_or_datetime_computation.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <algorithm>
#include <boost/iterator/iterator_facade.hpp>
#include <cstdint>
#include <memory>
#include <type_traits>
#include <utility>
Expand Down Expand Up @@ -1032,24 +1033,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<Int32>::create();
const auto& [argument_column, arg_is_const] =
unpack_if_const(block.get_by_position(arguments[0]).column);
const auto& column_data = assert_cast<const ColumnFloat64&>(*argument_column);
if (arg_is_const) {
double time = column_data.get_element(0);
res_col->insert_value(static_cast<int>(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<int>(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<const ColumnFloat64&>(*arg_col);

auto& res_data = res_col->get_data();
for (int i = 0; i < input_rows_count; ++i) {
res_data[i] = static_cast<int64_t>(column_data.get_element(i)) / (1000 * 1000);
}
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<const ColumnInt32&>(*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<double>(column_data.get_element(i));
}

block.replace_by_position(result, std::move(res_col));
return Status::OK();
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
{
"title": "sec_to_time",
"language": "en"
}
---

<!--
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.
-->

## 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
1 change: 1 addition & 0 deletions docs/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
{
"title": "sec_to_time",
"language": "zh-CN"
}
---

<!--
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.
-->

## 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
1 change: 1 addition & 0 deletions gensrc/script/doris_builtins_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,7 @@

[['to_days'], 'INT', ['DATEV2'], ''],
[['time_to_sec'], 'INT', ['TIME'], ''],
[['sec_to_time'], 'TIME', ['INT'], ''],

[['year'], 'SMALLINT', ['DATETIMEV2'], ''],
[['month'], 'TINYINT', ['DATETIMEV2'], ''],
Expand Down
13 changes: 13 additions & 0 deletions regression-test/data/correctness/test_time_function.out
Original file line number Diff line number Diff line change
@@ -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

42 changes: 42 additions & 0 deletions regression-test/suites/correctness/test_time_function.groovy
Original file line number Diff line number Diff line change
@@ -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);
"""



}