Skip to content

Commit

Permalink
fix(query): return semantic error for SRFs in WHERE and HAVING clauses (
Browse files Browse the repository at this point in the history
#17167)

* fix(query): raise semantic error for SRFs in WHERE and HAVING

* remove the test using flatten function
  • Loading branch information
takaebato authored Jan 4, 2025
1 parent 451da71 commit acd8e61
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/query/sql/src/planner/semantic/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2603,15 +2603,28 @@ impl<'a> TypeChecker<'a> {
func_name: &str,
args: &[&Expr],
) -> Result<Box<(ScalarExpr, DataType)>> {
if matches!(
self.bind_context.expr_context,
ExprContext::InSetReturningFunction
) {
return Err(ErrorCode::SemanticError(
"set-returning functions cannot be nested".to_string(),
)
.set_span(span));
match self.bind_context.expr_context {
ExprContext::InSetReturningFunction => {
return Err(ErrorCode::SemanticError(
"set-returning functions cannot be nested".to_string(),
)
.set_span(span));
}
ExprContext::WhereClause => {
return Err(ErrorCode::SemanticError(
"set-returning functions are not allowed in WHERE clause".to_string(),
)
.set_span(span));
}
ExprContext::HavingClause => {
return Err(ErrorCode::SemanticError(
"set-returning functions cannot be used in HAVING clause".to_string(),
)
.set_span(span));
}
_ => {}
}

if self.in_window_function {
return Err(ErrorCode::SemanticError(
"set-returning functions cannot be used in window spec",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,12 @@ select id, json_path_query(obj, '$.b?(@.c > 2)') from t2
statement error 1006
select id, json_path_query(obj, '--') from t2

statement error 1065
select id from t2 where json_path_query(obj, '$.a') = 1

statement error 1065
select id from t2 having json_path_query(obj, '$.a') = 1

query IT
select id, json_path_query_array(arr, '$[2, 1 to last -1]') from t1
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,12 @@ b
statement error 1065
select unnest(first_value('aa') OVER (PARTITION BY 'bb'))

statement error 1065
select * from numbers(10) where unnest([1,2,3])::BOOLEAN;

statement error 1065
select * from numbers(10) having unnest([1,2,3])::BOOLEAN;

statement ok
set max_block_size = 65535;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,12 @@ b [1,2,3]
c true
d {"k1":1,"k2":2}

statement error 1065
SELECT * FROM json_each(parse_json('{"a": true}')) WHERE json_each(parse_json('{"a": true}'))

statement error 1065
SELECT * FROM json_each(parse_json('{"a": true}')) HAVING json_each(parse_json('{"a": true}'))

query T
SELECT json_array_elements(NULL)
----
Expand Down Expand Up @@ -420,6 +426,12 @@ SELECT * FROM json_array_elements(parse_json('[1, [1,2,3], true, {"k1": 1, "k2":
true
{"k1":1,"k2":2}

statement error 1065
SELECT * FROM json_array_elements(parse_json('[true]')) WHERE json_array_elements(parse_json('[true]'))

statement error 1065
SELECT * FROM json_array_elements(parse_json('[true]')) HAVING json_array_elements(parse_json('[true]'))

query T
select parse_json('["1","2","3"]') ? NULL
----
Expand Down Expand Up @@ -1107,6 +1119,12 @@ ORDER BY jq:key;
2 {"key":"scores","value":"[92,88,95]"}
3 {"key":"scores","value":"[76,80,82]"}

statement error 1065
SELECT * FROM test_data WHERE jq('.scores | min', json_data) > 85

statement error 1065
SELECT * FROM test_data HAVING jq('.scores | min', json_data) > 85

statement ok
DROP TABLE test_data;

Expand Down

0 comments on commit acd8e61

Please sign in to comment.