Skip to content

Commit b114209

Browse files
committed
Add PreOptimizer infrastructure for async pre-optimization steps
1 parent 89f96e4 commit b114209

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.optimizer;
9+
10+
import org.elasticsearch.action.ActionListener;
11+
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan;
12+
13+
/**
14+
* The class is responsible for invoking any steps that need to be applied to the logical plan,
15+
* before this is being optimized.
16+
* <p>
17+
* This is useful, especially if you need to execute some async tasks before the plan is optimized.
18+
* </p>
19+
*/
20+
public class PreOptimizer {
21+
22+
public PreOptimizer() {
23+
24+
}
25+
26+
public void preOptimize(LogicalPlan plan, ActionListener<LogicalPlan> listener) {
27+
listener.onResponse(plan);
28+
}
29+
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/session/EsqlSession.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import org.elasticsearch.xpack.esql.optimizer.LogicalPlanOptimizer;
6767
import org.elasticsearch.xpack.esql.optimizer.PhysicalOptimizerContext;
6868
import org.elasticsearch.xpack.esql.optimizer.PhysicalPlanOptimizer;
69+
import org.elasticsearch.xpack.esql.optimizer.PreOptimizer;
6970
import org.elasticsearch.xpack.esql.parser.EsqlParser;
7071
import org.elasticsearch.xpack.esql.parser.QueryParams;
7172
import org.elasticsearch.xpack.esql.plan.IndexPattern;
@@ -138,6 +139,7 @@ public interface PlanRunner {
138139
private final EnrichPolicyResolver enrichPolicyResolver;
139140

140141
private final PreAnalyzer preAnalyzer;
142+
private final PreOptimizer preOptimizer;
141143
private final Verifier verifier;
142144
private final EsqlFunctionRegistry functionRegistry;
143145
private final LogicalPlanOptimizer logicalPlanOptimizer;
@@ -183,6 +185,7 @@ public EsqlSession(
183185
this.planTelemetry = planTelemetry;
184186
this.indicesExpressionGrouper = indicesExpressionGrouper;
185187
this.inferenceResolver = inferenceResolver;
188+
this.preOptimizer = new PreOptimizer();
186189
this.preMapper = new PreMapper(services);
187190
this.remoteClusterService = services.transportService().getRemoteClusterService();
188191
}
@@ -206,10 +209,10 @@ public void execute(EsqlQueryRequest request, EsqlExecutionInfo executionInfo, P
206209
analyzedPlan(parsed, executionInfo, request.filter(), new EsqlCCSUtils.CssPartialErrorsActionListener(executionInfo, listener) {
207210
@Override
208211
public void onResponse(LogicalPlan analyzedPlan) {
209-
preMapper.preMapper(
210-
analyzedPlan,
211-
listener.delegateFailureAndWrap((l, p) -> executeOptimizedPlan(request, executionInfo, planRunner, optimizedPlan(p), l))
212-
);
212+
SubscribableListener.<LogicalPlan>newForked(l -> preOptimizer.preOptimize(analyzedPlan, l))
213+
.<LogicalPlan>andThen((l, p) -> preMapper.preMapper(optimizedPlan(p), l))
214+
.<Result>andThen((l, p) -> executeOptimizedPlan(request, executionInfo, planRunner, p, l))
215+
.addListener(listener);
213216
}
214217
});
215218
}

0 commit comments

Comments
 (0)