[fix](Nereids) pull up cte anchor should also pull up cte in apply (#28214)

This commit is contained in:
morrySnow
2023-12-12 16:56:04 +08:00
committed by GitHub
parent 45b2dbab6a
commit 4e50b2791a
8 changed files with 61 additions and 3 deletions

View File

@ -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<List<LogicalCTEProducer
return rewriteRoot(plan, producers);
}
private Plan rewriteRoot(Plan plan, List<LogicalCTEProducer<Plan>> producers) {
public Plan rewriteRoot(Plan plan, List<LogicalCTEProducer<Plan>> producers) {
Plan root = plan.accept(this, producers);
for (LogicalCTEProducer<Plan> producer : producers) {
root = new LogicalCTEAnchor<>(producer.getCteId(), producer, root);
@ -71,4 +75,30 @@ public class PullUpCteAnchor extends DefaultPlanRewriter<List<LogicalCTEProducer
producers.addAll(childProducers);
return newProducer;
}
@Override
public Plan visitLogicalApply(LogicalApply<? extends Plan, ? extends Plan> apply,
List<LogicalCTEProducer<Plan>> producers) {
SubqueryExpr subqueryExpr = apply.getSubqueryExpr();
PullUpCteAnchor pullSubqueryExpr = new PullUpCteAnchor();
List<LogicalCTEProducer<Plan>> 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<LogicalCTEProducer<Plan>> 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));
}
}

View File

@ -71,7 +71,7 @@ public class ColumnStatsAdjustVisitor extends ExpressionVisitor<ColumnStatistic,
public ColumnStatistic visitCast(Cast cast, Statistics context) {
ColumnStatistic colStats = context.findColumnStatistics(cast);
if (colStats != null) {
if (colStats != null && colStats.minExpr != null && colStats.maxExpr != null) {
try {
DataType childNereidsType = cast.child().getDataType();
if (childNereidsType instanceof CharacterType) {

View File

@ -102,4 +102,9 @@ public class Exists extends SubqueryExpr {
public Expression withTypeCoercion(DataType dataType) {
return this;
}
@Override
public Exists withSubquery(LogicalPlan subquery) {
return new Exists(subquery, correlateSlots, typeCoercionExpr, isNot);
}
}

View File

@ -19,6 +19,7 @@ package org.apache.doris.nereids.trees.expressions;
import org.apache.doris.nereids.exceptions.UnboundException;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
import org.apache.doris.nereids.types.BooleanType;
import org.apache.doris.nereids.types.DataType;
@ -138,4 +139,9 @@ public class InSubquery extends SubqueryExpr {
: Optional.of(new Cast(listQuery.queryPlan.getOutput().get(0), dataType)),
isNot);
}
@Override
public InSubquery withSubquery(LogicalPlan subquery) {
return new InSubquery(compareExpr, listQuery.withSubquery(subquery), correlateSlots, typeCoercionExpr, isNot);
}
}

View File

@ -68,4 +68,9 @@ public class ListQuery extends SubqueryExpr {
? Optional.of(queryPlan.getOutput().get(0))
: Optional.of(new Cast(queryPlan.getOutput().get(0), dataType)));
}
@Override
public ListQuery withSubquery(LogicalPlan subquery) {
return new ListQuery(subquery, correlateSlots, typeCoercionExpr);
}
}

View File

@ -76,4 +76,9 @@ public class ScalarSubquery extends SubqueryExpr {
? Optional.of(queryPlan.getOutput().get(0))
: Optional.of(new Cast(queryPlan.getOutput().get(0), dataType)));
}
@Override
public ScalarSubquery withSubquery(LogicalPlan subquery) {
return new ScalarSubquery(subquery, correlateSlots, typeCoercionExpr);
}
}

View File

@ -137,4 +137,6 @@ public abstract class SubqueryExpr extends Expression implements LeafExpression
}
public abstract Expression withTypeCoercion(DataType dataType);
public abstract SubqueryExpr withSubquery(LogicalPlan subquery);
}

View File

@ -198,8 +198,13 @@ public class LogicalApply<LEFT_CHILD_TYPE extends Plan, RIGHT_CHILD_TYPE extends
.build();
}
public LogicalApply<Plan, Plan> withSubqueryExprAndChildren(SubqueryExpr subqueryExpr, List<Plan> children) {
return new LogicalApply<>(correlationSlot, subqueryExpr, correlationFilter,
markJoinSlotReference, needAddSubOutputToProjects, inProject, children.get(0), children.get(1));
}
@Override
public LogicalBinary<Plan, Plan> withChildren(List<Plan> children) {
public LogicalApply<Plan, Plan> withChildren(List<Plan> children) {
Preconditions.checkArgument(children.size() == 2);
return new LogicalApply<>(correlationSlot, subqueryExpr, correlationFilter,
markJoinSlotReference, needAddSubOutputToProjects, inProject,