Skip to content

Commit 4e0aaa7

Browse files
authored
[8.19] Make rule initialization static (#125590) (#128904)
1 parent 3a8da31 commit 4e0aaa7

File tree

6 files changed

+48
-87
lines changed

6 files changed

+48
-87
lines changed

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

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@
9393
import org.elasticsearch.xpack.esql.rule.ParameterizedRule;
9494
import org.elasticsearch.xpack.esql.rule.ParameterizedRuleExecutor;
9595
import org.elasticsearch.xpack.esql.rule.Rule;
96-
import org.elasticsearch.xpack.esql.rule.RuleExecutor;
9796
import org.elasticsearch.xpack.esql.session.Configuration;
9897
import org.elasticsearch.xpack.esql.telemetry.FeatureMetric;
9998
import org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter;
@@ -148,26 +147,17 @@ public class Analyzer extends ParameterizedRuleExecutor<LogicalPlan, AnalyzerCon
148147
public static final List<Attribute> NO_FIELDS = List.of(
149148
new ReferenceAttribute(Source.EMPTY, "<no-fields>", DataType.NULL, Nullability.TRUE, null, true)
150149
);
151-
private static final Iterable<RuleExecutor.Batch<LogicalPlan>> rules;
152-
153-
static {
154-
var init = new Batch<>(
155-
"Initialize",
156-
Limiter.ONCE,
157-
new ResolveTable(),
158-
new ResolveEnrich(),
159-
new ResolveLookupTables(),
160-
new ResolveFunctions()
161-
);
162-
var resolution = new Batch<>(
150+
151+
private static final List<Batch<LogicalPlan>> RULES = List.of(
152+
new Batch<>("Initialize", Limiter.ONCE, new ResolveTable(), new ResolveEnrich(), new ResolveLookupTables(), new ResolveFunctions()),
153+
new Batch<>(
163154
"Resolution",
164155
new ResolveRefs(),
165156
new ImplicitCasting(),
166157
new ResolveUnionTypes() // Must be after ResolveRefs, so union types can be found
167-
);
168-
var finish = new Batch<>("Finish Analysis", Limiter.ONCE, new AddImplicitLimit(), new UnionTypesCleanup());
169-
rules = List.of(init, resolution, finish);
170-
}
158+
),
159+
new Batch<>("Finish Analysis", Limiter.ONCE, new AddImplicitLimit(), new UnionTypesCleanup())
160+
);
171161

172162
private final Verifier verifier;
173163

@@ -190,8 +180,8 @@ public LogicalPlan verify(LogicalPlan plan, BitSet partialMetrics) {
190180
}
191181

192182
@Override
193-
protected Iterable<RuleExecutor.Batch<LogicalPlan>> batches() {
194-
return rules;
183+
protected List<Batch<LogicalPlan>> batches() {
184+
return RULES;
195185
}
196186

197187
private static class ResolveTable extends ParameterizedAnalyzerRule<UnresolvedRelation, AnalyzerContext> {

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import java.util.ArrayList;
2222
import java.util.List;
2323

24-
import static java.util.Arrays.asList;
24+
import static org.elasticsearch.common.util.CollectionUtils.arrayAsArrayList;
2525
import static org.elasticsearch.xpack.esql.optimizer.LogicalPlanOptimizer.cleanup;
2626
import static org.elasticsearch.xpack.esql.optimizer.LogicalPlanOptimizer.operators;
2727

@@ -33,30 +33,32 @@
3333
*/
3434
public class LocalLogicalPlanOptimizer extends ParameterizedRuleExecutor<LogicalPlan, LocalLogicalOptimizerContext> {
3535

36+
private static final List<Batch<LogicalPlan>> RULES = replaceRules(
37+
arrayAsArrayList(
38+
new Batch<>(
39+
"Local rewrite",
40+
Limiter.ONCE,
41+
new ReplaceTopNWithLimitAndSort(),
42+
new ReplaceFieldWithConstantOrNull(),
43+
new InferIsNotNull(),
44+
new InferNonNullAggConstraint()
45+
),
46+
operators(),
47+
cleanup()
48+
)
49+
);
50+
3651
public LocalLogicalPlanOptimizer(LocalLogicalOptimizerContext localLogicalOptimizerContext) {
3752
super(localLogicalOptimizerContext);
3853
}
3954

4055
@Override
4156
protected List<Batch<LogicalPlan>> batches() {
42-
var local = new Batch<>(
43-
"Local rewrite",
44-
Limiter.ONCE,
45-
new ReplaceTopNWithLimitAndSort(),
46-
new ReplaceFieldWithConstantOrNull(),
47-
new InferIsNotNull(),
48-
new InferNonNullAggConstraint()
49-
);
50-
51-
var rules = new ArrayList<Batch<LogicalPlan>>();
52-
rules.add(local);
53-
// TODO: if the local rules haven't touched the tree, the rest of the rules can be skipped
54-
rules.addAll(asList(operators(), cleanup()));
55-
return replaceRules(rules);
57+
return RULES;
5658
}
5759

5860
@SuppressWarnings("unchecked")
59-
private List<Batch<LogicalPlan>> replaceRules(List<Batch<LogicalPlan>> listOfRules) {
61+
private static List<Batch<LogicalPlan>> replaceRules(List<Batch<LogicalPlan>> listOfRules) {
6062
List<Batch<LogicalPlan>> newBatches = new ArrayList<>(listOfRules.size());
6163
for (var batch : listOfRules) {
6264
var rules = batch.rules();

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
import java.util.Collection;
2727
import java.util.List;
2828

29-
import static java.util.Arrays.asList;
30-
3129
/**
3230
* Manages field extraction and pushing parts of the query into Lucene. (Query elements that are not pushed into Lucene are executed via
3331
* the compute engine)
3432
*/
3533
public class LocalPhysicalPlanOptimizer extends ParameterizedRuleExecutor<PhysicalPlan, LocalPhysicalOptimizerContext> {
3634

35+
private static final List<Batch<PhysicalPlan>> RULES = rules(true);
36+
3737
private final PhysicalVerifier verifier = PhysicalVerifier.INSTANCE;
3838

3939
public LocalPhysicalPlanOptimizer(LocalPhysicalOptimizerContext context) {
@@ -54,13 +54,12 @@ PhysicalPlan verify(PhysicalPlan plan) {
5454

5555
@Override
5656
protected List<Batch<PhysicalPlan>> batches() {
57-
return rules(true);
57+
return RULES;
5858
}
5959

60-
protected List<Batch<PhysicalPlan>> rules(boolean optimizeForEsSource) {
60+
protected static List<Batch<PhysicalPlan>> rules(boolean optimizeForEsSource) {
6161
List<Rule<?, PhysicalPlan>> esSourceRules = new ArrayList<>(6);
6262
esSourceRules.add(new ReplaceSourceAttributes());
63-
6463
if (optimizeForEsSource) {
6564
esSourceRules.add(new PushTopNToSource());
6665
esSourceRules.add(new PushLimitToSource());
@@ -81,7 +80,6 @@ protected List<Batch<PhysicalPlan>> rules(boolean optimizeForEsSource) {
8180
new SpatialDocValuesExtraction(),
8281
new SpatialShapeBoundsExtraction()
8382
);
84-
return asList(pushdown, fieldExtraction);
83+
return List.of(pushdown, fieldExtraction);
8584
}
86-
8785
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,10 @@
6363
import org.elasticsearch.xpack.esql.optimizer.rules.logical.local.PruneLeftJoinOnNullMatchingField;
6464
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan;
6565
import org.elasticsearch.xpack.esql.rule.ParameterizedRuleExecutor;
66+
import org.elasticsearch.xpack.esql.rule.RuleExecutor;
6667

6768
import java.util.List;
6869

69-
import static java.util.Arrays.asList;
70-
7170
/**
7271
* <p>This class is part of the planner</p>
7372
* <p>Global optimizations based strictly on the structure of the query (i.e. not factoring in information about the backing indices).
@@ -92,6 +91,14 @@
9291
*/
9392
public class LogicalPlanOptimizer extends ParameterizedRuleExecutor<LogicalPlan, LogicalOptimizerContext> {
9493

94+
private static final List<RuleExecutor.Batch<LogicalPlan>> RULES = List.of(
95+
substitutions(),
96+
operators(),
97+
new Batch<>("Skip Compute", new SkipQueryOnLimitZero()),
98+
cleanup(),
99+
new Batch<>("Set as Optimized", Limiter.ONCE, new SetAsOptimized())
100+
);
101+
95102
private final LogicalVerifier verifier = LogicalVerifier.INSTANCE;
96103

97104
public LogicalPlanOptimizer(LogicalOptimizerContext optimizerContext) {
@@ -111,14 +118,7 @@ public LogicalPlan optimize(LogicalPlan verified) {
111118

112119
@Override
113120
protected List<Batch<LogicalPlan>> batches() {
114-
return rules();
115-
}
116-
117-
protected static List<Batch<LogicalPlan>> rules() {
118-
var skip = new Batch<>("Skip Compute", new SkipQueryOnLimitZero());
119-
var label = new Batch<>("Set as Optimized", Limiter.ONCE, new SetAsOptimized());
120-
121-
return asList(substitutions(), operators(), skip, cleanup(), label);
121+
return RULES;
122122
}
123123

124124
protected static Batch<LogicalPlan> substitutions() {

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818
import java.util.Collection;
1919
import java.util.List;
2020

21-
import static java.util.Arrays.asList;
22-
2321
/**
2422
* This class is part of the planner. Performs global (coordinator) optimization of the physical plan. Local (data-node) optimizations
2523
* occur later by operating just on a plan {@link FragmentExec} (subplan).
2624
*/
2725
public class PhysicalPlanOptimizer extends ParameterizedRuleExecutor<PhysicalPlan, PhysicalOptimizerContext> {
28-
private static final Iterable<RuleExecutor.Batch<PhysicalPlan>> rules = initializeRules(true);
26+
27+
private static final List<RuleExecutor.Batch<PhysicalPlan>> RULES = List.of(
28+
new Batch<>("Plan Boundary", Limiter.ONCE, new ProjectAwayColumns())
29+
);
2930

3031
private final PhysicalVerifier verifier = PhysicalVerifier.INSTANCE;
3132

@@ -45,13 +46,8 @@ PhysicalPlan verify(PhysicalPlan plan) {
4546
return plan;
4647
}
4748

48-
static List<RuleExecutor.Batch<PhysicalPlan>> initializeRules(boolean isOptimizedForEsSource) {
49-
var boundary = new Batch<>("Plan Boundary", Limiter.ONCE, new ProjectAwayColumns());
50-
return asList(boundary);
51-
}
52-
5349
@Override
54-
protected Iterable<RuleExecutor.Batch<PhysicalPlan>> batches() {
55-
return rules;
50+
protected List<RuleExecutor.Batch<PhysicalPlan>> batches() {
51+
return RULES;
5652
}
5753
}

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/optimizer/TestPhysicalPlanOptimizer.java

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

0 commit comments

Comments
 (0)