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: RANGE frame for corner cases with empty ORDER BY clause should be treated as constant sort #8445

Merged
merged 4 commits into from
Dec 8, 2023

Conversation

viirya
Copy link
Member

@viirya viirya commented Dec 6, 2023

Which issue does this PR close?

Closes #.

Rationale for this change

We currently regularize RANGE window frame with UNBOUNDED/CURRENT ROW bounds with empty ORDER BY clause to ROWS frame with the UNBOUNDED bounds. This produces inconsistent result compared to Postgres where the case is treated as ORDER BY with constant sort key and unchanged window frame.

What changes are included in this PR?

Are these changes tested?

Are there any user-facing changes?

@github-actions github-actions bot added sql SQL Planner logical-expr Logical plan and expressions sqllogictest SQL Logic Tests (.slt) labels Dec 6, 2023
// Note that this follows Postgres behavior.
// In these cases, we regularize the ORDER BY clause if the ORDER BY clause
// is absent. If an ORDER BY clause is present but has more than one column,
// the ORDER BY clause is unchanged. Note that this follows Postgres behavior.
if (frame.start_bound.is_unbounded()
|| frame.start_bound == WindowFrameBound::CurrentRow)
&& (frame.end_bound == WindowFrameBound::CurrentRow
|| frame.end_bound.is_unbounded())
Copy link
Member Author

@viirya viirya Dec 7, 2023

Choose a reason for hiding this comment

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

RANGE frame with UNBOUNDED or CURRENT ROW PRECEDING/FOLLOWING without ORDER BY, all rows of the partitions become peers of the current row, i.e., the sort expression returns the same for all rows.

Copy link
Contributor

@mustafasrepo mustafasrepo 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 this PR @viirya. It is LGTM!.

@mustafasrepo mustafasrepo merged commit 205e315 into apache:main Dec 8, 2023
22 checks passed
@viirya
Copy link
Member Author

viirya commented Dec 8, 2023

Thank you @mustafasrepo

appletreeisyellow pushed a commit to appletreeisyellow/datafusion that referenced this pull request Dec 15, 2023
…e treated as constant sort (apache#8445)

* fix: RANGE frame for corner cases with empty ORDER BY clause should be treated as constant sort

* fix

* Make the test not flaky

* fix clippy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
logical-expr Logical plan and expressions sql SQL Planner sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants