Skip to content

Conversation

avantgardnerio
Copy link
Contributor

Which issue does this PR close?

Rationale for this change

We would like plans with window functions to go faster.

What changes are included in this PR?

A proof-of-concept level optimizer rule push limits past some window functions.

Are these changes tested?

So far only by the existing sqllogictest suite.

Are there any user-facing changes?

Queries with window functions may go faster, particularly if the TableProvider supports TopK pushdown.

@github-actions github-actions bot added documentation Improvements or additions to documentation optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) common Related to common crate labels Aug 28, 2025
@avantgardnerio avantgardnerio marked this pull request as ready for review August 29, 2025 14:46
Copy link
Contributor

@akurmustafa akurmustafa left a comment

Choose a reason for hiding this comment

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

Thanks @avantgardnerio for this PR

if let Some(window) = node.as_any().downcast_ref::<BoundedWindowAggExec>() {
for expr in window.window_expr().iter() {
let frame = expr.get_window_frame();
if frame.units != WindowFrameUnits::Rows {
Copy link
Contributor

Choose a reason for hiding this comment

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

As far as I can see, we update latest_max only for WindowFrameUnits::Rows cases. If I am not mistaken, it is also valid to update latest_max for WindowFrameUnits::Range and WindowFrameUnits::Groups as longs as end_bound is WindowFrameBound::Preceding(_). I think, we can extend checks to include this use case also.
This should change the plan for following kind of queries

SELECT
    c9,
    SUM(c9) OVER(ORDER BY c9 ASC RANGE BETWEEN 5 PRECEDING AND 1 PRECEDING) as sum1
    FROM aggregate_test_100
    LIMIT 5

However, we can do this change in subsequent PRs. I think in current form, this PR is correct and we can merge as is. Thanks @avantgardnerio for this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! I will file a subsequent PR 😄

@avantgardnerio avantgardnerio merged commit 25acb64 into apache:main Aug 29, 2025
29 checks passed
@avantgardnerio avantgardnerio deleted the bg_push_limits branch August 29, 2025 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
common Related to common crate documentation Improvements or additions to documentation optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimizer should push limits past window functions
4 participants