Skip to content

Commit dc51528

Browse files
nik9000albertzaharovits
authored andcommitted
ESQL: Flatten LogicalPlan extensions (#110715)
Previously you had to remember to use `EsqlAggregate` and `EsqlUnresolvedRelation` instead of `Aggregate` and `UnresolvedRelation`. This merges the `Esql*` extensions into their plainly named extensions for easier hacking.
1 parent 02066da commit dc51528

File tree

10 files changed

+144
-245
lines changed

10 files changed

+144
-245
lines changed

x-pack/plugin/esql/compute/src/main/generated/org/elasticsearch/compute/aggregation/MaxBooleanGroupingAggregatorFunction.java

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/analysis/Analyzer.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@
6464
import org.elasticsearch.xpack.esql.plan.logical.Drop;
6565
import org.elasticsearch.xpack.esql.plan.logical.Enrich;
6666
import org.elasticsearch.xpack.esql.plan.logical.EsRelation;
67-
import org.elasticsearch.xpack.esql.plan.logical.EsqlAggregate;
68-
import org.elasticsearch.xpack.esql.plan.logical.EsqlUnresolvedRelation;
6967
import org.elasticsearch.xpack.esql.plan.logical.Eval;
7068
import org.elasticsearch.xpack.esql.plan.logical.Keep;
7169
import org.elasticsearch.xpack.esql.plan.logical.Limit;
@@ -74,6 +72,7 @@
7472
import org.elasticsearch.xpack.esql.plan.logical.MvExpand;
7573
import org.elasticsearch.xpack.esql.plan.logical.Project;
7674
import org.elasticsearch.xpack.esql.plan.logical.Rename;
75+
import org.elasticsearch.xpack.esql.plan.logical.UnresolvedRelation;
7776
import org.elasticsearch.xpack.esql.plan.logical.local.EsqlProject;
7877
import org.elasticsearch.xpack.esql.plan.logical.local.LocalRelation;
7978
import org.elasticsearch.xpack.esql.plan.logical.local.LocalSupplier;
@@ -169,16 +168,17 @@ protected Iterable<RuleExecutor.Batch<LogicalPlan>> batches() {
169168
return rules;
170169
}
171170

172-
private static class ResolveTable extends ParameterizedAnalyzerRule<EsqlUnresolvedRelation, AnalyzerContext> {
171+
private static class ResolveTable extends ParameterizedAnalyzerRule<UnresolvedRelation, AnalyzerContext> {
173172

174173
@Override
175-
protected LogicalPlan rule(EsqlUnresolvedRelation plan, AnalyzerContext context) {
174+
protected LogicalPlan rule(UnresolvedRelation plan, AnalyzerContext context) {
176175
if (context.indexResolution().isValid() == false) {
177176
return plan.unresolvedMessage().equals(context.indexResolution().toString())
178177
? plan
179-
: new EsqlUnresolvedRelation(
178+
: new UnresolvedRelation(
180179
plan.source(),
181180
plan.table(),
181+
plan.frozen(),
182182
plan.metadataFields(),
183183
plan.indexMode(),
184184
context.indexResolution().toString()
@@ -187,9 +187,10 @@ protected LogicalPlan rule(EsqlUnresolvedRelation plan, AnalyzerContext context)
187187
TableIdentifier table = plan.table();
188188
if (context.indexResolution().matches(table.index()) == false) {
189189
// TODO: fix this (and tests), or drop check (seems SQL-inherited, where's also defective)
190-
new EsqlUnresolvedRelation(
190+
new UnresolvedRelation(
191191
plan.source(),
192192
plan.table(),
193+
plan.frozen(),
193194
plan.metadataFields(),
194195
plan.indexMode(),
195196
"invalid [" + table + "] resolution to [" + context.indexResolution() + "]"
@@ -449,7 +450,7 @@ private LogicalPlan resolveAggregate(Aggregate a, List<Attribute> childrenOutput
449450
}
450451
groupings = newGroupings;
451452
if (changed.get()) {
452-
a = new EsqlAggregate(a.source(), a.child(), a.aggregateType(), newGroupings, a.aggregates());
453+
a = new Aggregate(a.source(), a.child(), a.aggregateType(), newGroupings, a.aggregates());
453454
changed.set(false);
454455
}
455456
}
@@ -478,7 +479,7 @@ private LogicalPlan resolveAggregate(Aggregate a, List<Attribute> childrenOutput
478479
newAggregates.add(agg);
479480
}
480481

481-
a = changed.get() ? new EsqlAggregate(a.source(), a.child(), a.aggregateType(), groupings, newAggregates) : a;
482+
a = changed.get() ? new Aggregate(a.source(), a.child(), a.aggregateType(), groupings, newAggregates) : a;
482483
}
483484

484485
return a;
@@ -1088,7 +1089,7 @@ protected LogicalPlan doRule(LogicalPlan plan) {
10881089

10891090
// In ResolveRefs the aggregates are resolved from the groupings, which might have an unresolved MultiTypeEsField.
10901091
// Now that we have resolved those, we need to re-resolve the aggregates.
1091-
if (plan instanceof EsqlAggregate agg) {
1092+
if (plan instanceof Aggregate agg) {
10921093
// If the union-types resolution occurred in a child of the aggregate, we need to check the groupings
10931094
plan = agg.transformExpressionsOnly(FieldAttribute.class, UnresolveUnionTypes::checkUnresolved);
10941095

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/analysis/PreAnalyzer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
import org.elasticsearch.xpack.esql.core.analyzer.TableInfo;
1111
import org.elasticsearch.xpack.esql.plan.logical.Enrich;
12-
import org.elasticsearch.xpack.esql.plan.logical.EsqlUnresolvedRelation;
1312
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan;
13+
import org.elasticsearch.xpack.esql.plan.logical.UnresolvedRelation;
1414

1515
import java.util.ArrayList;
1616
import java.util.List;
@@ -43,7 +43,7 @@ protected PreAnalysis doPreAnalyze(LogicalPlan plan) {
4343
List<TableInfo> indices = new ArrayList<>();
4444
List<Enrich> unresolvedEnriches = new ArrayList<>();
4545

46-
plan.forEachUp(EsqlUnresolvedRelation.class, p -> { indices.add(new TableInfo(p.table(), p.frozen())); });
46+
plan.forEachUp(UnresolvedRelation.class, p -> indices.add(new TableInfo(p.table(), p.frozen())));
4747
plan.forEachUp(Enrich.class, unresolvedEnriches::add);
4848

4949
// mark plan as preAnalyzed (if it were marked, there would be no analysis)

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/LogicalPlanBuilder.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
import org.elasticsearch.xpack.esql.plan.logical.Dissect;
4242
import org.elasticsearch.xpack.esql.plan.logical.Drop;
4343
import org.elasticsearch.xpack.esql.plan.logical.Enrich;
44-
import org.elasticsearch.xpack.esql.plan.logical.EsqlAggregate;
45-
import org.elasticsearch.xpack.esql.plan.logical.EsqlUnresolvedRelation;
4644
import org.elasticsearch.xpack.esql.plan.logical.Eval;
4745
import org.elasticsearch.xpack.esql.plan.logical.Explain;
4846
import org.elasticsearch.xpack.esql.plan.logical.Filter;
@@ -56,6 +54,7 @@
5654
import org.elasticsearch.xpack.esql.plan.logical.OrderBy;
5755
import org.elasticsearch.xpack.esql.plan.logical.Rename;
5856
import org.elasticsearch.xpack.esql.plan.logical.Row;
57+
import org.elasticsearch.xpack.esql.plan.logical.UnresolvedRelation;
5958
import org.elasticsearch.xpack.esql.plan.logical.meta.MetaFunctions;
6059
import org.elasticsearch.xpack.esql.plan.logical.show.ShowInfo;
6160

@@ -271,13 +270,20 @@ public LogicalPlan visitFromCommand(EsqlBaseParser.FromCommandContext ctx) {
271270
}
272271
}
273272
}
274-
return new EsqlUnresolvedRelation(source, table, Arrays.asList(metadataMap.values().toArray(Attribute[]::new)), IndexMode.STANDARD);
273+
return new UnresolvedRelation(
274+
source,
275+
table,
276+
false,
277+
List.of(metadataMap.values().toArray(Attribute[]::new)),
278+
IndexMode.STANDARD,
279+
null
280+
);
275281
}
276282

277283
@Override
278284
public PlanFactory visitStatsCommand(EsqlBaseParser.StatsCommandContext ctx) {
279285
final Stats stats = stats(source(ctx), ctx.grouping, ctx.stats);
280-
return input -> new EsqlAggregate(source(ctx), input, Aggregate.AggregateType.STANDARD, stats.groupings, stats.aggregates);
286+
return input -> new Aggregate(source(ctx), input, Aggregate.AggregateType.STANDARD, stats.groupings, stats.aggregates);
281287
}
282288

283289
private record Stats(List<Expression> groupings, List<? extends NamedExpression> aggregates) {
@@ -459,16 +465,18 @@ public LogicalPlan visitMetricsCommand(EsqlBaseParser.MetricsCommandContext ctx)
459465
TableIdentifier table = new TableIdentifier(source, null, visitIndexPattern(ctx.indexPattern()));
460466

461467
if (ctx.aggregates == null && ctx.grouping == null) {
462-
return new EsqlUnresolvedRelation(source, table, List.of(), IndexMode.STANDARD);
468+
return new UnresolvedRelation(source, table, false, List.of(), IndexMode.STANDARD, null);
463469
}
464470
final Stats stats = stats(source, ctx.grouping, ctx.aggregates);
465-
var relation = new EsqlUnresolvedRelation(
471+
var relation = new UnresolvedRelation(
466472
source,
467473
table,
474+
false,
468475
List.of(new MetadataAttribute(source, MetadataAttribute.TSID_FIELD, DataType.KEYWORD, false)),
469-
IndexMode.TIME_SERIES
476+
IndexMode.TIME_SERIES,
477+
null
470478
);
471-
return new EsqlAggregate(source, relation, Aggregate.AggregateType.METRICS, stats.groupings, stats.aggregates);
479+
return new Aggregate(source, relation, Aggregate.AggregateType.METRICS, stats.groupings, stats.aggregates);
472480
}
473481

474482
@Override

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
import java.util.List;
2424
import java.util.Objects;
2525

26+
import static java.util.Collections.emptyList;
27+
import static org.elasticsearch.xpack.esql.expression.NamedExpressions.mergeOutputAttributes;
28+
2629
public class Aggregate extends UnaryPlan {
30+
private List<Attribute> lazyOutput;
2731

2832
public enum AggregateType {
2933
STANDARD,
@@ -111,7 +115,10 @@ public boolean expressionsResolved() {
111115

112116
@Override
113117
public List<Attribute> output() {
114-
return Expressions.asAttributes(aggregates);
118+
if (lazyOutput == null) {
119+
lazyOutput = mergeOutputAttributes(Expressions.asAttributes(aggregates()), emptyList());
120+
}
121+
return lazyOutput;
115122
}
116123

117124
@Override

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

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

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

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

0 commit comments

Comments
 (0)