Skip to content

Commit

Permalink
minor: fix to support scalars (apache#8559)
Browse files Browse the repository at this point in the history
* minor: fix  to support scalars

* Update datafusion/sql/src/expr/function.rs

Co-authored-by: Andrew Lamb <[email protected]>

---------

Co-authored-by: Mustafa Akur <[email protected]>
Co-authored-by: Andrew Lamb <[email protected]>
  • Loading branch information
3 people authored Dec 18, 2023
1 parent fc6cc48 commit b287cda
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
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(
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
# 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

0 comments on commit b287cda

Please sign in to comment.