Skip to content

Commit 8a47dce

Browse files
authored
fix(query): window function support params (#17282)
1 parent f716352 commit 8a47dce

File tree

4 files changed

+169
-5
lines changed

4 files changed

+169
-5
lines changed

src/query/ast/src/parser/expr.rs

+25-5
Original file line numberDiff line numberDiff line change
@@ -1083,18 +1083,36 @@ pub fn expr_element(i: Input) -> IResult<WithSpan<ExprElement>> {
10831083
},
10841084
},
10851085
);
1086+
let function_call_with_params_window = map(
1087+
rule! {
1088+
#function_name
1089+
~ "(" ~ #comma_separated_list1(subexpr(0)) ~ ")"
1090+
~ "(" ~ DISTINCT? ~ #comma_separated_list0(subexpr(0))? ~ ")"
1091+
~ #window_function
1092+
},
1093+
|(name, _, params, _, _, opt_distinct, opt_args, _, window)| ExprElement::FunctionCall {
1094+
func: FunctionCall {
1095+
distinct: opt_distinct.is_some(),
1096+
name,
1097+
args: opt_args.unwrap_or_default(),
1098+
params,
1099+
window: Some(window),
1100+
lambda: None,
1101+
},
1102+
},
1103+
);
10861104
let function_call_with_params = map(
10871105
rule! {
10881106
#function_name
1089-
~ ("(" ~ #comma_separated_list1(subexpr(0)) ~ ")")?
1107+
~ "(" ~ #comma_separated_list1(subexpr(0)) ~ ")"
10901108
~ "(" ~ DISTINCT? ~ #comma_separated_list0(subexpr(0))? ~ ")"
10911109
},
1092-
|(name, params, _, opt_distinct, opt_args, _)| ExprElement::FunctionCall {
1110+
|(name, _, params, _, _, opt_distinct, opt_args, _)| ExprElement::FunctionCall {
10931111
func: FunctionCall {
10941112
distinct: opt_distinct.is_some(),
10951113
name,
10961114
args: opt_args.unwrap_or_default(),
1097-
params: params.map(|(_, x, _)| x).unwrap_or_default(),
1115+
params,
10981116
window: None,
10991117
lambda: None,
11001118
},
@@ -1376,7 +1394,6 @@ pub fn expr_element(i: Input) -> IResult<WithSpan<ExprElement>> {
13761394
| #interval_expr : "`INTERVAL <str_literal>`"
13771395
| #extract : "`EXTRACT((YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | SECOND | WEEK) FROM ...)`"
13781396
| #date_part : "`DATE_PART((YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | SECOND | WEEK), ...)`"
1379-
13801397
),
13811398
rule!(
13821399
#substring : "`SUBSTRING(... [FROM ...] [FOR ...])`"
@@ -1388,9 +1405,12 @@ pub fn expr_element(i: Input) -> IResult<WithSpan<ExprElement>> {
13881405
| #count_all_with_window : "`COUNT(*) OVER ...`"
13891406
| #function_call_with_lambda : "`function(..., x -> ...)`"
13901407
| #function_call_with_window : "`function(...) OVER ([ PARTITION BY <expr>, ... ] [ ORDER BY <expr>, ... ] [ <window frame> ])`"
1408+
| #function_call_with_params_window : "`function(...)(...) OVER ([ PARTITION BY <expr>, ... ] [ ORDER BY <expr>, ... ] [ <window frame> ])`"
13911409
| #function_call_with_params : "`function(...)(...)`"
13921410
| #function_call : "`function(...)`"
1393-
| #case : "`CASE ... END`"
1411+
),
1412+
rule!(
1413+
#case : "`CASE ... END`"
13941414
| #tuple : "`(<expr> [, ...])`"
13951415
| #subquery : "`(SELECT ...)`"
13961416
| #column_ref : "<column>"

src/query/ast/tests/it/parser.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,7 @@ fn test_expr() {
12511251
r#"COUNT() OVER (ORDER BY hire_date ROWS UNBOUNDED PRECEDING)"#,
12521252
r#"COUNT() OVER (ORDER BY hire_date ROWS CURRENT ROW)"#,
12531253
r#"COUNT() OVER (ORDER BY hire_date ROWS 3 PRECEDING)"#,
1254+
r#"QUANTILE_CONT(0.5)(salary) OVER (PARTITION BY department ORDER BY hire_date)"#,
12541255
r#"ARRAY_APPLY([1,2,3], x -> x + 1)"#,
12551256
r#"ARRAY_FILTER(col, y -> y % 2 = 0)"#,
12561257
r#"(current_timestamp, current_timestamp(), now())"#,

src/query/ast/tests/it/testdata/expr.txt

+114
Original file line numberDiff line numberDiff line change
@@ -4229,6 +4229,120 @@ FunctionCall {
42294229
}
42304230

42314231

4232+
---------- Input ----------
4233+
QUANTILE_CONT(0.5)(salary) OVER (PARTITION BY department ORDER BY hire_date)
4234+
---------- Output ---------
4235+
QUANTILE_CONT(0.5)(salary) OVER (PARTITION BY department ORDER BY hire_date)
4236+
---------- AST ------------
4237+
FunctionCall {
4238+
span: Some(
4239+
0..76,
4240+
),
4241+
func: FunctionCall {
4242+
distinct: false,
4243+
name: Identifier {
4244+
span: Some(
4245+
0..13,
4246+
),
4247+
name: "QUANTILE_CONT",
4248+
quote: None,
4249+
ident_type: None,
4250+
},
4251+
args: [
4252+
ColumnRef {
4253+
span: Some(
4254+
19..25,
4255+
),
4256+
column: ColumnRef {
4257+
database: None,
4258+
table: None,
4259+
column: Name(
4260+
Identifier {
4261+
span: Some(
4262+
19..25,
4263+
),
4264+
name: "salary",
4265+
quote: None,
4266+
ident_type: None,
4267+
},
4268+
),
4269+
},
4270+
},
4271+
],
4272+
params: [
4273+
Literal {
4274+
span: Some(
4275+
14..17,
4276+
),
4277+
value: Decimal256 {
4278+
value: 5,
4279+
precision: 76,
4280+
scale: 1,
4281+
},
4282+
},
4283+
],
4284+
window: Some(
4285+
WindowDesc {
4286+
ignore_nulls: None,
4287+
window: WindowSpec(
4288+
WindowSpec {
4289+
existing_window_name: None,
4290+
partition_by: [
4291+
ColumnRef {
4292+
span: Some(
4293+
46..56,
4294+
),
4295+
column: ColumnRef {
4296+
database: None,
4297+
table: None,
4298+
column: Name(
4299+
Identifier {
4300+
span: Some(
4301+
46..56,
4302+
),
4303+
name: "department",
4304+
quote: None,
4305+
ident_type: None,
4306+
},
4307+
),
4308+
},
4309+
},
4310+
],
4311+
order_by: [
4312+
OrderByExpr {
4313+
expr: ColumnRef {
4314+
span: Some(
4315+
66..75,
4316+
),
4317+
column: ColumnRef {
4318+
database: None,
4319+
table: None,
4320+
column: Name(
4321+
Identifier {
4322+
span: Some(
4323+
66..75,
4324+
),
4325+
name: "hire_date",
4326+
quote: None,
4327+
ident_type: None,
4328+
},
4329+
),
4330+
},
4331+
},
4332+
asc: None,
4333+
nulls_first: None,
4334+
},
4335+
],
4336+
window_frame: None,
4337+
},
4338+
),
4339+
},
4340+
),
4341+
lambda: None,
4342+
},
4343+
}
4344+
4345+
42324346
---------- Input ----------
42334347
ARRAY_APPLY([1,2,3], x -> x + 1)
42344348
---------- Output ---------

tests/sqllogictests/suites/query/window_function/window_basic.test

+29
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,34 @@ select grouping(salary), grouping(depname), sum(grouping(salary)) over (partitio
606606
1 0 3
607607
1 1 1
608608

609+
query TII
610+
SELECT depname, empno, quantile_cont(salary) OVER (PARTITION BY depname ORDER BY empno) FROM empsalary ORDER BY depname, empno
611+
----
612+
develop 7 4200.0
613+
develop 8 5100.0
614+
develop 9 4500.0
615+
develop 10 4850.0
616+
develop 11 5200.0
617+
personnel 2 3900.0
618+
personnel 5 3700.0
619+
sales 1 5000.0
620+
sales 3 4900.0
621+
sales 4 4800.0
622+
623+
query TII
624+
SELECT depname, empno, quantile_cont(0.8)(salary) OVER (PARTITION BY depname ORDER BY empno) FROM empsalary ORDER BY depname, empno
625+
----
626+
develop 7 4200.0
627+
develop 8 5640.0
628+
develop 9 5400.0
629+
develop 10 5520.0
630+
develop 11 5360.0
631+
personnel 2 3900.0
632+
personnel 5 3820.0
633+
sales 1 5000.0
634+
sales 3 4960.0
635+
sales 4 4920.0
636+
609637
# Window func in subquery
610638
query I
611639
SELECT * FROM (SELECT row_number() OVER (PARTITION BY depname ORDER BY salary) rn FROM empsalary ORDER BY depname, rn) order by 1;
@@ -846,3 +874,4 @@ Product B 1200 NULL
846874

847875
statement ok
848876
DROP DATABASE test_window_basic;
877+

0 commit comments

Comments
 (0)