diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PullUpCteAnchor.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PullUpCteAnchor.java index c6461be271..4bfb36db81 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PullUpCteAnchor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PullUpCteAnchor.java @@ -18,12 +18,16 @@ package org.apache.doris.nereids.rules.rewrite; import org.apache.doris.nereids.jobs.JobContext; +import org.apache.doris.nereids.trees.expressions.SubqueryExpr; import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalApply; 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.LogicalPlan; import org.apache.doris.nereids.trees.plans.visitor.CustomRewriter; import org.apache.doris.nereids.trees.plans.visitor.DefaultPlanRewriter; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import java.util.List; @@ -42,7 +46,7 @@ public class PullUpCteAnchor extends DefaultPlanRewriter> producers) { + public Plan rewriteRoot(Plan plan, List> producers) { Plan root = plan.accept(this, producers); for (LogicalCTEProducer producer : producers) { root = new LogicalCTEAnchor<>(producer.getCteId(), producer, root); @@ -71,4 +75,30 @@ public class PullUpCteAnchor extends DefaultPlanRewriter apply, + List> producers) { + SubqueryExpr subqueryExpr = apply.getSubqueryExpr(); + PullUpCteAnchor pullSubqueryExpr = new PullUpCteAnchor(); + List> subqueryExprProducers = Lists.newArrayList(); + Plan newPlanInExpr = pullSubqueryExpr.rewriteRoot(subqueryExpr.getQueryPlan(), subqueryExprProducers); + while (newPlanInExpr instanceof LogicalCTEAnchor) { + newPlanInExpr = ((LogicalCTEAnchor) newPlanInExpr).right(); + } + SubqueryExpr newSubqueryExpr = subqueryExpr.withSubquery((LogicalPlan) newPlanInExpr); + + Plan newApplyLeft = apply.left().accept(this, producers); + + Plan applyRight = apply.right(); + PullUpCteAnchor pullApplyRight = new PullUpCteAnchor(); + List> childProducers = Lists.newArrayList(); + Plan newApplyRight = pullApplyRight.rewriteRoot(applyRight, childProducers); + while (newApplyRight instanceof LogicalCTEAnchor) { + newApplyRight = ((LogicalCTEAnchor) newApplyRight).right(); + } + producers.addAll(childProducers); + return apply.withSubqueryExprAndChildren(newSubqueryExpr, + ImmutableList.of(newApplyLeft, newApplyRight)); + } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/stats/ColumnStatsAdjustVisitor.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/stats/ColumnStatsAdjustVisitor.java index ff4290557d..eaf2ce3734 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/stats/ColumnStatsAdjustVisitor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/stats/ColumnStatsAdjustVisitor.java @@ -71,7 +71,7 @@ public class ColumnStatsAdjustVisitor extends ExpressionVisitor withSubqueryExprAndChildren(SubqueryExpr subqueryExpr, List children) { + return new LogicalApply<>(correlationSlot, subqueryExpr, correlationFilter, + markJoinSlotReference, needAddSubOutputToProjects, inProject, children.get(0), children.get(1)); + } + @Override - public LogicalBinary withChildren(List children) { + public LogicalApply withChildren(List children) { Preconditions.checkArgument(children.size() == 2); return new LogicalApply<>(correlationSlot, subqueryExpr, correlationFilter, markJoinSlotReference, needAddSubOutputToProjects, inProject,