From 2afbece0b8d2338c27ae990e05f2755041b76cb6 Mon Sep 17 00:00:00 2001 From: zclllyybb Date: Fri, 1 Dec 2023 09:58:20 +0800 Subject: [PATCH] [Fix](type) fix wrong type transform for unix_timestamp (#27728) fix wrong type transform for unix_timestamp --- .../apache/doris/analysis/FunctionCallExpr.java | 6 ++---- .../functions/scalar/UnixTimestamp.java | 4 ++-- .../datetime_functions/test_date_function.out | 10 +++++----- .../datetime_functions/test_date_function.groovy | 15 ++++++++------- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java index d05de49ed9..ff113892b6 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java @@ -1693,11 +1693,9 @@ public class FunctionCallExpr extends Expr { type.getScalarScale())); } else if (getChild(0).type.isStringType()) { // use DATETIME to make scale adaptive - ScalarType type = ((ScalarType) (((StringLiteral) getChild(0)) - .uncheckedCastTo(ScalarType.DATETIME).type)); + ScalarType type = ((ScalarType) (getChild(0).uncheckedCastTo(ScalarType.DATETIME).type)); if (type.isDatetimeV2()) { - int scale = ((ScalarType) (((StringLiteral) getChild(0)) - .uncheckedCastTo(ScalarType.DATETIME).type)).getScalarScale(); + int scale = type.getScalarScale(); fn.setReturnType( ScalarType.createDecimalType(PrimitiveType.DECIMAL64, 10 + scale, scale)); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/UnixTimestamp.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/UnixTimestamp.java index ab65e27fb1..9166dddf03 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/UnixTimestamp.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/UnixTimestamp.java @@ -83,8 +83,8 @@ public class UnixTimestamp extends ScalarFunction * [['unix_timestamp'], 'INT', ['DATE'], 'DEPEND_ON_ARGUMENT'], * [['unix_timestamp'], 'DECIMAL64', ['DATETIMEV2'], 'DEPEND_ON_ARGUMENT'], * [['unix_timestamp'], 'INT', ['DATEV2'], 'DEPEND_ON_ARGUMENT'], - * [['unix_timestamp'], 'INT', ['VARCHAR', 'VARCHAR'], 'ALWAYS_NULLABLE'], - * [['unix_timestamp'], 'INT', ['STRING', 'STRING'], 'ALWAYS_NULLABLE'], + * [['unix_timestamp'], 'DECIMAL64', ['VARCHAR', 'VARCHAR'], 'ALWAYS_NULLABLE'], + * [['unix_timestamp'], 'DECIMAL64', ['STRING', 'STRING'], 'ALWAYS_NULLABLE'], */ @Override public boolean nullable() { diff --git a/regression-test/data/nereids_p0/sql_functions/datetime_functions/test_date_function.out b/regression-test/data/nereids_p0/sql_functions/datetime_functions/test_date_function.out index 542b301ddd..086faf0c85 100644 --- a/regression-test/data/nereids_p0/sql_functions/datetime_functions/test_date_function.out +++ b/regression-test/data/nereids_p0/sql_functions/datetime_functions/test_date_function.out @@ -652,9 +652,9 @@ true 2023-08-17T17:41:18 -- !sql_dt_null_1 -- -1607702400.0000 1607702400 1607702400 1607702400 -1607746332.0000 1607746332 1607702400 1607702400 -1607746332.0000 1607746332 1607702400 1607702400 -1607746332.1230 1607746332 1607702400 1607702400 -1607746332.6667 1607746333 1607702400 1607702400 +1607702400.0000 1607702400 1607702400 1607702400 1607702400 +1607746332.0000 1607746332 1607702400 1607702400 1607746332 +1607746332.0000 1607746332 1607702400 1607702400 1607746332 +1607746332.1230 1607746332 1607702400 1607702400 1607746332 +1607746332.6667 1607746333 1607702400 1607702400 1607746333 diff --git a/regression-test/suites/nereids_p0/sql_functions/datetime_functions/test_date_function.groovy b/regression-test/suites/nereids_p0/sql_functions/datetime_functions/test_date_function.groovy index 447dda31cd..633e884dad 100644 --- a/regression-test/suites/nereids_p0/sql_functions/datetime_functions/test_date_function.groovy +++ b/regression-test/suites/nereids_p0/sql_functions/datetime_functions/test_date_function.groovy @@ -680,15 +680,16 @@ suite("test_date_function") { `dtv24` datetimev2(4) NOT NULL, `dtv20n` datetimev2(0) NULL, `dv2` datev2 NOT NULL, - `dv2n` datev2 NULL + `dv2n` datev2 NULL, + `str` VARCHAR NULL ) DISTRIBUTED BY HASH(`k1`) BUCKETS 5 properties("replication_num" = "1"); """ - sql """ insert into dt_null values ('1', '2020-12-12', '2020-12-12', '2020-12-12', '2020-12-12'), - ('2', '2020-12-12 12:12:12', '2020-12-12 12:12:12', '2020-12-12 12:12:12', '2020-12-12 12:12:12'), - ('3', '2020-12-12 12:12:12.0', '2020-12-12 12:12:12.0', '2020-12-12 12:12:12.0', '2020-12-12 12:12:12.0'), - ('4', '2020-12-12 12:12:12.123', '2020-12-12 12:12:12.123', '2020-12-12 12:12:12.123', '2020-12-12 12:12:12.123'), - ('5', '2020-12-12 12:12:12.666666', '2020-12-12 12:12:12.666666', '2020-12-12 12:12:12.666666', '2020-12-12 12:12:12.666666'); """ + sql """ insert into dt_null values ('1', '2020-12-12', '2020-12-12', '2020-12-12', '2020-12-12', '2020-12-12'), + ('2', '2020-12-12 12:12:12', '2020-12-12 12:12:12', '2020-12-12 12:12:12', '2020-12-12 12:12:12', '2020-12-12 12:12:12'), + ('3', '2020-12-12 12:12:12.0', '2020-12-12 12:12:12.0', '2020-12-12 12:12:12.0', '2020-12-12 12:12:12.0', '2020-12-12 12:12:12.0'), + ('4', '2020-12-12 12:12:12.123', '2020-12-12 12:12:12.123', '2020-12-12 12:12:12.123', '2020-12-12 12:12:12.123', '2020-12-12 12:12:12.123'), + ('5', '2020-12-12 12:12:12.666666', '2020-12-12 12:12:12.666666', '2020-12-12 12:12:12.666666', '2020-12-12 12:12:12.666666', '2020-12-12 12:12:12.666666'); """ - qt_sql_dt_null_1 """ select unix_timestamp(dtv24), unix_timestamp(dtv20n),unix_timestamp(dv2),unix_timestamp(dv2n) from dt_null order by k1; """ + qt_sql_dt_null_1 """ select unix_timestamp(dtv24), unix_timestamp(dtv20n), unix_timestamp(dv2), unix_timestamp(dv2n), unix_timestamp(str) from dt_null order by k1; """ }