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

morningman 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 ff5a0e9  [improvement](planner) make BinaryPredicate do not cast date 
to datetime/varchar (#7045)
ff5a0e9 is described below

commit ff5a0e98b0e0f141f2e57ab2bb12ae16698ce8f4
Author: Pxl <[email protected]>
AuthorDate: Fri Dec 24 21:22:43 2021 +0800

    [improvement](planner) make BinaryPredicate do not cast date to 
datetime/varchar (#7045)
---
 be/src/exprs/bloomfilter_predicate.h               | 13 ++---
 .../org/apache/doris/analysis/BinaryPredicate.java | 63 ++++++++++------------
 .../main/java/org/apache/doris/catalog/Type.java   | 33 +++++++-----
 .../org/apache/doris/planner/QueryPlanTest.java    |  5 ++
 4 files changed, 55 insertions(+), 59 deletions(-)

diff --git a/be/src/exprs/bloomfilter_predicate.h 
b/be/src/exprs/bloomfilter_predicate.h
index e90f02d..11a8725 100644
--- a/be/src/exprs/bloomfilter_predicate.h
+++ b/be/src/exprs/bloomfilter_predicate.h
@@ -229,16 +229,9 @@ struct DateTimeFindOp : public CommonFindOp<DateTimeValue, 
BloomFilterAdaptor> {
 template <class BloomFilterAdaptor>
 struct DateFindOp : public CommonFindOp<DateTimeValue, BloomFilterAdaptor> {
     bool find_olap_engine(const BloomFilterAdaptor& bloom_filter, const void* 
data) const {
-        uint24_t date = *static_cast<const uint24_t*>(data);
-        uint64_t value = uint32_t(date);
-
-        DateTimeValue date_value;
-        date_value.from_olap_date(value);
-        date_value.to_datetime();
-
-        char data_bytes[sizeof(date_value)];
-        memcpy(&data_bytes, &date_value, sizeof(date_value));
-        return bloom_filter.test_bytes(data_bytes, sizeof(DateTimeValue));
+        DateTimeValue value;
+        value.from_olap_date(*reinterpret_cast<const uint24_t*>(data));
+        return bloom_filter.test_bytes((char*)&value, sizeof(DateTimeValue));
     }
 };
 
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 274bd03..94e55b0 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
@@ -265,26 +265,7 @@ public class BinaryPredicate extends Predicate implements 
Writable {
         LOG.debug(debugString() + " opcode: " + vectorOpcode);
     }
 
-    private boolean canCompareDate(PrimitiveType t1, PrimitiveType t2) {
-        if (t1.isDateType()) {
-            if (t2.isDateType() || t2.isStringType() || t2.isIntegerType()) {
-                return true;
-            }
-            return false;
-        } else if (t2.isDateType()) {
-            if (t1.isStringType() || t1.isIntegerType()) {
-                return true;
-            }
-            return false;
-        } else {
-            return false;
-        }
-    }
-
     private Type getCmpType() throws AnalysisException {
-        PrimitiveType t1 = 
getChild(0).getType().getResultType().getPrimitiveType();
-        PrimitiveType t2 = 
getChild(1).getType().getResultType().getPrimitiveType();
-
         for (Expr e : getChildren()) {
             if (e.getType().getPrimitiveType() == PrimitiveType.HLL) {
                 throw new AnalysisException("Hll type dose not support 
operand: " + toSql());
@@ -294,29 +275,39 @@ public class BinaryPredicate extends Predicate implements 
Writable {
             }
         }
 
-        if (canCompareDate(getChild(0).getType().getPrimitiveType(), 
getChild(1).getType().getPrimitiveType())) {
+        Type t1 = getChild(0).getType();
+        Type t2 = getChild(1).getType();
+
+        if (Type.canCompareDate(t1.getPrimitiveType(), t2.getPrimitiveType())) 
{
+            return Type.DATE;
+        }
+
+        if (Type.canCompareDatetime(t1.getPrimitiveType(), 
t2.getPrimitiveType())) {
             return Type.DATETIME;
         }
 
+        PrimitiveType t1ResultType = t1.getResultType().getPrimitiveType();
+        PrimitiveType t2ResultType = t2.getResultType().getPrimitiveType();
+
         // Following logical is compatible with MySQL:
-        //    Cast to DOUBLE by default, because DOUBLE has the largest range 
of values.
-        if (t1 == PrimitiveType.VARCHAR && t2 == PrimitiveType.VARCHAR) {
+        // Cast to DOUBLE by default, because DOUBLE has the largest range of 
values.
+        if (t1ResultType == PrimitiveType.VARCHAR && t2ResultType == 
PrimitiveType.VARCHAR) {
             return Type.VARCHAR;
         }
-        if (t1 == PrimitiveType.STRING && t2 == PrimitiveType.STRING
-                || t1 == PrimitiveType.STRING && t2 == PrimitiveType.VARCHAR
-                || t1 == PrimitiveType.VARCHAR && t2 == PrimitiveType.STRING) {
+        if (t1ResultType == PrimitiveType.STRING && t2ResultType == 
PrimitiveType.STRING
+                || t1ResultType == PrimitiveType.STRING && t2ResultType == 
PrimitiveType.VARCHAR
+                || t1ResultType == PrimitiveType.VARCHAR && t2ResultType == 
PrimitiveType.STRING) {
             return Type.STRING;
         }
-        if (t1 == PrimitiveType.BIGINT && t2 == PrimitiveType.BIGINT) {
-            return Type.getAssignmentCompatibleType(getChild(0).getType(), 
getChild(1).getType(), false);
+        if (t1ResultType == PrimitiveType.BIGINT && t2ResultType == 
PrimitiveType.BIGINT) {
+            return Type.getAssignmentCompatibleType(t1, t2, false);
         }
-        if ((t1 == PrimitiveType.BIGINT || t1 == PrimitiveType.DECIMALV2)
-                && (t2 == PrimitiveType.BIGINT || t2 == 
PrimitiveType.DECIMALV2)) {
+        if ((t1ResultType == PrimitiveType.BIGINT || t1ResultType == 
PrimitiveType.DECIMALV2)
+                && (t2ResultType == PrimitiveType.BIGINT || t2ResultType == 
PrimitiveType.DECIMALV2)) {
             return Type.DECIMALV2;
         }
-        if ((t1 == PrimitiveType.BIGINT || t1 == PrimitiveType.LARGEINT)
-                && (t2 == PrimitiveType.BIGINT || t2 == 
PrimitiveType.LARGEINT)) {
+        if ((t1ResultType == PrimitiveType.BIGINT || t1ResultType == 
PrimitiveType.LARGEINT)
+                && (t2ResultType == PrimitiveType.BIGINT || t2ResultType == 
PrimitiveType.LARGEINT)) {
             return Type.LARGEINT;
         }
 
@@ -327,17 +318,19 @@ public class BinaryPredicate extends Predicate implements 
Writable {
         // When int column compares with string, Mysql will convert string to 
int.
         // So it is also compatible with Mysql.
 
-        if (t1 == PrimitiveType.BIGINT && (t2 == PrimitiveType.VARCHAR || t2 
==PrimitiveType.STRING)) {
+        if (t1ResultType == PrimitiveType.BIGINT
+                && (t2ResultType == PrimitiveType.VARCHAR || t2ResultType == 
PrimitiveType.STRING)) {
             Expr rightChild = getChild(1);
             Long parsedLong = Type.tryParseToLong(rightChild);
-            if(parsedLong != null) {
+            if (parsedLong != null) {
                 return Type.BIGINT;
             }
         }
-        if ((t1 == PrimitiveType.VARCHAR || t1 ==PrimitiveType.STRING) && t2 
== PrimitiveType.BIGINT) {
+        if ((t1ResultType == PrimitiveType.VARCHAR || t1ResultType == 
PrimitiveType.STRING)
+                && t2ResultType == PrimitiveType.BIGINT) {
             Expr leftChild = getChild(0);
             Long parsedLong = Type.tryParseToLong(leftChild);
-            if(parsedLong != null) {
+            if (parsedLong != null) {
                 return Type.BIGINT;
             }
         }
diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Type.java 
b/fe/fe-core/src/main/java/org/apache/doris/catalog/Type.java
index 7e2d2ab..3b3905f 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Type.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Type.java
@@ -1005,15 +1005,20 @@ public abstract class Type {
             return t1;
         }
 
-        PrimitiveType t1ResultType = t1.getResultType().getPrimitiveType();
-        PrimitiveType t2ResultType = t2.getResultType().getPrimitiveType();
         if (canCompareDate(t1.getPrimitiveType(), t2.getPrimitiveType())) {
+            return Type.DATE;
+        }
+
+        if (canCompareDatetime(t1.getPrimitiveType(), t2.getPrimitiveType())) {
             return Type.DATETIME;
         }
 
+        PrimitiveType t1ResultType = t1.getResultType().getPrimitiveType();
+        PrimitiveType t2ResultType = t2.getResultType().getPrimitiveType();
+
         // Following logical is compatible with MySQL.
         if (t1ResultType == PrimitiveType.VARCHAR && t2ResultType == 
PrimitiveType.VARCHAR) {
-            return Type.VARCHAR; 
+            return Type.VARCHAR;
         }
         if ((t1ResultType == PrimitiveType.STRING && t2ResultType == 
PrimitiveType.STRING)
                 || (t1ResultType == PrimitiveType.STRING && t2ResultType == 
PrimitiveType.VARCHAR)
@@ -1022,30 +1027,30 @@ public abstract class Type {
         }
 
         // int family type and char family type should cast to char family type
-        if ((t1ResultType.isFixedPointType() && t2ResultType.isCharFamily()) ||
-                (t2ResultType.isFixedPointType() && 
t1ResultType.isCharFamily())) {
-            return t1.isStringType() ?  t1 : t2;
+        if ((t1ResultType.isFixedPointType() && t2ResultType.isCharFamily())
+                || (t2ResultType.isFixedPointType() && 
t1ResultType.isCharFamily())) {
+            return t1.isStringType() ? t1 : t2;
         }
 
         if (t1ResultType == PrimitiveType.BIGINT && t2ResultType == 
PrimitiveType.BIGINT) {
             return getAssignmentCompatibleType(t1, t2, false);
         }
-        if ((t1ResultType == PrimitiveType.BIGINT
-                || t1ResultType == PrimitiveType.DECIMALV2)
-                && (t2ResultType == PrimitiveType.BIGINT
-                || t2ResultType == PrimitiveType.DECIMALV2)) {
+        if ((t1ResultType == PrimitiveType.BIGINT || t1ResultType == 
PrimitiveType.DECIMALV2)
+                && (t2ResultType == PrimitiveType.BIGINT || t2ResultType == 
PrimitiveType.DECIMALV2)) {
             return Type.DECIMALV2;
         }
-        if ((t1ResultType == PrimitiveType.BIGINT
-                || t1ResultType == PrimitiveType.LARGEINT)
-                && (t2ResultType == PrimitiveType.BIGINT
-                || t2ResultType == PrimitiveType.LARGEINT)) {
+        if ((t1ResultType == PrimitiveType.BIGINT || t1ResultType == 
PrimitiveType.LARGEINT)
+                && (t2ResultType == PrimitiveType.BIGINT || t2ResultType == 
PrimitiveType.LARGEINT)) {
             return Type.LARGEINT;
         }
         return Type.DOUBLE;
     }
 
     public static boolean canCompareDate(PrimitiveType t1, PrimitiveType t2) {
+        return (t1 == PrimitiveType.DATE && t2 == PrimitiveType.DATE);
+    }
+
+    public static boolean canCompareDatetime(PrimitiveType t1, PrimitiveType 
t2) {
         if (t1.isDateType()) {
             if (t2.isDateType() || t2.isStringType() || t2.isIntegerType()) {
                 return true;
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/planner/QueryPlanTest.java 
b/fe/fe-core/src/test/java/org/apache/doris/planner/QueryPlanTest.java
index 31607c5..5c7f098 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/planner/QueryPlanTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/planner/QueryPlanTest.java
@@ -1596,6 +1596,11 @@ public class QueryPlanTest {
         String sql = "select day from tbl_int_date where day = '2020-10-30'";
         String explainString = 
UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, "EXPLAIN " + sql);
         Assert.assertTrue(explainString.contains("PREDICATES: `day` = 
'2020-10-30 00:00:00'"));
+
+        sql = "select day from tbl_int_date where day = cast('2020-10-30' as 
date)";
+        explainString = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, 
"EXPLAIN " + sql);
+        Assert.assertTrue(explainString.contains("PREDICATES: `day` = 
'2020-10-30'"));
+
         sql = "select day from tbl_int_date where day = 
from_unixtime(1196440219)";
         explainString = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, 
"EXPLAIN " + sql);
         Assert.assertTrue(explainString.contains("PREDICATES: `day` = 
'2007-12-01 00:30:19'"));

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to