Skip to content

Conversation

dnhatn
Copy link
Member

@dnhatn dnhatn commented Sep 10, 2025

Add validations to reject the following queries until supported:

  1. Limit and sort cannot be used to alter the time-series source:

    • TS metrics | LIMIT ... | STATS ...
    • TS metrics | SORT BY ... | STATS ...
  2. Over-time aggregation without an outer aggregation (to be supported soon):

    • TS metrics | STATS rate(requests)
    • TS metrics | STATS last_over_time(requests)
  3. Reject lookup join, enrich, change point before the first stats.

Closes #134366
Closes #134372

@dnhatn dnhatn force-pushed the disallow-sorting-ts branch from 984cc32 to 5dcb2bf Compare September 10, 2025 05:34
@dnhatn dnhatn changed the title Disallow some commands in time-series More validation for time-series aggregations Sep 10, 2025
@dnhatn dnhatn added :StorageEngine/TSDB You know, for Metrics >non-issue labels Sep 10, 2025
@dnhatn dnhatn requested a review from kkrik-es September 10, 2025 05:41
@dnhatn dnhatn marked this pull request as ready for review September 10, 2025 05:41
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-storage-engine (Team:StorageEngine)

failures.add(
fail(
ts,
"over-time aggregate function [{}] can only be used with the TS command and inside another aggregate function",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (Expression g : groupings) {
boolean timeBucket = g.anyMatch(
c -> (c instanceof Bucket b && b.field().equals(timestamp.get())
|| (c instanceof TBucket tb && tb.field().equals(timestamp.get())))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for date_trunk too?

nested -> failures.add(
fail(
this,
"cannot use aggregate function [{}] inside over-time aggregation function [{}]",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: maybe replace over-time with time-series, here and below?

1:22: nested aggregations [avg(rate(network.bytes_in))] \
not allowed inside other aggregations [max(avg(rate(network.bytes_in)))]
line 1:12: cannot use aggregate function [avg(rate(network.bytes_in))] \
inside over-time aggregation function [rate(network.bytes_in)]"""));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This second error is somewhat confusing - avg is not used inside rate. Overtriggering?

1:23: limiting [LIMIT 10] the time-series source \
before the first aggregation [STATS avg(network.connections)] is not allowed; filter data with a WHERE command instead
line 1:11: sorting [SORT host] between the time-series source \
and the first aggregation [STATS avg(network.connections)] is not allowed"""));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a couple of examples with SORT and LIMIT after STATS to verify that the error doesn't overtrigger.

assertThat(
error("TS test | INLINESTATS v = avg(network.connections) | STATS max(v)", tsdb),
equalTo("1:11: InlineStats [INLINESTATS v = avg(network.connections)] in time-series is only allowed after an aggregation")
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a simple example with INLINESTATS after STATS - if that makes sense?

Copy link
Contributor

@kkrik-es kkrik-es left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was quick, thanks Nhat!

@dnhatn dnhatn enabled auto-merge (squash) September 18, 2025 05:12
@dnhatn dnhatn merged commit f82eec3 into elastic:main Sep 18, 2025
34 checks passed
@dnhatn dnhatn deleted the disallow-sorting-ts branch September 18, 2025 06:06
gmjehovich pushed a commit to gmjehovich/elasticsearch that referenced this pull request Sep 18, 2025
phananh1010 added a commit to phananh1010/elasticsearch that referenced this pull request Sep 23, 2025
BASE=3c3058e6f63c5ebdcc4513fc5dcc4ffb3ec8a752
HEAD=69b95e47c4d65a8c6903b6f532b08d03d5406360
Branch=main
phananh1010 added a commit to phananh1010/elasticsearch that referenced this pull request Oct 1, 2025
BASE=3c3058e6f63c5ebdcc4513fc5dcc4ffb3ec8a752
HEAD=69b95e47c4d65a8c6903b6f532b08d03d5406360
Branch=main
phananh1010 added a commit to phananh1010/elasticsearch that referenced this pull request Oct 7, 2025
BASE=3c3058e6f63c5ebdcc4513fc5dcc4ffb3ec8a752
HEAD=69b95e47c4d65a8c6903b6f532b08d03d5406360
Branch=main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Disallow sorting between TS and STATS Disallow bare {agg}_over_time with grouping attributes
3 participants