-
Notifications
You must be signed in to change notification settings - Fork 25.7k
ESQL: Push down MvExpand past Project
#136398
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
Open
kanoshiou
wants to merge
27
commits into
elastic:main
Choose a base branch
from
kanoshiou:push-down-mv_expand-past-project
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
ca954c4
Push down MvExpand past Project
kanoshiou 55c37c2
Update docs/changelog/136398.yaml
kanoshiou 91f86d6
Merge remote-tracking branch 'origin/main' into push-down-mv_expand-p…
kanoshiou a8b3897
Insert an eval if target in mv_expand is aliased in child project
kanoshiou 83aa387
Add tests for #136596
kanoshiou 2095c44
Update docs/changelog/136398.yaml
kanoshiou 8d64398
Merge branch 'refs/heads/main' into push-down-mv_expand-past-project
kanoshiou a46cc7d
Merge branch 'refs/heads/main' into push-down-mv_expand-past-project
kanoshiou 471124c
Merge branch 'refs/heads/main' into push-down-mv_expand-past-project
kanoshiou d08a6ea
refactor(esql): Improve MvExpand push-down logic in logical optimizer
kanoshiou 075e826
refactor(esql): Improve MvExpand push-down logic in logical optimizer
kanoshiou 6018ce0
Merge branch 'refs/heads/main' into push-down-mv_expand-past-project
kanoshiou 3afecaa
refactor(esql): Improve MvExpand push-down logic in logical optimizer
kanoshiou 2b3d55a
refactor(esql): Improve MvExpand push-down logic in logical optimizer
kanoshiou 8e69eb0
Merge branch 'main' into push-down-mv_expand-past-project
kanoshiou faea78b
refactor(esql): Improve MvExpand push-down logic in PushDownMvExpandP…
kanoshiou 7f8faf6
Merge branch 'refs/heads/main' into push-down-mv_expand-past-project
kanoshiou 92dacb5
fix: refine `PushDownMvExpandPastProject` logic by correcting the ali…
kanoshiou eb3f373
Merge branch 'refs/heads/main' into push-down-mv_expand-past-project
kanoshiou adab9f8
fix(esql): simplify MvExpand pushdown logic by extracting to utility …
kanoshiou dd86f32
Update test
kanoshiou b042096
fix(esql): refactor MvExpand pushdown logic to directly manipulate pr…
kanoshiou d8c221a
Avoid inconsistent plans
kanoshiou b5ede88
Create a temporary attribute for the UnionAll case
kanoshiou cd844d0
Add more tests for UnionAll cases
kanoshiou 2c45495
Merge branch 'refs/heads/main' into push-down-mv_expand-past-project
kanoshiou 9410846
Merge branch 'refs/heads/main' into push-down-mv_expand-past-project
kanoshiou 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| pr: 136398 | ||
| summary: "ESQL: Push down `MvExpand` past `Project`" | ||
| area: ES|QL | ||
| type: enhancement | ||
| issues: | ||
| - 136292 | ||
| - 136596 | ||
| - 119074 |
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
162 changes: 162 additions & 0 deletions
162
...ava/org/elasticsearch/xpack/esql/optimizer/rules/logical/PushDownMvExpandPastProject.java
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 |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.esql.optimizer.rules.logical; | ||
|
|
||
| import org.elasticsearch.xpack.esql.core.expression.Alias; | ||
| import org.elasticsearch.xpack.esql.core.expression.Attribute; | ||
| import org.elasticsearch.xpack.esql.core.expression.NamedExpression; | ||
| import org.elasticsearch.xpack.esql.core.expression.Nullability; | ||
| import org.elasticsearch.xpack.esql.core.expression.ReferenceAttribute; | ||
| import org.elasticsearch.xpack.esql.plan.logical.Eval; | ||
| import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan; | ||
| import org.elasticsearch.xpack.esql.plan.logical.MvExpand; | ||
| import org.elasticsearch.xpack.esql.plan.logical.Project; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public final class PushDownMvExpandPastProject extends OptimizerRules.OptimizerRule<MvExpand> { | ||
| @Override | ||
| protected LogicalPlan rule(MvExpand mvExpand) { | ||
| if (mvExpand.child() instanceof Project pj) { | ||
| LogicalPlan finalChild = pj.child(); | ||
| NamedExpression finalTarget = mvExpand.target(); | ||
| Attribute finalExpanded = mvExpand.expanded(); | ||
|
|
||
| String expandedFieldName = finalExpanded.name(); | ||
| List<NamedExpression> projections = new ArrayList<>(pj.projections()); | ||
| Set<String> inputNames = pj.inputSet().stream().map(NamedExpression::name).collect(Collectors.toSet()); | ||
|
|
||
| // Find if the target is aliased in the project and create an alias with temporary names for it. | ||
| for (int i = 0; i < projections.size(); i++) { | ||
| if (projections.get(i) instanceof Alias alias) { | ||
| boolean replaced = false; | ||
| /* | ||
| * If the expanded field has the same name as a field in the projection's input set, | ||
| * and the projection shadows that specific field from the projection input set. | ||
| * Pushing down the MvExpand in such cases would cause duplicate output attributes. | ||
| * To avoid this case, we create a temporary attribute for the expanded field and | ||
| * update the projection to alias this temporary attribute back to the original name. | ||
| * This can happen with aliases generated by ResolveUnionTypesInUnionAll. | ||
| * | ||
| * Example query: | ||
| * from employees, (from employees | keep salary) | ||
| * | eval salary = salary::keyword | ||
| * | keep salary | ||
| * | mv_expand salary | ||
| * | ||
| * From plan: | ||
| * MvExpand[language_code{r}#4,language_code{r}#17] | ||
| * \_Project[[$$language_code$converted_to$keyword{r$}#20 AS language_code#4]] | ||
| * \_UnionAll[[language_code{r}#15, $$language_code$converted_to$keyword{r$}#20, language_name{r}#16]] | ||
| * | ||
| * To plan: | ||
| * Project[[$$language_code$temp_name$21{r$}#22 AS language_code#17]] | ||
| * \_MvExpand[$$language_code$converted_to$keyword{r$}#20,$$language_code$temp_name$21{r$}#22] | ||
| * \_UnionAll[[language_code{r}#15, $$language_code$converted_to$keyword{r$}#20, language_name{r}#16]] | ||
| * | ||
| * | ||
| * If the original mv_expand target field is referenced elsewhere in the projections, | ||
| * a defensive eval will also be injected. | ||
| * | ||
| * Example query: | ||
| * from languages, (from languages | keep language_code) | ||
| * | eval language_code = language_code::keyword | ||
| * | eval tmp = language_code | ||
| * | keep language_code, tmp | ||
| * | mv_expand language_code | ||
| * | ||
| * From plan: | ||
| * MvExpand[language_code{r}#4,language_code{r}#22] | ||
| * \_Project[[$$language_code$converted_to$keyword{r$}#25 AS language_code#4,$$language_code$converted_to$keyword{r$}#25 | ||
| * AS tmp#7]] | ||
| * \_UnionAll[[language_code{r}#20, $$language_code$converted_to$keyword{r$}#25, language_name{r}#21]] | ||
| * | ||
| * To plan: | ||
| * Project[[$$language_code$temp_name$26{r$}#27 AS language_code#22, $$language_code$converted_to$keyword{r$}#25 | ||
| * AS tmp#7]] | ||
| * \_MvExpand[$$language_code$converted_to$keyword$language_code$0{r}#28,$$language_code$temp_name$26{r$}#27] | ||
| * \_Eval[[$$language_code$converted_to$keyword{r$}#25 AS $$language_code$converted_to$keyword$language_code$0#28]] | ||
| * \_UnionAll[[language_code{r}#20, $$language_code$converted_to$keyword{r$}#25, language_name{r}#21]] | ||
| */ | ||
| if (alias.toAttribute().semanticEquals(finalTarget.toAttribute())) { | ||
| if (inputNames.contains(expandedFieldName) && inputNames.contains(alias.toAttribute().name())) { | ||
| ReferenceAttribute tempAttribute = new ReferenceAttribute( | ||
| alias.source(), | ||
| null, | ||
| TemporaryNameUtils.locallyUniqueTemporaryName(alias.name()), | ||
| alias.dataType(), | ||
| Nullability.FALSE, | ||
| null, | ||
| true | ||
| ); | ||
| projections.set(i, new Alias(alias.source(), expandedFieldName, tempAttribute, finalExpanded.id())); | ||
| finalExpanded = tempAttribute; | ||
| replaced = true; | ||
| } | ||
|
|
||
| // Check if the alias's original field (child) is referenced elsewhere in the projections. | ||
| // If the original field is not referenced by any other projection or alias, | ||
| // we don't need to inject an Eval to preserve it, and can safely resolve renames and push down. | ||
| if (projections.stream() | ||
| .anyMatch( | ||
| ne -> ne.semanticEquals(alias.child()) | ||
| || ne instanceof Alias as && as.child().semanticEquals(alias.child()) && as != alias | ||
| ) == false) { | ||
| // The alias's original field is not referenced elsewhere, no need to preserve it, | ||
| finalTarget = (NamedExpression) alias.child(); | ||
| break; | ||
| } | ||
|
|
||
| // for query like: row a = 2 | eval b = a | keep * | mv_expand b | ||
| Alias aliasAlias = new Alias( | ||
| alias.source(), | ||
| TemporaryNameUtils.temporaryName(alias.child(), alias.toAttribute(), 0), | ||
| alias.child() | ||
| ); | ||
| if (replaced == false) { | ||
| projections.set(i, finalExpanded); | ||
| } | ||
| finalChild = new Eval(aliasAlias.source(), finalChild, List.of(aliasAlias)); | ||
| finalTarget = aliasAlias.toAttribute(); | ||
| break; | ||
| } else if (alias.child().semanticEquals(finalTarget.toAttribute())) { | ||
| // for query like: row a = 2 | eval b = a | keep * | mv_expand a | ||
| Alias aliasAlias = new Alias( | ||
| alias.source(), | ||
| TemporaryNameUtils.temporaryName(alias.child(), alias.toAttribute(), 0), | ||
| alias.child() | ||
| ); | ||
| projections.set(i, alias.replaceChild(aliasAlias.toAttribute())); | ||
| finalChild = new Eval(aliasAlias.source(), finalChild, List.of(aliasAlias)); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Push down the MvExpand past the Project | ||
| MvExpand pushedDownMvExpand = new MvExpand(mvExpand.source(), finalChild, finalTarget, finalExpanded); | ||
|
|
||
| // Update projections to point to the expanded attribute | ||
| Attribute target = finalTarget.toAttribute(); | ||
| for (int i = 0; i < projections.size(); i++) { | ||
| NamedExpression ne = projections.get(i); | ||
| if (ne instanceof Alias alias && alias.child().semanticEquals(target)) { | ||
| projections.set(i, alias.replaceChild(finalExpanded)); | ||
| } else if (ne.semanticEquals(target)) { | ||
| projections.set(i, finalExpanded); | ||
| } | ||
| } | ||
|
|
||
| return new Project(pj.source(), pushedDownMvExpand, projections); | ||
| } | ||
| return mvExpand; | ||
| } | ||
| } | ||
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.
Very helpful comments, btw!