Jefffrey commented on code in PR #18019:
URL: https://github.com/apache/datafusion/pull/18019#discussion_r2453937840
##########
datafusion/sql/tests/sql_integration.rs:
##########
@@ -3949,6 +3954,79 @@ fn test_double_quoted_literal_string() {
assert!(logical_plan("SELECT \"1\"").is_err());
}
+#[test]
+fn test_named_arguments_with_dialects() {
+ let sql = "SELECT my_func(arg1 => 'value1')";
Review Comment:
I find this test quite hard to read and understand what exactly it is
supposed to test; could we get away with removing this and potentially only
doing it via SLT test cases? (I see we already have some postgres & mssql
dialect cases so maybe those are already sufficient)
##########
datafusion/sql/src/expr/function.rs:
##########
@@ -668,8 +703,44 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
qualifier: qualifier.into(),
options: Box::new(WildcardOptions::default()),
};
-
- Ok(expr)
+ Ok((expr, None))
+ }
+ // PostgreSQL dialect uses ExprNamed variant with expression for
name
+ FunctionArg::ExprNamed {
+ name,
+ arg: FunctionArgExpr::Expr(arg),
+ operator: _,
+ } => {
+ let expr = self.sql_expr_to_logical_expr(arg, schema,
planner_context)?;
+ let arg_name = match name {
+ SQLExpr::Identifier(ident) =>
crate::utils::normalize_ident(ident),
+ _ => {
+ return plan_err!(
+ "Named argument must use a simple identifier, got:
{name:?}"
+ )
+ }
+ };
+ Ok((expr, Some(arg_name)))
+ }
Review Comment:
```suggestion
FunctionArg::ExprNamed {
name: SQLExpr::Identifier(name),
arg: FunctionArgExpr::Expr(arg),
operator: _,
} => {
let expr = self.sql_expr_to_logical_expr(arg, schema,
planner_context)?;
let arg_name = crate::utils::normalize_ident(name);
Ok((expr, Some(arg_name)))
}
```
Thoughts on simplifying to something like this? (same for below arm)
I can't visualize the case this is guarding against so I don't know if its
better to trade off more concise code with a less specific error (since we'd
then hit the `not_impl_err!("Unsupported qualified wildcard argument:
{sql:?}")` catchall now) or if it is indeed better to keep current version and
have a more specific error 🤔
##########
datafusion/sqllogictest/test_files/named_arguments.slt:
##########
@@ -0,0 +1,135 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+#############
+## Tests for Named Arguments (PostgreSQL-style param => value syntax)
+#############
+
+# Test positional arguments still work (baseline)
+query T
+SELECT substr('hello world', 7, 5);
+----
+world
+
+# Test named arguments in order
+query T
+SELECT substr(str => 'hello world', start_pos => 7, length => 5);
+----
+world
+
+# Test named arguments out of order
+query T
+SELECT substr(length => 5, str => 'hello world', start_pos => 7);
+----
+world
+
+# Test mixed positional and named arguments
+query T
+SELECT substr('hello world', start_pos => 7, length => 5);
+----
+world
+
+# Test with only 2 parameters (length optional)
+query T
+SELECT substr(str => 'hello world', start_pos => 7);
+----
+world
+
+# Test all parameters named with substring alias
+query T
+SELECT substring(str => 'hello', start_pos => 1, length => 3);
+----
+hel
+
+# Error: positional argument after named argument
+query error DataFusion error: Error during planning: Positional
argument.*follows named argument
+SELECT substr(str => 'hello', 1, 3);
+
+# Error: unknown parameter name
+query error DataFusion error: Error during planning: Unknown parameter name
'invalid'
+SELECT substr(invalid => 'hello', start_pos => 1, length => 3);
+
+# Error: duplicate parameter name
+query error DataFusion error: Error during planning: Parameter 'str' specified
multiple times
+SELECT substr(str => 'hello', str => 'world', start_pos => 1);
+
+# Test case-insensitive parameter names (unquoted identifiers)
+query T
+SELECT substr(STR => 'hello world', START_POS => 7, LENGTH => 5);
+----
+world
+
+# Test case-insensitive with mixed case
+query T
+SELECT substr(Str => 'hello world', Start_Pos => 7);
+----
+world
+
+# Error: case-sensitive quoted parameter names don't match
+query error DataFusion error: Error during planning: Unknown parameter name
'STR'
+SELECT substr("STR" => 'hello world', "start_pos" => 7);
+
+# Error: wrong number of arguments
+# This query provides only 1 argument but substr requires 2 or 3
+query error DataFusion error: Error during planning: Execution error: Function
'substr' user-defined coercion failed with "Error during planning: The substr
function requires 2 or 3 arguments, but got 1."
+SELECT substr(str => 'hello world');
+
+#############
+## PostgreSQL Dialect Tests (uses ExprNamed variant)
+#############
+
+statement ok
+set datafusion.sql_parser.dialect = 'PostgreSQL';
+
+# Test named arguments in order
+query T
+SELECT substr(str => 'hello world', start_pos => 7, length => 5);
+----
+world
+
+# Test named arguments out of order
+query T
+SELECT substr(length => 5, str => 'hello world', start_pos => 7);
+----
+world
+
+# Test mixed positional and named arguments
+query T
+SELECT substr('hello world', start_pos => 7, length => 5);
+----
+world
+
+# Test with only 2 parameters (length optional)
+query T
+SELECT substr(str => 'hello world', start_pos => 7);
+----
+world
+
+# Reset to default dialect
+statement ok
+set datafusion.sql_parser.dialect = 'Generic';
+
+#############
+## MsSQL Dialect Tests (does NOT support => operator)
+#############
+
+statement ok
+set datafusion.sql_parser.dialect = 'MsSQL';
+
+# Error: MsSQL dialect does not support => operator
+query error DataFusion error: SQL error: ParserError\("Expected: \), found: =>
at Line: 1, Column: 19"\)
+SELECT substr(str => 'hello world', start_pos => 7, length => 5);
Review Comment:
```suggestion
SELECT substr(str => 'hello world', start_pos => 7, length => 5);
# Reset to default dialect
statement ok
set datafusion.sql_parser.dialect = 'Generic';
```
nit: so if more cases are added below we don't forget to reset the dialect
(there's been a few cases where we forget to unset configs in our SLT tests 😅 )
--
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]