Skip to content

Commit acd8e61

Browse files
authored
fix(query): return semantic error for SRFs in WHERE and HAVING clauses (#17167)
* fix(query): raise semantic error for SRFs in WHERE and HAVING * remove the test using flatten function
1 parent 451da71 commit acd8e61

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

src/query/sql/src/planner/semantic/type_check.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2603,15 +2603,28 @@ impl<'a> TypeChecker<'a> {
26032603
func_name: &str,
26042604
args: &[&Expr],
26052605
) -> Result<Box<(ScalarExpr, DataType)>> {
2606-
if matches!(
2607-
self.bind_context.expr_context,
2608-
ExprContext::InSetReturningFunction
2609-
) {
2610-
return Err(ErrorCode::SemanticError(
2611-
"set-returning functions cannot be nested".to_string(),
2612-
)
2613-
.set_span(span));
2606+
match self.bind_context.expr_context {
2607+
ExprContext::InSetReturningFunction => {
2608+
return Err(ErrorCode::SemanticError(
2609+
"set-returning functions cannot be nested".to_string(),
2610+
)
2611+
.set_span(span));
2612+
}
2613+
ExprContext::WhereClause => {
2614+
return Err(ErrorCode::SemanticError(
2615+
"set-returning functions are not allowed in WHERE clause".to_string(),
2616+
)
2617+
.set_span(span));
2618+
}
2619+
ExprContext::HavingClause => {
2620+
return Err(ErrorCode::SemanticError(
2621+
"set-returning functions cannot be used in HAVING clause".to_string(),
2622+
)
2623+
.set_span(span));
2624+
}
2625+
_ => {}
26142626
}
2627+
26152628
if self.in_window_function {
26162629
return Err(ErrorCode::SemanticError(
26172630
"set-returning functions cannot be used in window spec",

tests/sqllogictests/suites/query/functions/02_0051_function_semi_structureds_get.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,12 @@ select id, json_path_query(obj, '$.b?(@.c > 2)') from t2
572572
statement error 1006
573573
select id, json_path_query(obj, '--') from t2
574574

575+
statement error 1065
576+
select id from t2 where json_path_query(obj, '$.a') = 1
577+
578+
statement error 1065
579+
select id from t2 having json_path_query(obj, '$.a') = 1
580+
575581
query IT
576582
select id, json_path_query_array(arr, '$[2, 1 to last -1]') from t1
577583
----

tests/sqllogictests/suites/query/functions/02_0062_function_unnest.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,12 @@ b
512512
statement error 1065
513513
select unnest(first_value('aa') OVER (PARTITION BY 'bb'))
514514

515+
statement error 1065
516+
select * from numbers(10) where unnest([1,2,3])::BOOLEAN;
517+
518+
statement error 1065
519+
select * from numbers(10) having unnest([1,2,3])::BOOLEAN;
520+
515521
statement ok
516522
set max_block_size = 65535;
517523

tests/sqllogictests/suites/query/functions/02_0065_function_json.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,12 @@ b [1,2,3]
370370
c true
371371
d {"k1":1,"k2":2}
372372

373+
statement error 1065
374+
SELECT * FROM json_each(parse_json('{"a": true}')) WHERE json_each(parse_json('{"a": true}'))
375+
376+
statement error 1065
377+
SELECT * FROM json_each(parse_json('{"a": true}')) HAVING json_each(parse_json('{"a": true}'))
378+
373379
query T
374380
SELECT json_array_elements(NULL)
375381
----
@@ -420,6 +426,12 @@ SELECT * FROM json_array_elements(parse_json('[1, [1,2,3], true, {"k1": 1, "k2":
420426
true
421427
{"k1":1,"k2":2}
422428

429+
statement error 1065
430+
SELECT * FROM json_array_elements(parse_json('[true]')) WHERE json_array_elements(parse_json('[true]'))
431+
432+
statement error 1065
433+
SELECT * FROM json_array_elements(parse_json('[true]')) HAVING json_array_elements(parse_json('[true]'))
434+
423435
query T
424436
select parse_json('["1","2","3"]') ? NULL
425437
----
@@ -1107,6 +1119,12 @@ ORDER BY jq:key;
11071119
2 {"key":"scores","value":"[92,88,95]"}
11081120
3 {"key":"scores","value":"[76,80,82]"}
11091121

1122+
statement error 1065
1123+
SELECT * FROM test_data WHERE jq('.scores | min', json_data) > 85
1124+
1125+
statement error 1065
1126+
SELECT * FROM test_data HAVING jq('.scores | min', json_data) > 85
1127+
11101128
statement ok
11111129
DROP TABLE test_data;
11121130

0 commit comments

Comments
 (0)