Skip to content

Commit 2ce2fda

Browse files
authored
Make rule initialization static (#125590)
1 parent 611f5cb commit 2ce2fda

File tree

6 files changed

+48
-86
lines changed

6 files changed

+48
-86
lines changed

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

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@
9898
import org.elasticsearch.xpack.esql.rule.ParameterizedRule;
9999
import org.elasticsearch.xpack.esql.rule.ParameterizedRuleExecutor;
100100
import org.elasticsearch.xpack.esql.rule.Rule;
101-
import org.elasticsearch.xpack.esql.rule.RuleExecutor;
102101
import org.elasticsearch.xpack.esql.session.Configuration;
103102
import org.elasticsearch.xpack.esql.telemetry.FeatureMetric;
104103
import org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter;
@@ -153,19 +152,18 @@ public class Analyzer extends ParameterizedRuleExecutor<LogicalPlan, AnalyzerCon
153152
public static final List<Attribute> NO_FIELDS = List.of(
154153
new ReferenceAttribute(Source.EMPTY, "<no-fields>", DataType.NULL, Nullability.TRUE, null, true)
155154
);
156-
private static final Iterable<RuleExecutor.Batch<LogicalPlan>> rules;
157155

158-
static {
159-
var init = new Batch<>(
156+
private static final List<Batch<LogicalPlan>> RULES = List.of(
157+
new Batch<>(
160158
"Initialize",
161159
Limiter.ONCE,
162160
new ResolveTable(),
163161
new ResolveEnrich(),
164162
new ResolveLookupTables(),
165163
new ResolveFunctions(),
166164
new ResolveForkFunctions()
167-
);
168-
var resolution = new Batch<>(
165+
),
166+
new Batch<>(
169167
"Resolution",
170168
/*
171169
* ImplicitCasting must be before ResolveRefs. Because a reference is created for a Bucket in Aggregate's aggregates,
@@ -176,16 +174,9 @@ public class Analyzer extends ParameterizedRuleExecutor<LogicalPlan, AnalyzerCon
176174
new ImplicitForkCasting(),
177175
new ResolveRefs(),
178176
new ResolveUnionTypes() // Must be after ResolveRefs, so union types can be found
179-
);
180-
var finish = new Batch<>(
181-
"Finish Analysis",
182-
Limiter.ONCE,
183-
new AddImplicitLimit(),
184-
new AddImplicitForkLimit(),
185-
new UnionTypesCleanup()
186-
);
187-
rules = List.of(init, resolution, finish);
188-
}
177+
),
178+
new Batch<>("Finish Analysis", Limiter.ONCE, new AddImplicitLimit(), new AddImplicitForkLimit(), new UnionTypesCleanup())
179+
);
189180

190181
private final Verifier verifier;
191182

@@ -208,8 +199,8 @@ public LogicalPlan verify(LogicalPlan plan, BitSet partialMetrics) {
208199
}
209200

210201
@Override
211-
protected Iterable<RuleExecutor.Batch<LogicalPlan>> batches() {
212-
return rules;
202+
protected List<Batch<LogicalPlan>> batches() {
203+
return RULES;
213204
}
214205

215206
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 ReplaceMissingFieldWithNull(),
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 ReplaceMissingFieldWithNull(),
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
@@ -64,11 +64,10 @@
6464
import org.elasticsearch.xpack.esql.optimizer.rules.logical.TranslateMetricsAggregate;
6565
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan;
6666
import org.elasticsearch.xpack.esql.rule.ParameterizedRuleExecutor;
67+
import org.elasticsearch.xpack.esql.rule.RuleExecutor;
6768

6869
import java.util.List;
6970

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

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

98105
public LogicalPlanOptimizer(LogicalOptimizerContext optimizerContext) {
@@ -112,14 +119,7 @@ public LogicalPlan optimize(LogicalPlan verified) {
112119

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

125125
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)