Skip to content

Commit e76f0ee

Browse files
Acfboyalamb
andauthored
fix: IS NULL panic with invalid function without input arguments (#20306)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #20201 . ## Rationale for this change Unlike `Projection`, expressions within a `Filter` node do not always have their types resolved during the initial LogicalPlan generation. Validation is often deferred until the type_coercion phase. When invoking f_up on the expression tree to perform type coercion, the check is bypassed for function nodes with empty arguments, leading to a panic during subsequent execution. For example, all statements below cause a panic: ``` SELECT * FROM (SELECT 1) WHERE (STARTS_WITH() IS NULL); SELECT * FROM (SELECT 1) WHERE (STARTS_WITH() IS NOT NULL); SELECT * FROM (SELECT 'a') WHERE (STARTS_WITH() SIMILAR TO 'abc%'); SELECT * FROM (SELECT 1) WHERE CAST(STARTS_WITH() AS STRING) = 'x'; SELECT * FROM (SELECT 1) WHERE TRY_CAST(STARTS_WITH() AS INT) IS NULL; ``` This pr aims to stop panic. It would be better if reject these invalid cases at planning. ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ```diff fn coerce_arguments_for_signature<F: UDFCoercionExt>( schema: &DFSchema, func: &F, ) -> Result<Vec<Expr>> { - if expressions.is_empty() { - return Ok(expressions); - } ``` Deleted the early return. Thanks to @neilconway . ## Are these changes tested? All original tests passed. Added some unit tests and sqllogictests. ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> No. --------- Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
1 parent d6fb360 commit e76f0ee

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

datafusion/optimizer/src/analyzer/type_coercion.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -954,10 +954,6 @@ fn coerce_arguments_for_signature<F: UDFCoercionExt>(
954954
schema: &DFSchema,
955955
func: &F,
956956
) -> Result<Vec<Expr>> {
957-
if expressions.is_empty() {
958-
return Ok(expressions);
959-
}
960-
961957
let current_fields = expressions
962958
.iter()
963959
.map(|e| e.to_field(schema).map(|(_, f)| f))

datafusion/sqllogictest/test_files/type_coercion.slt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,24 @@ SELECT true FROM t0 WHERE (REGEXP_MATCH(t0.v1, t0.v1)) NOT ILIKE [];
281281

282282
statement ok
283283
DROP TABLE t0;
284+
285+
#############################################################
286+
## Test validation for functions with empty argument lists ##
287+
#############################################################
288+
289+
# https://github.com/apache/datafusion/issues/20201
290+
291+
query error does not support zero arguments
292+
SELECT * FROM (SELECT 1) WHERE (STARTS_WITH() IS NULL);
293+
294+
query error does not support zero arguments
295+
SELECT * FROM (SELECT 1) WHERE (STARTS_WITH() IS NOT NULL);
296+
297+
query error does not support zero arguments
298+
SELECT * FROM (SELECT 'a') WHERE (STARTS_WITH() SIMILAR TO 'abc%');
299+
300+
query error does not support zero arguments
301+
SELECT * FROM (SELECT 1) WHERE CAST(STARTS_WITH() AS STRING) = 'x';
302+
303+
query error does not support zero arguments
304+
SELECT * FROM (SELECT 1) WHERE TRY_CAST(STARTS_WITH() AS INT) = 1;

0 commit comments

Comments
 (0)