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

minor: fix to support scalars #8559

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions datafusion/sql/src/expr/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
let partition_by = window
.partition_by
.into_iter()
// ignore window spec PARTITION BY for scalar values
// as they do not change and thus do not generate new partitions
.filter(|e| !matches!(e, sqlparser::ast::Expr::Value { .. },))
.map(|e| self.sql_expr_to_logical_expr(e, schema, planner_context))
.collect::<Result<Vec<_>>>()?;
let mut order_by = self.order_by_to_sort_expr(
Copy link
Contributor

Choose a reason for hiding this comment

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

According to test it seems that we have no problem with ScalarValues inside window order_by. However, I think we can apply similar filtering done for window.partition_by to the window.order_by.

Expand Down
28 changes: 28 additions & 0 deletions datafusion/sqllogictest/test_files/window.slt
Original file line number Diff line number Diff line change
Expand Up @@ -3794,8 +3794,36 @@ select a,
1 1
2 1

# support scalar value in ORDER BY
query I
select rank() over (order by 1) rnk from (select 1 a union all select 2 a) x
----
1
1

# support scalar value in both ORDER BY and PARTITION BY, RANK function
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 as @mustafasrepo points out

# TODO: fix the test, some issue in RANK
#query IIIIII
#select rank() over (partition by 1 order by 1) rnk,
# rank() over (partition by a, 1 order by 1) rnk1,
# rank() over (partition by a, 1 order by a, 1) rnk2,
# rank() over (partition by 1) rnk3,
# rank() over (partition by null) rnk4,
# rank() over (partition by 1, null, a) rnk5
#from (select 1 a union all select 2 a) x
#----
#1 1 1 1 1 1
#1 1 1 1 1 1

# support scalar value in both ORDER BY and PARTITION BY, ROW_NUMBER function
query IIIIII
select row_number() over (partition by 1 order by 1) rn,
row_number() over (partition by a, 1 order by 1) rn1,
row_number() over (partition by a, 1 order by a, 1) rn2,
row_number() over (partition by 1) rn3,
row_number() over (partition by null) rn4,
row_number() over (partition by 1, null, a) rn5
from (select 1 a union all select 2 a) x;
----
1 1 1 1 1 1
2 1 1 2 2 1