From d39bca5ec76a1997c8a048f879eb10ad11a5de3f Mon Sep 17 00:00:00 2001 From: starocean999 <40539150+starocean999@users.noreply.github.com> Date: Fri, 7 Jul 2023 18:12:28 +0800 Subject: [PATCH] [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 dab88e686e4432..11700ecb7785c3 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.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 @@ private LogicalPlan rewrite(LogicalPlan p, CascadesContext cascadesContext) { } 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 d2b6eefa0dbd33..15a7afc9556edb 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" + } + }