-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[feature](nereids)support correlated scalar subquery without scalar agg #39471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
edccdbe
[feature](nereids)support correlated scalar subquery without scalar agg
starocean999 d6a74e5
fix case
starocean999 438221a
update code
starocean999 f466ab7
add case
starocean999 481b6f4
fix case
starocean999 6d93fe8
update shape file
starocean999 fd27d1d
add more case
starocean999 a7dbcbc
fix failed case
starocean999 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| package org.apache.doris.nereids.rules.analysis; | ||
|
|
||
| import org.apache.doris.common.Pair; | ||
| import org.apache.doris.nereids.analyzer.Scope; | ||
| import org.apache.doris.nereids.exceptions.AnalysisException; | ||
| import org.apache.doris.nereids.properties.OrderKey; | ||
| import org.apache.doris.nereids.rules.Rule; | ||
|
|
@@ -52,21 +53,27 @@ | |
| * Resolve having clause to the aggregation/repeat. | ||
| * need Top to Down to traverse plan, | ||
| * because we need to process FILL_UP_SORT_HAVING_AGGREGATE before FILL_UP_HAVING_AGGREGATE. | ||
| * be aware that when filling up the missing slots, we should exclude outer query's correlated slots. | ||
| * because these correlated slots belong to outer query, so should not try to find them in child node. | ||
| */ | ||
| public class FillUpMissingSlots implements AnalysisRuleFactory { | ||
| @Override | ||
| public List<Rule> buildRules() { | ||
| return ImmutableList.of( | ||
| RuleType.FILL_UP_SORT_PROJECT.build( | ||
| logicalSort(logicalProject()) | ||
| .then(sort -> { | ||
| .thenApply(ctx -> { | ||
| LogicalSort<LogicalProject<Plan>> sort = ctx.root; | ||
| Optional<Scope> outerScope = ctx.cascadesContext.getOuterScope(); | ||
| LogicalProject<Plan> project = sort.child(); | ||
| Set<Slot> projectOutputSet = project.getOutputSet(); | ||
| Set<Slot> notExistedInProject = sort.getOrderKeys().stream() | ||
| .map(OrderKey::getExpr) | ||
| .map(Expression::getInputSlots) | ||
| .flatMap(Set::stream) | ||
| .filter(s -> !projectOutputSet.contains(s)) | ||
| .filter(s -> !projectOutputSet.contains(s) | ||
| && (!outerScope.isPresent() || !outerScope.get() | ||
| .getCorrelatedSlots().contains(s))) | ||
| .collect(Collectors.toSet()); | ||
| if (notExistedInProject.isEmpty()) { | ||
| return null; | ||
|
|
@@ -82,7 +89,9 @@ public List<Rule> buildRules() { | |
| aggregate(logicalHaving(aggregate())) | ||
| .when(a -> a.getOutputExpressions().stream().allMatch(SlotReference.class::isInstance)) | ||
| ).when(this::checkSort) | ||
| .then(sort -> processDistinctProjectWithAggregate(sort, sort.child(), sort.child().child().child())) | ||
| .thenApply(ctx -> processDistinctProjectWithAggregate(ctx.root, | ||
| ctx.root.child(), ctx.root.child().child().child(), | ||
| ctx.cascadesContext.getOuterScope())) | ||
| ), | ||
| // ATTN: process aggregate with distinct project, must run this rule before FILL_UP_SORT_AGGREGATE | ||
| // because this pattern will always fail in FILL_UP_SORT_AGGREGATE | ||
|
|
@@ -91,14 +100,17 @@ public List<Rule> buildRules() { | |
| aggregate(aggregate()) | ||
| .when(a -> a.getOutputExpressions().stream().allMatch(SlotReference.class::isInstance)) | ||
| ).when(this::checkSort) | ||
| .then(sort -> processDistinctProjectWithAggregate(sort, sort.child(), sort.child().child())) | ||
| .thenApply(ctx -> processDistinctProjectWithAggregate(ctx.root, | ||
| ctx.root.child(), ctx.root.child().child(), | ||
| ctx.cascadesContext.getOuterScope())) | ||
| ), | ||
| RuleType.FILL_UP_SORT_AGGREGATE.build( | ||
| logicalSort(aggregate()) | ||
| .when(this::checkSort) | ||
| .then(sort -> { | ||
| .thenApply(ctx -> { | ||
| LogicalSort<Aggregate<Plan>> sort = ctx.root; | ||
| Aggregate<Plan> agg = sort.child(); | ||
| Resolver resolver = new Resolver(agg); | ||
| Resolver resolver = new Resolver(agg, ctx.cascadesContext.getOuterScope()); | ||
| sort.getExpressions().forEach(resolver::resolve); | ||
| return createPlan(resolver, agg, (r, a) -> { | ||
| List<OrderKey> newOrderKeys = sort.getOrderKeys().stream() | ||
|
|
@@ -118,10 +130,11 @@ public List<Rule> buildRules() { | |
| RuleType.FILL_UP_SORT_HAVING_AGGREGATE.build( | ||
| logicalSort(logicalHaving(aggregate())) | ||
| .when(this::checkSort) | ||
| .then(sort -> { | ||
| .thenApply(ctx -> { | ||
| LogicalSort<LogicalHaving<Aggregate<Plan>>> sort = ctx.root; | ||
| LogicalHaving<Aggregate<Plan>> having = sort.child(); | ||
| Aggregate<Plan> agg = having.child(); | ||
| Resolver resolver = new Resolver(agg); | ||
| Resolver resolver = new Resolver(agg, ctx.cascadesContext.getOuterScope()); | ||
| sort.getExpressions().forEach(resolver::resolve); | ||
| return createPlan(resolver, agg, (r, a) -> { | ||
| List<OrderKey> newOrderKeys = sort.getOrderKeys().stream() | ||
|
|
@@ -138,13 +151,17 @@ public List<Rule> buildRules() { | |
| }) | ||
| ), | ||
| RuleType.FILL_UP_SORT_HAVING_PROJECT.build( | ||
| logicalSort(logicalHaving(logicalProject())).then(sort -> { | ||
| logicalSort(logicalHaving(logicalProject())).thenApply(ctx -> { | ||
| LogicalSort<LogicalHaving<LogicalProject<Plan>>> sort = ctx.root; | ||
| Optional<Scope> outerScope = ctx.cascadesContext.getOuterScope(); | ||
| Set<Slot> childOutput = sort.child().getOutputSet(); | ||
| Set<Slot> notExistedInProject = sort.getOrderKeys().stream() | ||
| .map(OrderKey::getExpr) | ||
| .map(Expression::getInputSlots) | ||
| .flatMap(Set::stream) | ||
| .filter(s -> !childOutput.contains(s)) | ||
| .filter(s -> !childOutput.contains(s) | ||
| && (!outerScope.isPresent() || !outerScope.get() | ||
| .getCorrelatedSlots().contains(s))) | ||
| .collect(Collectors.toSet()); | ||
| if (notExistedInProject.isEmpty()) { | ||
| return null; | ||
|
|
@@ -158,9 +175,10 @@ public List<Rule> buildRules() { | |
| }) | ||
| ), | ||
| RuleType.FILL_UP_HAVING_AGGREGATE.build( | ||
| logicalHaving(aggregate()).then(having -> { | ||
| logicalHaving(aggregate()).thenApply(ctx -> { | ||
| LogicalHaving<Aggregate<Plan>> having = ctx.root; | ||
| Aggregate<Plan> agg = having.child(); | ||
| Resolver resolver = new Resolver(agg); | ||
| Resolver resolver = new Resolver(agg, ctx.cascadesContext.getOuterScope()); | ||
| having.getConjuncts().forEach(resolver::resolve); | ||
| return createPlan(resolver, agg, (r, a) -> { | ||
| Set<Expression> newConjuncts = ExpressionUtils.replace( | ||
|
|
@@ -175,7 +193,9 @@ public List<Rule> buildRules() { | |
| ), | ||
| // Convert having to filter | ||
| RuleType.FILL_UP_HAVING_PROJECT.build( | ||
| logicalHaving(logicalProject()).then(having -> { | ||
| logicalHaving(logicalProject()).thenApply(ctx -> { | ||
| LogicalHaving<LogicalProject<Plan>> having = ctx.root; | ||
| Optional<Scope> outerScope = ctx.cascadesContext.getOuterScope(); | ||
| if (having.getExpressions().stream().anyMatch(e -> e.containsType(AggregateFunction.class))) { | ||
| // This is very weird pattern. | ||
| // There are some aggregate functions in having, but its child is project. | ||
|
|
@@ -198,7 +218,7 @@ public List<Rule> buildRules() { | |
| ImmutableList.of(), ImmutableList.of(), project.child()); | ||
| // avoid throw exception even if having have slot from its child. | ||
| // because we will add a project between having and project. | ||
| Resolver resolver = new Resolver(agg, false); | ||
| Resolver resolver = new Resolver(agg, false, outerScope); | ||
| having.getConjuncts().forEach(resolver::resolve); | ||
| agg = agg.withAggOutput(resolver.getNewOutputSlots()); | ||
| Set<Expression> newConjuncts = ExpressionUtils.replace( | ||
|
|
@@ -212,7 +232,9 @@ public List<Rule> buildRules() { | |
| Set<Slot> notExistedInProject = having.getExpressions().stream() | ||
| .map(Expression::getInputSlots) | ||
| .flatMap(Set::stream) | ||
| .filter(s -> !projectOutputSet.contains(s)) | ||
| .filter(s -> !projectOutputSet.contains(s) | ||
| && (!outerScope.isPresent() || !outerScope.get() | ||
| .getCorrelatedSlots().contains(s))) | ||
| .collect(Collectors.toSet()); | ||
| if (notExistedInProject.isEmpty()) { | ||
| return null; | ||
|
|
@@ -235,18 +257,28 @@ static class Resolver { | |
| private final List<NamedExpression> newOutputSlots = Lists.newArrayList(); | ||
| private final Map<Slot, Expression> outputSubstitutionMap; | ||
| private final boolean checkSlot; | ||
| private final Optional<Scope> outerScope; | ||
|
|
||
| Resolver(Aggregate<?> aggregate, boolean checkSlot) { | ||
| Resolver(Aggregate<?> aggregate, boolean checkSlot, Optional<Scope> outerScope) { | ||
| outputExpressions = aggregate.getOutputExpressions(); | ||
| groupByExpressions = aggregate.getGroupByExpressions(); | ||
| outputSubstitutionMap = outputExpressions.stream().filter(Alias.class::isInstance) | ||
| .collect(Collectors.toMap(NamedExpression::toSlot, alias -> alias.child(0), | ||
| (k1, k2) -> k1)); | ||
| this.checkSlot = checkSlot; | ||
| this.outerScope = outerScope; | ||
| } | ||
|
|
||
| Resolver(Aggregate<?> aggregate, boolean checkSlot) { | ||
| this(aggregate, checkSlot, Optional.empty()); | ||
| } | ||
|
|
||
| Resolver(Aggregate<?> aggregate) { | ||
| this(aggregate, true); | ||
| this(aggregate, true, Optional.empty()); | ||
| } | ||
|
|
||
| Resolver(Aggregate<?> aggregate, Optional<Scope> outerScope) { | ||
| this(aggregate, true, outerScope); | ||
| } | ||
|
|
||
| public void resolve(Expression expression) { | ||
|
|
@@ -274,7 +306,8 @@ public void resolve(Expression expression) { | |
| // We couldn't find the equivalent expression in output expressions and group-by expressions, | ||
| // so we should check whether the expression is valid. | ||
| if (expression instanceof SlotReference) { | ||
| if (checkSlot) { | ||
| if (checkSlot && (!outerScope.isPresent() | ||
| || !outerScope.get().getCorrelatedSlots().contains(expression))) { | ||
|
Comment on lines
+309
to
+310
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add some comments |
||
| throw new AnalysisException(expression.toSql() + " should be grouped by."); | ||
| } | ||
| } else if (expression instanceof AggregateFunction) { | ||
|
|
@@ -401,8 +434,8 @@ private boolean checkSort(LogicalSort<? extends Plan> logicalSort) { | |
| * @return filled up plan | ||
| */ | ||
| private Plan processDistinctProjectWithAggregate(LogicalSort<?> sort, | ||
| Aggregate<?> upperAggregate, Aggregate<Plan> bottomAggregate) { | ||
| Resolver resolver = new Resolver(bottomAggregate); | ||
| Aggregate<?> upperAggregate, Aggregate<Plan> bottomAggregate, Optional<Scope> outerScope) { | ||
| Resolver resolver = new Resolver(bottomAggregate, outerScope); | ||
| sort.getExpressions().forEach(resolver::resolve); | ||
| return createPlan(resolver, bottomAggregate, (r, a) -> { | ||
| List<OrderKey> newOrderKeys = sort.getOrderKeys().stream() | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add some comments?