You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by kx...@apache.org on 2023/06/06 15:15:03 UTC

[doris] 01/36: [Fix](Planner)fix cast date/datev2/datetime to float/double return null. (#20008)

This is an automated email from the ASF dual-hosted git repository.

kxiao pushed a commit to branch branch-2.0-beta
in repository https://gitbox.apache.org/repos/asf/doris.git

commit eb044d8b752e830c5b823f8b46a08736321017ae
Author: mch_ucchi <41...@users.noreply.github.com>
AuthorDate: Mon Jun 5 19:06:50 2023 +0800

    [Fix](Planner)fix cast date/datev2/datetime to float/double return null. (#20008)
---
 fe/fe-core/src/main/cup/sql_parser.cup             |  6 ++--
 .../java/org/apache/doris/analysis/ExprTest.java   | 10 +++++++
 .../sql/dateTimeOperatorsAccessible.out            |  2 +-
 .../suites/query_p0/cast/test_cast.groovy          | 34 ++++++++++++++++++++++
 4 files changed, 48 insertions(+), 4 deletions(-)

diff --git a/fe/fe-core/src/main/cup/sql_parser.cup b/fe/fe-core/src/main/cup/sql_parser.cup
index 6542ad40e4..3a85b99dd4 100644
--- a/fe/fe-core/src/main/cup/sql_parser.cup
+++ b/fe/fe-core/src/main/cup/sql_parser.cup
@@ -6421,11 +6421,11 @@ non_pred_expr ::=
   | function_call_expr:e
   {: RESULT = e; :}
   | KW_DATE STRING_LITERAL:l
-  {: RESULT = new StringLiteral(l); :}
+  {: RESULT = new CastExpr(Type.DATE, new StringLiteral(l)); :}
   | KW_DATEV2 STRING_LITERAL:l
-  {: RESULT = new StringLiteral(l); :}
+  {: RESULT = new CastExpr(Type.DATEV2, new StringLiteral(l)); :}
   | KW_TIMESTAMP STRING_LITERAL:l
-  {: RESULT = new StringLiteral(l); :}
+  {: RESULT = new CastExpr(Type.DATETIME, new StringLiteral(l)); :}
   | KW_EXTRACT LPAREN function_name:fn_name KW_FROM func_arg_list:exprs RPAREN
   {: RESULT = new FunctionCallExpr(fn_name, exprs); :}
   //| function_name:fn_name LPAREN RPAREN
diff --git a/fe/fe-core/src/test/java/org/apache/doris/analysis/ExprTest.java b/fe/fe-core/src/test/java/org/apache/doris/analysis/ExprTest.java
index 475d9dc8fb..5262a29034 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/analysis/ExprTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/ExprTest.java
@@ -127,6 +127,16 @@ public class ExprTest {
         Assert.assertEquals(0, castLiteral.getMinute());
         Assert.assertEquals(0, castLiteral.getSecond());
 
+        DateLiteral srcDate = new DateLiteral("2020-01-01", Type.DATE);
+        DateLiteral srcDateV2 = new DateLiteral("2020-01-01", Type.DATEV2);
+        DateLiteral srcDateTime = new DateLiteral("2020-01-01 12:34:45", Type.DATETIME);
+        Assert.assertEquals(20200101L, ((FloatLiteral) (new CastExpr(Type.FLOAT, srcDate)
+                .castTo(Type.FLOAT)).getResultValue(false)).getLongValue());
+        Assert.assertEquals(20200101L, ((FloatLiteral) new CastExpr(Type.FLOAT, srcDateV2)
+                .castTo(Type.FLOAT).getResultValue(false)).getLongValue());
+        Assert.assertEquals(20200101123445L, ((FloatLiteral) new CastExpr(Type.FLOAT, srcDateTime)
+                .castTo(Type.FLOAT).getResultValue(false)).getLongValue());
+
         // float
         FloatLiteral floatLiteral = new FloatLiteral(0.1, Type.FLOAT);
         Assert.assertEquals(floatLiteral.getType(), Type.FLOAT);
diff --git a/regression-test/data/query_p0/sql_functions/horology_functions/sql/dateTimeOperatorsAccessible.out b/regression-test/data/query_p0/sql_functions/horology_functions/sql/dateTimeOperatorsAccessible.out
index bfac26aba9..a6071694b7 100644
--- a/regression-test/data/query_p0/sql_functions/horology_functions/sql/dateTimeOperatorsAccessible.out
+++ b/regression-test/data/query_p0/sql_functions/horology_functions/sql/dateTimeOperatorsAccessible.out
@@ -1,4 +1,4 @@
 -- This file is automatically generated. You should know what you did if you want to edit this
 -- !dateTimeOperatorsAccessible --
-2012-08-10T00:00	2012-08-09T06:00	2012-11-30T01:00	2012-08-06T00:00	2012-08-06T20:00	2012-09-30T01:00
+2012-08-10	2012-08-09T06:00	2012-11-30T01:00	2012-08-06	2012-08-06T20:00	2012-09-30T01:00
 
diff --git a/regression-test/suites/query_p0/cast/test_cast.groovy b/regression-test/suites/query_p0/cast/test_cast.groovy
new file mode 100644
index 0000000000..bfe4a87989
--- /dev/null
+++ b/regression-test/suites/query_p0/cast/test_cast.groovy
@@ -0,0 +1,34 @@
+// 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.
+
+suite('test_cast') {
+    def date = "date '2020-01-01'"
+    def datev2 = "datev2 '2020-01-01'"
+    def datetime = "timestamp '2020-01-01 12:34:45'"
+    test {
+        sql "select cast(${date} as int), cast(${date} as bigint), cast(${date} as float), cast(${date} as double)"
+        result([[20200101, 20200101l, ((float) 20200101), ((double) 20200101)]])
+    }
+    test {
+        sql "select cast(${datev2} as int), cast(${datev2} as bigint), cast(${datev2} as float), cast(${datev2} as double)"
+        result([[20200101, 20200101l, ((float) 20200101), ((double) 20200101)]])
+    }
+    test {
+        sql "select cast(${datetime} as int), cast(${datetime} as bigint), cast(${datetime} as float), cast(${datetime} as double)"
+        result([[869930357, 20200101123445l, ((float) 20200101123445l), ((double) 20200101123445l)]])
+    }
+}
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org