This is an automated email from the ASF dual-hosted git repository. morningman pushed a commit to branch dev-1.0.1 in repository https://gitbox.apache.org/repos/asf/doris.git
commit 70354f2f4d4edce7a0d624bd0eb4b90dfbecdf44 Author: morrySnow <101034200+morrys...@users.noreply.github.com> AuthorDate: Sat Jul 2 22:41:04 2022 +0800 [fix](planner)infer predicate generate infered predicate using wrong information from another scope (#10519) This PR fix a bug in predicate inference. The original predicate inference compare two slot without SlotId. This will arise an error when a query has SetOperand and more than one SetOperand's child use same table alias. e.g. ``` select * from tb1 inner join tb2 on tb1.k1 = tb2.k1 union select * from tb1 inner join tb2 on tb1.k2 = tb2.k2 where tb1.k1 = 3; ``` in this case, we infer a predicate `tb2.k1 = 3` on table 'tbl2' of SetOperand's second child by mistake. --- .../java/org/apache/doris/analysis/SlotRef.java | 2 +- .../org/apache/doris/rewrite/InferFiltersRule.java | 90 ++++++++++------------ .../apache/doris/rewrite/InferFiltersRuleTest.java | 70 +++++++++++++---- 3 files changed, 99 insertions(+), 63 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/SlotRef.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/SlotRef.java index f2f87e0586..09c6138a6d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/SlotRef.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/SlotRef.java @@ -303,7 +303,7 @@ public class SlotRef extends Expr { if ((col == null) != (other.col == null)) { return false; } - if (col != null && !col.toLowerCase().equals(other.col.toLowerCase())) { + if (col != null && !col.equalsIgnoreCase(other.col)) { return false; } return true; diff --git a/fe/fe-core/src/main/java/org/apache/doris/rewrite/InferFiltersRule.java b/fe/fe-core/src/main/java/org/apache/doris/rewrite/InferFiltersRule.java index 47f8153485..c83d3b94c8 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/rewrite/InferFiltersRule.java +++ b/fe/fe-core/src/main/java/org/apache/doris/rewrite/InferFiltersRule.java @@ -44,6 +44,8 @@ import java.util.Map; /** * The function of this rule is to derive a new predicate based on the current predicate. + * + * <pre> * eg. * t1.id = t2.id and t2.id = t3.id and t3.id = 100; * --> @@ -52,8 +54,9 @@ import java.util.Map; * 1. Register a new rule InferFiltersRule and add it to GlobalState. * 2. Traverse Conjunct to construct on/where equivalence connection, numerical connection and isNullPredicate. * 3. Use Warshall to infer all equivalence connections. - * details:https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm + * details: <a href="url">https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm</a> * 4. Construct additional numerical connections and isNullPredicate. + * </pre> */ public class InferFiltersRule implements ExprRewriteRule { private final static Logger LOG = LogManager.getLogger(InferFiltersRule.class); @@ -127,10 +130,10 @@ public class InferFiltersRule implements ExprRewriteRule { if (!newExprWithState.isEmpty()) { Expr rewriteExpr = expr; - for (int index = 0; index < newExprWithState.size(); index++) { - if (newExprWithState.get(index).second) { - rewriteExpr = new CompoundPredicate(CompoundPredicate.Operator.AND, - rewriteExpr, newExprWithState.get(index).first); + for (Pair<Expr, Boolean> exprBooleanPair : newExprWithState) { + if (exprBooleanPair.second) { + rewriteExpr = new CompoundPredicate(CompoundPredicate.Operator.AND, rewriteExpr, + exprBooleanPair.first); } } return rewriteExpr; @@ -169,9 +172,9 @@ public class InferFiltersRule implements ExprRewriteRule { } if (conjunct instanceof BinaryPredicate - && conjunct.getChild(0) != null - && conjunct.getChild(1) != null) { - if (conjunct.getChild(0).unwrapSlotRef() instanceof SlotRef + && conjunct.getChild(0) != null + && conjunct.getChild(1) != null) { + if (conjunct.getChild(0).unwrapSlotRef() != null && conjunct.getChild(1) instanceof LiteralExpr) { Pair<Expr, Expr> pair = new Pair<>(conjunct.getChild(0).unwrapSlotRef(), conjunct.getChild(1)); if (!slotToLiteralDeDuplication.contains(pair)) { @@ -184,8 +187,8 @@ public class InferFiltersRule implements ExprRewriteRule { analyzer.registerGlobalSlotToLiteralDeDuplication(pair); } } else if (((BinaryPredicate) conjunct).getOp().isEquivalence() - && conjunct.getChild(0).unwrapSlotRef() instanceof SlotRef - && conjunct.getChild(1).unwrapSlotRef() instanceof SlotRef) { + && conjunct.getChild(0).unwrapSlotRef() != null + && conjunct.getChild(1).unwrapSlotRef() != null) { Pair<Expr, Expr> pair = new Pair<>(conjunct.getChild(0).unwrapSlotRef(), conjunct.getChild(1).unwrapSlotRef()); Pair<Expr, Expr> eqPair = new Pair<>(conjunct.getChild(1).unwrapSlotRef(), @@ -202,7 +205,7 @@ public class InferFiltersRule implements ExprRewriteRule { } } else if (conjunct instanceof IsNullPredicate && conjunct.getChild(0) != null - && conjunct.getChild(0).unwrapSlotRef() instanceof SlotRef) { + && conjunct.getChild(0).unwrapSlotRef() != null) { if (!isNullDeDuplication.contains(conjunct.getChild(0).unwrapSlotRef()) && ((IsNullPredicate) conjunct).isNotNull()) { isNullDeDuplication.add(conjunct.getChild(0).unwrapSlotRef()); @@ -214,7 +217,7 @@ public class InferFiltersRule implements ExprRewriteRule { } } else if (conjunct instanceof InPredicate && conjunct.getChild(0) != null - && conjunct.getChild(0).unwrapSlotRef() instanceof SlotRef) { + && conjunct.getChild(0).unwrapSlotRef() != null) { if (!inDeDuplication.contains(conjunct.getChild(0).unwrapSlotRef())) { inDeDuplication.add(conjunct.getChild(0).unwrapSlotRef()); inExpr.add(conjunct); @@ -234,10 +237,10 @@ public class InferFiltersRule implements ExprRewriteRule { * old expr:t1.id = t2.id and t2.id = t3.id and t3.id = t4.id * new expr:t1.id = t2.id and t2.id = t3.id and t3.id = t4.id and t1.id = t3.id and t1.id = t4.id and t2.id = t4.id * - * @param slotEqSlotExpr - * @param slotEqSlotDeDuplication + * @param slotEqSlotExpr slot to slot exprs + * @param slotEqSlotDeDuplication set pairs in slot = slot exprs * @param exprToWarshallArraySubscript: A Map the key is Expr, the value is int - * @param warshallArraySubscriptToExpr: A Map the key is int, the value is exper + * @param warshallArraySubscriptToExpr: A Map the key is int, the value is expr */ private void genNewSlotEqSlotPredicate(List<Expr> slotEqSlotExpr, Set<Pair<Expr, Expr>> slotEqSlotDeDuplication, @@ -267,9 +270,9 @@ public class InferFiltersRule implements ExprRewriteRule { * * @param warshall: Two-dimensional array * @param arrayMaxSize: slotEqSlotExpr.size() * 2 - * @param slotEqSlotExpr - * @param exprToWarshallArraySubscript - * @param warshallArraySubscriptToExpr + * @param slotEqSlotExpr slot to slot exprs + * @param exprToWarshallArraySubscript expr to offset in Warshall array + * @param warshallArraySubscriptToExpr offset in Warshall array to expr * @return needGenWarshallArray. True:needGen; False:don't needGen */ private boolean initWarshallArray(int warshall[][], @@ -280,8 +283,8 @@ public class InferFiltersRule implements ExprRewriteRule { boolean needGenWarshallArray = false; int index = 0; for (Expr slotEqSlot : slotEqSlotExpr) { - int row = 0; - int column = 0; + int row; + int column; if (!exprToWarshallArraySubscript.containsKey(slotEqSlot.getChild(0))) { exprToWarshallArraySubscript.put(slotEqSlot.getChild(0), index); warshallArraySubscriptToExpr.put(index, slotEqSlot.getChild(0)); @@ -302,7 +305,7 @@ public class InferFiltersRule implements ExprRewriteRule { if (row >= arrayMaxSize || column >= arrayMaxSize) { - LOG.debug("Error row or column", row, column, arrayMaxSize); + LOG.debug("Error row {} or column {}, but max size is {}.", row, column, arrayMaxSize); needGenWarshallArray = false; break; } else { @@ -394,19 +397,18 @@ public class InferFiltersRule implements ExprRewriteRule { Analyzer analyzer, ExprRewriter.ClauseType clauseType) { SlotRef checkSlot = slotToLiteral.getChild(0).unwrapSlotRef(); - if (checkSlot instanceof SlotRef) { + if (checkSlot != null) { for (Expr conjunct : slotEqSlotExpr) { SlotRef leftSlot = conjunct.getChild(0).unwrapSlotRef(); SlotRef rightSlot = conjunct.getChild(1).unwrapSlotRef(); - if (leftSlot instanceof SlotRef - && rightSlot instanceof SlotRef) { - if (checkSlot.notCheckDescIdEquals(leftSlot)) { + if (leftSlot != null && rightSlot != null) { + if (checkSlot.equals(leftSlot)) { addNewBinaryPredicate(genNewBinaryPredicate(slotToLiteral, rightSlot), slotToLiteralDeDuplication, newExprWithState, isNeedInfer(rightSlot, leftSlot, analyzer, clauseType), analyzer, clauseType); - } else if (checkSlot.notCheckDescIdEquals(rightSlot)) { + } else if (checkSlot.equals(rightSlot)) { addNewBinaryPredicate(genNewBinaryPredicate(slotToLiteral, leftSlot), slotToLiteralDeDuplication, newExprWithState, isNeedInfer(leftSlot, rightSlot, analyzer, clauseType), @@ -429,17 +431,15 @@ public class InferFiltersRule implements ExprRewriteRule { boolean ret = false; TupleId newTid = newSlot.getDesc().getParent().getRef().getId(); TupleId checkTid = checkSlot.getDesc().getParent().getRef().getId(); - boolean needChange = false; Pair<TupleId, TupleId> tids = new Pair<>(newTid, checkTid); if (analyzer.isContainTupleIds(tids)) { JoinOperator joinOperator = analyzer.getAnyTwoTablesJoinOp(tids); - ret = checkNeedInfer(joinOperator, needChange, clauseType); + ret = checkNeedInfer(joinOperator, false, clauseType); } else { Pair<TupleId, TupleId> changeTids = new Pair<>(checkTid, newTid); if (analyzer.isContainTupleIds(changeTids)) { - needChange = true; JoinOperator joinOperator = analyzer.getAnyTwoTablesJoinOp(changeTids); - ret = checkNeedInfer(joinOperator, needChange, clauseType); + ret = checkNeedInfer(joinOperator, true, clauseType); } } return ret; @@ -475,8 +475,7 @@ public class InferFiltersRule implements ExprRewriteRule { private Expr genNewBinaryPredicate(Expr oldExpr, Expr newSlot) { if (oldExpr instanceof BinaryPredicate) { BinaryPredicate oldBP = (BinaryPredicate) oldExpr; - BinaryPredicate newBP = new BinaryPredicate(oldBP.getOp(), newSlot, oldBP.getChild(1)); - return newBP; + return new BinaryPredicate(oldBP.getOp(), newSlot, oldBP.getChild(1)); } return oldExpr; } @@ -535,17 +534,16 @@ public class InferFiltersRule implements ExprRewriteRule { if (expr instanceof IsNullPredicate) { IsNullPredicate isNullPredicate = (IsNullPredicate)expr; SlotRef checkSlot = isNullPredicate.getChild(0).unwrapSlotRef(); - if (checkSlot instanceof SlotRef) { + if (checkSlot != null) { for (Expr conjunct : slotEqSlotExpr) { SlotRef leftSlot = conjunct.getChild(0).unwrapSlotRef(); SlotRef rightSlot = conjunct.getChild(1).unwrapSlotRef(); - if (leftSlot instanceof SlotRef - && rightSlot instanceof SlotRef) { - if (checkSlot.notCheckDescIdEquals(leftSlot) && isNullPredicate.isNotNull()) { + if (leftSlot != null && rightSlot != null) { + if (checkSlot.equals(leftSlot) && isNullPredicate.isNotNull()) { addNewIsNotNullPredicate(genNewIsNotNullPredicate(isNullPredicate, rightSlot), isNullDeDuplication, newExprWithState, analyzer, clauseType); - } else if (checkSlot.notCheckDescIdEquals(rightSlot)) { + } else if (checkSlot.equals(rightSlot)) { addNewIsNotNullPredicate(genNewIsNotNullPredicate(isNullPredicate, leftSlot), isNullDeDuplication, newExprWithState, analyzer, clauseType); } @@ -560,11 +558,7 @@ public class InferFiltersRule implements ExprRewriteRule { * @return new IsNullPredicate. */ private Expr genNewIsNotNullPredicate(IsNullPredicate oldExpr, Expr newSlot) { - if (oldExpr instanceof IsNullPredicate) { - IsNullPredicate newExpr = new IsNullPredicate(newSlot, oldExpr.isNotNull()); - return newExpr; - } - return oldExpr; + return oldExpr != null ? new IsNullPredicate(newSlot, oldExpr.isNotNull()) : null; } /** @@ -616,19 +610,18 @@ public class InferFiltersRule implements ExprRewriteRule { if (inExpr instanceof InPredicate) { InPredicate inpredicate = (InPredicate) inExpr; SlotRef checkSlot = inpredicate.getChild(0).unwrapSlotRef(); - if (checkSlot instanceof SlotRef) { + if (checkSlot != null) { for (Expr conjunct : slotEqSlotExpr) { SlotRef leftSlot = conjunct.getChild(0).unwrapSlotRef(); SlotRef rightSlot = conjunct.getChild(1).unwrapSlotRef(); - if (leftSlot instanceof SlotRef - && rightSlot instanceof SlotRef) { - if (checkSlot.notCheckDescIdEquals(leftSlot)) { + if (leftSlot != null && rightSlot != null) { + if (checkSlot.equals(leftSlot)) { addNewInPredicate(genNewInPredicate(inpredicate, rightSlot), inDeDuplication, newExprWithState, isNeedInfer(rightSlot, leftSlot, analyzer, clauseType), analyzer, clauseType); - } else if (checkSlot.notCheckDescIdEquals(rightSlot)) { + } else if (checkSlot.equals(rightSlot)) { addNewInPredicate(genNewInPredicate(inpredicate, leftSlot), inDeDuplication, newExprWithState, isNeedInfer(leftSlot, rightSlot, analyzer, clauseType), @@ -647,8 +640,7 @@ public class InferFiltersRule implements ExprRewriteRule { private Expr genNewInPredicate(Expr oldExpr, Expr newSlot) { if (oldExpr instanceof InPredicate) { InPredicate oldBP = (InPredicate) oldExpr; - InPredicate newBP = new InPredicate(newSlot, oldBP.getListChildren(), oldBP.isNotIn()); - return newBP; + return new InPredicate(newSlot, oldBP.getListChildren(), oldBP.isNotIn()); } return oldExpr; } diff --git a/fe/fe-core/src/test/java/org/apache/doris/rewrite/InferFiltersRuleTest.java b/fe/fe-core/src/test/java/org/apache/doris/rewrite/InferFiltersRuleTest.java index 1ceb7f63d9..c5c5be25a6 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/rewrite/InferFiltersRuleTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/rewrite/InferFiltersRuleTest.java @@ -33,8 +33,8 @@ import org.junit.Test; import java.util.UUID; public class InferFiltersRuleTest { - private static String baseDir = "fe"; - private static String runningDir = baseDir + "/mocked/InferFiltersRuleTest/" + private static final String baseDir = "fe"; + private static final String runningDir = baseDir + "/mocked/InferFiltersRuleTest/" + UUID.randomUUID() + "/"; private static DorisAssert dorisAssert; private static final String DB_NAME = "db1"; @@ -58,7 +58,8 @@ public class InferFiltersRuleTest { + "distributed by hash(k1) buckets 3 properties('replication_num' = '1');"; dorisAssert.withTable(createTableSQL); createTableSQL = "create table " + DB_NAME + "." + TABLE_NAME_3 - + " (k1 tinyint, k2 smallint, k3 int, k4 bigint, k5 largeint, k6 date, k7 datetime, k8 float, k9 double) " + + " (k1 tinyint, k2 smallint, k3 int, k4 bigint," + + " k5 largeint, k6 date, k7 datetime, k8 float, k9 double) " + "distributed by hash(k1) buckets 3 properties('replication_num' = '1');"; dorisAssert.withTable(createTableSQL); } @@ -104,7 +105,8 @@ public class InferFiltersRuleTest { SessionVariable sessionVariable = dorisAssert.getSessionVariable(); sessionVariable.setEnableInferPredicate(true); Assert.assertTrue(sessionVariable.isEnableInferPredicate()); - String query = "select * from tb1 inner join tb2 inner join tb3 where tb1.k1 = tb2.k1 and tb2.k1 = tb3.k1 and tb3.k1 = 1"; + String query = "select * from tb1 inner join tb2 inner join tb3" + + " where tb1.k1 = tb2.k1 and tb2.k1 = tb3.k1 and tb3.k1 = 1"; String planString = dorisAssert.query(query).explainQuery(); Assert.assertTrue(planString.contains("`tb2`.`k1` = 1")); Assert.assertTrue(planString.contains("`tb1`.`k1` = 1")); @@ -154,7 +156,8 @@ public class InferFiltersRuleTest { SessionVariable sessionVariable = dorisAssert.getSessionVariable(); sessionVariable.setEnableInferPredicate(true); Assert.assertTrue(sessionVariable.isEnableInferPredicate()); - String query = "select * from tb1 inner join tb2 on tb1.k1 = tb2.k1 right outer join tb3 on tb2.k1 = tb3.k1 and tb2.k1 = 1"; + String query = "select * from tb1 inner join tb2 on tb1.k1 = tb2.k1" + + " right outer join tb3 on tb2.k1 = tb3.k1 and tb2.k1 = 1"; String planString = dorisAssert.query(query).explainQuery(); Assert.assertTrue(planString.contains("`tb1`.`k1` = 1")); Assert.assertFalse(planString.contains("`tb3`.`k1` = 1")); @@ -165,7 +168,8 @@ public class InferFiltersRuleTest { SessionVariable sessionVariable = dorisAssert.getSessionVariable(); sessionVariable.setEnableInferPredicate(true); Assert.assertTrue(sessionVariable.isEnableInferPredicate()); - String query = "select * from tb1 inner join tb2 on tb1.k1 = tb2.k1 right outer join tb3 on tb2.k1 = tb3.k1 and tb3.k1 = 1"; + String query = "select * from tb1 inner join tb2 on tb1.k1 = tb2.k1" + + " right outer join tb3 on tb2.k1 = tb3.k1 and tb3.k1 = 1"; String planString = dorisAssert.query(query).explainQuery(); Assert.assertTrue(planString.contains("`tb2`.`k1` = 1")); Assert.assertTrue(planString.contains("`tb1`.`k1` = 1")); @@ -176,7 +180,8 @@ public class InferFiltersRuleTest { SessionVariable sessionVariable = dorisAssert.getSessionVariable(); sessionVariable.setEnableInferPredicate(true); Assert.assertTrue(sessionVariable.isEnableInferPredicate()); - String query = "select * from tb1 inner join tb2 on tb1.k1 = tb2.k1 right outer join tb3 on tb2.k1 = tb3.k1 where tb1.k1 = tb2.k1 and tb2.k1 = tb3.k1 and tb2.k1 = 1"; + String query = "select * from tb1 inner join tb2 on tb1.k1 = tb2.k1 right outer join tb3 on tb2.k1 = tb3.k1" + + " where tb1.k1 = tb2.k1 and tb2.k1 = tb3.k1 and tb2.k1 = 1"; String planString = dorisAssert.query(query).explainQuery(); Assert.assertTrue(planString.contains("`tb1`.`k1` = 1")); Assert.assertTrue(planString.contains("`tb3`.`k1` = 1")); @@ -219,7 +224,8 @@ public class InferFiltersRuleTest { SessionVariable sessionVariable = dorisAssert.getSessionVariable(); sessionVariable.setEnableInferPredicate(true); Assert.assertTrue(sessionVariable.isEnableInferPredicate()); - String query = "select * from tb1 inner join tb2 inner join tb3 where tb1.k1 = tb2.k1 and tb2.k1 = tb3.k1 and tb3.k1 = 1"; + String query = "select * from tb1 inner join tb2 inner join tb3" + + " where tb1.k1 = tb2.k1 and tb2.k1 = tb3.k1 and tb3.k1 = 1"; String planString = dorisAssert.query(query).explainQuery(); Assert.assertTrue(planString.contains("`tb2`.`k1` = 1")); Assert.assertTrue(planString.contains("`tb1`.`k1` = 1")); @@ -230,7 +236,8 @@ public class InferFiltersRuleTest { SessionVariable sessionVariable = dorisAssert.getSessionVariable(); sessionVariable.setEnableInferPredicate(true); Assert.assertTrue(sessionVariable.isEnableInferPredicate()); - String query = "select * from tb1 inner join tb2 left outer join tb3 on tb3.k1 = tb2.k1 where tb1.k1 = tb2.k1 and tb2.k1 = tb3.k1 and tb3.k1 = 1"; + String query = "select * from tb1 inner join tb2 left outer join tb3 on tb3.k1 = tb2.k1" + + " where tb1.k1 = tb2.k1 and tb2.k1 = tb3.k1 and tb3.k1 = 1"; String planString = dorisAssert.query(query).explainQuery(); Assert.assertTrue(planString.contains("`tb2`.`k1` = 1")); Assert.assertTrue(planString.contains("`tb1`.`k1` = 1")); @@ -241,7 +248,8 @@ public class InferFiltersRuleTest { SessionVariable sessionVariable = dorisAssert.getSessionVariable(); sessionVariable.setEnableInferPredicate(true); Assert.assertTrue(sessionVariable.isEnableInferPredicate()); - String query = "select * from tb1 inner join tb2 left outer join tb3 on tb3.k1 = tb2.k1 where tb1.k1 = tb2.k1 and tb2.k1 = tb3.k1 and tb2.k1 = 1"; + String query = "select * from tb1 inner join tb2 left outer join tb3 on tb3.k1 = tb2.k1" + + " where tb1.k1 = tb2.k1 and tb2.k1 = tb3.k1 and tb2.k1 = 1"; String planString = dorisAssert.query(query).explainQuery(); Assert.assertTrue(planString.contains("`tb1`.`k1` = 1")); Assert.assertFalse(planString.contains("`tb3`.`k1` = 1")); @@ -252,7 +260,8 @@ public class InferFiltersRuleTest { SessionVariable sessionVariable = dorisAssert.getSessionVariable(); sessionVariable.setEnableInferPredicate(true); Assert.assertTrue(sessionVariable.isEnableInferPredicate()); - String query = "select * from tb1 inner join tb2 on tb1.k1 = tb2.k1 right outer join tb3 on tb2.k1 = tb3.k1 where tb1.k1 = tb2.k1 and tb2.k1 = tb3.k1 and tb2.k1 = 1"; + String query = "select * from tb1 inner join tb2 on tb1.k1 = tb2.k1 right outer join tb3 on tb2.k1 = tb3.k1" + + " where tb1.k1 = tb2.k1 and tb2.k1 = tb3.k1 and tb2.k1 = 1"; String planString = dorisAssert.query(query).explainQuery(); Assert.assertTrue(planString.contains("`tb1`.`k1` = 1")); Assert.assertTrue(planString.contains("`tb3`.`k1` = 1")); @@ -263,7 +272,8 @@ public class InferFiltersRuleTest { SessionVariable sessionVariable = dorisAssert.getSessionVariable(); sessionVariable.setEnableInferPredicate(true); Assert.assertTrue(sessionVariable.isEnableInferPredicate()); - String query = "select * from tb1 inner join tb2 on tb1.k1 = tb2.k1 right outer join tb3 on tb2.k1 = tb3.k1 where tb1.k1 = tb2.k1 and tb2.k1 = tb3.k1 and tb3.k1 = 1"; + String query = "select * from tb1 inner join tb2 on tb1.k1 = tb2.k1 right outer join tb3 on tb2.k1 = tb3.k1" + + " where tb1.k1 = tb2.k1 and tb2.k1 = tb3.k1 and tb3.k1 = 1"; String planString = dorisAssert.query(query).explainQuery(); Assert.assertFalse(planString.contains("`tb2`.`k1` = 1")); Assert.assertFalse(planString.contains("`tb1`.`k1` = 1")); @@ -274,7 +284,8 @@ public class InferFiltersRuleTest { SessionVariable sessionVariable = dorisAssert.getSessionVariable(); sessionVariable.setEnableInferPredicate(true); Assert.assertTrue(sessionVariable.isEnableInferPredicate()); - String query = "select * from tb1 inner join tb2 inner join tb3 where tb1.k1 = tb3.k1 and tb2.k1 = tb3.k1 and tb1.k1 is not null"; + String query = "select * from tb1 inner join tb2 inner join tb3" + + " where tb1.k1 = tb3.k1 and tb2.k1 = tb3.k1 and tb1.k1 is not null"; String planString = dorisAssert.query(query).explainQuery(); Assert.assertTrue(planString.contains("`tb3`.`k1` IS NOT NULL")); Assert.assertTrue(planString.contains("`tb2`.`k1` IS NOT NULL")); @@ -340,4 +351,37 @@ public class InferFiltersRuleTest { String planString = dorisAssert.query(query).explainQuery(); Assert.assertTrue(planString.contains("`tb2`.`k1` = 1")); } + + @Test + public void testSameAliasWithSlotEqualToLiteralInDifferentUnionChildren() throws Exception { + SessionVariable sessionVariable = dorisAssert.getSessionVariable(); + sessionVariable.setEnableInferPredicate(true); + Assert.assertTrue(sessionVariable.isEnableInferPredicate()); + String query = "select * from tb1 inner join tb2 on tb1.k1 = tb2.k1" + + " union select * from tb1 inner join tb2 on tb1.k2 = tb2.k2 where tb1.k1 = 3"; + String planString = dorisAssert.query(query).explainQuery(); + Assert.assertFalse(planString.contains("`tb2`.`k1` = 3")); + } + + @Test + public void testSameAliasWithSlotInPredicateInDifferentUnionChildren() throws Exception { + SessionVariable sessionVariable = dorisAssert.getSessionVariable(); + sessionVariable.setEnableInferPredicate(true); + Assert.assertTrue(sessionVariable.isEnableInferPredicate()); + String query = "select * from tb1 inner join tb2 on tb1.k1 = tb2.k1" + + " union select * from tb1 inner join tb2 on tb1.k2 = tb2.k2 where tb1.k1 in (3, 4, 5)"; + String planString = dorisAssert.query(query).explainQuery(); + Assert.assertFalse(planString.contains("`tb2`.`k1` IN (3, 4, 5)")); + } + + @Test + public void testSameAliasWithSlotIsNullInDifferentUnionChildren() throws Exception { + SessionVariable sessionVariable = dorisAssert.getSessionVariable(); + sessionVariable.setEnableInferPredicate(true); + Assert.assertTrue(sessionVariable.isEnableInferPredicate()); + String query = "select * from tb1 inner join tb2 on tb1.k1 = tb2.k1" + + " union select * from tb1 inner join tb2 on tb1.k2 = tb2.k2 where tb1.k1 is not null"; + String planString = dorisAssert.query(query).explainQuery(); + Assert.assertFalse(planString.contains("`tb2`.`k1` IS NOT NULL")); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org