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/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 10134ea8c6 [fix](planner) fix RewriteInPredicateRule may be useless 
(#20668)
10134ea8c6 is described below

commit 10134ea8c6d49b2f6646f715ce32af71f0c4d1b7
Author: zxealous <zhouchang...@baidu.com>
AuthorDate: Mon Jun 12 14:39:01 2023 +0800

    [fix](planner) fix RewriteInPredicateRule may be useless (#20668)
    
    Issue Number: close #20669
    
    RewriteInPredicateRule may cast InPredicate expr's two child to the same 
type, for example: where cast(age as char) in ('11'), the type of age is int, 
RewriteInPredicateRule will cast expr's two child type to int. As in the 
example above, child 0 will be such struct:
    ```
    child 0: type: int
        |---  child: type : char
                |-- child: type : int
    ```
    
    Due to the RewriteInPredicateRule cast the type of the expr to int, it will 
reanalyze stmt, but it will reset stmt first before reanalyze the stmt, and 
reset opt will change child 0 to such struct:
    ```
    child: type : char
        |-- child: type : int
    ```
    It cause two child's type will be cast to varchar in func 
castAllToCompatibleType, the logic of RewriteInPredicateRule will be useless.
    
    In 1.1-lts and 1.2-lts, such case  " where cast(age as char) in ('11')"  
can't work well,  because func castAllToCompatibleType will cast int to char 
but int can't cast to char(master can work well because func 
castAllToCompatibleType will cast int to varchar in such case).
    ```
    MySQL [test]> select user_id from test_cast where cast(age as char) in 
('45');
    ERROR 1105 (HY000): errCode = 2, detailMessage = type not match, 
originType=INT, targeType=CHAR(*)
    ```
---
 .../org/apache/doris/rewrite/RewriteInPredicateRule.java   | 14 ++++++++++++--
 .../sql_functions/conditional_functions/test_query_in.out  |  3 +++
 .../sql_functions/conditional_functions/test_query_in.out  |  3 +++
 .../conditional_functions/test_query_in.groovy             |  1 +
 .../conditional_functions/test_query_in.groovy             |  1 +
 5 files changed, 20 insertions(+), 2 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/rewrite/RewriteInPredicateRule.java 
b/fe/fe-core/src/main/java/org/apache/doris/rewrite/RewriteInPredicateRule.java
index ea7996c3fc..acf26abffb 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/rewrite/RewriteInPredicateRule.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/rewrite/RewriteInPredicateRule.java
@@ -19,6 +19,7 @@ package org.apache.doris.rewrite;
 
 import org.apache.doris.analysis.Analyzer;
 import org.apache.doris.analysis.BoolLiteral;
+import org.apache.doris.analysis.CastExpr;
 import org.apache.doris.analysis.Expr;
 import org.apache.doris.analysis.InPredicate;
 import org.apache.doris.analysis.LiteralExpr;
@@ -68,8 +69,17 @@ public class RewriteInPredicateRule implements 
ExprRewriteRule {
             return expr;
         }
 
-        Expr newColumnExpr = expr.getChild(0).getType().getPrimitiveType() == 
columnType.getPrimitiveType()
-                ? expr.getChild(0) : expr.getChild(0).castTo(columnType);
+        Expr newColumnExpr;
+        if (expr.getChild(0).getType().getPrimitiveType() == 
columnType.getPrimitiveType()) {
+            newColumnExpr = expr.getChild(0);
+        } else {
+            if (inPredicate.getChild(0) instanceof CastExpr && 
inPredicate.getChild(0).getChild(0).getType()
+                    .equals(columnType)) {
+                newColumnExpr = inPredicate.getChild(0).getChild(0);
+            } else {
+                newColumnExpr = expr.getChild(0).castTo(columnType);
+            }
+        }
         List<Expr> newInList = Lists.newArrayList();
         boolean isCast = false;
         for (int i = 1; i < inPredicate.getChildren().size(); ++i) {
diff --git 
a/regression-test/data/nereids_p0/sql_functions/conditional_functions/test_query_in.out
 
b/regression-test/data/nereids_p0/sql_functions/conditional_functions/test_query_in.out
index c4977ab256..3131d52fdc 100644
--- 
a/regression-test/data/nereids_p0/sql_functions/conditional_functions/test_query_in.out
+++ 
b/regression-test/data/nereids_p0/sql_functions/conditional_functions/test_query_in.out
@@ -100,3 +100,6 @@ jj  -28532
 -- !in31 --
 jj     -28532
 
+-- !in32 --
+false  1       1989    1001    11011902        123.123 true    1989-03-21      
1989-03-21T13:00        wangjuoo4       0.1     6.333   string12345     
170141183460469231731687303715884105727
+false  3       1989    1002    11011905        24453.325       false   
2012-03-14      2000-01-01T00:00        yunlj8@nk       78945.0 3654.0  
string12345     0
diff --git 
a/regression-test/data/query_p0/sql_functions/conditional_functions/test_query_in.out
 
b/regression-test/data/query_p0/sql_functions/conditional_functions/test_query_in.out
index 1a805eef88..6681fea423 100644
--- 
a/regression-test/data/query_p0/sql_functions/conditional_functions/test_query_in.out
+++ 
b/regression-test/data/query_p0/sql_functions/conditional_functions/test_query_in.out
@@ -93,3 +93,6 @@ jj    -28532
 -- !in31 --
 jj     -28532
 
+-- !in32 --
+false  1       1989    1001    11011902        123.123 true    1989-03-21      
1989-03-21T13:00        wangjuoo4       0.1     6.333   string12345     
170141183460469231731687303715884105727
+false  3       1989    1002    11011905        24453.325       false   
2012-03-14      2000-01-01T00:00        yunlj8@nk       78945.0 3654.0  
string12345     0
diff --git 
a/regression-test/suites/nereids_p0/sql_functions/conditional_functions/test_query_in.groovy
 
b/regression-test/suites/nereids_p0/sql_functions/conditional_functions/test_query_in.groovy
index 902c455f8c..1042922275 100644
--- 
a/regression-test/suites/nereids_p0/sql_functions/conditional_functions/test_query_in.groovy
+++ 
b/regression-test/suites/nereids_p0/sql_functions/conditional_functions/test_query_in.groovy
@@ -63,4 +63,5 @@ suite("test_query_in", "query,p0") {
             where kk1 in ('jj')"""
     qt_in31 """select * from (select 'jj' as kk1, sum(k2) from ${tableName2} 
where k10 = '2015-04-02' group by kk1)tt
             where kk1 = 'jj'"""
+    qt_in32 """select * from ${tableName1} where cast(k1 as char) in (1, -1, 
5, 0.1, 3.000) order by k1, k2, k3, k4"""
 }
diff --git 
a/regression-test/suites/query_p0/sql_functions/conditional_functions/test_query_in.groovy
 
b/regression-test/suites/query_p0/sql_functions/conditional_functions/test_query_in.groovy
index de8b2870ca..6cd267dee7 100644
--- 
a/regression-test/suites/query_p0/sql_functions/conditional_functions/test_query_in.groovy
+++ 
b/regression-test/suites/query_p0/sql_functions/conditional_functions/test_query_in.groovy
@@ -60,4 +60,5 @@ suite("test_query_in", "query,p0") {
             where kk1 in ('jj')"""
     qt_in31 """select * from (select 'jj' as kk1, sum(k2) from ${tableName2} 
where k10 = '2015-04-02' group by kk1)tt
             where kk1 = 'jj'"""
+    qt_in32 """select * from ${tableName1} where cast(k1 as char) in (1, -1, 
5, 0.1, 3.000) order by k1, k2, k3, k4"""
 }


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

Reply via email to