This is an automated email from the ASF dual-hosted git repository. kxiao pushed a commit to branch branch-2.0 in repository https://gitbox.apache.org/repos/asf/doris.git
commit aa4ad7b8ff2aedf8ccc220ec8c1bd131046ff100 Author: starocean999 <40539150+starocean...@users.noreply.github.com> AuthorDate: Fri Jul 7 18:12:28 2023 +0800 [fix](nereids) don't build cte producer if the consumer is empty relation (#21317) explain WITH cte_0 AS ( SELECT 1 AS a ) SELECT * from cte_0 t1 join cte_0 t2 on true WHERE false; before: ``` +----------------------------+ | Explain String | +----------------------------+ | PLAN FRAGMENT 0 | | OUTPUT EXPRS: | | a[#1] | | a[#2] | | PARTITION: UNPARTITIONED | | | | VRESULT SINK | | | | 1:VEMPTYSET | | | | PLAN FRAGMENT 1 | | OUTPUT EXPRS: | | a[#0] | | PARTITION: UNPARTITIONED | | | | MultiCastDataSinks | | | | 0:VUNION | | constant exprs: | | 1 | +----------------------------+ ``` after: ``` +----------------------------+ | Explain String | +----------------------------+ | PLAN FRAGMENT 0 | | OUTPUT EXPRS: | | a[#0] | | a[#1] | | PARTITION: UNPARTITIONED | | | | VRESULT SINK | | | | 0:VEMPTYSET | +----------------------------+ ``` --- .../rewrite/BuildCTEAnchorAndCTEProducer.java | 23 ++++++++++++---------- .../suites/nereids_syntax_p0/cte.groovy | 5 +++++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/BuildCTEAnchorAndCTEProducer.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/BuildCTEAnchorAndCTEProducer.java index dab88e686e..11700ecb77 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/BuildCTEAnchorAndCTEProducer.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/BuildCTEAnchorAndCTEProducer.java @@ -24,6 +24,7 @@ import org.apache.doris.nereids.trees.expressions.CTEId; import org.apache.doris.nereids.trees.plans.logical.LogicalCTE; import org.apache.doris.nereids.trees.plans.logical.LogicalCTEAnchor; import org.apache.doris.nereids.trees.plans.logical.LogicalCTEProducer; +import org.apache.doris.nereids.trees.plans.logical.LogicalEmptyRelation; import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; import org.apache.doris.nereids.trees.plans.logical.LogicalSubQueryAlias; import org.apache.doris.qe.ConnectContext; @@ -47,17 +48,19 @@ public class BuildCTEAnchorAndCTEProducer extends OneRewriteRuleFactory { } LogicalCTE logicalCTE = (LogicalCTE) p; LogicalPlan child = (LogicalPlan) logicalCTE.child(); - for (int i = logicalCTE.getAliasQueries().size() - 1; i >= 0; i--) { - LogicalSubQueryAlias s = (LogicalSubQueryAlias) logicalCTE.getAliasQueries().get(i); - CTEId id = logicalCTE.findCTEId(s.getAlias()); - if (cascadesContext.cteReferencedCount(id) - <= ConnectContext.get().getSessionVariable().inlineCTEReferencedThreshold - || !ConnectContext.get().getSessionVariable().getEnablePipelineEngine()) { - continue; + if (!(child instanceof LogicalEmptyRelation)) { + for (int i = logicalCTE.getAliasQueries().size() - 1; i >= 0; i--) { + LogicalSubQueryAlias s = (LogicalSubQueryAlias) logicalCTE.getAliasQueries().get(i); + CTEId id = logicalCTE.findCTEId(s.getAlias()); + if (cascadesContext.cteReferencedCount(id) + <= ConnectContext.get().getSessionVariable().inlineCTEReferencedThreshold + || !ConnectContext.get().getSessionVariable().getEnablePipelineEngine()) { + continue; + } + LogicalCTEProducer logicalCTEProducer = new LogicalCTEProducer( + rewrite((LogicalPlan) s.child(), cascadesContext), id); + child = new LogicalCTEAnchor(logicalCTEProducer, child, id); } - LogicalCTEProducer logicalCTEProducer = new LogicalCTEProducer( - rewrite((LogicalPlan) s.child(), cascadesContext), id); - child = new LogicalCTEAnchor(logicalCTEProducer, child, id); } return child; } diff --git a/regression-test/suites/nereids_syntax_p0/cte.groovy b/regression-test/suites/nereids_syntax_p0/cte.groovy index d2b6eefa0d..15a7afc955 100644 --- a/regression-test/suites/nereids_syntax_p0/cte.groovy +++ b/regression-test/suites/nereids_syntax_p0/cte.groovy @@ -301,5 +301,10 @@ suite("cte") { exception = "[cte1] cannot be used more than once" } + explain { + sql("WITH cte_0 AS ( SELECT 1 AS a ) SELECT * from cte_0 t1 join cte_0 t2 on true WHERE false;") + notContains "MultiCastDataSinks" + } + } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org