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

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


The following commit(s) were added to refs/heads/master by this push:
     new feacb15e71 [Improvement](datev2) push down datev2 predicates with date 
literal (#17522)
feacb15e71 is described below

commit feacb15e7163c2a7452c5ec0f4ff5ac566b3b8ee
Author: Gabriel <gabrielleeb...@gmail.com>
AuthorDate: Wed Mar 8 16:54:54 2023 +0800

    [Improvement](datev2) push down datev2 predicates with date literal (#17522)
---
 .../main/java/org/apache/doris/catalog/Type.java   |  4 ++
 .../java/org/apache/doris/analysis/Analyzer.java   |  2 +
 .../org/apache/doris/analysis/BinaryPredicate.java | 20 +++++++
 .../org/apache/doris/analysis/DateLiteral.java     | 12 ++++
 .../doris/rewrite/EraseRedundantCastExpr.java      | 70 ++++++++++++++++++++++
 5 files changed, 108 insertions(+)

diff --git a/fe/fe-common/src/main/java/org/apache/doris/catalog/Type.java 
b/fe/fe-common/src/main/java/org/apache/doris/catalog/Type.java
index 810cbf835e..bc696c4753 100644
--- a/fe/fe-common/src/main/java/org/apache/doris/catalog/Type.java
+++ b/fe/fe-common/src/main/java/org/apache/doris/catalog/Type.java
@@ -508,6 +508,10 @@ public abstract class Type {
         return isScalarType(PrimitiveType.DATEV2);
     }
 
+    public boolean isDateV2OrDateTimeV2() {
+        return isScalarType(PrimitiveType.DATEV2) || 
isScalarType(PrimitiveType.DATETIMEV2);
+    }
+
     public boolean hasTemplateType() {
         return false;
     }
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java
index dc85d48f57..cae02941b9 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java
@@ -47,6 +47,7 @@ import org.apache.doris.planner.RuntimeFilter;
 import org.apache.doris.qe.ConnectContext;
 import org.apache.doris.rewrite.BetweenToCompoundRule;
 import org.apache.doris.rewrite.CompoundPredicateWriteRule;
+import org.apache.doris.rewrite.EraseRedundantCastExpr;
 import org.apache.doris.rewrite.ExprRewriteRule;
 import org.apache.doris.rewrite.ExprRewriter;
 import org.apache.doris.rewrite.ExtractCommonFactorsRule;
@@ -415,6 +416,7 @@ public class Analyzer {
             rules.add(RewriteImplicitCastRule.INSTANCE);
             rules.add(RoundLiteralInBinaryPredicatesRule.INSTANCE);
             rules.add(FoldConstantsRule.INSTANCE);
+            rules.add(EraseRedundantCastExpr.INSTANCE);
             rules.add(RewriteFromUnixTimeRule.INSTANCE);
             rules.add(CompoundPredicateWriteRule.INSTANCE);
             rules.add(RewriteDateLiteralRule.INSTANCE);
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/analysis/BinaryPredicate.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/BinaryPredicate.java
index ad0f9e6a23..c0bff0c759 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/BinaryPredicate.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/BinaryPredicate.java
@@ -364,6 +364,26 @@ public class BinaryPredicate extends Predicate implements 
Writable {
             } else if (getChild(1).getType().isDateV2()
                     && (getChild(0).getType().isDate() || 
getChild(0).getType().isDateV2())) {
                 return getChild(1).getType();
+            } else if (getChild(0).getType().isDateV2()
+                    && (getChild(1).getType().isStringType() && getChild(1) 
instanceof StringLiteral)) {
+                if (((StringLiteral) 
getChild(1)).canConvertToDateV2(Type.DATEV2)) {
+                    return Type.DATEV2;
+                } else {
+                    return Type.DATETIMEV2;
+                }
+            } else if (getChild(1).getType().isDateV2()
+                    && (getChild(0).getType().isStringType() && getChild(0) 
instanceof StringLiteral)) {
+                if (((StringLiteral) 
getChild(0)).canConvertToDateV2(Type.DATEV2)) {
+                    return Type.DATEV2;
+                } else {
+                    return Type.DATETIMEV2;
+                }
+            } else if (getChild(0).getType().isDatetimeV2()
+                    && (getChild(1).getType().isStringType() && getChild(1) 
instanceof StringLiteral)) {
+                return getChild(0).getType();
+            } else if (getChild(1).getType().isDatetimeV2()
+                    && (getChild(0).getType().isStringType() && getChild(0) 
instanceof StringLiteral)) {
+                return getChild(1).getType();
             } else {
                 return Type.DATETIME;
             }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/analysis/DateLiteral.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/DateLiteral.java
index d1f9e8a270..969ae83877 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/DateLiteral.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/DateLiteral.java
@@ -677,6 +677,18 @@ public class DateLiteral extends LiteralExpr {
         second = 0;
     }
 
+    public boolean hasTimePart() {
+        if (this.type.isDateV2() || this.type.isDate()) {
+            return false;
+        } else {
+            if (hour != 0 || minute != 0 || second != 0) {
+                return true;
+            } else {
+                return !this.type.isDatetime() && microsecond != 0;
+            }
+        }
+    }
+
     private long makePackedDatetime() {
         long ymd = ((year * 13 + month) << 5) | day;
         long hms = (hour << 12) | (minute << 6) | second;
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/rewrite/EraseRedundantCastExpr.java 
b/fe/fe-core/src/main/java/org/apache/doris/rewrite/EraseRedundantCastExpr.java
new file mode 100644
index 0000000000..829562f783
--- /dev/null
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/rewrite/EraseRedundantCastExpr.java
@@ -0,0 +1,70 @@
+// 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.
+// This file is copied from
+// 
https://github.com/apache/impala/blob/branch-2.9.0/fe/src/main/java/org/apache/impala/FoldConstantsRule.java
+// and modified by Doris
+
+package org.apache.doris.rewrite;
+
+
+import org.apache.doris.analysis.Analyzer;
+import org.apache.doris.analysis.BinaryPredicate;
+import org.apache.doris.analysis.CastExpr;
+import org.apache.doris.analysis.DateLiteral;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.SlotRef;
+import org.apache.doris.common.AnalysisException;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+/**
+ * This rule erase redundant cast between datev2/datetimev2 and date literal.
+ *
+ * for expression with pattern:
+ * BinaryPredicate
+ *  CAST (SlotRef(TYPE = DATEV2/DATETIMEV2), TYPE = DATETIME)
+ *  DATELITERAL(TYPE = DATETIME)
+ *
+ * this rule will be applied.
+ */
+public class EraseRedundantCastExpr implements ExprRewriteRule {
+    private static final Logger LOG = 
LogManager.getLogger(EraseRedundantCastExpr.class);
+
+    public static EraseRedundantCastExpr INSTANCE = new 
EraseRedundantCastExpr();
+
+    @Override
+    public Expr apply(Expr expr, Analyzer analyzer, ExprRewriter.ClauseType 
clauseType) throws AnalysisException {
+        // BinaryPredicate
+        //   CAST (SlotRef(TYPE = DATEV2/DATETIMEV2), TYPE = DATETIME)
+        //   DATELITERAL(TYPE = DATETIME)
+        if (!(expr instanceof BinaryPredicate) || !(expr.getChild(0) 
instanceof CastExpr)
+                || !expr.getChild(0).getType().isDatetime() || 
!(expr.getChild(1)).getType().isDatetime()
+                || 
!expr.getChild(0).getChild(0).getType().isDateV2OrDateTimeV2()
+                || !(expr.getChild(0).getChild(0) instanceof SlotRef)
+                || !(expr.getChild(1) instanceof DateLiteral)) {
+            return expr;
+        }
+
+        if (!((DateLiteral) expr.getChild(1)).hasTimePart()
+                || !(expr.getChild(1).getType().isDatetime() && 
expr.getChild(0).getChild(0).getType().isDateV2())) {
+            expr.getChild(1).setType(expr.getChild(0).getChild(0).getType());
+            expr.setChild(0, expr.getChild(0).getChild(0));
+        }
+        return expr;
+    }
+}


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

Reply via email to