Skip to content

Conversation

@yashmayya
Copy link
Contributor

@yashmayya yashmayya commented Oct 22, 2024

  • Fixes [Multi Stage] Add ROWS support for aggregation window functions #11406.
  • Background reading - https://www.postgresql.org/docs/current/tutorial-window.html, https://www.postgresql.org/docs/current/functions-window.html, https://www.postgresql.org/docs/current/sql-expressions.html#SYNTAX-WINDOW-FUNCTIONS (this one is most relevant to this PR).
  • Currently, Pinot's window function implementations have limited or even incorrect support for window frame bounds. For instance, FIRST_VALUE / LAST_VALUE assume that the window frame is always ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING even though the default window frame as per standard SQL is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. Furthermore, support for defining the lower bound explicitly as UNBOUNDED PRECEDING / CURRENT ROW / n FOLLOWING / n PRECEDING and the upper bound as UNBOUNDED FOLLOWING / CURRENT ROW / n FOLLOWING / n PRECEDING does not exist.
  • This patch adds support for any custom bounds (offset based or otherwise) for ROWS window frames, and also adds support for UNBOUNDED PRECEDING / CURRENT ROW / UNBOUNDED FOLLOWING bounds for RANGE window frames. There are a ton of edge cases to be handled here but this patch attempts to add test cases to cover most of these scenarios.
  • Note that Calcite (and hence Pinot) only supports ROWS and RANGE based window frame bounds, whereas Postgres also supports GROUPS.
  • The planner side changes (mainly literal extraction) are built over [WIP] Support preceding and following in WINDOW #14233.
  • This patch also introduces a new WindowValueAggregator interface along with implementations for SUM, COUNT, MIN, MAX, BOOLAND, BOOLOR. These use sliding window based aggregation algorithms to efficiently compute aggregations for the aggregate type window functions. This replaces the older Merger interface and ensures that the time complexity for computing aggregate window functions for ROWS based windows is O(N) and not O(N * K) (where N is the total number of rows and K is the window size) which would've been the case if we continued using the Merger based approach. We still need to add support for type specific window value aggregators (which was also the case with the mergers) so this isn't a regression.
  • Note that all the changes in this patch only affect the aggregate window functions (SUM, COUNT, MIN, MAX etc.) and FIRST_VALUE / LAST_VALUE. The other window functions currently supported by Pinot (LAG, LEAD, RANK, DENSE_RANK, ROW_NUMBER) don't support custom window frame bounds and Calcite ensures that during query planning.
  • Calcite also does some other validation for window frame bounds like making sure lower bound isn't UNBOUNDED FOLLOWING / upper bound isn't UNBOUNDED PRECEDING, lower bound isn't n FOLLOWING if upper bound is n PRECEDING and vice versa etc.

@codecov-commenter
Copy link

codecov-commenter commented Oct 22, 2024

Codecov Report

Attention: Patch coverage is 87.22359% with 52 lines in your changes missing coverage. Please review.

Project coverage is 63.76%. Comparing base (59551e4) to head (7be678a).
Report is 1234 commits behind head on master.

Files with missing lines Patch % Lines
.../query/planner/logical/PlanNodeToRelConverter.java 0.00% 11 Missing ⚠️
...e/rel/rules/PinotWindowExchangeNodeInsertRule.java 85.18% 4 Missing and 4 partials ⚠️
...tor/window/aggregate/MaxWindowValueAggregator.java 79.31% 2 Missing and 4 partials ⚠️
...tor/window/aggregate/MinWindowValueAggregator.java 79.31% 2 Missing and 4 partials ⚠️
.../query/planner/logical/RelToPlanNodeConverter.java 75.00% 1 Missing and 3 partials ⚠️
...ator/window/aggregate/AggregateWindowFunction.java 94.52% 1 Missing and 3 partials ⚠️
...r/window/aggregate/CountWindowValueAggregator.java 72.72% 3 Missing ⚠️
...not/query/runtime/operator/window/WindowFrame.java 84.61% 1 Missing and 1 partial ⚠️
...rator/window/aggregate/BoolAndValueAggregator.java 92.59% 1 Missing and 1 partial ⚠️
...erator/window/aggregate/BoolOrValueAggregator.java 92.59% 1 Missing and 1 partial ⚠️
... and 4 more
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #14273      +/-   ##
============================================
+ Coverage     61.75%   63.76%   +2.01%     
- Complexity      207     1555    +1348     
============================================
  Files          2436     2659     +223     
  Lines        133233   145441   +12208     
  Branches      20636    22218    +1582     
============================================
+ Hits          82274    92744   +10470     
- Misses        44911    45843     +932     
- Partials       6048     6854     +806     
Flag Coverage Δ
custom-integration1 100.00% <ø> (+99.99%) ⬆️
integration 100.00% <ø> (+99.99%) ⬆️
integration1 100.00% <ø> (+99.99%) ⬆️
integration2 0.00% <ø> (ø)
java-11 63.70% <87.22%> (+1.99%) ⬆️
java-21 63.65% <87.22%> (+2.02%) ⬆️
skip-bytebuffers-false 63.74% <87.22%> (+2.00%) ⬆️
skip-bytebuffers-true 63.61% <87.22%> (+35.88%) ⬆️
temurin 63.76% <87.22%> (+2.01%) ⬆️
unittests 63.76% <87.22%> (+2.01%) ⬆️
unittests1 55.41% <87.22%> (+8.52%) ⬆️
unittests2 34.25% <0.49%> (+6.52%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yashmayya yashmayya marked this pull request as ready for review October 23, 2024 04:47
@yashmayya yashmayya changed the title Add WindowValueAggregator interface with implementations to perform efficient sliding window based aggregations for aggregate window functions Add support for defining custom window frame bounds for window functions Oct 23, 2024
@yashmayya yashmayya added feature release-notes Referenced by PRs that need attention when compiling the next release notes multi-stage Related to the multi-stage query engine labels Oct 23, 2024
Copy link
Contributor

@Jackie-Jiang Jackie-Jiang left a comment

Choose a reason for hiding this comment

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

Well done!

…indow value aggregators; use primitive double in sum window value aggregator; update condition for removal support in aggregate window function; throw exception if unable to read literal offset value for window bounds
Copy link
Contributor Author

@yashmayya yashmayya left a comment

Choose a reason for hiding this comment

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

Thanks for the review Jackie! I've made the requested changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature multi-stage Related to the multi-stage query engine release-notes Referenced by PRs that need attention when compiling the next release notes window-functions Related to SQL window functions on the multi-stage query engine

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Multi Stage] Add ROWS support for aggregation window functions

3 participants