From c2155678ca1266433a1437b53eff5796e85579bb Mon Sep 17 00:00:00 2001 From: zclllyybb Date: Fri, 28 Jul 2023 14:07:56 +0800 Subject: [PATCH] [fix](functions) fix now(null) crash (#22321) before: BE crash now: mysql [test]>select now(null); +-----------+ | now(NULL) | +-----------+ | NULL | +-----------+ 1 row in set (0.06 sec) --- .../function_date_or_datetime_computation.cpp | 2 +- .../function_date_or_datetime_computation.h | 4 +--- .../trees/expressions/functions/scalar/Now.java | 16 +++++++++++++--- gensrc/script/doris_builtins_functions.py | 4 ++-- .../datetime_functions/test_date_function.groovy | 2 ++ 5 files changed, 19 insertions(+), 9 deletions(-) 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 8034d34c19..9506835c5d 100644 --- a/be/src/vec/functions/function_date_or_datetime_computation.cpp +++ b/be/src/vec/functions/function_date_or_datetime_computation.cpp @@ -89,7 +89,7 @@ using FunctionLocalTimestamp = FunctionCurrentDateOrDateTime>; using FunctionNowWithPrecision = - FunctionCurrentDateOrDateTime, false>; + FunctionCurrentDateOrDateTime>; using FunctionCurrentTimestampWithPrecision = FunctionCurrentDateOrDateTime>; using FunctionLocalTimeWithPrecision = 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 f91fe643b3..3ba1808c9c 100644 --- a/be/src/vec/functions/function_date_or_datetime_computation.h +++ b/be/src/vec/functions/function_date_or_datetime_computation.h @@ -832,7 +832,7 @@ public: } }; -template +template class FunctionCurrentDateOrDateTime : public IFunction { public: static constexpr bool has_variadic_argument = @@ -845,8 +845,6 @@ public: size_t get_number_of_arguments() const override { return 0; } - bool use_default_implementation_for_nulls() const override { return DefaultNullable; } - DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override { return std::make_shared(); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Now.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Now.java index 19feb7684c..90b1f77a37 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Now.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Now.java @@ -19,7 +19,6 @@ 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.AlwaysNotNullable; import org.apache.doris.nereids.trees.expressions.functions.DateTimeWithPrecision; import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature; import org.apache.doris.nereids.trees.expressions.functions.Nondeterministic; @@ -36,8 +35,7 @@ import java.util.List; /** * ScalarFunction 'now'. This class is generated by GenerateFunction. */ -public class Now extends DateTimeWithPrecision - implements ExplicitlyCastableSignature, Nondeterministic, AlwaysNotNullable { +public class Now extends DateTimeWithPrecision implements ExplicitlyCastableSignature, Nondeterministic { public static final List SIGNATURES = ImmutableList.of( FunctionSignature.ret(DateTimeType.INSTANCE).args(), @@ -72,6 +70,18 @@ public class Now extends DateTimeWithPrecision } } + /** + * Depend on child. + */ + @Override + public boolean nullable() { + if (arity() == 0) { + return false; + } + Preconditions.checkArgument(children.size() == 1); + return child(0).nullable(); + } + @Override public List getSignatures() { return SIGNATURES; diff --git a/gensrc/script/doris_builtins_functions.py b/gensrc/script/doris_builtins_functions.py index 31c1bef74d..a0141972a9 100644 --- a/gensrc/script/doris_builtins_functions.py +++ b/gensrc/script/doris_builtins_functions.py @@ -857,8 +857,8 @@ visible_functions = { [['from_unixtime'], 'VARCHAR', ['INT'], 'ALWAYS_NULLABLE'], [['from_unixtime'], 'VARCHAR', ['INT', 'VARCHAR'], 'ALWAYS_NULLABLE'], [['from_unixtime'], 'VARCHAR', ['INT', 'STRING'], 'ALWAYS_NULLABLE'], - [['now', 'current_timestamp', 'localtime', 'localtimestamp'], 'DATETIME', [], 'ALWAYS_NOT_NULLABLE'], - [['now', 'current_timestamp', 'localtime', 'localtimestamp'], 'DATETIMEV2', ['INT'], 'ALWAYS_NOT_NULLABLE'], + [['now', 'current_timestamp', 'localtime', 'localtimestamp'], 'DATETIME', [], 'DEPEND_ON_ARGUMENT'], + [['now', 'current_timestamp', 'localtime', 'localtimestamp'], 'DATETIMEV2', ['INT'], 'DEPEND_ON_ARGUMENT'], [['curtime', 'current_time'], 'TIME', [], 'ALWAYS_NOT_NULLABLE'], [['curdate', 'current_date'], 'DATE', [], 'ALWAYS_NOT_NULLABLE'], [['utc_timestamp'], 'DATETIME', [], 'ALWAYS_NOT_NULLABLE'], diff --git a/regression-test/suites/query_p0/sql_functions/datetime_functions/test_date_function.groovy b/regression-test/suites/query_p0/sql_functions/datetime_functions/test_date_function.groovy index 6debd4d0b8..f3d775408e 100644 --- a/regression-test/suites/query_p0/sql_functions/datetime_functions/test_date_function.groovy +++ b/regression-test/suites/query_p0/sql_functions/datetime_functions/test_date_function.groovy @@ -317,6 +317,8 @@ suite("test_date_function") { // NOW def now_result = sql """ select now() """ assertTrue(now_result[0].size() == 1) + def now_null_result = sql """ select now(null) """ + assertTrue(now_null_result[0].size() == 1) // SECOND qt_sql """ select second('2018-12-31 23:59:59') """