-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Push down field extraction to time-series source #127445
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aa331a5
Push down field extraction to time-series source
dnhatn 7e510ae
naming
dnhatn 845c2d6
consistency
dnhatn 77af752
merge with stored-fields from loaders
dnhatn c034b2f
leftover
dnhatn 144f13d
oops
dnhatn 164d788
javadoc
dnhatn 558f5de
Merge remote-tracking branch 'elastic/main' into time-series-field-ex…
dnhatn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
348 changes: 275 additions & 73 deletions
348
...src/main/java/org/elasticsearch/compute/lucene/TimeSeriesSortedSourceOperatorFactory.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
.../xpack/esql/optimizer/rules/physical/local/PushDownFieldExtractionToTimeSeriesSource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* 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.optimizer.rules.physical.local; | ||
|
||
import org.elasticsearch.index.IndexMode; | ||
import org.elasticsearch.xpack.esql.core.expression.Attribute; | ||
import org.elasticsearch.xpack.esql.core.util.Holder; | ||
import org.elasticsearch.xpack.esql.optimizer.LocalPhysicalOptimizerContext; | ||
import org.elasticsearch.xpack.esql.optimizer.PhysicalOptimizerRules; | ||
import org.elasticsearch.xpack.esql.plan.physical.EsQueryExec; | ||
import org.elasticsearch.xpack.esql.plan.physical.FieldExtractExec; | ||
import org.elasticsearch.xpack.esql.plan.physical.FilterExec; | ||
import org.elasticsearch.xpack.esql.plan.physical.LimitExec; | ||
import org.elasticsearch.xpack.esql.plan.physical.PhysicalPlan; | ||
import org.elasticsearch.xpack.esql.plan.physical.TimeSeriesSourceExec; | ||
import org.elasticsearch.xpack.esql.plan.physical.TopNExec; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* An optimization rule that pushes down field extractions to occur at the lowest filter, limit, or topN in the time-series source plan. | ||
* For example: | ||
* `TS index | WHERE host = 'a' AND cluster = 'b' | STATS max(rate(counter)) BY host, bucket(1minute)` | ||
* In this query, the extraction of the `host` and `cluster` fields will be pushed down to the time-series source, | ||
* while the extraction of the `counter` field will occur later. In such cases, the `doc_ids` still need to be returned | ||
* for the later extraction. However, if the filter (`host = 'a' AND cluster = 'b'`) is pushed down to Lucene, all field extractions | ||
* (e.g., `host` and `counter`) will be pushed down to the time-series source, and `doc_ids` will not be returned. | ||
*/ | ||
public class PushDownFieldExtractionToTimeSeriesSource extends PhysicalOptimizerRules.ParameterizedOptimizerRule< | ||
PhysicalPlan, | ||
LocalPhysicalOptimizerContext> { | ||
|
||
@Override | ||
public PhysicalPlan rule(PhysicalPlan plan, LocalPhysicalOptimizerContext context) { | ||
if (plan.anyMatch(p -> p instanceof EsQueryExec q && q.indexMode() == IndexMode.TIME_SERIES) == false) { | ||
return plan; | ||
} | ||
final List<FieldExtractExec> pushDownExtracts = new ArrayList<>(); | ||
final Holder<Boolean> keepDocIds = new Holder<>(Boolean.FALSE); | ||
plan.forEachDown(p -> { | ||
if (p instanceof FieldExtractExec) { | ||
pushDownExtracts.add((FieldExtractExec) p); | ||
} else if (stopPushDownExtract(p)) { | ||
if (pushDownExtracts.isEmpty() == false) { | ||
keepDocIds.set(Boolean.TRUE); | ||
pushDownExtracts.clear(); | ||
} | ||
} | ||
}); | ||
final Holder<Boolean> aborted = new Holder<>(Boolean.FALSE); | ||
return plan.transformUp(PhysicalPlan.class, p -> { | ||
if (aborted.get()) { | ||
return p; | ||
} | ||
if (p instanceof EsQueryExec q && q.indexMode() == IndexMode.TIME_SERIES) { | ||
return addFieldExtract(context, q, keepDocIds.get(), pushDownExtracts); | ||
} | ||
if (stopPushDownExtract(p)) { | ||
aborted.set(Boolean.TRUE); | ||
return p; | ||
} | ||
if (p instanceof FieldExtractExec e) { | ||
return e.child(); | ||
} | ||
return p; | ||
}); | ||
} | ||
|
||
private static boolean stopPushDownExtract(PhysicalPlan p) { | ||
return p instanceof FilterExec || p instanceof TopNExec || p instanceof LimitExec; | ||
} | ||
|
||
private TimeSeriesSourceExec addFieldExtract( | ||
LocalPhysicalOptimizerContext context, | ||
EsQueryExec query, | ||
boolean keepDocAttribute, | ||
List<FieldExtractExec> extracts | ||
) { | ||
Set<Attribute> docValuesAttributes = new HashSet<>(); | ||
Set<Attribute> boundsAttributes = new HashSet<>(); | ||
List<Attribute> attributesToExtract = new ArrayList<>(); | ||
for (FieldExtractExec extract : extracts) { | ||
docValuesAttributes.addAll(extract.docValuesAttributes()); | ||
boundsAttributes.addAll(extract.boundsAttributes()); | ||
attributesToExtract.addAll(extract.attributesToExtract()); | ||
} | ||
List<Attribute> attrs = query.attrs(); | ||
if (keepDocAttribute == false) { | ||
attrs = attrs.stream().filter(a -> EsQueryExec.isSourceAttribute(a) == false).toList(); | ||
} | ||
return new TimeSeriesSourceExec( | ||
query.source(), | ||
attrs, | ||
query.query(), | ||
query.limit(), | ||
context.configuration().pragmas().fieldExtractPreference(), | ||
docValuesAttributes, | ||
boundsAttributes, | ||
attributesToExtract, | ||
query.estimatedRowSize() | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the main change in planning, where field extractions are pushed down to the time-series source