Skip to content

Commit ed97c40

Browse files
pabloemelasticsearchmachine
andauthored
Forbidding metrics used in aggregations for timeseries (#135223)
* Forbidding metrics used in aggregations for timeseries * comments * [CI] Update transport version definitions * fixup --------- Co-authored-by: elasticsearchmachine <[email protected]>
1 parent 8a75ecd commit ed97c40

File tree

4 files changed

+53
-21
lines changed

4 files changed

+53
-21
lines changed

x-pack/plugin/esql/qa/testFixtures/src/main/resources/k8s-timeseries-avg-over-time.csv-spec

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,13 @@ sum:double | pod:keyword | time_bucket:datetime
236236

237237
avg_over_time_nested_expression_in_grouping_with_alias
238238
required_capability: ts_command_v0
239-
TS k8s | STATS min = min(avg_over_time(network.bytes_in)) by rx = (network.eth0.rx + 2000), time_bucket = bucket(@timestamp, 1minute) | SORT rx desc, time_bucket | LIMIT 5;
240-
241-
min:double | rx:long | time_bucket:datetime
242-
557.0 | 3398 | 2024-05-10T00:21:00.000Z
243-
206.0 | 3398 | 2024-05-10T00:22:00.000Z
244-
557.0 | 3300 | 2024-05-10T00:21:00.000Z
245-
312.0 | 3262 | 2024-05-10T00:18:00.000Z
246-
312.0 | 3206 | 2024-05-10T00:18:00.000Z
239+
TS k8s | STATS min = min(avg_over_time(network.bytes_in)) by time_bucket = bucket(date_trunc(2 minutes, @timestamp), 5 minutes) | SORT time_bucket | LIMIT 5;
240+
241+
min:double | time_bucket:datetime
242+
382.0416666666667 | 2024-05-10T00:00:00.000Z
243+
382.0416666666667 | 2024-05-10T00:05:00.000Z
244+
382.0416666666667 | 2024-05-10T00:10:00.000Z
245+
382.0416666666667 | 2024-05-10T00:15:00.000Z
246+
382.0416666666667 | 2024-05-10T00:20:00.000Z
247+
247248
;

x-pack/plugin/esql/qa/testFixtures/src/main/resources/k8s-timeseries-sum-over-time.csv-spec

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,13 @@ sum:long | pod:keyword | time_bucket:datetime
220220

221221
sum_over_time_nested_expression_in_grouping_with_alias
222222
required_capability: ts_command_v0
223-
TS k8s | STATS min = min(sum_over_time(network.bytes_in)) by rx = (network.eth0.rx + 2000), time_bucket = bucket(@timestamp, 1minute) | SORT rx desc, time_bucket | LIMIT 10;
224-
225-
min:long | rx:long | time_bucket:datetime
226-
1114 | 3398 | 2024-05-10T00:21:00.000Z
227-
206 | 3398 | 2024-05-10T00:22:00.000Z
228-
1114 | 3300 | 2024-05-10T00:21:00.000Z
229-
936 | 3262 | 2024-05-10T00:18:00.000Z
230-
936 | 3206 | 2024-05-10T00:18:00.000Z
231-
936 | 3205 | 2024-05-10T00:18:00.000Z
232-
1583 | 3167 | 2024-05-10T00:17:00.000Z
233-
913 | 3141 | 2024-05-10T00:20:00.000Z
234-
72 | 3122 | 2024-05-10T00:22:00.000Z
235-
1583 | 3120 | 2024-05-10T00:17:00.000Z
223+
TS k8s | STATS min = min(sum_over_time(network.bytes_in)) by time_bucket = bucket(date_trunc(2 minutes, @timestamp), 5 minutes) | SORT time_bucket | LIMIT 10;
224+
225+
226+
min:long | time_bucket:datetime
227+
6160 | 2024-05-10T00:00:00.000Z
228+
6160 | 2024-05-10T00:05:00.000Z
229+
6160 | 2024-05-10T00:10:00.000Z
230+
6160 | 2024-05-10T00:15:00.000Z
231+
6160 | 2024-05-10T00:20:00.000Z
236232
;

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plan/logical/TimeSeriesAggregate.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.xpack.esql.common.Failures;
1414
import org.elasticsearch.xpack.esql.core.expression.Alias;
1515
import org.elasticsearch.xpack.esql.core.expression.Expression;
16+
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
1617
import org.elasticsearch.xpack.esql.core.expression.NamedExpression;
1718
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
1819
import org.elasticsearch.xpack.esql.core.tree.Source;
@@ -118,6 +119,19 @@ public boolean equals(Object obj) {
118119
@Override
119120
public void postAnalysisVerification(Failures failures) {
120121
super.postAnalysisVerification(failures);
122+
groupings().forEach(g -> g.forEachDown(e -> {
123+
if (e instanceof FieldAttribute fieldAttr && fieldAttr.isMetric()) {
124+
failures.add(
125+
fail(
126+
fieldAttr,
127+
"cannot group by a metric field [{}] in a time-series aggregation. "
128+
+ "If you want to group by a metric field, use the FROM "
129+
+ "command instead of the TS command.",
130+
fieldAttr.sourceText()
131+
)
132+
);
133+
}
134+
}));
121135
child().forEachDown(p -> {
122136
// reject `TS metrics | SORT BY ... | STATS ...`
123137
if (p instanceof OrderBy orderBy) {

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/analysis/VerifierTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2683,6 +2683,27 @@ public void testFuse() {
26832683
);
26842684
}
26852685

2686+
public void testNoMetricInStatsByClause() {
2687+
assertThat(
2688+
error("TS test | STATS avg(rate(network.bytes_in)) BY bucket(@timestamp, 1 minute), host, round(network.connections)", tsdb),
2689+
equalTo(
2690+
"1:90: cannot group by a metric field [network.connections] in a time-series aggregation. "
2691+
+ "If you want to group by a metric field, use the FROM command instead of the TS command."
2692+
)
2693+
);
2694+
assertThat(
2695+
error("TS test | STATS avg(rate(network.bytes_in)) BY bucket(@timestamp, 1 minute), host, network.bytes_in", tsdb),
2696+
equalTo("1:84: cannot group by on [counter_long] type for grouping [network.bytes_in]")
2697+
);
2698+
assertThat(
2699+
error("TS test | STATS avg(rate(network.bytes_in)) BY bucket(@timestamp, 1 minute), host, to_long(network.bytes_in)", tsdb),
2700+
equalTo(
2701+
"1:92: cannot group by a metric field [network.bytes_in] in a time-series aggregation. "
2702+
+ "If you want to group by a metric field, use the FROM command instead of the TS command."
2703+
)
2704+
);
2705+
}
2706+
26862707
public void testSortInTimeSeries() {
26872708
assertThat(
26882709
error("TS test | SORT host | STATS avg(last_over_time(network.connections))", tsdb),

0 commit comments

Comments
 (0)