-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[feature](nereids)support subquery in select list #23271
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
4 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -21,9 +21,7 @@ | |
| import org.apache.doris.nereids.StatementContext; | ||
| import org.apache.doris.nereids.rules.Rule; | ||
| import org.apache.doris.nereids.rules.RuleType; | ||
| import org.apache.doris.nereids.trees.expressions.Alias; | ||
| import org.apache.doris.nereids.trees.expressions.BinaryOperator; | ||
| import org.apache.doris.nereids.trees.expressions.CaseWhen; | ||
| import org.apache.doris.nereids.trees.expressions.Exists; | ||
| import org.apache.doris.nereids.trees.expressions.Expression; | ||
| import org.apache.doris.nereids.trees.expressions.InSubquery; | ||
|
|
@@ -47,7 +45,6 @@ | |
|
|
||
| import java.util.Collection; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
@@ -68,8 +65,8 @@ public List<Rule> buildRules() { | |
| logicalFilter().thenApply(ctx -> { | ||
| LogicalFilter<Plan> filter = ctx.root; | ||
|
|
||
| ImmutableList<Set> subqueryExprsList = filter.getConjuncts().stream() | ||
| .map(e -> (Set) e.collect(SubqueryExpr.class::isInstance)) | ||
| ImmutableList<Set<SubqueryExpr>> subqueryExprsList = filter.getConjuncts().stream() | ||
| .map(e -> (Set<SubqueryExpr>) e.collect(SubqueryExpr.class::isInstance)) | ||
| .collect(ImmutableList.toImmutableList()); | ||
| if (subqueryExprsList.stream() | ||
| .flatMap(Collection::stream).noneMatch(SubqueryExpr.class::isInstance)) { | ||
|
|
@@ -104,8 +101,7 @@ public List<Rule> buildRules() { | |
| tmpPlan = applyPlan; | ||
| newConjuncts.add(conjunct); | ||
| } | ||
| Set<Expression> conjuncts = new LinkedHashSet<>(); | ||
| conjuncts.addAll(newConjuncts.build()); | ||
| Set<Expression> conjuncts = ImmutableSet.copyOf(newConjuncts.build()); | ||
| Plan newFilter = new LogicalFilter<>(conjuncts, applyPlan); | ||
| if (conjuncts.stream().flatMap(c -> c.children().stream()) | ||
| .anyMatch(MarkJoinSlotReference.class::isInstance)) { | ||
|
|
@@ -116,36 +112,44 @@ public List<Rule> buildRules() { | |
| return new LogicalFilter<>(conjuncts, applyPlan); | ||
| }) | ||
| ), | ||
| RuleType.PROJECT_SUBQUERY_TO_APPLY.build( | ||
| logicalProject().thenApply(ctx -> { | ||
| LogicalProject<Plan> project = ctx.root; | ||
| Set<SubqueryExpr> subqueryExprs = new LinkedHashSet<>(); | ||
| project.getProjects().stream() | ||
| .filter(Alias.class::isInstance) | ||
| .map(Alias.class::cast) | ||
| .filter(alias -> alias.child() instanceof CaseWhen) | ||
| .forEach(alias -> alias.child().children().stream() | ||
| .forEach(e -> | ||
| subqueryExprs.addAll(e.collect(SubqueryExpr.class::isInstance)))); | ||
| if (subqueryExprs.isEmpty()) { | ||
| return project; | ||
| } | ||
|
|
||
| SubqueryContext context = new SubqueryContext(subqueryExprs); | ||
| return new LogicalProject(project.getProjects().stream() | ||
| .map(p -> p.withChildren( | ||
| new ReplaceSubquery(ctx.statementContext, true) | ||
| .replace(p, context))) | ||
| .collect(ImmutableList.toImmutableList()), | ||
| subqueryToApply( | ||
| subqueryExprs.stream().collect(ImmutableList.toImmutableList()), | ||
| (LogicalPlan) project.child(), | ||
| context.getSubqueryToMarkJoinSlot(), | ||
| ctx.cascadesContext, | ||
| Optional.empty(), true | ||
| )); | ||
| }) | ||
| ) | ||
| RuleType.PROJECT_SUBQUERY_TO_APPLY.build(logicalProject().thenApply(ctx -> { | ||
| LogicalProject<Plan> project = ctx.root; | ||
| ImmutableList<Set<SubqueryExpr>> subqueryExprsList = project.getProjects().stream() | ||
| .map(e -> (Set<SubqueryExpr>) e.collect(SubqueryExpr.class::isInstance)) | ||
| .collect(ImmutableList.toImmutableList()); | ||
| if (subqueryExprsList.stream().flatMap(Collection::stream).count() == 0) { | ||
| return project; | ||
| } | ||
| List<NamedExpression> oldProjects = ImmutableList.copyOf(project.getProjects()); | ||
|
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. This copy is redundant? |
||
| ImmutableList.Builder<NamedExpression> newProjects = new ImmutableList.Builder<>(); | ||
| LogicalPlan childPlan = (LogicalPlan) project.child(); | ||
| LogicalPlan applyPlan; | ||
| for (int i = 0; i < subqueryExprsList.size(); ++i) { | ||
| Set<SubqueryExpr> subqueryExprs = subqueryExprsList.get(i); | ||
| if (subqueryExprs.isEmpty()) { | ||
| newProjects.add(oldProjects.get(i)); | ||
| continue; | ||
| } | ||
|
|
||
| // first step: Replace the subquery in logcialProject's project list | ||
| // second step: Replace subquery with LogicalApply | ||
| ReplaceSubquery replaceSubquery = | ||
| new ReplaceSubquery(ctx.statementContext, true); | ||
| SubqueryContext context = new SubqueryContext(subqueryExprs); | ||
| Expression newProject = | ||
| replaceSubquery.replace(oldProjects.get(i), context); | ||
|
|
||
| applyPlan = subqueryToApply( | ||
| subqueryExprs.stream().collect(ImmutableList.toImmutableList()), | ||
| childPlan, context.getSubqueryToMarkJoinSlot(), | ||
| ctx.cascadesContext, | ||
| Optional.of(newProject), true); | ||
| childPlan = applyPlan; | ||
| newProjects.add((NamedExpression) newProject); | ||
| } | ||
|
|
||
| return project.withProjectsAndChild(newProjects.build(), childPlan); | ||
| })) | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -249,28 +253,30 @@ public Expression visitExistsSubquery(Exists exists, SubqueryContext context) { | |
| // The result set when NULL is specified in the subquery and still evaluates to TRUE by using EXISTS | ||
| // When the number of rows returned is empty, agg will return null, so if there is more agg, | ||
| // it will always consider the returned result to be true | ||
| boolean needCreateMarkJoinSlot = isMarkJoin || isProject; | ||
| MarkJoinSlotReference markJoinSlotReference = null; | ||
| if (exists.getQueryPlan().anyMatch(Aggregate.class::isInstance) && isMarkJoin) { | ||
| if (exists.getQueryPlan().anyMatch(Aggregate.class::isInstance) && needCreateMarkJoinSlot) { | ||
| markJoinSlotReference = | ||
| new MarkJoinSlotReference(statementContext.generateColumnName(), true); | ||
| } else if (isMarkJoin) { | ||
| } else if (needCreateMarkJoinSlot) { | ||
| markJoinSlotReference = | ||
| new MarkJoinSlotReference(statementContext.generateColumnName()); | ||
| } | ||
| if (isMarkJoin) { | ||
| if (needCreateMarkJoinSlot) { | ||
| context.setSubqueryToMarkJoinSlot(exists, Optional.of(markJoinSlotReference)); | ||
| } | ||
| return isMarkJoin ? markJoinSlotReference : BooleanLiteral.TRUE; | ||
| return needCreateMarkJoinSlot ? markJoinSlotReference : BooleanLiteral.TRUE; | ||
| } | ||
|
|
||
| @Override | ||
| public Expression visitInSubquery(InSubquery in, SubqueryContext context) { | ||
| MarkJoinSlotReference markJoinSlotReference = | ||
| new MarkJoinSlotReference(statementContext.generateColumnName()); | ||
| if (isMarkJoin) { | ||
| boolean needCreateMarkJoinSlot = isMarkJoin || isProject; | ||
| if (needCreateMarkJoinSlot) { | ||
| context.setSubqueryToMarkJoinSlot(in, Optional.of(markJoinSlotReference)); | ||
| } | ||
| return isMarkJoin ? markJoinSlotReference : BooleanLiteral.TRUE; | ||
| return needCreateMarkJoinSlot ? markJoinSlotReference : BooleanLiteral.TRUE; | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| import org.apache.doris.nereids.trees.expressions.Alias; | ||
| import org.apache.doris.nereids.trees.expressions.ExprId; | ||
| import org.apache.doris.nereids.trees.expressions.Expression; | ||
| import org.apache.doris.nereids.trees.expressions.MarkJoinSlotReference; | ||
| import org.apache.doris.nereids.trees.expressions.NamedExpression; | ||
| import org.apache.doris.nereids.trees.expressions.Slot; | ||
| import org.apache.doris.nereids.trees.plans.Plan; | ||
|
|
@@ -45,7 +46,9 @@ public class PushdownAliasThroughJoin extends OneRewriteRuleFactory { | |
| public Rule build() { | ||
| return logicalProject(logicalJoin()) | ||
| .when(project -> project.getProjects().stream().allMatch(expr -> | ||
| (expr instanceof Slot) || (expr instanceof Alias && ((Alias) expr).child() instanceof Slot))) | ||
| (expr instanceof Slot && !(expr instanceof MarkJoinSlotReference)) | ||
| || (expr instanceof Alias && ((Alias) expr).child() instanceof Slot | ||
| && !(((Alias) expr).child() instanceof MarkJoinSlotReference)))) | ||
|
Comment on lines
+49
to
+51
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. do we need add more ut about this change? |
||
| .when(project -> project.getProjects().stream().anyMatch(expr -> expr instanceof Alias)) | ||
| .then(project -> { | ||
| LogicalJoin<? extends Plan, ? extends Plan> join = project.child(); | ||
|
|
||
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
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.
could we put them into costBased(...) ?