Skip to content

Commit 27dc5d6

Browse files
authored
fix 621, where unnamed window functions shall be differentiated by partition and order by clause (#622)
1 parent ffb195c commit 27dc5d6

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

datafusion/src/logical_plan/expr.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,14 +1594,25 @@ fn create_name(e: &Expr, input_schema: &DFSchema) -> Result<String> {
15941594
fun,
15951595
args,
15961596
window_frame,
1597-
..
1597+
partition_by,
1598+
order_by,
15981599
} => {
1599-
let fun_name =
1600-
create_function_name(&fun.to_string(), false, args, input_schema)?;
1601-
Ok(match window_frame {
1602-
Some(window_frame) => format!("{} {}", fun_name, window_frame),
1603-
None => fun_name,
1604-
})
1600+
let mut parts: Vec<String> = vec![create_function_name(
1601+
&fun.to_string(),
1602+
false,
1603+
args,
1604+
input_schema,
1605+
)?];
1606+
if !partition_by.is_empty() {
1607+
parts.push(format!("PARTITION BY {:?}", partition_by));
1608+
}
1609+
if !order_by.is_empty() {
1610+
parts.push(format!("ORDER BY {:?}", order_by));
1611+
}
1612+
if let Some(window_frame) = window_frame {
1613+
parts.push(format!("{}", window_frame));
1614+
}
1615+
Ok(parts.join(" "))
16051616
}
16061617
Expr::AggregateFunction {
16071618
fun,

datafusion/src/sql/planner.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,6 +2823,17 @@ mod tests {
28232823
quick_test(sql, expected);
28242824
}
28252825

2826+
#[test]
2827+
fn empty_over_dup_with_different_sort() {
2828+
let sql = "SELECT order_id oid, MAX(order_id) OVER (), MAX(order_id) OVER (ORDER BY order_id) from orders";
2829+
let expected = "\
2830+
Projection: #orders.order_id AS oid, #MAX(orders.order_id), #MAX(orders.order_id) ORDER BY [#orders.order_id ASC NULLS FIRST]\
2831+
\n WindowAggr: windowExpr=[[MAX(#orders.order_id)]]\
2832+
\n WindowAggr: windowExpr=[[MAX(#orders.order_id) ORDER BY [#orders.order_id ASC NULLS FIRST]]]\
2833+
\n TableScan: orders projection=None";
2834+
quick_test(sql, expected);
2835+
}
2836+
28262837
#[test]
28272838
fn empty_over_plus() {
28282839
let sql = "SELECT order_id, MAX(qty * 1.1) OVER () from orders";
@@ -2857,7 +2868,7 @@ mod tests {
28572868
fn over_partition_by() {
28582869
let sql = "SELECT order_id, MAX(qty) OVER (PARTITION BY order_id) from orders";
28592870
let expected = "\
2860-
Projection: #orders.order_id, #MAX(orders.qty)\
2871+
Projection: #orders.order_id, #MAX(orders.qty) PARTITION BY [#orders.order_id]\
28612872
\n WindowAggr: windowExpr=[[MAX(#orders.qty) PARTITION BY [#orders.order_id]]]\
28622873
\n TableScan: orders projection=None";
28632874
quick_test(sql, expected);
@@ -2879,7 +2890,7 @@ mod tests {
28792890
fn over_order_by() {
28802891
let sql = "SELECT order_id, MAX(qty) OVER (ORDER BY order_id), MIN(qty) OVER (ORDER BY order_id DESC) from orders";
28812892
let expected = "\
2882-
Projection: #orders.order_id, #MAX(orders.qty), #MIN(orders.qty)\
2893+
Projection: #orders.order_id, #MAX(orders.qty) ORDER BY [#orders.order_id ASC NULLS FIRST], #MIN(orders.qty) ORDER BY [#orders.order_id DESC NULLS FIRST]\
28832894
\n WindowAggr: windowExpr=[[MAX(#orders.qty) ORDER BY [#orders.order_id ASC NULLS FIRST]]]\
28842895
\n WindowAggr: windowExpr=[[MIN(#orders.qty) ORDER BY [#orders.order_id DESC NULLS FIRST]]]\
28852896
\n TableScan: orders projection=None";
@@ -2890,7 +2901,7 @@ mod tests {
28902901
fn over_order_by_with_window_frame_double_end() {
28912902
let sql = "SELECT order_id, MAX(qty) OVER (ORDER BY order_id ROWS BETWEEN 3 PRECEDING and 3 FOLLOWING), MIN(qty) OVER (ORDER BY order_id DESC) from orders";
28922903
let expected = "\
2893-
Projection: #orders.order_id, #MAX(orders.qty) ROWS BETWEEN 3 PRECEDING AND 3 FOLLOWING, #MIN(orders.qty)\
2904+
Projection: #orders.order_id, #MAX(orders.qty) ORDER BY [#orders.order_id ASC NULLS FIRST] ROWS BETWEEN 3 PRECEDING AND 3 FOLLOWING, #MIN(orders.qty) ORDER BY [#orders.order_id DESC NULLS FIRST]\
28942905
\n WindowAggr: windowExpr=[[MAX(#orders.qty) ORDER BY [#orders.order_id ASC NULLS FIRST] ROWS BETWEEN 3 PRECEDING AND 3 FOLLOWING]]\
28952906
\n WindowAggr: windowExpr=[[MIN(#orders.qty) ORDER BY [#orders.order_id DESC NULLS FIRST]]]\
28962907
\n TableScan: orders projection=None";
@@ -2901,7 +2912,7 @@ mod tests {
29012912
fn over_order_by_with_window_frame_single_end() {
29022913
let sql = "SELECT order_id, MAX(qty) OVER (ORDER BY order_id ROWS 3 PRECEDING), MIN(qty) OVER (ORDER BY order_id DESC) from orders";
29032914
let expected = "\
2904-
Projection: #orders.order_id, #MAX(orders.qty) ROWS BETWEEN 3 PRECEDING AND CURRENT ROW, #MIN(orders.qty)\
2915+
Projection: #orders.order_id, #MAX(orders.qty) ORDER BY [#orders.order_id ASC NULLS FIRST] ROWS BETWEEN 3 PRECEDING AND CURRENT ROW, #MIN(orders.qty) ORDER BY [#orders.order_id DESC NULLS FIRST]\
29052916
\n WindowAggr: windowExpr=[[MAX(#orders.qty) ORDER BY [#orders.order_id ASC NULLS FIRST] ROWS BETWEEN 3 PRECEDING AND CURRENT ROW]]\
29062917
\n WindowAggr: windowExpr=[[MIN(#orders.qty) ORDER BY [#orders.order_id DESC NULLS FIRST]]]\
29072918
\n TableScan: orders projection=None";
@@ -2944,7 +2955,7 @@ mod tests {
29442955
fn over_order_by_with_window_frame_single_end_groups() {
29452956
let sql = "SELECT order_id, MAX(qty) OVER (ORDER BY order_id GROUPS 3 PRECEDING), MIN(qty) OVER (ORDER BY order_id DESC) from orders";
29462957
let expected = "\
2947-
Projection: #orders.order_id, #MAX(orders.qty) GROUPS BETWEEN 3 PRECEDING AND CURRENT ROW, #MIN(orders.qty)\
2958+
Projection: #orders.order_id, #MAX(orders.qty) ORDER BY [#orders.order_id ASC NULLS FIRST] GROUPS BETWEEN 3 PRECEDING AND CURRENT ROW, #MIN(orders.qty) ORDER BY [#orders.order_id DESC NULLS FIRST]\
29482959
\n WindowAggr: windowExpr=[[MAX(#orders.qty) ORDER BY [#orders.order_id ASC NULLS FIRST] GROUPS BETWEEN 3 PRECEDING AND CURRENT ROW]]\
29492960
\n WindowAggr: windowExpr=[[MIN(#orders.qty) ORDER BY [#orders.order_id DESC NULLS FIRST]]]\
29502961
\n TableScan: orders projection=None";
@@ -2967,7 +2978,7 @@ mod tests {
29672978
fn over_order_by_two_sort_keys() {
29682979
let sql = "SELECT order_id, MAX(qty) OVER (ORDER BY order_id), MIN(qty) OVER (ORDER BY (order_id + 1)) from orders";
29692980
let expected = "\
2970-
Projection: #orders.order_id, #MAX(orders.qty), #MIN(orders.qty)\
2981+
Projection: #orders.order_id, #MAX(orders.qty) ORDER BY [#orders.order_id ASC NULLS FIRST], #MIN(orders.qty) ORDER BY [#orders.order_id Plus Int64(1) ASC NULLS FIRST]\
29712982
\n WindowAggr: windowExpr=[[MAX(#orders.qty) ORDER BY [#orders.order_id ASC NULLS FIRST]]]\
29722983
\n WindowAggr: windowExpr=[[MIN(#orders.qty) ORDER BY [#orders.order_id Plus Int64(1) ASC NULLS FIRST]]]\
29732984
\n TableScan: orders projection=None";
@@ -2991,7 +3002,7 @@ mod tests {
29913002
fn over_order_by_sort_keys_sorting() {
29923003
let sql = "SELECT order_id, MAX(qty) OVER (ORDER BY qty, order_id), SUM(qty) OVER (), MIN(qty) OVER (ORDER BY order_id, qty) from orders";
29933004
let expected = "\
2994-
Projection: #orders.order_id, #MAX(orders.qty), #SUM(orders.qty), #MIN(orders.qty)\
3005+
Projection: #orders.order_id, #MAX(orders.qty) ORDER BY [#orders.qty ASC NULLS FIRST, #orders.order_id ASC NULLS FIRST], #SUM(orders.qty), #MIN(orders.qty) ORDER BY [#orders.order_id ASC NULLS FIRST, #orders.qty ASC NULLS FIRST]\
29953006
\n WindowAggr: windowExpr=[[SUM(#orders.qty)]]\
29963007
\n WindowAggr: windowExpr=[[MAX(#orders.qty) ORDER BY [#orders.qty ASC NULLS FIRST, #orders.order_id ASC NULLS FIRST]]]\
29973008
\n WindowAggr: windowExpr=[[MIN(#orders.qty) ORDER BY [#orders.order_id ASC NULLS FIRST, #orders.qty ASC NULLS FIRST]]]\
@@ -3016,7 +3027,7 @@ mod tests {
30163027
fn over_order_by_sort_keys_sorting_prefix_compacting() {
30173028
let sql = "SELECT order_id, MAX(qty) OVER (ORDER BY order_id), SUM(qty) OVER (), MIN(qty) OVER (ORDER BY order_id, qty) from orders";
30183029
let expected = "\
3019-
Projection: #orders.order_id, #MAX(orders.qty), #SUM(orders.qty), #MIN(orders.qty)\
3030+
Projection: #orders.order_id, #MAX(orders.qty) ORDER BY [#orders.order_id ASC NULLS FIRST], #SUM(orders.qty), #MIN(orders.qty) ORDER BY [#orders.order_id ASC NULLS FIRST, #orders.qty ASC NULLS FIRST]\
30203031
\n WindowAggr: windowExpr=[[SUM(#orders.qty)]]\
30213032
\n WindowAggr: windowExpr=[[MAX(#orders.qty) ORDER BY [#orders.order_id ASC NULLS FIRST]]]\
30223033
\n WindowAggr: windowExpr=[[MIN(#orders.qty) ORDER BY [#orders.order_id ASC NULLS FIRST, #orders.qty ASC NULLS FIRST]]]\
@@ -3045,7 +3056,7 @@ mod tests {
30453056
let sql = "SELECT order_id, MAX(qty) OVER (ORDER BY qty, order_id), SUM(qty) OVER (), MIN(qty) OVER (ORDER BY order_id, qty) from orders ORDER BY order_id";
30463057
let expected = "\
30473058
Sort: #orders.order_id ASC NULLS FIRST\
3048-
\n Projection: #orders.order_id, #MAX(orders.qty), #SUM(orders.qty), #MIN(orders.qty)\
3059+
\n Projection: #orders.order_id, #MAX(orders.qty) ORDER BY [#orders.qty ASC NULLS FIRST, #orders.order_id ASC NULLS FIRST], #SUM(orders.qty), #MIN(orders.qty) ORDER BY [#orders.order_id ASC NULLS FIRST, #orders.qty ASC NULLS FIRST]\
30493060
\n WindowAggr: windowExpr=[[SUM(#orders.qty)]]\
30503061
\n WindowAggr: windowExpr=[[MAX(#orders.qty) ORDER BY [#orders.qty ASC NULLS FIRST, #orders.order_id ASC NULLS FIRST]]]\
30513062
\n WindowAggr: windowExpr=[[MIN(#orders.qty) ORDER BY [#orders.order_id ASC NULLS FIRST, #orders.qty ASC NULLS FIRST]]]\
@@ -3067,7 +3078,7 @@ mod tests {
30673078
let sql =
30683079
"SELECT order_id, MAX(qty) OVER (PARTITION BY order_id ORDER BY qty) from orders";
30693080
let expected = "\
3070-
Projection: #orders.order_id, #MAX(orders.qty)\
3081+
Projection: #orders.order_id, #MAX(orders.qty) PARTITION BY [#orders.order_id] ORDER BY [#orders.qty ASC NULLS FIRST]\
30713082
\n WindowAggr: windowExpr=[[MAX(#orders.qty) PARTITION BY [#orders.order_id] ORDER BY [#orders.qty ASC NULLS FIRST]]]\
30723083
\n TableScan: orders projection=None";
30733084
quick_test(sql, expected);
@@ -3087,7 +3098,7 @@ mod tests {
30873098
let sql =
30883099
"SELECT order_id, MAX(qty) OVER (PARTITION BY order_id, qty ORDER BY qty) from orders";
30893100
let expected = "\
3090-
Projection: #orders.order_id, #MAX(orders.qty)\
3101+
Projection: #orders.order_id, #MAX(orders.qty) PARTITION BY [#orders.order_id, #orders.qty] ORDER BY [#orders.qty ASC NULLS FIRST]\
30913102
\n WindowAggr: windowExpr=[[MAX(#orders.qty) PARTITION BY [#orders.order_id, #orders.qty] ORDER BY [#orders.qty ASC NULLS FIRST]]]\
30923103
\n TableScan: orders projection=None";
30933104
quick_test(sql, expected);
@@ -3110,7 +3121,7 @@ mod tests {
31103121
let sql =
31113122
"SELECT order_id, MAX(qty) OVER (PARTITION BY order_id, qty ORDER BY qty), MIN(qty) OVER (PARTITION BY qty ORDER BY order_id) from orders";
31123123
let expected = "\
3113-
Projection: #orders.order_id, #MAX(orders.qty), #MIN(orders.qty)\
3124+
Projection: #orders.order_id, #MAX(orders.qty) PARTITION BY [#orders.order_id, #orders.qty] ORDER BY [#orders.qty ASC NULLS FIRST], #MIN(orders.qty) PARTITION BY [#orders.qty] ORDER BY [#orders.order_id ASC NULLS FIRST]\
31143125
\n WindowAggr: windowExpr=[[MAX(#orders.qty) PARTITION BY [#orders.order_id, #orders.qty] ORDER BY [#orders.qty ASC NULLS FIRST]]]\
31153126
\n WindowAggr: windowExpr=[[MIN(#orders.qty) PARTITION BY [#orders.qty] ORDER BY [#orders.order_id ASC NULLS FIRST]]]\
31163127
\n TableScan: orders projection=None";
@@ -3133,7 +3144,7 @@ mod tests {
31333144
let sql =
31343145
"SELECT order_id, MAX(qty) OVER (PARTITION BY order_id ORDER BY qty), MIN(qty) OVER (PARTITION BY order_id, qty ORDER BY price) from orders";
31353146
let expected = "\
3136-
Projection: #orders.order_id, #MAX(orders.qty), #MIN(orders.qty)\
3147+
Projection: #orders.order_id, #MAX(orders.qty) PARTITION BY [#orders.order_id] ORDER BY [#orders.qty ASC NULLS FIRST], #MIN(orders.qty) PARTITION BY [#orders.order_id, #orders.qty] ORDER BY [#orders.price ASC NULLS FIRST]\
31373148
\n WindowAggr: windowExpr=[[MAX(#orders.qty) PARTITION BY [#orders.order_id] ORDER BY [#orders.qty ASC NULLS FIRST]]]\
31383149
\n WindowAggr: windowExpr=[[MIN(#orders.qty) PARTITION BY [#orders.order_id, #orders.qty] ORDER BY [#orders.price ASC NULLS FIRST]]]\
31393150
\n TableScan: orders projection=None";

0 commit comments

Comments
 (0)