Skip to content

Commit

Permalink
[fix](nereids) don't build cte producer if the consumer is empty rela…
Browse files Browse the repository at this point in the history
…tion (apache#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              |
+----------------------------+
```
  • Loading branch information
starocean999 authored Jul 7, 2023
1 parent cad9e88 commit d39bca5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions regression-test/suites/nereids_syntax_p0/cte.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

}

0 comments on commit d39bca5

Please sign in to comment.