Skip to content

Commit

Permalink
fix window spec check (databendlabs#13034)
Browse files Browse the repository at this point in the history
  • Loading branch information
ariesdevil authored and andylokandy committed Nov 27, 2023
1 parent e784d34 commit 8f3359d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/query/sql/src/planner/semantic/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,13 @@ impl<'a> TypeChecker<'a> {
.set_span(*span));
}

if self.in_window_function {
return Err(ErrorCode::SemanticError(
"set-returning functions cannot be used in window spec",
)
.set_span(*span));
}

if !matches!(self.bind_context.expr_context, ExprContext::SelectClause) {
return Err(ErrorCode::SemanticError(
"set-returning functions can only be used in SELECT".to_string(),
Expand Down Expand Up @@ -1145,13 +1152,14 @@ impl<'a> TypeChecker<'a> {
.clone(),
};

self.in_window_function = true;
let mut partitions = Vec::with_capacity(spec.partition_by.len());
for p in spec.partition_by.iter() {
let box (part, _part_type) = self.resolve(p).await?;
partitions.push(part);
}
let mut order_by = Vec::with_capacity(spec.order_by.len());

let mut order_by = Vec::with_capacity(spec.order_by.len());
for o in spec.order_by.iter() {
let box (order, _) = self.resolve(&o.expr).await?;
order_by.push(WindowOrderBy {
Expand All @@ -1160,6 +1168,8 @@ impl<'a> TypeChecker<'a> {
nulls_first: o.nulls_first,
})
}
self.in_window_function = false;

let frame = self
.resolve_window_frame(
span,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,11 @@ SELECT a, sum(a) OVER w FROM t1 WINDOW w AS (PARTITION BY a) ORDER BY count() OV
5 10
5 10

statement error 1065
SELECT sum(a) OVER w FROM t1 WINDOW w AS (ORDER BY unnest([1,2,3]))

statement error 1065
SELECT sum(a) OVER w FROM t1 WINDOW w AS (PARTITION BY (sum(a) OVER()))

statement ok
DROP DATABASE test_named_window_basic
Original file line number Diff line number Diff line change
Expand Up @@ -708,5 +708,11 @@ select last_value(last_value(salary) over ()) over () from empsalary
statement error 1065
select sum(sum(salary) over()) over() from empsalary

statement error 1065
select sum(salary) over(order by unnest([1,2,3])) from empsalary

statement error 1065
select sum(salary) over(order by (sum(salary) over())) from empsalary

statement ok
DROP DATABASE test_window_basic

0 comments on commit 8f3359d

Please sign in to comment.