-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add max_over_time aggregation for TSDB #126498
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 4 commits
94eb026
ee4b00c
4695dd7
5e6ebfa
6706b4a
2e0ee80
3188c7d
4a7119c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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.expression.function.aggregate; | ||
|
|
||
| import org.elasticsearch.common.io.stream.NamedWriteableRegistry; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.xpack.esql.core.expression.Expression; | ||
| import org.elasticsearch.xpack.esql.core.expression.Literal; | ||
| import org.elasticsearch.xpack.esql.core.tree.NodeInfo; | ||
| import org.elasticsearch.xpack.esql.core.tree.Source; | ||
| import org.elasticsearch.xpack.esql.core.type.DataType; | ||
| import org.elasticsearch.xpack.esql.expression.function.FunctionInfo; | ||
| import org.elasticsearch.xpack.esql.expression.function.FunctionType; | ||
| import org.elasticsearch.xpack.esql.expression.function.Param; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
|
|
||
| import static java.util.Collections.emptyList; | ||
|
|
||
| /** | ||
| * Similar to {@link Max}, but it is used to calculate the maximum value over a time series of values from the given field. | ||
| */ | ||
| public class MaxOverTime extends TimeSeriesAggregateFunction { | ||
| public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry( | ||
| Expression.class, | ||
| "MaxOverTime", | ||
| MaxOverTime::new | ||
| ); | ||
|
|
||
| @FunctionInfo( | ||
| returnType = { "boolean", "double", "integer", "long", "date", "date_nanos", "ip", "keyword", "long", "version" }, | ||
| description = "The maximum over time value of a field.", | ||
| type = FunctionType.AGGREGATE | ||
| ) | ||
| public MaxOverTime( | ||
| Source source, | ||
| @Param( | ||
| name = "field", | ||
| type = { | ||
| "aggregate_metric_double", | ||
| "boolean", | ||
| "double", | ||
| "integer", | ||
| "long", | ||
| "date", | ||
| "date_nanos", | ||
| "ip", | ||
| "keyword", | ||
| "text", | ||
| "long", | ||
| "version" } | ||
| ) Expression field | ||
| ) { | ||
| this(source, field, Literal.TRUE); | ||
| } | ||
|
|
||
| public MaxOverTime(Source source, Expression field, Expression filter) { | ||
| super(source, field, filter, emptyList()); | ||
| } | ||
|
|
||
| private MaxOverTime(StreamInput in) throws IOException { | ||
| super(in); | ||
| } | ||
|
|
||
| @Override | ||
| public String getWriteableName() { | ||
| return ENTRY.name; | ||
| } | ||
|
|
||
| @Override | ||
| public MaxOverTime withFilter(Expression filter) { | ||
| return new MaxOverTime(source(), field(), filter); | ||
| } | ||
|
|
||
| @Override | ||
| protected NodeInfo<MaxOverTime> info() { | ||
| return NodeInfo.create(this, MaxOverTime::new, field(), filter()); | ||
| } | ||
|
|
||
| @Override | ||
| public MaxOverTime replaceChildren(List<Expression> newChildren) { | ||
| return new MaxOverTime(source(), newChildren.get(0), newChildren.get(1)); | ||
| } | ||
|
|
||
| @Override | ||
| protected TypeResolution resolveType() { | ||
| return perTimeSeriesAggregation().resolveType(); | ||
| } | ||
|
|
||
| @Override | ||
| public DataType dataType() { | ||
| return perTimeSeriesAggregation().dataType(); | ||
| } | ||
|
|
||
| @Override | ||
| public Max perTimeSeriesAggregation() { | ||
| return new Max(source(), field(), filter()); | ||
| } | ||
| } |
| 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.expression.function.aggregate; | ||
|
|
||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.xpack.esql.core.expression.Expression; | ||
| import org.elasticsearch.xpack.esql.core.tree.Source; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Extends {@link AggregateFunction} to support aggregation per time_series, | ||
| * such as {@link Rate} or {@link MaxOverTime}. | ||
| */ | ||
| public abstract class TimeSeriesAggregateFunction extends AggregateFunction { | ||
|
|
||
| protected TimeSeriesAggregateFunction(Source source, Expression field, Expression filter, List<? extends Expression> parameters) { | ||
| super(source, field, filter, parameters); | ||
| } | ||
|
|
||
| protected TimeSeriesAggregateFunction(StreamInput in) throws IOException { | ||
| super(in); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the aggregation function to be used in the first aggregation stage, | ||
| * which is grouped by `_tsid` (and `time_bucket`). | ||
| * | ||
| * @see org.elasticsearch.xpack.esql.optimizer.rules.logical.TranslateTimeSeriesAggregate | ||
| */ | ||
| public abstract AggregateFunction perTimeSeriesAggregation(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ | |
| import org.elasticsearch.xpack.esql.core.util.Holder; | ||
| import org.elasticsearch.xpack.esql.expression.function.aggregate.AggregateFunction; | ||
| import org.elasticsearch.xpack.esql.expression.function.aggregate.FromPartial; | ||
| import org.elasticsearch.xpack.esql.expression.function.aggregate.Rate; | ||
| import org.elasticsearch.xpack.esql.expression.function.aggregate.TimeSeriesAggregateFunction; | ||
| import org.elasticsearch.xpack.esql.expression.function.aggregate.ToPartial; | ||
| import org.elasticsearch.xpack.esql.expression.function.aggregate.Values; | ||
| import org.elasticsearch.xpack.esql.expression.function.grouping.Bucket; | ||
|
|
@@ -34,7 +34,7 @@ | |
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Rate aggregation is special because it must be computed per time series, regardless of the grouping keys. | ||
| * Time-series aggregation is special because it must be computed per time series, regardless of the grouping keys. | ||
| * The keys must be `_tsid` or a pair of `_tsid` and `time_bucket`. To support user-defined grouping keys, | ||
| * we first execute the rate aggregation using the time-series keys, then perform another aggregation with | ||
| * the resulting rate using the user-specific keys. | ||
|
|
@@ -113,6 +113,17 @@ | |
| * | STATS rate(request), $p1=to_partial(min(memory_used)), VALUES(pod) BY _tsid, `bucket(@timestamp, 5m)` | ||
| * | STATS sum(`rate(request)`), `min(memory_used)` = from_partial($p1, min($)) BY pod=`VALUES(pod)`, `bucket(@timestamp, 5m)` | ||
| * | KEEP `min(memory_used)`, `sum(rate(request))`, pod, `bucket(@timestamp, 5m)` | ||
| * | ||
| * _over_time time-series aggregation will be rewritten in the similar way | ||
|
||
| * | ||
| * TS k8s | STATS sum(max_over_time(memory_usage)) BY host, bucket(@timestamp, 1minute) | ||
| * | ||
| * becomes | ||
| * | ||
| * TS k8s | ||
| * | STATS max(memory_usage), VALUES(host) BY _tsid, bucket(@timestamp, 1minute) | ||
|
||
| * | STATS sum(`max(memory_usage)`) BY host=`VALUES(host)`, `bucket(@timestamp, 1minute)` | ||
| * | ||
| * </pre> | ||
| */ | ||
| public final class TranslateTimeSeriesAggregate extends OptimizerRules.OptimizerRule<Aggregate> { | ||
|
|
@@ -131,20 +142,21 @@ protected LogicalPlan rule(Aggregate aggregate) { | |
| } | ||
|
|
||
| LogicalPlan translate(TimeSeriesAggregate aggregate) { | ||
| Map<Rate, Alias> rateAggs = new HashMap<>(); | ||
| Map<AggregateFunction, Alias> timeSeriesAggs = new HashMap<>(); | ||
| List<NamedExpression> firstPassAggs = new ArrayList<>(); | ||
| List<NamedExpression> secondPassAggs = new ArrayList<>(); | ||
| for (NamedExpression agg : aggregate.aggregates()) { | ||
| if (agg instanceof Alias alias && alias.child() instanceof AggregateFunction af) { | ||
| Holder<Boolean> changed = new Holder<>(Boolean.FALSE); | ||
| Expression outerAgg = af.transformDown(Rate.class, rate -> { | ||
| Expression outerAgg = af.transformDown(TimeSeriesAggregateFunction.class, tsAgg -> { | ||
| changed.set(Boolean.TRUE); | ||
| Alias rateAgg = rateAggs.computeIfAbsent(rate, k -> { | ||
| Alias newRateAgg = new Alias(rate.source(), agg.name(), rate); | ||
| firstPassAggs.add(newRateAgg); | ||
| return newRateAgg; | ||
| AggregateFunction firstStageFn = tsAgg.perTimeSeriesAggregation(); | ||
| Alias newAgg = timeSeriesAggs.computeIfAbsent(firstStageFn, k -> { | ||
| Alias firstStageAlias = new Alias(tsAgg.source(), agg.name(), firstStageFn); | ||
| firstPassAggs.add(firstStageAlias); | ||
| return firstStageAlias; | ||
| }); | ||
| return rateAgg.toAttribute(); | ||
| return newAgg.toAttribute(); | ||
| }); | ||
| if (changed.get()) { | ||
| secondPassAggs.add(new Alias(alias.source(), alias.name(), outerAgg, agg.id())); | ||
|
|
@@ -156,7 +168,7 @@ LogicalPlan translate(TimeSeriesAggregate aggregate) { | |
| } | ||
| } | ||
| } | ||
| if (rateAggs.isEmpty()) { | ||
| if (timeSeriesAggs.isEmpty()) { | ||
| // no time-series aggregations, run a regular aggregation instead. | ||
| return new Aggregate(aggregate.source(), aggregate.child(), aggregate.groupings(), aggregate.aggregates()); | ||
| } | ||
|
|
||
This file was deleted.
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.
Nit: add explanatory comment as the rest of the entries.