Skip to content
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

fix(over window): fix over window predicate pushdown #13662

Merged
merged 13 commits into from
Nov 29, 2023

Conversation

stdrc
Copy link
Member

@stdrc stdrc commented Nov 27, 2023

I hereby agree to the terms of the RisingWave Labs, Inc. Contributor License Agreement.

What's changed and what's your intention?

Given the following queries:

select rule, lag(rule) over (partition by cat order by at) from t where rule = 'B'; -- Q1
select * from (select rule, lag(rule) over (partition by cat order by at) as prev_rule from t) where rule = 'B'; -- Q2

By their semantics, Q1 requires filtering rows read from t, while Q2 requires filtering rows after window function calculation. Previously, for Q2, our optimizer incorrectly pushed the predicate down before over window, which diverged from the expected semantics.

Thanks to @xiangjinwu's #13653 for revealing such a long-existing bug. This PR fixes #13653.

Checklist

  • I have written necessary rustdoc comments
  • I have added necessary unit tests and integration tests
  • I have added test labels as necessary. See details.
  • I have added fuzzing tests or opened an issue to track them. (Optional, recommended for new SQL features Sqlsmith: Sql feature generation #7934).
  • My PR contains breaking changes. (If it deprecates some features, please create a tracking issue to remove them in the future).
  • All checks passed in ./risedev check (or alias, ./risedev c)
  • My PR changes performance-critical code. (Please run macro/micro-benchmarks and show the results.)
  • My PR contains critical fixes that are necessary to be merged into the latest release. (Please check out the details)

Documentation

  • My PR needs documentation updates. (Please use the Release note section below to summarize the impact on users)

Release note

If this PR includes changes that directly affect users or other significant modifications relevant to the community, kindly draft a release note to provide a concise summary of these changes. Please prioritize highlighting the impact these changes will have on users.

@github-actions github-actions bot added the type/fix Bug fix label Nov 27, 2023
.collect();
over_window_related_cols.grow(out_schema_len);

let (window_pred, other_pred) = predicate.split_disjoint(&over_window_related_cols);
Copy link
Contributor

Choose a reason for hiding this comment

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

Considering this case, even if we use a id which should be not over_window_related_cols column, we should not push this filter through the over window.

 select * from (select id, rule, at, row_number() over (partition by cat order by at) as rank from t) x where id = 2;

Copy link
Member Author

Choose a reason for hiding this comment

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

Then it seems that we cannot push any predicate through over window.

Copy link
Contributor

@chenzl25 chenzl25 Nov 27, 2023

Choose a reason for hiding this comment

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

However, partition only related filter could be pushed down. For example

select * from (select id, cat, rule, at, count(*) over (partition by cat order by at) as cnt from t) x where cat = 'foo';

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed.

@stdrc stdrc requested a review from chenzl25 November 28, 2023 08:42
Copy link
Contributor

@chenzl25 chenzl25 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

Signed-off-by: Richard Chien <[email protected]>
Signed-off-by: Richard Chien <[email protected]>
Copy link

codecov bot commented Nov 28, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (e6d76a2) 68.28% compared to head (eb5765b) 68.30%.
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #13662      +/-   ##
==========================================
+ Coverage   68.28%   68.30%   +0.01%     
==========================================
  Files        1523     1523              
  Lines      261607   261619      +12     
==========================================
+ Hits       178631   178688      +57     
+ Misses      82976    82931      -45     
Flag Coverage Δ
rust 68.30% <100.00%> (+0.01%) ⬆️

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.

@stdrc stdrc added this pull request to the merge queue Nov 28, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Nov 28, 2023
@stdrc stdrc added this pull request to the merge queue Nov 28, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Nov 28, 2023
Copy link
Contributor

@xiangjinwu xiangjinwu left a comment

Choose a reason for hiding this comment

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

Just need to merge main one more time ...

@st1page st1page added this pull request to the merge queue Nov 29, 2023
Merged via the queue into main with commit c111340 Nov 29, 2023
26 of 27 checks passed
@st1page st1page deleted the rc/fix-over-window-predicate-pushdown branch November 29, 2023 18:25
stdrc added a commit that referenced this pull request Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/fix Bug fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug(optimizer): PredicatePushdown across OverWindow
4 participants