|
7 | 7 |
|
8 | 8 | package org.elasticsearch.xpack.esql.optimizer.rules.logical; |
9 | 9 |
|
| 10 | +import org.elasticsearch.index.IndexMode; |
10 | 11 | import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException; |
11 | 12 | import org.elasticsearch.xpack.esql.core.expression.Alias; |
12 | 13 | import org.elasticsearch.xpack.esql.core.expression.Attribute; |
|
19 | 20 | import org.elasticsearch.xpack.esql.core.util.Holder; |
20 | 21 | import org.elasticsearch.xpack.esql.expression.function.aggregate.AggregateFunction; |
21 | 22 | import org.elasticsearch.xpack.esql.expression.function.aggregate.FromPartial; |
| 23 | +import org.elasticsearch.xpack.esql.expression.function.aggregate.Rate; |
22 | 24 | import org.elasticsearch.xpack.esql.expression.function.aggregate.TimeSeriesAggregateFunction; |
23 | 25 | import org.elasticsearch.xpack.esql.expression.function.aggregate.ToPartial; |
24 | 26 | import org.elasticsearch.xpack.esql.expression.function.aggregate.Values; |
@@ -154,11 +156,15 @@ LogicalPlan translate(TimeSeriesAggregate aggregate) { |
154 | 156 | Map<AggregateFunction, Alias> timeSeriesAggs = new HashMap<>(); |
155 | 157 | List<NamedExpression> firstPassAggs = new ArrayList<>(); |
156 | 158 | List<NamedExpression> secondPassAggs = new ArrayList<>(); |
| 159 | + Holder<Boolean> hasRateAggregates = new Holder<>(Boolean.FALSE); |
157 | 160 | for (NamedExpression agg : aggregate.aggregates()) { |
158 | 161 | if (agg instanceof Alias alias && alias.child() instanceof AggregateFunction af) { |
159 | 162 | Holder<Boolean> changed = new Holder<>(Boolean.FALSE); |
160 | 163 | Expression outerAgg = af.transformDown(TimeSeriesAggregateFunction.class, tsAgg -> { |
161 | 164 | changed.set(Boolean.TRUE); |
| 165 | + if (tsAgg instanceof Rate) { |
| 166 | + hasRateAggregates.set(Boolean.TRUE); |
| 167 | + } |
162 | 168 | AggregateFunction firstStageFn = tsAgg.perTimeSeriesAggregation(); |
163 | 169 | Alias newAgg = timeSeriesAggs.computeIfAbsent(firstStageFn, k -> { |
164 | 170 | Alias firstStageAlias = new Alias(tsAgg.source(), agg.name(), firstStageFn); |
@@ -231,16 +237,17 @@ LogicalPlan translate(TimeSeriesAggregate aggregate) { |
231 | 237 | secondPassGroupings.add(new Alias(g.source(), g.name(), newFinalGroup.toAttribute(), g.id())); |
232 | 238 | } |
233 | 239 | LogicalPlan newChild = aggregate.child().transformUp(EsRelation.class, r -> { |
| 240 | + IndexMode indexMode = hasRateAggregates.get() ? r.indexMode() : IndexMode.STANDARD; |
234 | 241 | if (r.output().contains(tsid.get()) == false) { |
235 | 242 | return new EsRelation( |
236 | 243 | r.source(), |
237 | 244 | r.indexPattern(), |
238 | | - r.indexMode(), |
| 245 | + indexMode, |
239 | 246 | r.indexNameWithModes(), |
240 | 247 | CollectionUtils.combine(r.output(), tsid.get()) |
241 | 248 | ); |
242 | 249 | } else { |
243 | | - return r; |
| 250 | + return new EsRelation(r.source(), r.indexPattern(), indexMode, r.indexNameWithModes(), r.output()); |
244 | 251 | } |
245 | 252 | }); |
246 | 253 | final var firstPhase = new TimeSeriesAggregate( |
|
0 commit comments