-
Notifications
You must be signed in to change notification settings - Fork 25.2k
ESQL: introduce a pre-mapping logical plan processing step #121260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 16 commits
b09553a
cbddbc6
baec4ee
b17f6e6
8b5d4a8
8fbd0fc
62a5862
6040f3a
865efe5
aebbccf
39954c6
77518cd
e08fd4d
155acc7
1df65e6
affc2e4
04ef37d
d9a6807
fde7ea7
adada00
da8849c
da56299
9993e4a
49071e0
7804f28
26a16c7
4d8fd09
d3ac9d8
2919025
2008096
daddb62
1fac8fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.esql.expression.function.fulltext; | ||
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.ResolvedIndices; | ||
import org.elasticsearch.index.query.QueryBuilder; | ||
import org.elasticsearch.index.query.QueryRewriteContext; | ||
import org.elasticsearch.index.query.Rewriteable; | ||
import org.elasticsearch.xpack.esql.core.util.Holder; | ||
import org.elasticsearch.xpack.esql.plan.logical.EsRelation; | ||
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan; | ||
import org.elasticsearch.xpack.esql.planner.TranslatorHandler; | ||
import org.elasticsearch.xpack.esql.plugin.TransportActionServices; | ||
import org.elasticsearch.xpack.esql.session.IndexResolver; | ||
|
||
import java.io.IOException; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* Some {@link FullTextFunction} implementations such as {@link org.elasticsearch.xpack.esql.expression.function.fulltext.Match} | ||
* will be translated to a {@link QueryBuilder} that require a rewrite phase on the coordinator. | ||
* {@link QueryBuilderResolver#resolveQueryBuilders(LogicalPlan, TransportActionServices, ActionListener)} will rewrite the plan by | ||
* replacing {@link FullTextFunction} expression with new ones that hold rewritten {@link QueryBuilder}s. | ||
*/ | ||
public final class QueryBuilderResolver { | ||
|
||
public static final QueryBuilderResolver INSTANCE = new QueryBuilderResolver(); | ||
bpintea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private QueryBuilderResolver() {} | ||
|
||
public void resolveQueryBuilders(LogicalPlan plan, TransportActionServices services, ActionListener<LogicalPlan> listener) { | ||
var hasFullTextFunctions = plan.anyMatch(p -> { | ||
Holder<Boolean> hasFullTextFunction = new Holder<>(false); | ||
p.forEachExpression(FullTextFunction.class, unused -> hasFullTextFunction.set(true)); | ||
return hasFullTextFunction.get(); | ||
}); | ||
if (hasFullTextFunctions) { | ||
Rewriteable.rewriteAndFetch( | ||
new FullTextFunctionsRewritable(plan), | ||
queryRewriteContext(services, indexNames(plan)), | ||
listener.delegateFailureAndWrap((l, r) -> l.onResponse(r.plan)) | ||
); | ||
} else { | ||
listener.onResponse(plan); | ||
} | ||
} | ||
|
||
private static QueryRewriteContext queryRewriteContext(TransportActionServices services, Set<String> indexNames) { | ||
ResolvedIndices resolvedIndices = ResolvedIndices.resolveWithIndexNamesAndOptions( | ||
indexNames.toArray(String[]::new), | ||
IndexResolver.FIELD_CAPS_INDICES_OPTIONS, | ||
services.clusterService().state(), | ||
services.indexNameExpressionResolver(), | ||
services.transportService().getRemoteClusterService(), | ||
System.currentTimeMillis() | ||
); | ||
|
||
return services.searchService().getRewriteContext(System::currentTimeMillis, resolvedIndices, null); | ||
} | ||
|
||
public Set<String> indexNames(LogicalPlan plan) { | ||
bpintea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Set<String> indexNames = new HashSet<>(); | ||
plan.forEachDown(EsRelation.class, esRelation -> indexNames.addAll(esRelation.concreteIndices())); | ||
return indexNames; | ||
} | ||
|
||
private record FullTextFunctionsRewritable(LogicalPlan plan) implements Rewriteable<QueryBuilderResolver.FullTextFunctionsRewritable> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A nice proposal, but I'd like to keep it separated, just to maintain the legibility of that method (which would otherwise grow quite a bit). |
||
@Override | ||
public FullTextFunctionsRewritable rewrite(QueryRewriteContext ctx) throws IOException { | ||
Holder<IOException> exceptionHolder = new Holder<>(); | ||
Holder<Boolean> updated = new Holder<>(false); | ||
LogicalPlan newPlan = plan.transformExpressionsDown(FullTextFunction.class, f -> { | ||
QueryBuilder builder = f.queryBuilder(), initial = builder; | ||
builder = builder == null ? f.asQuery(TranslatorHandler.TRANSLATOR_HANDLER).asBuilder() : builder; | ||
try { | ||
builder = builder.rewrite(ctx); | ||
} catch (IOException e) { | ||
exceptionHolder.trySet(e); | ||
} | ||
var rewritten = builder != initial; | ||
updated.set(updated.get() || rewritten); | ||
return rewritten ? f.replaceQueryBuilder(builder) : f; | ||
}); | ||
if (exceptionHolder.get() != null) { | ||
throw exceptionHolder.get(); | ||
} | ||
return updated.get() ? new FullTextFunctionsRewritable(newPlan) : this; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.esql.planner.premapper; | ||
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.xpack.esql.expression.function.fulltext.QueryBuilderResolver; | ||
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan; | ||
import org.elasticsearch.xpack.esql.plugin.TransportActionServices; | ||
|
||
/** | ||
* The class is responsible for invoking any premapping steps that need to be applied to the logical plan, | ||
* before this is being mapped to a physical one. | ||
*/ | ||
public class PreMapper { | ||
|
||
private final TransportActionServices services; | ||
|
||
public PreMapper(TransportActionServices services) { | ||
this.services = services; | ||
} | ||
|
||
/** | ||
* Invokes any premapping steps that need to be applied to the logical plan, before this is being mapped to a physical one. | ||
*/ | ||
public void preMapper(LogicalPlan plan, ActionListener<LogicalPlan> listener) { | ||
queryRewrite(plan, listener); | ||
} | ||
|
||
private void queryRewrite(LogicalPlan plan, ActionListener<LogicalPlan> listener) { | ||
QueryBuilderResolver.INSTANCE.resolveQueryBuilders(plan, services, listener); | ||
bpintea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.esql.plugin; | ||
|
||
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; | ||
import org.elasticsearch.cluster.service.ClusterService; | ||
import org.elasticsearch.compute.operator.exchange.ExchangeService; | ||
import org.elasticsearch.search.SearchService; | ||
import org.elasticsearch.transport.TransportService; | ||
import org.elasticsearch.usage.UsageService; | ||
|
||
public record TransportActionServices( | ||
TransportService transportService, | ||
SearchService searchService, | ||
ExchangeService exchangeService, | ||
ClusterService clusterService, | ||
IndexNameExpressionResolver indexNameExpressionResolver, | ||
UsageService usageService | ||
) {} |
Uh oh!
There was an error while loading. Please reload this page.