Skip to content

Commit db554d6

Browse files
committed
Avoid sorted source for time_series aggs without rates
1 parent c662590 commit db554d6

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
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: 11 additions & 4 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;
@@ -120,7 +122,7 @@
120122
*
121123
* becomes
122124
*
123-
* TS k8s
125+
* FROM k8s
124126
* | STATS max_memory_usage = max(memory_usage), host_values=VALUES(host) BY _tsid, time_bucket=bucket(@timestamp, 1minute)
125127
* | STATS sum(max_memory_usage) BY host_values, time_bucket
126128
*
@@ -129,7 +131,7 @@
129131
*
130132
* becomes
131133
*
132-
* TS k8s
134+
* FROM k8s
133135
* | STATS avg_memory_usage = avg(memory_usage), host_values=VALUES(host) BY _tsid, time_bucket=bucket(@timestamp, 1minute)
134136
* | STATS sum(avg_memory_usage) BY host_values, time_bucket
135137
*
@@ -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(

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/optimizer/LogicalPlanOptimizerTests.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6829,7 +6829,8 @@ public void testTranslateMixedAggsWithMathWithoutGrouping() {
68296829
Eval addEval = as(aggsByTsid.child(), Eval.class);
68306830
assertThat(addEval.fields(), hasSize(1));
68316831
Add add = as(Alias.unwrap(addEval.fields().get(0)), Add.class);
6832-
as(addEval.child(), EsRelation.class);
6832+
EsRelation relation = as(addEval.child(), EsRelation.class);
6833+
assertThat(relation.indexMode(), equalTo(IndexMode.TIME_SERIES));
68336834

68346835
assertThat(Expressions.attribute(mul.left()).id(), equalTo(finalAggs.aggregates().get(1).id()));
68356836
assertThat(mul.right().fold(FoldContext.small()), equalTo(1.1));
@@ -6859,7 +6860,8 @@ public void testTranslateMetricsGroupedByOneDimension() {
68596860
TimeSeriesAggregate aggsByTsid = as(aggsByCluster.child(), TimeSeriesAggregate.class);
68606861
assertThat(aggsByTsid.aggregates(), hasSize(2)); // _tsid is dropped
68616862
assertNull(aggsByTsid.timeBucket());
6862-
as(aggsByTsid.child(), EsRelation.class);
6863+
EsRelation relation = as(aggsByTsid.child(), EsRelation.class);
6864+
assertThat(relation.indexMode(), equalTo(IndexMode.TIME_SERIES));
68636865

68646866
Sum sum = as(Alias.unwrap(aggsByCluster.aggregates().get(0)), Sum.class);
68656867
assertThat(Expressions.attribute(sum.field()).id(), equalTo(aggsByTsid.aggregates().get(0).id()));
@@ -6886,7 +6888,8 @@ public void testTranslateMetricsGroupedByTwoDimension() {
68866888
TimeSeriesAggregate aggsByTsid = as(finalAggs.child(), TimeSeriesAggregate.class);
68876889
assertThat(aggsByTsid.aggregates(), hasSize(3)); // _tsid is dropped
68886890
assertNull(aggsByTsid.timeBucket());
6889-
as(aggsByTsid.child(), EsRelation.class);
6891+
EsRelation relation = as(aggsByTsid.child(), EsRelation.class);
6892+
assertThat(relation.indexMode(), equalTo(IndexMode.TIME_SERIES));
68906893

68916894
Div div = as(Alias.unwrap(eval.fields().get(0)), Div.class);
68926895
assertThat(Expressions.attribute(div.left()).id(), equalTo(finalAggs.aggregates().get(0).id()));
@@ -6925,7 +6928,8 @@ public void testTranslateMetricsGroupedByTimeBucket() {
69256928
assertThat(aggsByTsid.timeBucket().buckets().fold(FoldContext.small()), equalTo(Duration.ofHours(1)));
69266929
Eval eval = as(aggsByTsid.child(), Eval.class);
69276930
assertThat(eval.fields(), hasSize(1));
6928-
as(eval.child(), EsRelation.class);
6931+
EsRelation relation = as(eval.child(), EsRelation.class);
6932+
assertThat(relation.indexMode(), equalTo(IndexMode.TIME_SERIES));
69296933

69306934
Sum sum = as(Alias.unwrap(finalAgg.aggregates().get(0)), Sum.class);
69316935
assertThat(Expressions.attribute(sum.field()).id(), equalTo(aggsByTsid.aggregates().get(0).id()));
@@ -6959,7 +6963,8 @@ public void testTranslateMetricsGroupedByTimeBucketAndDimensions() {
69596963
assertNotNull(aggsByTsid.timeBucket());
69606964
assertThat(aggsByTsid.timeBucket().buckets().fold(FoldContext.small()), equalTo(Duration.ofMinutes(5)));
69616965
Eval bucket = as(aggsByTsid.child(), Eval.class);
6962-
as(bucket.child(), EsRelation.class);
6966+
EsRelation relation = as(bucket.child(), EsRelation.class);
6967+
assertThat(relation.indexMode(), equalTo(IndexMode.TIME_SERIES));
69636968
assertThat(Expressions.attribute(div.left()).id(), equalTo(finalAgg.aggregates().get(0).id()));
69646969
assertThat(Expressions.attribute(div.right()).id(), equalTo(finalAgg.aggregates().get(1).id()));
69656970

@@ -7000,7 +7005,8 @@ public void testTranslateMixedAggsGroupedByTimeBucketAndDimensions() {
70007005
assertNotNull(aggsByTsid.timeBucket());
70017006
assertThat(aggsByTsid.timeBucket().buckets().fold(FoldContext.small()), equalTo(Duration.ofMinutes(5)));
70027007
Eval bucket = as(aggsByTsid.child(), Eval.class);
7003-
as(bucket.child(), EsRelation.class);
7008+
EsRelation relation = as(bucket.child(), EsRelation.class);
7009+
assertThat(relation.indexMode(), equalTo(IndexMode.TIME_SERIES));
70047010
assertThat(Expressions.attribute(div.left()).id(), equalTo(finalAgg.aggregates().get(0).id()));
70057011
assertThat(Expressions.attribute(div.right()).id(), equalTo(finalAgg.aggregates().get(1).id()));
70067012

@@ -7064,7 +7070,8 @@ public void testAdjustMetricsRateBeforeFinalAgg() {
70647070
Eval evalBucket = as(aggsByTsid.child(), Eval.class);
70657071
assertThat(evalBucket.fields(), hasSize(1));
70667072
Bucket bucket = as(Alias.unwrap(evalBucket.fields().get(0)), Bucket.class);
7067-
as(evalBucket.child(), EsRelation.class);
7073+
EsRelation relation = as(evalBucket.child(), EsRelation.class);
7074+
assertThat(relation.indexMode(), equalTo(IndexMode.TIME_SERIES));
70687075

70697076
assertThat(Expressions.attribute(div.left()).id(), equalTo(finalAgg.aggregates().get(0).id()));
70707077
assertThat(Expressions.attribute(div.right()).id(), equalTo(finalAgg.aggregates().get(1).id()));
@@ -7102,7 +7109,8 @@ public void testTranslateMaxOverTime() {
71027109
assertThat(aggsByTsid.timeBucket().buckets().fold(FoldContext.small()), equalTo(Duration.ofHours(1)));
71037110
Eval eval = as(aggsByTsid.child(), Eval.class);
71047111
assertThat(eval.fields(), hasSize(1));
7105-
as(eval.child(), EsRelation.class);
7112+
EsRelation relation = as(eval.child(), EsRelation.class);
7113+
assertThat(relation.indexMode(), equalTo(IndexMode.STANDARD));
71067114

71077115
Sum sum = as(Alias.unwrap(finalAgg.aggregates().get(0)), Sum.class);
71087116
assertThat(Expressions.attribute(sum.field()).id(), equalTo(aggsByTsid.aggregates().get(0).id()));
@@ -7131,7 +7139,8 @@ public void testTranslateAvgOverTime() {
71317139
assertThat(aggsByTsid.timeBucket().buckets().fold(FoldContext.small()), equalTo(Duration.ofHours(1)));
71327140
Eval evalBucket = as(aggsByTsid.child(), Eval.class);
71337141
assertThat(evalBucket.fields(), hasSize(1));
7134-
as(evalBucket.child(), EsRelation.class);
7142+
EsRelation relation = as(evalBucket.child(), EsRelation.class);
7143+
assertThat(relation.indexMode(), equalTo(IndexMode.STANDARD));
71357144

71367145
Sum sum = as(Alias.unwrap(finalAgg.aggregates().get(0)), Sum.class);
71377146
assertThat(Expressions.attribute(sum.field()).id(), equalTo(evalAvg.fields().get(0).id()));

0 commit comments

Comments
 (0)