-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Fork time-series source to allow field extractions #127375
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
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
37 changes: 37 additions & 0 deletions
37
...arch/xpack/esql/optimizer/rules/physical/local/PushFieldExtractionToTimeSeriesSource.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,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.optimizer.rules.physical.local; | ||
|
|
||
| import org.elasticsearch.index.IndexMode; | ||
| import org.elasticsearch.xpack.esql.optimizer.LocalPhysicalOptimizerContext; | ||
| import org.elasticsearch.xpack.esql.optimizer.PhysicalOptimizerRules; | ||
| import org.elasticsearch.xpack.esql.optimizer.rules.logical.OptimizerRules; | ||
| import org.elasticsearch.xpack.esql.plan.physical.EsQueryExec; | ||
| import org.elasticsearch.xpack.esql.plan.physical.PhysicalPlan; | ||
| import org.elasticsearch.xpack.esql.plan.physical.TimeSeriesSourceExec; | ||
|
|
||
| /** | ||
| * A rule that moves field extractions to occur before the time-series aggregation in the time-series source plan. | ||
| */ | ||
| public class PushFieldExtractionToTimeSeriesSource extends PhysicalOptimizerRules.ParameterizedOptimizerRule< | ||
| EsQueryExec, | ||
| LocalPhysicalOptimizerContext> { | ||
|
|
||
| public PushFieldExtractionToTimeSeriesSource() { | ||
| super(OptimizerRules.TransformDirection.UP); | ||
| } | ||
|
|
||
| @Override | ||
| public PhysicalPlan rule(EsQueryExec plan, LocalPhysicalOptimizerContext context) { | ||
| if (plan.indexMode() == IndexMode.TIME_SERIES) { | ||
| return new TimeSeriesSourceExec(plan.source(), plan.output(), plan.query(), plan.limit(), plan.estimatedRowSize()); | ||
| } else { | ||
| return plan; | ||
| } | ||
| } | ||
| } |
130 changes: 130 additions & 0 deletions
130
...n/esql/src/main/java/org/elasticsearch/xpack/esql/plan/physical/TimeSeriesSourceExec.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,130 @@ | ||
| /* | ||
| * 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.plan.physical; | ||
|
|
||
| import org.elasticsearch.common.Strings; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.index.query.QueryBuilder; | ||
| import org.elasticsearch.xpack.esql.core.expression.Attribute; | ||
| import org.elasticsearch.xpack.esql.core.expression.Expression; | ||
| import org.elasticsearch.xpack.esql.core.tree.NodeInfo; | ||
| import org.elasticsearch.xpack.esql.core.tree.NodeUtils; | ||
| import org.elasticsearch.xpack.esql.core.tree.Source; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Similar to {@link EsQueryExec}, but this is a physical plan specifically for time series indices. | ||
| * This plan is forked from {@link EsQueryExec} to allow field extractions, leveraging caching | ||
| * and avoiding the cost of sorting and rebuilding blocks. | ||
| */ | ||
| public class TimeSeriesSourceExec extends LeafExec implements EstimatesRowSize { | ||
| private final List<Attribute> attrs; | ||
| private final QueryBuilder query; | ||
| private final Expression limit; | ||
|
|
||
| /** | ||
| * Estimate of the number of bytes that'll be loaded per position before | ||
| * the stream of pages is consumed. | ||
| */ | ||
| private final Integer estimatedRowSize; | ||
|
|
||
| public TimeSeriesSourceExec(Source source, List<Attribute> attrs, QueryBuilder query, Expression limit, Integer estimatedRowSize) { | ||
| super(source); | ||
| this.attrs = attrs; | ||
| this.query = query; | ||
| this.limit = limit; | ||
| this.estimatedRowSize = estimatedRowSize; | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(StreamOutput out) throws IOException { | ||
| throw new UnsupportedOperationException("local plan"); | ||
| } | ||
|
|
||
| @Override | ||
| public String getWriteableName() { | ||
| throw new UnsupportedOperationException("local plan"); | ||
| } | ||
|
|
||
| @Override | ||
| protected NodeInfo<TimeSeriesSourceExec> info() { | ||
| return NodeInfo.create(this, TimeSeriesSourceExec::new, attrs, query, limit, estimatedRowSize); | ||
| } | ||
|
|
||
| public QueryBuilder query() { | ||
| return query; | ||
| } | ||
|
|
||
| @Override | ||
| public List<Attribute> output() { | ||
| return attrs; | ||
| } | ||
|
|
||
| public List<Attribute> attrs() { | ||
| return attrs; | ||
| } | ||
|
|
||
| public Expression limit() { | ||
| return limit; | ||
| } | ||
|
|
||
| /** | ||
| * Estimate of the number of bytes that'll be loaded per position before | ||
| * the stream of pages is consumed. | ||
| */ | ||
| public Integer estimatedRowSize() { | ||
| return estimatedRowSize; | ||
| } | ||
|
|
||
| @Override | ||
| public PhysicalPlan estimateRowSize(State state) { | ||
| state.add(false, Integer.BYTES * 2); | ||
| state.add(false, 22); // tsid | ||
| state.add(false, 8); // timestamp | ||
| int size = state.consumeAllFields(false); | ||
| return Objects.equals(this.estimatedRowSize, size) ? this : new TimeSeriesSourceExec(source(), attrs, query, limit, size); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(attrs, query, limit, estimatedRowSize); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (this == obj) { | ||
| return true; | ||
| } | ||
|
|
||
| if (obj == null || getClass() != obj.getClass()) { | ||
| return false; | ||
| } | ||
|
|
||
| TimeSeriesSourceExec other = (TimeSeriesSourceExec) obj; | ||
| return Objects.equals(attrs, other.attrs) | ||
| && Objects.equals(query, other.query) | ||
| && Objects.equals(limit, other.limit) | ||
| && Objects.equals(estimatedRowSize, other.estimatedRowSize); | ||
| } | ||
|
|
||
| @Override | ||
| public String nodeString() { | ||
| return nodeName() | ||
| + "[" | ||
| + "query[" | ||
| + (query != null ? Strings.toString(query, false, true) : "") | ||
| + "] attributes: [" | ||
| + NodeUtils.limitedToString(attrs) | ||
| + "], estimatedRowSize[" | ||
| + estimatedRowSize | ||
| + "]"; | ||
| } | ||
| } |
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
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.
Super nit: this should be eventually move to a helper function to avoid duplication.