[feature](cast) remove some unused in functioncast and support some function in nereids (#22729)

1 ConvertImplGenericFromString do not need a template StringColumnType
2 remove timev1 in function cast
3 support time_to_sec , sec_to_time in nereids
This commit is contained in:
Mryange
2023-08-10 10:10:32 +08:00
committed by GitHub
parent 919bfd73f1
commit b25d52b736
6 changed files with 152 additions and 21 deletions

View File

@ -204,7 +204,7 @@ bool call_on_index_and_data_type(TypeIndex number, F&& f) {
case TypeIndex::Float64:
return f(TypePair<DataTypeNumber<Float64>, T>());
case TypeIndex::Time:
return f(TypePair<DataTypeTime, T>());
return f(TypePair<DataTypeTimeV2, T>());
case TypeIndex::TimeV2:
return f(TypePair<DataTypeTimeV2, T>());
case TypeIndex::Decimal32:

View File

@ -541,19 +541,15 @@ struct ConvertImplGenericToString {
}
};
//this is for data in compound type
template <typename StringColumnType>
struct ConvertImplGenericFromString {
static Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
const size_t result, size_t input_rows_count) {
static_assert(std::is_same_v<StringColumnType, ColumnString>,
"Can be used only to parse from ColumnString");
const auto& col_with_type_and_name = block.get_by_position(arguments[0]);
const IColumn& col_from = *col_with_type_and_name.column;
// result column must set type
DCHECK(block.get_by_position(result).type != nullptr);
auto data_type_to = block.get_by_position(result).type;
if (const StringColumnType* col_from_string =
check_and_get_column<StringColumnType>(&col_from)) {
if (const ColumnString* col_from_string = check_and_get_column<ColumnString>(&col_from)) {
auto col_to = data_type_to->create_column();
size_t size = col_from.size();
@ -561,14 +557,14 @@ struct ConvertImplGenericFromString {
ColumnUInt8::MutablePtr col_null_map_to = ColumnUInt8::create(size);
ColumnUInt8::Container* vec_null_map_to = &col_null_map_to->get_data();
const bool is_complex = is_complex_type(data_type_to);
for (size_t i = 0; i < size; ++i) {
const auto& val = col_from_string->get_data_at(i);
// Note: here we should handle the null element
if (val.size == 0) {
col_to->insert_default();
// empty string('') is an invalid format for complex type, set null_map to 1
if (is_complex_type(data_type_to)) {
if (is_complex) {
(*vec_null_map_to)[i] = 1;
}
continue;
@ -1168,7 +1164,6 @@ using FunctionToFloat32 =
using FunctionToFloat64 =
FunctionConvert<DataTypeFloat64, NameToFloat64, ToNumberMonotonicity<Float64>>;
using FunctionToTime = FunctionConvert<DataTypeTime, NameToFloat64, ToNumberMonotonicity<Float64>>;
using FunctionToTimeV2 =
FunctionConvert<DataTypeTimeV2, NameToFloat64, ToNumberMonotonicity<Float64>>;
using FunctionToString = FunctionConvert<DataTypeString, NameToString, ToStringMonotonicity>;
@ -1266,10 +1261,6 @@ struct FunctionTo<DataTypeDateTimeV2> {
using Type = FunctionToDateTimeV2;
};
template <>
struct FunctionTo<DataTypeTime> {
using Type = FunctionToTime;
};
template <>
struct FunctionTo<DataTypeTimeV2> {
using Type = FunctionToTimeV2;
};
@ -1706,7 +1697,7 @@ private:
const DataTypeHLL& to_type) const {
/// Conversion from String through parsing.
if (check_and_get_data_type<DataTypeString>(from_type_untyped.get())) {
return &ConvertImplGenericFromString<ColumnString>::execute;
return &ConvertImplGenericFromString::execute;
}
//TODO if from is not string, it must be HLL?
@ -1725,7 +1716,7 @@ private:
const DataTypeArray& to_type) const {
/// Conversion from String through parsing.
if (check_and_get_data_type<DataTypeString>(from_type_untyped.get())) {
return &ConvertImplGenericFromString<ColumnString>::execute;
return &ConvertImplGenericFromString::execute;
}
const auto* from_type = check_and_get_data_type<DataTypeArray>(from_type_untyped.get());
@ -1833,7 +1824,7 @@ private:
case TypeIndex::Float64:
return &ConvertImplNumberToJsonb<ColumnFloat64>::execute;
case TypeIndex::String:
return &ConvertImplGenericFromString<ColumnString>::execute;
return &ConvertImplGenericFromString::execute;
default:
return &ConvertImplGenericToJsonb::execute;
}
@ -1843,7 +1834,7 @@ private:
WrapperType create_map_wrapper(const DataTypePtr& from_type, const DataTypeMap& to_type) const {
switch (from_type->get_type_id()) {
case TypeIndex::String:
return &ConvertImplGenericFromString<ColumnString>::execute;
return &ConvertImplGenericFromString::execute;
default:
return create_unsupport_wrapper(from_type->get_name(), to_type.get_name());
}
@ -1870,7 +1861,7 @@ private:
const DataTypeStruct& to_type) const {
// support CAST AS Struct from string
if (from_type->get_type_id() == TypeIndex::String) {
return &ConvertImplGenericFromString<ColumnString>::execute;
return &ConvertImplGenericFromString::execute;
}
// only support CAST AS Struct from struct or string types
@ -2072,7 +2063,6 @@ private:
std::is_same_v<ToDataType, DataTypeDateTime> ||
std::is_same_v<ToDataType, DataTypeDateV2> ||
std::is_same_v<ToDataType, DataTypeDateTimeV2> ||
std::is_same_v<ToDataType, DataTypeTime> ||
std::is_same_v<ToDataType, DataTypeTimeV2>) {
ret = create_wrapper(from_type, check_and_get_data_type<ToDataType>(to_type.get()),
requested_result_is_nullable);

View File

@ -250,6 +250,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.RoundBankers;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Rpad;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Rtrim;
import org.apache.doris.nereids.trees.expressions.functions.scalar.RunningDifference;
import org.apache.doris.nereids.trees.expressions.functions.scalar.SecToTime;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Second;
import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondCeil;
import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondFloor;
@ -304,6 +305,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.Substring;
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.TimeDiff;
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.ToBase64;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ToBitmap;
@ -591,12 +593,14 @@ public class BuiltinScalarFunctions implements FunctionHelper {
scalar(SecondsAdd.class, "seconds_add"),
scalar(SecondsDiff.class, "seconds_diff"),
scalar(SecondsSub.class, "seconds_sub"),
scalar(SecToTime.class, "sec_to_time"),
scalar(Sign.class, "sign"),
scalar(Sin.class, "sin"),
scalar(Size.class, "size"),
scalar(Sleep.class, "sleep"),
scalar(Sm3.class, "sm3"),
scalar(Sm3sum.class, "sm3sum"),
scalar(Sm3sum.class,
"sm3sum"),
scalar(Sm4Decrypt.class, "sm4_decrypt"),
scalar(Sm4DecryptV2.class, "sm4_decrypt_v2"),
scalar(Sm4Encrypt.class, "sm4_encrypt"),
@ -639,6 +643,7 @@ public class BuiltinScalarFunctions implements FunctionHelper {
scalar(SubstringIndex.class, "substring_index"),
scalar(Tan.class, "tan"),
scalar(TimeDiff.class, "timediff"),
scalar(TimeToSec.class, "time_to_sec"),
scalar(Timestamp.class, "timestamp"),
scalar(ToBase64.class, "to_base64"),
scalar(ToBitmap.class, "to_bitmap"),

View File

@ -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.PropagateNullable;
import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.IntegerType;
import org.apache.doris.nereids.types.TimeType;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.util.List;
/**
* ScalarFunction 'sec_to_time'.
*/
public class SecToTime extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, PropagateNullable {
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(TimeType.INSTANCE).args(IntegerType.INSTANCE));
/**
* constructor with 1 argument.
*/
public SecToTime(Expression arg) {
super("sec_to_time", arg);
}
/**
* withChildren.
*/
@Override
public SecToTime withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 1);
return new SecToTime(children.get(0));
}
@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}
@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitScalarFunction(this, context);
}
}

View File

@ -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.PropagateNullable;
import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.IntegerType;
import org.apache.doris.nereids.types.TimeType;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.util.List;
/**
* ScalarFunction 'time_to_sec'.
*/
public class TimeToSec extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, PropagateNullable {
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(IntegerType.INSTANCE).args(TimeType.INSTANCE));
/**
* constructor with 1 argument.
*/
public TimeToSec(Expression arg) {
super("time_to_sec", arg);
}
/**
* withChildren.
*/
@Override
public TimeToSec withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 1);
return new TimeToSec(children.get(0));
}
@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}
@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitScalarFunction(this, context);
}
}

View File

@ -17,7 +17,7 @@
suite("test_time_function") {
sql """
set enable_nereids_planner=true,enable_fold_constant_by_be = false
set enable_nereids_planner=true,enable_fallback_to_original_planner=false
"""
qt_select1 """
select sec_to_time(time_to_sec(cast('16:32:18' as time)));