|
16 | 16 | import org.elasticsearch.xpack.esql.core.tree.Source;
|
17 | 17 | import org.elasticsearch.xpack.esql.core.util.Holder;
|
18 | 18 | import org.elasticsearch.xpack.esql.expression.function.aggregate.AggregateFunction;
|
19 |
| -import org.elasticsearch.xpack.esql.expression.function.grouping.Categorize; |
| 19 | +import org.elasticsearch.xpack.esql.expression.function.grouping.GroupingFunction; |
20 | 20 | import org.elasticsearch.xpack.esql.plan.logical.Aggregate;
|
21 | 21 | import org.elasticsearch.xpack.esql.plan.logical.Eval;
|
22 | 22 | import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan;
|
@@ -50,20 +50,20 @@ public ReplaceAggregateAggExpressionWithEval() {
|
50 | 50 |
|
51 | 51 | @Override
|
52 | 52 | protected LogicalPlan rule(Aggregate aggregate) {
|
53 |
| - // build alias map |
| 53 | + // an alias map for evaluatable grouping functions |
54 | 54 | AttributeMap.Builder<Expression> aliasesBuilder = AttributeMap.builder();
|
55 |
| - aggregate.forEachExpressionUp(Alias.class, a -> aliasesBuilder.put(a.toAttribute(), a.child())); |
56 |
| - var aliases = aliasesBuilder.build(); |
57 |
| - |
58 |
| - // Build Categorize grouping functions map. |
59 |
| - // Functions like BUCKET() shouldn't reach this point, |
60 |
| - // as they are moved to an early EVAL by ReplaceAggregateNestedExpressionWithEval |
61 |
| - Map<Categorize, Attribute> groupingAttributes = new HashMap<>(); |
| 55 | + // a function map for non-evaluatable grouping functions |
| 56 | + Map<GroupingFunction.NonEvaluatableGroupingFunction, Attribute> nonEvalGroupingAttributes = new HashMap<>( |
| 57 | + aggregate.groupings().size() |
| 58 | + ); |
62 | 59 | aggregate.forEachExpressionUp(Alias.class, a -> {
|
63 |
| - if (a.child() instanceof Categorize groupingFunction) { |
64 |
| - groupingAttributes.put(groupingFunction, a.toAttribute()); |
| 60 | + if (a.child() instanceof GroupingFunction.NonEvaluatableGroupingFunction groupingFunction) { |
| 61 | + nonEvalGroupingAttributes.put(groupingFunction, a.toAttribute()); |
| 62 | + } else { |
| 63 | + aliasesBuilder.put(a.toAttribute(), a.child()); |
65 | 64 | }
|
66 | 65 | });
|
| 66 | + var aliases = aliasesBuilder.build(); |
67 | 67 |
|
68 | 68 | // break down each aggregate into AggregateFunction and/or grouping key
|
69 | 69 | // preserve the projection at the end
|
@@ -123,8 +123,11 @@ protected LogicalPlan rule(Aggregate aggregate) {
|
123 | 123 | return alias.toAttribute();
|
124 | 124 | });
|
125 | 125 |
|
126 |
| - // replace grouping functions with their references |
127 |
| - aggExpression = aggExpression.transformUp(Categorize.class, groupingAttributes::get); |
| 126 | + // replace non-evaluatable grouping functions with their references |
| 127 | + aggExpression = aggExpression.transformUp( |
| 128 | + GroupingFunction.NonEvaluatableGroupingFunction.class, |
| 129 | + nonEvalGroupingAttributes::get |
| 130 | + ); |
128 | 131 |
|
129 | 132 | Alias alias = as.replaceChild(aggExpression);
|
130 | 133 | newEvals.add(alias);
|
@@ -152,7 +155,7 @@ protected LogicalPlan rule(Aggregate aggregate) {
|
152 | 155 | return plan;
|
153 | 156 | }
|
154 | 157 |
|
155 |
| - static String syntheticName(Expression expression, Expression af, int counter) { |
| 158 | + private static String syntheticName(Expression expression, Expression af, int counter) { |
156 | 159 | return TemporaryNameUtils.temporaryName(expression, af, counter);
|
157 | 160 | }
|
158 | 161 | }
|
0 commit comments