-
Notifications
You must be signed in to change notification settings - Fork 25.3k
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
Changes from 1 commit
aa331a5
7e510ae
845c2d6
77af752
c034b2f
144f13d
164d788
558f5de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* A rule that pushes down field extractions to occur before filter/limit/topN in the time-series source plan. | ||
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. Don't we want filtering to happen before field extraction? Or combine them, at least? Maybe I misunderstood this comment.. 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. For the query 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. Correct, what if we have
I'd think we want to push down the filters on 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. I expanded the javadoc: 164d788 |
||
*/ | ||
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<>(); | ||
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. This is the main change in planning, where field extractions are pushed down to the time-series source |
||
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() | ||
); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.