- 
                Notifications
    You must be signed in to change notification settings 
- Fork 25.6k
Add window to aggregation function #137344
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
Conversation
| Pinging @elastic/es-analytical-engine (Team:Analytics) | 
| Pinging @elastic/es-storage-engine (Team:StorageEngine) | 
        
          
                ...sql/src/main/java/org/elasticsearch/xpack/esql/expression/function/EsqlFunctionRegistry.java
          
            Show resolved
            Hide resolved
        
              
          
                .../org/elasticsearch/xpack/esql/expression/function/aggregate/TimeSeriesAggregateFunction.java
          
            Show resolved
            Hide resolved
        
              
          
                ...plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plan/logical/TimeSeriesAggregate.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                ...plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plan/logical/TimeSeriesAggregate.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                ...plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plan/logical/TimeSeriesAggregate.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                ...plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plan/logical/TimeSeriesAggregate.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
      | 
 Thanks Kostas! The issue is that time-series aggregations are translated to regular aggregations (e.g.,  Line 293 in 793b0ae 
 | 
| Makes sense, thanks for the detailed explanation. Window functions will be generalized down the road, so this is unavoidable either way. | 
| * Whether the aggregate function has a window different than NO_WINDOW. | ||
| */ | ||
| public boolean hasWindow() { | ||
| boolean zero = window instanceof Literal lit && lit.value() instanceof Duration duration && duration.isZero(); | 
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.
Should this be:
if (window instanceof Literal lit && lit.value() instanceof Duration duration) {
  return duration.isZero() == false;
}
return false;
| @Override | ||
| protected NodeInfo<Rate> info() { | ||
| return NodeInfo.create(this, Rate::new, field(), timestamp); | ||
| return NodeInfo.create(this, Rate::new, field(), filter(), window(), timestamp); | 
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: we missed filter, how important is this? Shall we backport just this to 9.2?
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.
I will open a PR for 9.2.1.
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.
Nice, good progress!
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.
I suppose the need to add the window param to AggregateFunction (vs just TimeSeriesAggregateFunction) will be revealed later (possibly streaming command?).
| * such as {@link Rate} or {@link MaxOverTime}. | ||
| */ | ||
| public abstract class TimeSeriesAggregateFunction extends AggregateFunction { | ||
| public static final Literal NO_WINDOW = Literal.timeDuration(Source.EMPTY, Duration.ZERO); | 
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.
This is a dup of the one in AggregateFunction.
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.
yes, removed it in 3ec11b9.
| super(source, CollectionUtils.combine(asList(field, filter, window), parameters)); | ||
| this.field = field; | ||
| this.filter = filter; | ||
| this.window = Objects.requireNonNull(window, "[window] must be specified; use NO_WINDOW instead"); | 
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.
I guess we expect functions to check against negative durations?
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.
Yes, we should, but our random tests may pass any expression for this parameter.
This change adds a
windowparameter toAggregateFunction, similar to the existingfilterparameter. Thewindowparameter is optional but must never be null. This PR also generalizesAggregateFunctionto be composed of[source, field, filter, window, extra parameters], with all extra parameters placed afterfilterandwindow.The implementation of the window function will be added in a follow-up and will initially be available only for time-series aggregations such as
rate,avg_over_time, etc.