quwin commented on code in PR #25039:
URL: https://github.com/apache/datafusion/pull/25039#discussion_r4058246132


##########
datafusion/sql/src/statement.rs:
##########
@@ -224,6 +225,56 @@ fn calc_inline_constraints_from_columns(columns: 
&[ColumnDef]) -> Vec<TableConst
     constraints
 }
 
+/// Rejects placeholders in a `CREATE FUNCTION` body or argument default
+/// expression that do not reference a declared argument (e.g. `$3` for a
+/// two-argument function) at definition time, instead of deferring the error
+/// to function invocation.
+fn validate_function_body_placeholders(expr: &Expr, arg_count: usize) -> 
Result<()> {

Review Comment:
   Thanks, placeholder validation now lives in 
`datafusion_expr::CreateFunction::try_new` , rather than in the SQL planner. 
The constructor validates the function body and argument default expressions, 
including placeholders inside  `ScalarSubquery`, `Exists`, `InSubquery`, 
`SetComparison`, and nested subquery plans.
   



##########
datafusion/core/tests/user_defined/user_defined_scalar_functions.rs:
##########
@@ -1125,23 +1125,169 @@ async fn create_scalar_function_from_sql_statement() 
-> Result<()> {
     "#;
     assert!(ctx.sql(bad_definition_sql).await.is_err());
 
-    // FIXME: Definitions with invalid placeholders are allowed, fail at 
runtime
+    // invalid placeholders are rejected at definition time
     let bad_expression_sql = r#"
     CREATE FUNCTION better_add(DOUBLE, DOUBLE)
         RETURNS DOUBLE
         RETURN $1 + $3
     "#;
-    assert!(ctx.sql(bad_expression_sql).await.is_ok());
+    let err = ctx
+        .sql(bad_expression_sql)
+        .await
+        .expect_err("invalid placeholder");
+    let expected = "Error during planning: Invalid placeholder, out of range: 
$3";

Review Comment:
   Got it, the end-to-end SQL assertions for definition-time placeholder errors 
have been moved to `datafusion/sqllogictest/test_files/create_function.slt`. 
The remaining `datafusion-expr` unit test only verifies the low-level traversal 
behavior, including nested subquery plans, which is not practical to test as 
directly through sqllogictest.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to