|
7 | 7 |
|
8 | 8 | package org.elasticsearch.xpack.esql.optimizer.rules.logical; |
9 | 9 |
|
| 10 | +import org.elasticsearch.xpack.esql.core.expression.Alias; |
| 11 | +import org.elasticsearch.xpack.esql.core.expression.Attribute; |
| 12 | +import org.elasticsearch.xpack.esql.core.expression.AttributeMap; |
| 13 | +import org.elasticsearch.xpack.esql.core.expression.Expression; |
| 14 | +import org.elasticsearch.xpack.esql.core.expression.Expressions; |
| 15 | +import org.elasticsearch.xpack.esql.core.expression.NamedExpression; |
| 16 | +import org.elasticsearch.xpack.esql.plan.logical.Eval; |
10 | 17 | import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan; |
11 | 18 | import org.elasticsearch.xpack.esql.plan.logical.Project; |
12 | | -import org.elasticsearch.xpack.esql.plan.logical.Eval; |
13 | 19 | import org.elasticsearch.xpack.esql.plan.logical.join.Join; |
14 | 20 | import org.elasticsearch.xpack.esql.plan.logical.join.JoinTypes; |
15 | 21 |
|
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Set; |
| 26 | + |
16 | 27 | /** |
17 | 28 | * If a {@link Project} is found in the left child of a left {@link Join}, perform it after. Due to requiring the projected attributes |
18 | 29 | * later, field extractions can also happen later, making joins cheapter to execute on data nodes. |
|
26 | 37 | public final class PushDownJoinPastProject extends OptimizerRules.OptimizerRule<Join> { |
27 | 38 | @Override |
28 | 39 | protected LogicalPlan rule(Join join) { |
29 | | - if (join.left() instanceof Project project && join.config().type() == JoinTypes.LEFT) |
30 | | - { |
31 | | - // 1. Propagate any renames into the Join, as we will remove the upstream Project. |
32 | | - // E.g. `RENAME field AS key | LOOKUP JOIN idx ON key` -> `LOOKUP JOIN idx ON field | ...` |
33 | | - // 2. Construct the downstream Project using the Join's output. |
34 | | - // Use trivial aliases for now, so we can easily update the expressions. |
35 | | - // This is like adding a `RENAME field1 AS field1, field2 AS field2, ...` after the Join, where we can easily change the |
36 | | - // referenced field names to deal with name conflicts. |
37 | | - // 3. Propagate any renames from the upstream Project into the new downstream Project. |
38 | | - // 4. Look for name conflicts: any name shadowed by the `LOOKUP JOIN` that was used in the original Project needs to be |
39 | | - // aliased temporarily. Add an Eval upstream from the `LOOKUP JOIN` and propagate its renames into the new downstream |
40 | | - // Project. |
41 | | - // 5. Remove any trivial aliases from the new downstream Project - `RENAME field AS field` just becomes (the equivalent of) |
42 | | - // `KEEP field`. |
| 40 | + if (join.left() instanceof Project project && join.config().type() == JoinTypes.LEFT) { |
| 41 | + AttributeMap.Builder<Expression> aliasBuilder = AttributeMap.builder(); |
| 42 | + project.forEachExpression(Alias.class, a -> aliasBuilder.put(a.toAttribute(), a.child())); |
| 43 | + var aliasesFromProject = aliasBuilder.build(); |
| 44 | + |
| 45 | + // Propagate any renames into the Join, as we will remove the upstream Project. |
| 46 | + // E.g. `RENAME field AS key | LOOKUP JOIN idx ON key` -> `LOOKUP JOIN idx ON field | ...` |
| 47 | + Join updatedJoin = PushDownUtils.resolveRenamesFromMap(join, aliasesFromProject); |
| 48 | + |
| 49 | + // Construct the expressions for the new downstream Project using the Join's output. |
| 50 | + // We need to carry over RENAMEs/aliases from the original upstream Project. |
| 51 | + List<Attribute> originalOutput = join.output(); |
| 52 | + List<NamedExpression> newProjections = new ArrayList<>(originalOutput.size()); |
| 53 | + for (Attribute attr : originalOutput) { |
| 54 | + Attribute resolved = (Attribute) aliasesFromProject.resolve(attr, attr); |
| 55 | + if (attr.semanticEquals(resolved)) { |
| 56 | + newProjections.add(attr); |
| 57 | + } else { |
| 58 | + Alias renamed = new Alias(attr.source(), attr.name(), resolved, attr.id(), attr.synthetic()); |
| 59 | + newProjections.add(renamed); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // This doesn't deal with name conflicts yet. Any name shadowed by a lookup field from the `LOOKUP JOIN` could still have been |
| 64 | + // used in the original Project; any such conflict needs to be resolved by copying the attribute under a temporary name via an |
| 65 | + // Eval - and using the attribute from said Eval in the new downstream Project. |
| 66 | + Set<String> lookupFieldNames = new HashSet<>(Expressions.names(join.rightOutputFields())); |
| 67 | + PushDownUtils.AttributeReplacement replacement = PushDownUtils.renameAttributesInExpressions(lookupFieldNames, newProjections); |
| 68 | + |
| 69 | + List<Expression> conflictFreeProjections = replacement.rewrittenExpressions(); |
| 70 | + List<Alias> evalAliases = new ArrayList<>(replacement.replacedAttributes().values()); |
| 71 | + |
| 72 | + if (evalAliases.isEmpty()) { |
| 73 | + // No name conflicts, so no eval needed. |
| 74 | + return new Project(project.source(), updatedJoin.replaceLeft(project.child()), newProjections); |
| 75 | + } |
| 76 | + |
| 77 | + // The conflict free projections replaced any shadowed attributes by temporary attributes that we'll create in an Eval. |
| 78 | + // That's good if the replaced attribute was in an Alias; if the projection was a mere attribute to begin with, we need to |
| 79 | + // alias it back to the name/id that's expected in the original output. |
| 80 | + List<NamedExpression> finalProjections = new ArrayList<>(conflictFreeProjections.size()); |
| 81 | + for (int i = 0; i < newProjections.size(); i++) { |
| 82 | + Expression conflictFreeProj = conflictFreeProjections.get(i); |
| 83 | + if (conflictFreeProj instanceof Alias as) { |
| 84 | + // Already aliased - keep it, it's fine if the child was rewritten. |
| 85 | + finalProjections.add(as); |
| 86 | + } else if (conflictFreeProj instanceof Attribute conflictFreeAttr) { |
| 87 | + Attribute expectedOutputAttr = (Attribute) newProjections.get(i); |
| 88 | + if (expectedOutputAttr.semanticEquals(conflictFreeAttr)) { |
| 89 | + // no conflict, wasn't rewritten |
| 90 | + finalProjections.add(expectedOutputAttr); |
| 91 | + } else { |
| 92 | + Alias renameBack = new Alias( |
| 93 | + expectedOutputAttr.source(), |
| 94 | + expectedOutputAttr.name(), |
| 95 | + conflictFreeAttr, |
| 96 | + expectedOutputAttr.id(), |
| 97 | + expectedOutputAttr.synthetic() |
| 98 | + ); |
| 99 | + finalProjections.add(renameBack); |
| 100 | + } |
| 101 | + } else { |
| 102 | + throw new IllegalStateException("Resolving a list of projections must yield a list of projections again"); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + Eval eval = new Eval(project.source(), project.child(), evalAliases); |
| 107 | + Join finalJoin = new Join(join.source(), eval, updatedJoin.right(), updatedJoin.config()); |
| 108 | + |
| 109 | + return new Project(project.source(), finalJoin, finalProjections); |
43 | 110 | } |
44 | 111 |
|
45 | 112 | return join; |
|
0 commit comments