Skip to content
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

[Feature](function)Support Function Time #47320

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ void register_function_date_time_computation(SimpleFunctionFactory& factory) {
factory.register_function<FunctionMicroSecToDateTime>();
factory.register_function<FunctionMilliSecToDateTime>();
factory.register_function<FunctionSecToDateTime>();
factory.register_function<FunctionTime>();

// alias
factory.register_alias("days_add", "date_add");
Expand Down
28 changes: 28 additions & 0 deletions be/src/vec/functions/function_date_or_datetime_computation.h
Original file line number Diff line number Diff line change
Expand Up @@ -995,4 +995,32 @@ class CurrentDateFunctionBuilder : public FunctionBuilderImpl {
}
};

class FunctionTime : public IFunction {
public:
static constexpr auto name = "time";
static FunctionPtr create() { return std::make_shared<FunctionTime>(); }
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<DataTypeTimeV2>();
}

Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
uint32_t result, size_t input_rows_count) const override {
DCHECK_EQ(arguments.size(), 1);
ColumnPtr col = block.get_by_position(arguments[0]).column;
const auto* arg = assert_cast<const ColumnUInt64*>(col.get());
auto res = ColumnFloat64::create(input_rows_count);
auto& res_data = res->get_data();
for (int i = 0; i < arg->size(); i++) {
auto v = arg->get_element(i);
res_data[i] = TimeValue::make_time((v & 0x1f00000000) >> 32, (v & 0xfc000000) >> 26,
(v & 0x3f00000) >> 20);
}
block.replace_by_position(result, std::move(res));
return Status::OK();
}
};

} // namespace doris::vectorized
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.SubstringIndex;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Tan;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Tanh;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Time;
import org.apache.doris.nereids.trees.expressions.functions.scalar.TimeDiff;
import org.apache.doris.nereids.trees.expressions.functions.scalar.TimeToSec;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Timestamp;
Expand Down Expand Up @@ -917,6 +918,7 @@ public class BuiltinScalarFunctions implements FunctionHelper {
scalar(SubstringIndex.class, "substring_index"),
scalar(Tan.class, "tan"),
scalar(Tanh.class, "tanh"),
scalar(Time.class, "time"),
scalar(TimeDiff.class, "timediff"),
scalar(TimeToSec.class, "time_to_sec"),
scalar(Timestamp.class, "timestamp"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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.

package org.apache.doris.nereids.trees.expressions.functions.scalar;

import org.apache.doris.catalog.FunctionSignature;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullableOnDateLikeV2Args;
import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.DateTimeV2Type;
import org.apache.doris.nereids.types.TimeV2Type;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import java.util.List;

/**
* ScalarFunction 'time'. This class is generated by GenerateFunction.
*/
public class Time extends ScalarFunction
implements BinaryExpression, ExplicitlyCastableSignature, PropagateNullableOnDateLikeV2Args {

private static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(TimeV2Type.INSTANCE).args(DateTimeV2Type.SYSTEM_DEFAULT));

/**
* constructor with 1 argument.
*/
public Time(Expression arg) {
super("time", arg);
}

/**
* withChildren.
*/
@Override
public Time withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 1);
return new Time(children.get(0));
}

@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitTime(this, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.SubstringIndex;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Tan;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Tanh;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Time;
import org.apache.doris.nereids.trees.expressions.functions.scalar.TimeDiff;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Timestamp;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ToBase64;
Expand Down Expand Up @@ -2065,6 +2066,10 @@ default R visitTanh(Tanh tanh, C context) {
return visitScalarFunction(tanh, context);
}

default R visitTime(Time time, C context) {
return visitScalarFunction(time, context);
}

default R visitTimeDiff(TimeDiff timeDiff, C context) {
return visitScalarFunction(timeDiff, context);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !sql --
12:12:12

-- !sql --
\N

-- !sql --
\N

-- !sql --
\N

-- !sql_time --
\N
13:21:03
13:22:03
14:21:03
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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") {
qt_sql """ select time('2025-1-1 12:12:12') """
qt_sql """ select time('2025-1-1 12:12:61') """
qt_sql """ select time('2025-1-1 12:61:12') """
qt_sql """ select time('2025-1-1 25:12:12') """
def tableName = "test_time_function"

sql """ DROP TABLE IF EXISTS ${tableName} """
sql """
CREATE TABLE IF NOT EXISTS ${tableName} (
test_time datetimev2 NULL COMMENT ""
) ENGINE=OLAP
DUPLICATE KEY(test_time)
COMMENT "OLAP"
DISTRIBUTED BY HASH(test_time) BUCKETS 1
PROPERTIES (
"replication_allocation" = "tag.location.default: 1",
"in_memory" = "false",
"storage_format" = "V2"
)
"""
sql """ insert into ${tableName} values
("2019-08-01 13:21:03"),
("2019-08-01 13:22:03"),
("2019-08-01 14:21:03"),
(null);
"""
qt_sql_time "select time(test_time) from ${tableName}";
}
Loading