From f0ff773696e44b143f1818898522d476fa639a8f Mon Sep 17 00:00:00 2001 From: Mryange <59914473+Mryange@users.noreply.github.com> Date: Thu, 26 Sep 2024 22:27:32 +0800 Subject: [PATCH 1/3] [fix](function) add time type in conditional-functions (#41270) before ``` mysql [(none)]>select sec_to_time(time_to_sec(cast('2024-09-24 16:00:00' as varchar))); +---------------------------------------------------------------------------------------+ | sec_to_time(time_to_sec(cast(cast('2024-09-24 16:00:00' as VARCHAR(65533)) as TIME))) | +---------------------------------------------------------------------------------------+ | 16:00:00 | +---------------------------------------------------------------------------------------+ mysql [(none)]>select ifnull(sec_to_time(time_to_sec(cast('2024-09-24 16:00:00' as varchar))), cast(300 as time)); +--------------------------------------------------------------------------------------------------------------------------------------------------+ | ifnull(cast(sec_to_time(time_to_sec(cast(cast('2024-09-24 16:00:00' as VARCHAR(65533)) as TIME))) as DOUBLE), cast(cast(300 as TIME) as DOUBLE)) | +--------------------------------------------------------------------------------------------------------------------------------------------------+ | 57600000000 | +--------------------------------------------------------------------------------------------------------------------------------------------------+ ``` now ``` mysql [(none)]>select ifnull(sec_to_time(time_to_sec(cast('2024-09-24 16:00:00' as varchar))), cast(300 as time)); +------------------------------------------------------------------------------------------------------------------+ | ifnull(sec_to_time(time_to_sec(cast(cast('2024-09-24 16:00:00' as VARCHAR(65533)) as TIME))), cast(300 as TIME)) | +------------------------------------------------------------------------------------------------------------------+ | 16:00:00 | +------------------------------------------------------------------------------------------------------------------+ ``` --- .../trees/expressions/functions/scalar/Coalesce.java | 4 ++++ .../trees/expressions/functions/scalar/If.java | 9 ++++++++- .../trees/expressions/functions/scalar/NullIf.java | 4 ++++ .../trees/expressions/functions/scalar/Nvl.java | 8 +++++++- .../data/correctness_p0/test_case_when_decimal.out | 6 ++++++ .../correctness_p0/test_case_when_decimal.groovy | 12 ++++++++++++ 6 files changed, 41 insertions(+), 2 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Coalesce.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Coalesce.java index f1d122d0179d4f..2ed864ba9a0c30 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Coalesce.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Coalesce.java @@ -36,6 +36,8 @@ import org.apache.doris.nereids.types.LargeIntType; import org.apache.doris.nereids.types.SmallIntType; import org.apache.doris.nereids.types.StringType; +import org.apache.doris.nereids.types.TimeType; +import org.apache.doris.nereids.types.TimeV2Type; import org.apache.doris.nereids.types.TinyIntType; import org.apache.doris.nereids.types.VarcharType; import org.apache.doris.nereids.util.ExpressionUtils; @@ -64,6 +66,8 @@ public class Coalesce extends ScalarFunction FunctionSignature.ret(DateTimeType.INSTANCE).varArgs(DateTimeType.INSTANCE), FunctionSignature.ret(DateV2Type.INSTANCE).varArgs(DateV2Type.INSTANCE), FunctionSignature.ret(DateType.INSTANCE).varArgs(DateType.INSTANCE), + FunctionSignature.ret(TimeType.INSTANCE).varArgs(TimeType.INSTANCE), + FunctionSignature.ret(TimeV2Type.INSTANCE).varArgs(TimeV2Type.INSTANCE), FunctionSignature.ret(DecimalV3Type.WILDCARD).varArgs(DecimalV3Type.WILDCARD), FunctionSignature.ret(DecimalV2Type.SYSTEM_DEFAULT).varArgs(DecimalV2Type.SYSTEM_DEFAULT), FunctionSignature.ret(BitmapType.INSTANCE).varArgs(BitmapType.INSTANCE), diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/If.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/If.java index b350b114525c77..aece9d99ac9663 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/If.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/If.java @@ -43,6 +43,8 @@ import org.apache.doris.nereids.types.NullType; import org.apache.doris.nereids.types.SmallIntType; import org.apache.doris.nereids.types.StringType; +import org.apache.doris.nereids.types.TimeType; +import org.apache.doris.nereids.types.TimeV2Type; import org.apache.doris.nereids.types.TinyIntType; import org.apache.doris.nereids.types.VarcharType; import org.apache.doris.nereids.util.TypeCoercionUtils; @@ -85,7 +87,12 @@ public class If extends ScalarFunction .args(BooleanType.INSTANCE, DoubleType.INSTANCE, DoubleType.INSTANCE), FunctionSignature.ret(DateTimeType.INSTANCE) .args(BooleanType.INSTANCE, DateTimeType.INSTANCE, DateTimeType.INSTANCE), - FunctionSignature.ret(DateType.INSTANCE).args(BooleanType.INSTANCE, DateType.INSTANCE, DateType.INSTANCE), + FunctionSignature.ret(DateType.INSTANCE).args(BooleanType.INSTANCE, DateType.INSTANCE, + DateType.INSTANCE), + FunctionSignature.ret(TimeType.INSTANCE).args(BooleanType.INSTANCE, TimeType.INSTANCE, + TimeType.INSTANCE), + FunctionSignature.ret(TimeV2Type.INSTANCE).args(BooleanType.INSTANCE, TimeV2Type.INSTANCE, + TimeV2Type.INSTANCE), FunctionSignature.ret(DecimalV3Type.WILDCARD) .args(BooleanType.INSTANCE, DecimalV3Type.WILDCARD, DecimalV3Type.WILDCARD), FunctionSignature.ret(DecimalV2Type.SYSTEM_DEFAULT) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/NullIf.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/NullIf.java index 447e60a752fc64..fe6164dc085252 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/NullIf.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/NullIf.java @@ -37,6 +37,8 @@ import org.apache.doris.nereids.types.LargeIntType; import org.apache.doris.nereids.types.SmallIntType; import org.apache.doris.nereids.types.StringType; +import org.apache.doris.nereids.types.TimeType; +import org.apache.doris.nereids.types.TimeV2Type; import org.apache.doris.nereids.types.TinyIntType; import org.apache.doris.nereids.types.VarcharType; @@ -65,6 +67,8 @@ public class NullIf extends ScalarFunction FunctionSignature.ret(DateTimeV2Type.SYSTEM_DEFAULT) .args(DateTimeV2Type.SYSTEM_DEFAULT, DateTimeV2Type.SYSTEM_DEFAULT), FunctionSignature.ret(DateV2Type.INSTANCE).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE), + FunctionSignature.ret(TimeType.INSTANCE).args(TimeType.INSTANCE, TimeType.INSTANCE), + FunctionSignature.ret(TimeV2Type.INSTANCE).args(TimeV2Type.INSTANCE, TimeV2Type.INSTANCE), FunctionSignature.ret(DecimalV2Type.SYSTEM_DEFAULT) .args(DecimalV2Type.SYSTEM_DEFAULT, DecimalV2Type.SYSTEM_DEFAULT), FunctionSignature.ret(DecimalV3Type.WILDCARD) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Nvl.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Nvl.java index 2bf82e27476da8..bcb55f948f7c41 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Nvl.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Nvl.java @@ -36,6 +36,8 @@ import org.apache.doris.nereids.types.LargeIntType; import org.apache.doris.nereids.types.SmallIntType; import org.apache.doris.nereids.types.StringType; +import org.apache.doris.nereids.types.TimeType; +import org.apache.doris.nereids.types.TimeV2Type; import org.apache.doris.nereids.types.TinyIntType; import org.apache.doris.nereids.types.VarcharType; @@ -66,7 +68,11 @@ public class Nvl extends ScalarFunction FunctionSignature.ret(DateTimeV2Type.SYSTEM_DEFAULT) .args(DateTimeV2Type.SYSTEM_DEFAULT, DateTimeV2Type.SYSTEM_DEFAULT), FunctionSignature.ret(DateV2Type.INSTANCE) - .args(DateV2Type.INSTANCE, DateV2Type.INSTANCE), + .args(DateV2Type.INSTANCE, DateV2Type.INSTANCE), + FunctionSignature.ret(TimeType.INSTANCE) + .args(TimeType.INSTANCE, TimeType.INSTANCE), + FunctionSignature.ret(TimeV2Type.INSTANCE) + .args(TimeV2Type.INSTANCE, TimeV2Type.INSTANCE), FunctionSignature.ret(BitmapType.INSTANCE).args(BitmapType.INSTANCE, BitmapType.INSTANCE), FunctionSignature.ret(VarcharType.SYSTEM_DEFAULT) .args(VarcharType.SYSTEM_DEFAULT, VarcharType.SYSTEM_DEFAULT), diff --git a/regression-test/data/correctness_p0/test_case_when_decimal.out b/regression-test/data/correctness_p0/test_case_when_decimal.out index 1a888306ed3048..ba416b026bf7c2 100644 --- a/regression-test/data/correctness_p0/test_case_when_decimal.out +++ b/regression-test/data/correctness_p0/test_case_when_decimal.out @@ -8,3 +8,9 @@ -- !sql3 -- 4.41 +-- !sql4 -- +00:01:23 00:01:23 00:01:23 00:01:23 + +-- !sql5 -- +00:01:23 00:01:23 00:01:23 00:01:23 + diff --git a/regression-test/suites/correctness_p0/test_case_when_decimal.groovy b/regression-test/suites/correctness_p0/test_case_when_decimal.groovy index baf7f52690e916..4f120f06bbd22d 100644 --- a/regression-test/suites/correctness_p0/test_case_when_decimal.groovy +++ b/regression-test/suites/correctness_p0/test_case_when_decimal.groovy @@ -97,4 +97,16 @@ suite("test_case_when_decimal") { sql """ DROP TABLE IF EXISTS `decimal_to_double_table1`; """ sql """ DROP TABLE IF EXISTS `decimal_to_double_table2`; """ + + sql """ set debug_skip_fold_constant = true; """ + + qt_sql4 """ + select ifnull(cast(123 as time) , cast(300 as time)) , coalesce(cast(123 as time) , cast(300 as time)) , if(true ,cast(123 as time) , cast(300 as time)) , nullif(cast(123 as time) , cast(300 as time)); + """ + + sql """ set debug_skip_fold_constant = false; """ + + qt_sql5 """ + select ifnull(cast(123 as time) , cast(300 as time)) , coalesce(cast(123 as time) , cast(300 as time)) , if(true ,cast(123 as time) , cast(300 as time)) , nullif(cast(123 as time) , cast(300 as time)); + """ } From 0a91d5ac13662bb24ab20ff96c583abcb0a6117a Mon Sep 17 00:00:00 2001 From: Mryange <2319153948@qq.com> Date: Fri, 27 Sep 2024 10:08:47 +0800 Subject: [PATCH 2/3] case --- .../suites/correctness_p0/test_case_when_decimal.groovy | 2 -- 1 file changed, 2 deletions(-) diff --git a/regression-test/suites/correctness_p0/test_case_when_decimal.groovy b/regression-test/suites/correctness_p0/test_case_when_decimal.groovy index 4f120f06bbd22d..e2f079c0062db1 100644 --- a/regression-test/suites/correctness_p0/test_case_when_decimal.groovy +++ b/regression-test/suites/correctness_p0/test_case_when_decimal.groovy @@ -98,13 +98,11 @@ suite("test_case_when_decimal") { sql """ DROP TABLE IF EXISTS `decimal_to_double_table1`; """ sql """ DROP TABLE IF EXISTS `decimal_to_double_table2`; """ - sql """ set debug_skip_fold_constant = true; """ qt_sql4 """ select ifnull(cast(123 as time) , cast(300 as time)) , coalesce(cast(123 as time) , cast(300 as time)) , if(true ,cast(123 as time) , cast(300 as time)) , nullif(cast(123 as time) , cast(300 as time)); """ - sql """ set debug_skip_fold_constant = false; """ qt_sql5 """ select ifnull(cast(123 as time) , cast(300 as time)) , coalesce(cast(123 as time) , cast(300 as time)) , if(true ,cast(123 as time) , cast(300 as time)) , nullif(cast(123 as time) , cast(300 as time)); From 38e8b4b3b8d67e0713b0c68b82d97be8dcf39a76 Mon Sep 17 00:00:00 2001 From: Mryange <2319153948@qq.com> Date: Fri, 27 Sep 2024 16:16:55 +0800 Subject: [PATCH 3/3] upd --- .../src/main/java/org/apache/doris/catalog/Type.java | 4 ++++ .../src/main/java/org/apache/doris/analysis/CastExpr.java | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/fe/fe-common/src/main/java/org/apache/doris/catalog/Type.java b/fe/fe-common/src/main/java/org/apache/doris/catalog/Type.java index e5f14a58127904..97afc6cea46130 100644 --- a/fe/fe-common/src/main/java/org/apache/doris/catalog/Type.java +++ b/fe/fe-common/src/main/java/org/apache/doris/catalog/Type.java @@ -429,6 +429,10 @@ public boolean isTimeV2() { return isScalarType(PrimitiveType.TIMEV2); } + public boolean isTimeLikeType() { + return isTimeV2() || isTime(); + } + public boolean isWildcardDecimal() { return false; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java index 5e97bf5ba90322..32c07f7e0b1e68 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java @@ -81,6 +81,8 @@ public class CastExpr extends Expr { TYPE_NULLABLE_MODE.put(Pair.of(fromType, toType), Function.NullableMode.ALWAYS_NULLABLE); } else if (!fromType.isDateType() && toType.isDateType()) { TYPE_NULLABLE_MODE.put(Pair.of(fromType, toType), Function.NullableMode.ALWAYS_NULLABLE); + } else if (!fromType.isTimeLikeType() && toType.isTimeLikeType()) { + TYPE_NULLABLE_MODE.put(Pair.of(fromType, toType), Function.NullableMode.ALWAYS_NULLABLE); } else { TYPE_NULLABLE_MODE.put(Pair.of(fromType, toType), Function.NullableMode.DEPEND_ON_ARGUMENT); } @@ -571,7 +573,8 @@ private int getDigital(String desc, List parameters, List inputPar public boolean isNullable() { return children.get(0).isNullable() || (children.get(0).getType().isStringType() && !getType().isStringType()) - || (!children.get(0).getType().isDateType() && getType().isDateType()); + || (!children.get(0).getType().isDateType() && getType().isDateType()) + || (!children.get(0).getType().isTimeLikeType() && getType().isTimeLikeType()); } @Override