Skip to content

Commit ab0452b

Browse files
committed
Revert "WIP: TopNAggregate nodes, exec not fully implemented yet"
This reverts commit ee00361.
1 parent ee00361 commit ab0452b

File tree

14 files changed

+102
-468
lines changed

14 files changed

+102
-468
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/LocalLogicalPlanOptimizer.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.elasticsearch.xpack.esql.optimizer.rules.logical.local.InferNonNullAggConstraint;
1515
import org.elasticsearch.xpack.esql.optimizer.rules.logical.local.LocalPropagateEmptyRelation;
1616
import org.elasticsearch.xpack.esql.optimizer.rules.logical.local.ReplaceFieldWithConstantOrNull;
17-
import org.elasticsearch.xpack.esql.optimizer.rules.logical.local.ReplaceTopNAggregateWithTopNAndAggregate;
1817
import org.elasticsearch.xpack.esql.optimizer.rules.logical.local.ReplaceTopNWithLimitAndSort;
1918
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan;
2019
import org.elasticsearch.xpack.esql.rule.ParameterizedRuleExecutor;
@@ -39,7 +38,6 @@ public class LocalLogicalPlanOptimizer extends ParameterizedRuleExecutor<Logical
3938
new Batch<>(
4039
"Local rewrite",
4140
Limiter.ONCE,
42-
new ReplaceTopNAggregateWithTopNAndAggregate(),
4341
new ReplaceTopNWithLimitAndSort(),
4442
new ReplaceFieldWithConstantOrNull(),
4543
new InferIsNotNull(),

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/LogicalPlanOptimizer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import org.elasticsearch.xpack.esql.optimizer.rules.logical.PropagateEvalFoldables;
2929
import org.elasticsearch.xpack.esql.optimizer.rules.logical.PropagateInlineEvals;
3030
import org.elasticsearch.xpack.esql.optimizer.rules.logical.PropagateNullable;
31-
import org.elasticsearch.xpack.esql.optimizer.rules.logical.ReplaceTopNAndAggregateWithTopNAggregate;
31+
import org.elasticsearch.xpack.esql.optimizer.rules.logical.PropagateTopNToAggregates;
3232
import org.elasticsearch.xpack.esql.optimizer.rules.logical.PropgateUnmappedFields;
3333
import org.elasticsearch.xpack.esql.optimizer.rules.logical.PruneColumns;
3434
import org.elasticsearch.xpack.esql.optimizer.rules.logical.PruneEmptyPlans;
@@ -209,6 +209,6 @@ protected static Batch<LogicalPlan> operators() {
209209
}
210210

211211
protected static Batch<LogicalPlan> cleanup() {
212-
return new Batch<>("Clean Up", new ReplaceLimitAndSortAsTopN(), new ReplaceTopNAndAggregateWithTopNAggregate(), new ReplaceRowAsLocalRelation(), new PropgateUnmappedFields());
212+
return new Batch<>("Clean Up", new ReplaceLimitAndSortAsTopN(), new PropagateTopNToAggregates(), new ReplaceRowAsLocalRelation(), new PropgateUnmappedFields());
213213
}
214214
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/PhysicalPlanOptimizer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.elasticsearch.xpack.esql.VerificationException;
1111
import org.elasticsearch.xpack.esql.common.Failures;
1212
import org.elasticsearch.xpack.esql.optimizer.rules.physical.ProjectAwayColumns;
13+
import org.elasticsearch.xpack.esql.optimizer.rules.logical.PropagateTopNToAggregates;
1314
import org.elasticsearch.xpack.esql.plan.physical.FragmentExec;
1415
import org.elasticsearch.xpack.esql.plan.physical.PhysicalPlan;
1516
import org.elasticsearch.xpack.esql.rule.ParameterizedRuleExecutor;

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/rules/logical/ReplaceTopNAndAggregateWithTopNAggregate.java renamed to x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/rules/logical/PropagateTopNToAggregates.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010
import org.elasticsearch.xpack.esql.plan.logical.Aggregate;
1111
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan;
1212
import org.elasticsearch.xpack.esql.plan.logical.TopN;
13-
import org.elasticsearch.xpack.esql.plan.logical.TopNAggregate;
1413
import org.elasticsearch.xpack.esql.rule.Rule;
1514

1615
/**
1716
* Looks for the structure:
1817
* <pre>
19-
* {@link TopN}
20-
* \_{@link Aggregate}
18+
* TopN
19+
* \_Aggregate
2120
* </pre>
22-
* And replaces it with {@link TopNAggregate}.
21+
* And replaces the Aggregate with an Aggregate with the TopN data.
22+
* (TODO: Create a new TopNAggregate node instead)
2323
*/
24-
public class ReplaceTopNAndAggregateWithTopNAggregate extends Rule<TopN, LogicalPlan> {
24+
public class PropagateTopNToAggregates extends Rule<TopN, LogicalPlan> {
2525

2626
@Override
2727
public LogicalPlan apply(LogicalPlan plan) {
@@ -31,16 +31,18 @@ public LogicalPlan apply(LogicalPlan plan) {
3131
);
3232
}
3333

34-
private LogicalPlan applyRule(TopN topN) {
34+
private TopN applyRule(TopN topN) {
3535
// TODO: Handle TimeSeriesAggregate
3636
if (topN.child() instanceof Aggregate aggregate) {
37-
return new TopNAggregate(
38-
aggregate.source(),
39-
aggregate.child(),
40-
aggregate.groupings(),
41-
aggregate.aggregates(),
42-
topN.order(),
43-
topN.limit()
37+
return topN.replaceChild(
38+
new Aggregate(
39+
aggregate.source(),
40+
aggregate.child(),
41+
aggregate.groupings(),
42+
aggregate.aggregates(),
43+
topN.order(),
44+
topN.limit()
45+
)
4446
);
4547
}
4648
return topN;

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/rules/logical/local/ReplaceTopNAggregateWithTopNAndAggregate.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plan/PlanWritables.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.elasticsearch.xpack.esql.plan.logical.Sample;
2525
import org.elasticsearch.xpack.esql.plan.logical.TimeSeriesAggregate;
2626
import org.elasticsearch.xpack.esql.plan.logical.TopN;
27-
import org.elasticsearch.xpack.esql.plan.logical.TopNAggregate;
2827
import org.elasticsearch.xpack.esql.plan.logical.inference.Completion;
2928
import org.elasticsearch.xpack.esql.plan.logical.inference.Rerank;
3029
import org.elasticsearch.xpack.esql.plan.logical.join.InlineJoin;
@@ -53,7 +52,6 @@
5352
import org.elasticsearch.xpack.esql.plan.physical.ShowExec;
5453
import org.elasticsearch.xpack.esql.plan.physical.SubqueryExec;
5554
import org.elasticsearch.xpack.esql.plan.physical.TimeSeriesAggregateExec;
56-
import org.elasticsearch.xpack.esql.plan.physical.TopNAggregateExec;
5755
import org.elasticsearch.xpack.esql.plan.physical.TopNExec;
5856
import org.elasticsearch.xpack.esql.plan.physical.inference.CompletionExec;
5957
import org.elasticsearch.xpack.esql.plan.physical.inference.RerankExec;
@@ -93,8 +91,7 @@ public static List<NamedWriteableRegistry.Entry> logical() {
9391
Rerank.ENTRY,
9492
Sample.ENTRY,
9593
TimeSeriesAggregate.ENTRY,
96-
TopN.ENTRY,
97-
TopNAggregate.ENTRY
94+
TopN.ENTRY
9895
);
9996
}
10097

@@ -124,8 +121,7 @@ public static List<NamedWriteableRegistry.Entry> physical() {
124121
ShowExec.ENTRY,
125122
SubqueryExec.ENTRY,
126123
TimeSeriesAggregateExec.ENTRY,
127-
TopNExec.ENTRY,
128-
TopNAggregateExec.ENTRY
124+
TopNExec.ENTRY
129125
);
130126
}
131127
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plan/QueryPlan.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ public AttributeSet references() {
8989
/**
9090
* This very likely needs to be overridden for {@link QueryPlan#references} to be correct when inheriting.
9191
* This can be called on unresolved plans and therefore must not rely on calls to {@link QueryPlan#output()}.
92-
* @see #references()
9392
*/
9493
protected AttributeSet computeReferences() {
9594
return Expressions.references(expressions());

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plan/logical/Aggregate.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
1111
import org.elasticsearch.common.io.stream.StreamInput;
1212
import org.elasticsearch.common.io.stream.StreamOutput;
13+
import org.elasticsearch.core.Nullable;
1314
import org.elasticsearch.index.IndexMode;
1415
import org.elasticsearch.xpack.esql.capabilities.PostAnalysisVerificationAware;
1516
import org.elasticsearch.xpack.esql.capabilities.TelemetryAware;
@@ -27,6 +28,7 @@
2728
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
2829
import org.elasticsearch.xpack.esql.core.tree.Source;
2930
import org.elasticsearch.xpack.esql.core.util.Holder;
31+
import org.elasticsearch.xpack.esql.expression.Order;
3032
import org.elasticsearch.xpack.esql.expression.function.aggregate.AggregateFunction;
3133
import org.elasticsearch.xpack.esql.expression.function.aggregate.FilteredExpression;
3234
import org.elasticsearch.xpack.esql.expression.function.aggregate.Rate;
@@ -55,12 +57,32 @@ public class Aggregate extends UnaryPlan implements PostAnalysisVerificationAwar
5557
protected final List<Expression> groupings;
5658
protected final List<? extends NamedExpression> aggregates;
5759

60+
private final List<Order> order;
61+
@Nullable
62+
private final Expression limit;
63+
5864
protected List<Attribute> lazyOutput;
5965

6066
public Aggregate(Source source, LogicalPlan child, List<Expression> groupings, List<? extends NamedExpression> aggregates) {
6167
super(source, child);
6268
this.groupings = groupings;
6369
this.aggregates = aggregates;
70+
this.order = List.of();
71+
this.limit = null;
72+
}
73+
74+
public Aggregate(
75+
Source source,
76+
LogicalPlan child,
77+
List<Expression> groupings, List<? extends NamedExpression> aggregates,
78+
List<Order> order,
79+
@Nullable Expression limit
80+
) {
81+
super(source, child);
82+
this.groupings = groupings;
83+
this.aggregates = aggregates;
84+
this.order = order;
85+
this.limit = limit;
6486
}
6587

6688
public Aggregate(StreamInput in) throws IOException {
@@ -71,6 +93,13 @@ public Aggregate(StreamInput in) throws IOException {
7193
}
7294
this.groupings = in.readNamedWriteableCollectionAsList(Expression.class);
7395
this.aggregates = in.readNamedWriteableCollectionAsList(NamedExpression.class);
96+
if (in.getTransportVersion().onOrAfter(TransportVersions.ESQL_TOP_N_AGGREGATES)) {
97+
this.order = in.readCollectionAsList(Order::new);
98+
this.limit = in.readOptionalNamedWriteable(Expression.class);
99+
} else {
100+
this.order = emptyList();
101+
this.limit = null;
102+
}
74103
}
75104

76105
@Override
@@ -83,6 +112,10 @@ public void writeTo(StreamOutput out) throws IOException {
83112
}
84113
out.writeNamedWriteableCollection(groupings);
85114
out.writeNamedWriteableCollection(aggregates());
115+
if (out.getTransportVersion().onOrAfter(TransportVersions.ESQL_TOP_N_AGGREGATES)) {
116+
out.writeCollection(order);
117+
out.writeOptionalNamedWriteable(limit);
118+
}
86119
}
87120

88121
@Override
@@ -97,15 +130,15 @@ protected NodeInfo<? extends Aggregate> info() {
97130

98131
@Override
99132
public Aggregate replaceChild(LogicalPlan newChild) {
100-
return new Aggregate(source(), newChild, groupings, aggregates);
133+
return new Aggregate(source(), newChild, groupings, aggregates, order, limit);
101134
}
102135

103136
public Aggregate with(List<Expression> newGroupings, List<? extends NamedExpression> newAggregates) {
104137
return with(child(), newGroupings, newAggregates);
105138
}
106139

107140
public Aggregate with(LogicalPlan child, List<Expression> newGroupings, List<? extends NamedExpression> newAggregates) {
108-
return new Aggregate(source(), child, newGroupings, newAggregates);
141+
return new Aggregate(source(), child, newGroupings, newAggregates, order, limit);
109142
}
110143

111144
public List<Expression> groupings() {
@@ -116,6 +149,14 @@ public List<? extends NamedExpression> aggregates() {
116149
return aggregates;
117150
}
118151

152+
public List<Order> order() {
153+
return order;
154+
}
155+
156+
public Expression limit() {
157+
return limit;
158+
}
159+
119160
@Override
120161
public String telemetryLabel() {
121162
return "STATS";

0 commit comments

Comments
 (0)