Skip to content

Commit 3075cc5

Browse files
committed
Use unsorted source for time_series aggs without rates
1 parent 6335391 commit 3075cc5

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

server/src/main/java/org/elasticsearch/index/mapper/TimeSeriesIdFieldMapper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ public IndexFieldData.Builder fielddataBuilder(FieldDataContext fieldDataContext
132132
public Query termQuery(Object value, SearchExecutionContext context) {
133133
throw new IllegalArgumentException("[" + NAME + "] is not searchable");
134134
}
135+
136+
@Override
137+
public BlockLoader blockLoader(BlockLoaderContext blContext) {
138+
return new BlockDocValuesReader.BytesRefsFromOrdsBlockLoader(name());
139+
}
135140
}
136141

137142
private final boolean useDocValuesSkipper;

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/rules/logical/TranslateTimeSeriesAggregate.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package org.elasticsearch.xpack.esql.optimizer.rules.logical;
99

10+
import org.elasticsearch.index.IndexMode;
1011
import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException;
1112
import org.elasticsearch.xpack.esql.core.expression.Alias;
1213
import org.elasticsearch.xpack.esql.core.expression.Attribute;
@@ -19,6 +20,7 @@
1920
import org.elasticsearch.xpack.esql.core.util.Holder;
2021
import org.elasticsearch.xpack.esql.expression.function.aggregate.AggregateFunction;
2122
import org.elasticsearch.xpack.esql.expression.function.aggregate.FromPartial;
23+
import org.elasticsearch.xpack.esql.expression.function.aggregate.Rate;
2224
import org.elasticsearch.xpack.esql.expression.function.aggregate.TimeSeriesAggregateFunction;
2325
import org.elasticsearch.xpack.esql.expression.function.aggregate.ToPartial;
2426
import org.elasticsearch.xpack.esql.expression.function.aggregate.Values;
@@ -154,11 +156,15 @@ LogicalPlan translate(TimeSeriesAggregate aggregate) {
154156
Map<AggregateFunction, Alias> timeSeriesAggs = new HashMap<>();
155157
List<NamedExpression> firstPassAggs = new ArrayList<>();
156158
List<NamedExpression> secondPassAggs = new ArrayList<>();
159+
Holder<Boolean> hasRateAggregates = new Holder<>(Boolean.FALSE);
157160
for (NamedExpression agg : aggregate.aggregates()) {
158161
if (agg instanceof Alias alias && alias.child() instanceof AggregateFunction af) {
159162
Holder<Boolean> changed = new Holder<>(Boolean.FALSE);
160163
Expression outerAgg = af.transformDown(TimeSeriesAggregateFunction.class, tsAgg -> {
161164
changed.set(Boolean.TRUE);
165+
if (tsAgg instanceof Rate) {
166+
hasRateAggregates.set(Boolean.TRUE);
167+
}
162168
AggregateFunction firstStageFn = tsAgg.perTimeSeriesAggregation();
163169
Alias newAgg = timeSeriesAggs.computeIfAbsent(firstStageFn, k -> {
164170
Alias firstStageAlias = new Alias(tsAgg.source(), agg.name(), firstStageFn);
@@ -231,16 +237,17 @@ LogicalPlan translate(TimeSeriesAggregate aggregate) {
231237
secondPassGroupings.add(new Alias(g.source(), g.name(), newFinalGroup.toAttribute(), g.id()));
232238
}
233239
LogicalPlan newChild = aggregate.child().transformUp(EsRelation.class, r -> {
240+
IndexMode indexMode = hasRateAggregates.get() ? r.indexMode() : IndexMode.STANDARD;
234241
if (r.output().contains(tsid.get()) == false) {
235242
return new EsRelation(
236243
r.source(),
237244
r.indexPattern(),
238-
r.indexMode(),
245+
indexMode,
239246
r.indexNameWithModes(),
240247
CollectionUtils.combine(r.output(), tsid.get())
241248
);
242249
} else {
243-
return r;
250+
return new EsRelation(r.source(), r.indexPattern(), indexMode, r.indexNameWithModes(), r.output());
244251
}
245252
});
246253
final var firstPhase = new TimeSeriesAggregate(

0 commit comments

Comments
 (0)