You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by zh...@apache.org on 2020/04/03 04:08:28 UTC

[incubator-doris] branch master updated: Fix cast date type return wrong result (#3214)

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

zhaoc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 3f247b0  Fix cast date type return wrong result (#3214)
3f247b0 is described below

commit 3f247b0d2dc424335c0b81259733cc8cbc2fcc1f
Author: HangyuanLiu <46...@qq.com>
AuthorDate: Fri Apr 3 12:08:18 2020 +0800

    Fix cast date type return wrong result (#3214)
    
    We have multiple date type, and we also need to cast between different date types.
    If not cast, it will cause problems when binarypredicate
---
 .../java/org/apache/doris/analysis/DateLiteral.java   |  7 +++++++
 .../org/apache/doris/analysis/DateLiteralTest.java    | 19 +++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/fe/src/main/java/org/apache/doris/analysis/DateLiteral.java b/fe/src/main/java/org/apache/doris/analysis/DateLiteral.java
index cce2ad8..db0631a 100644
--- a/fe/src/main/java/org/apache/doris/analysis/DateLiteral.java
+++ b/fe/src/main/java/org/apache/doris/analysis/DateLiteral.java
@@ -320,6 +320,13 @@ public class DateLiteral extends LiteralExpr {
     @Override
     protected Expr uncheckedCastTo(Type targetType) throws AnalysisException {
         if (targetType.isDateType()) {
+            if (targetType.equals(Type.DATE)) {
+                this.castToDate();                            
+            } else if (targetType.equals(Type.DATETIME)) {
+                this.type = Type.DATETIME;                            
+            } else {
+                throw new AnalysisException("Error date literal type : " + type);
+            }
             return this;
         } else if (targetType.isStringType()) {
             return new StringLiteral(getStringValue());
diff --git a/fe/src/test/java/org/apache/doris/analysis/DateLiteralTest.java b/fe/src/test/java/org/apache/doris/analysis/DateLiteralTest.java
index 2121131..02b165b 100644
--- a/fe/src/test/java/org/apache/doris/analysis/DateLiteralTest.java
+++ b/fe/src/test/java/org/apache/doris/analysis/DateLiteralTest.java
@@ -55,4 +55,23 @@ public class DateLiteralTest {
         }
         Assert.assertFalse(hasException);
     }
+
+    @Test
+    public void uncheckedCastTo() {
+        boolean hasException = false;
+        try {
+            DateLiteral literal = new DateLiteral("1997-10-07", Type.DATE);
+            Expr castToExpr = literal.uncheckedCastTo(Type.DATETIME);
+            Assert.assertTrue(castToExpr instanceof DateLiteral);
+            Assert.assertEquals(castToExpr.type, Type.DATETIME);
+
+            DateLiteral literal2 = new DateLiteral("1997-10-07 12:23:23", Type.DATETIME);
+            Expr castToExpr2 = literal2.uncheckedCastTo(Type.DATETIME);
+            Assert.assertTrue(castToExpr2 instanceof DateLiteral);
+        } catch (AnalysisException e) {
+            e.printStackTrace();
+            hasException = true;
+        }
+        Assert.assertFalse(hasException);
+    }
 }


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