bubulalabu commented on code in PR #18019:
URL: https://github.com/apache/datafusion/pull/18019#discussion_r2443309793


##########
docs/source/library-user-guide/functions/adding-udfs.md:
##########
@@ -586,6 +586,119 @@ For async UDF implementation details, see 
[`async_udf.rs`](https://github.com/ap
 [`process_scalar_func_inputs`]: 
https://docs.rs/datafusion/latest/datafusion/physical_expr/functions/fn.process_scalar_func_inputs.html
 [`advanced_udf.rs`]: 
https://github.com/apache/datafusion/blob/main/datafusion-examples/examples/advanced_udf.rs
 
+## Named Arguments
+
+DataFusion supports PostgreSQL-style named arguments for scalar functions, 
allowing you to pass arguments by parameter name:
+
+```sql
+SELECT substr(str => 'hello', start_pos => 2, length => 3);
+```
+
+Named arguments can be mixed with positional arguments, but positional 
arguments must come first:
+
+```sql
+SELECT substr('hello', start_pos => 2, length => 3);  -- Valid
+```
+
+### Implementing Functions with Named Arguments
+
+To support named arguments in your UDF, add parameter names to your function's 
signature using `.with_parameter_names()`:
+
+```rust
+# use arrow::datatypes::DataType;
+# use datafusion_expr::{Signature, Volatility};
+#
+# #[derive(Debug)]
+# struct MyFunction {
+#     signature: Signature,
+# }
+#
+impl MyFunction {
+    fn new() -> Self {
+        Self {
+            signature: Signature::uniform(
+                2,
+                vec![DataType::Float64],
+                Volatility::Immutable
+            )
+            .with_parameter_names(vec![
+                "base".to_string(),
+                "exponent".to_string()
+            ])
+            .expect("valid parameter names"),
+        }
+    }
+}
+```
+
+The parameter names should match the order of arguments in your function's 
signature. DataFusion automatically resolves named arguments to the correct 
positional order before invoking your function.
+
+### Example
+
+```rust
+# use std::sync::Arc;
+# use std::any::Any;
+# use arrow::datatypes::DataType;
+# use datafusion_common::Result;
+# use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, Signature, 
Volatility};
+# use datafusion_expr::ScalarUDFImpl;
+
+#[derive(Debug, PartialEq, Eq, Hash)]
+struct PowerFunction {
+    signature: Signature,
+}
+
+impl PowerFunction {
+    fn new() -> Self {
+        Self {
+            signature: Signature::uniform(
+                2,
+                vec![DataType::Float64],
+                Volatility::Immutable
+            )
+            .with_parameter_names(vec![
+                "base".to_string(),
+                "exponent".to_string()
+            ])
+            .expect("valid parameter names"),
+        }
+    }
+}
+
+impl ScalarUDFImpl for PowerFunction {
+    fn as_any(&self) -> &dyn Any { self }
+    fn name(&self) -> &str { "power" }
+    fn signature(&self) -> &Signature { &self.signature }
+
+    fn return_type(&self, _args: &[DataType]) -> Result<DataType> {
+        Ok(DataType::Float64)
+    }
+
+    fn invoke_with_args(&self, _args: ScalarFunctionArgs) -> 
Result<ColumnarValue> {
+        // Your implementation - arguments are in correct positional order
+        unimplemented!()
+    }
+}
+```
+
+Once registered, users can call your function with named arguments:
+
+```sql
+SELECT power(base => 2.0, exponent => 3.0);
+SELECT power(2.0, exponent => 3.0);
+```
+
+### Error Messages
+
+When a function call fails due to incorrect arguments, DataFusion will show 
the parameter names in error messages to help users:
+
+```text
+No function matches the given name and argument types substr(Utf8).
+    Candidate functions:
+    substr(str, start_pos)
+    substr(str, start_pos, length)

Review Comment:
   no, it doesn't, thanks for pointing that out
   
   we now print the name and type for every argument



-- 
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