[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)
This commit is contained in:
zclllyybb
2023-07-28 14:07:56 +08:00
committed by GitHub
parent 1c6246f7ee
commit c2155678ca
5 changed files with 19 additions and 9 deletions

View File

@ -89,7 +89,7 @@ using FunctionLocalTimestamp =
FunctionCurrentDateOrDateTime<CurrentDateTimeImpl<LocalTimestampFunctionName, false>>;
using FunctionNowWithPrecision =
FunctionCurrentDateOrDateTime<CurrentDateTimeImpl<NowFunctionName, true>, false>;
FunctionCurrentDateOrDateTime<CurrentDateTimeImpl<NowFunctionName, true>>;
using FunctionCurrentTimestampWithPrecision =
FunctionCurrentDateOrDateTime<CurrentDateTimeImpl<CurrentTimestampFunctionName, true>>;
using FunctionLocalTimeWithPrecision =

View File

@ -832,7 +832,7 @@ public:
}
};
template <typename FunctionImpl, bool DefaultNullable = true>
template <typename FunctionImpl>
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<typename FunctionImpl::ReturnType>();
}

View File

@ -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<FunctionSignature> 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<FunctionSignature> getSignatures() {
return SIGNATURES;

View File

@ -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'],

View File

@ -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') """