This is an automated email from the ASF dual-hosted git repository. morningman pushed a commit to branch branch-1.2-lts in repository https://gitbox.apache.org/repos/asf/doris.git
commit 2ab07e515343c2da0ac6a0818ea72acda8431b4a Author: Mingyu Chen <[email protected]> AuthorDate: Fri Jan 20 14:53:48 2023 +0800 [fix](planner) extract common factor rule should consider not only where predicate (#16110) This PR #14381 limit the `ExtractCommonFactorsRule` to handle only `WHERE` predicate, but the predicate in `ON` clause should also be considered. Such as: ``` CREATE TABLE `nation` ( `n_nationkey` int(11) NOT NULL, `n_name` varchar(25) NOT NULL, `n_regionkey` int(11) NOT NULL, `n_comment` varchar(152) NULL ) DUPLICATE KEY(`n_nationkey`) DISTRIBUTED BY HASH(`n_nationkey`) BUCKETS 1 PROPERTIES ( "replication_allocation" = "tag.location.default: 1" ); select * from nation n1 join nation n2 on (n1.n_name = 'FRANCE' and n2.n_name = 'GERMANY') or (n1.n_name = 'GERMANY' and n2.n_name = 'FRANCE') ``` There should be predicates: ``` PREDICATES: `n1`.`n_name` IN ('FRANCE', 'GERMANY') PREDICATES: `n2`.`n_name` IN ('FRANCE', 'GERMANY') ``` On each scan node. This PR fix this issue by removing the limit of `ExtractCommonFactorsRule` --- .../doris/rewrite/ExtractCommonFactorsRule.java | 3 +-- .../ExtractCommonFactorsRuleFunctionTest.java | 24 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/rewrite/ExtractCommonFactorsRule.java b/fe/fe-core/src/main/java/org/apache/doris/rewrite/ExtractCommonFactorsRule.java index 3c808c6100..74130d52af 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/rewrite/ExtractCommonFactorsRule.java +++ b/fe/fe-core/src/main/java/org/apache/doris/rewrite/ExtractCommonFactorsRule.java @@ -29,7 +29,6 @@ import org.apache.doris.analysis.TableName; import org.apache.doris.common.AnalysisException; import org.apache.doris.planner.PlanNode; import org.apache.doris.qe.ConnectContext; -import org.apache.doris.rewrite.ExprRewriter.ClauseType; import com.google.common.base.Preconditions; import com.google.common.collect.BoundType; @@ -169,7 +168,7 @@ public class ExtractCommonFactorsRule implements ExprRewriteRule { } // 3. find merge cross the clause - if (analyzer.getContext() != null && clauseType == ClauseType.WHERE_CLAUSE + if (analyzer.getContext() != null && analyzer.getContext().getSessionVariable().isExtractWideRangeExpr()) { Expr wideCommonExpr = findWideRangeExpr(clearExprs); if (wideCommonExpr != null && !(wideCommonExpr instanceof CompoundPredicate diff --git a/fe/fe-core/src/test/java/org/apache/doris/rewrite/ExtractCommonFactorsRuleFunctionTest.java b/fe/fe-core/src/test/java/org/apache/doris/rewrite/ExtractCommonFactorsRuleFunctionTest.java index 588df5c6a6..2f994be85b 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/rewrite/ExtractCommonFactorsRuleFunctionTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/rewrite/ExtractCommonFactorsRuleFunctionTest.java @@ -41,6 +41,7 @@ public class ExtractCommonFactorsRuleFunctionTest { private static final String TABLE_NAME_1 = "tb1"; private static final String TABLE_NAME_2 = "tb2"; private static final String TABLE_NAME_3 = "tb3"; + private static final String TABLE_NAME_4 = "nation"; @BeforeClass public static void beforeClass() throws Exception { @@ -61,6 +62,18 @@ public class ExtractCommonFactorsRuleFunctionTest { + " (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); + createTableSQL = "CREATE TABLE " + DB_NAME + "." + TABLE_NAME_4 + "(\n" + + " `n_nationkey` int(11) NOT NULL,\n" + + " `n_name` varchar(25) NOT NULL,\n" + + " `n_regionkey` int(11) NOT NULL,\n" + + " `n_comment` varchar(152) NULL\n" + + ")\n" + + "DUPLICATE KEY(`n_nationkey`)\n" + + "DISTRIBUTED BY HASH(`n_nationkey`) BUCKETS 1\n" + + "PROPERTIES (\n" + + "\"replication_allocation\" = \"tag.location.default: 1\"\n" + + ");"; + dorisAssert.withTable(createTableSQL); } @AfterClass @@ -311,4 +324,15 @@ public class ExtractCommonFactorsRuleFunctionTest { String sql = "select * from tb3 where k9 like '%4%';"; LOG.info("EXPLAIN:{}", dorisAssert.query(sql).explainQuery()); } + + @Test + public void testExtractCommonFactorsWithOnClause() throws Exception { + String sql = "select * from\n" + + "db1.nation n1 join db1.nation n2\n" + + "on (n1.n_name = 'FRANCE' and n2.n_name = 'GERMANY')\n" + + "or (n1.n_name = 'GERMANY' and n2.n_name = 'FRANCE')"; + String explainStr = dorisAssert.query(sql).explainQuery(); + Assert.assertTrue(explainStr.contains("PREDICATES: `n1`.`n_name` IN ('FRANCE', 'GERMANY')")); + Assert.assertTrue(explainStr.contains("PREDICATES: `n2`.`n_name` IN ('FRANCE', 'GERMANY')")); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
