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

morrysnow 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 a9f9366736 [fix](nereids) the data type of compareExpr and listQuery 
should be the same when creating InSubquery (#18539)
a9f9366736 is described below

commit a9f93667360c8574171c9bb30590ca6ec83fc189
Author: starocean999 <[email protected]>
AuthorDate: Wed Apr 12 20:02:37 2023 +0800

    [fix](nereids) the data type of compareExpr and listQuery should be the 
same when creating InSubquery (#18539)
    
    Consider sql
    
    select table_B_alias.b from table_B_alias where table_B_alias.b in ( select 
a from table_A_alias );
    
    if table_B_alias.b is int and table_A_alias.a is bigint,
    we should cast(b as bigint) to make the data type the same as the 
InSubquery.
---
 .../rules/expression/rules/FunctionBinder.java     |  8 +++++--
 .../rules/rewrite/logical/CheckDataTypes.java      | 26 +++++++++++++++++-----
 .../data/nereids_syntax_p0/join_with_alias.out     |  3 +++
 .../nereids_syntax_p0/join_with_alias.groovy       |  4 ++++
 4 files changed, 34 insertions(+), 7 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FunctionBinder.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FunctionBinder.java
index a3c4a2ee52..0bd1a3ca06 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FunctionBinder.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FunctionBinder.java
@@ -229,8 +229,12 @@ public class FunctionBinder extends 
AbstractExpressionRewriteRule {
                 new EqualTo(newCompareExpr, ((ListQuery) 
newListQuery).getQueryPlan().getOutput().get(0));
         ComparisonPredicate afterTypeCoercion = (ComparisonPredicate) 
TypeCoercionUtils.processComparisonPredicate(
                 newCpAfterUnNestingSubquery, newCompareExpr, newListQuery);
-        if (!newCompareExpr.getDataType().isBigIntType() && 
newListQuery.getDataType().isBitmapType()) {
-            newCompareExpr = new Cast(newCompareExpr, BigIntType.INSTANCE);
+        if (newListQuery.getDataType().isBitmapType()) {
+            if (!newCompareExpr.getDataType().isBigIntType()) {
+                newCompareExpr = new Cast(newCompareExpr, BigIntType.INSTANCE);
+            }
+        } else {
+            newCompareExpr = afterTypeCoercion.left();
         }
         return new InSubquery(newCompareExpr, (ListQuery) 
afterTypeCoercion.right(),
             inSubquery.getCorrelateSlots(), ((ListQuery) 
afterTypeCoercion.right()).getTypeCoercionExpr(),
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/CheckDataTypes.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/CheckDataTypes.java
index 6baa461089..801dc784dc 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/CheckDataTypes.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/CheckDataTypes.java
@@ -23,6 +23,7 @@ import org.apache.doris.nereids.jobs.JobContext;
 import org.apache.doris.nereids.trees.expressions.Expression;
 import 
org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionVisitor;
 import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalJoin;
 import org.apache.doris.nereids.trees.plans.visitor.CustomRewriter;
 import org.apache.doris.nereids.types.ArrayType;
 import org.apache.doris.nereids.types.DataType;
@@ -43,14 +44,29 @@ public class CheckDataTypes implements CustomRewriter {
             MapType.class, StructType.class, JsonType.class, ArrayType.class);
 
     @Override
-    public Plan rewriteRoot(Plan plan, JobContext jobContext) {
-        return rewrite(plan);
+    public Plan rewriteRoot(Plan rootPlan, JobContext jobContext) {
+        checkPlan(rootPlan);
+        return rootPlan;
     }
 
-    private Plan rewrite(Plan plan) {
+    private void checkPlan(Plan plan) {
+        if (plan instanceof LogicalJoin) {
+            checkLogicalJoin((LogicalJoin) plan);
+        }
         plan.getExpressions().forEach(ExpressionChecker.INSTANCE::check);
-        plan.children().forEach(child -> rewrite(child));
-        return plan;
+        plan.children().forEach(child -> checkPlan(child));
+    }
+
+    private void checkLogicalJoin(LogicalJoin plan) {
+        plan.getHashJoinConjuncts().stream().forEach(expr -> {
+            DataType leftType = ((Expression) expr).child(0).getDataType();
+            DataType rightType = ((Expression) expr).child(1).getDataType();
+            if (!leftType.acceptsType(rightType)) {
+                throw new AnalysisException(
+                        String.format("type %s is not same as %s in hash join 
condition %s",
+                                leftType, rightType, ((Expression) 
expr).toSql()));
+            }
+        });
     }
 
     private static class ExpressionChecker extends 
DefaultExpressionVisitor<Expression, Void> {
diff --git a/regression-test/data/nereids_syntax_p0/join_with_alias.out 
b/regression-test/data/nereids_syntax_p0/join_with_alias.out
index a4cca003b9..3f4ebc452b 100644
--- a/regression-test/data/nereids_syntax_p0/join_with_alias.out
+++ b/regression-test/data/nereids_syntax_p0/join_with_alias.out
@@ -2,3 +2,6 @@
 -- !sql --
 1      1
 
+-- !sql2 --
+1
+
diff --git a/regression-test/suites/nereids_syntax_p0/join_with_alias.groovy 
b/regression-test/suites/nereids_syntax_p0/join_with_alias.groovy
index 408da58d21..5478d2866f 100644
--- a/regression-test/suites/nereids_syntax_p0/join_with_alias.groovy
+++ b/regression-test/suites/nereids_syntax_p0/join_with_alias.groovy
@@ -49,6 +49,10 @@ suite("join_with_alias") {
             select a1, a2 from ( select a as a1, a as a2 from table_A_alias 
join table_B_alias on a = b ) t;
         """
 
+    qt_sql2"""
+            select table_B_alias.b from table_B_alias where table_B_alias.b in 
( select a from table_A_alias );
+        """
+
     sql """ drop table if exists table_A_alias;"""
 
     sql """ drop table if exists table_B_alias;"""


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

Reply via email to