dtenedor commented on code in PR #41429:
URL: https://github.com/apache/spark/pull/41429#discussion_r1227109912
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/ExpressionParserSuite.scala:
##########
@@ -329,6 +329,18 @@ class ExpressionParserSuite extends AnalysisTest {
parameters = Map("error" -> "'x'", "hint" -> ": extra input 'x'"))
}
+ test("function expressions with named arguments") {
+ assertEqual("encode(value => 'abc', charset => 'utf-8')",
Review Comment:
Please also add positive tests with:
* constant but non-literal values, e.g. `'utf' || '-8'` or `abs(42)`
* double parentheses in a named argument, e.g. `arg1 => ((select 1))`
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/NamedArgumentExpression.scala:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.
+ */
+
+package org.apache.spark.sql.catalyst.expressions
+
+import org.apache.spark.sql.types.DataType
+
+/**
+ * An unevaluable unary expression specifically for named argument functions
+ *
+ * SQL Syntax: key => value
+ * SQL grammar: key=identifier FAT_ARROW value=expression
+ *
+ * Example usage in encode:
+ * SELECT encode("abc", charset => "utf-8");
+ * The second argument generates NamedArgumentExpression("charset",
Literal("utf-8"))
+ * SELECT encode(charset => "utf-8", value => "abc");
+ *
+ * @param key The name of the function argument
+ * @param value The value of the function argument
+ */
+case class NamedArgumentExpression(key: String, value: Expression)
+ extends UnaryExpression with Unevaluable {
+ override def nullable: Boolean = value.nullable
+
+ override def dataType: DataType = value.dataType
+
+ override def toString: String = s"""$key => $value"""
Review Comment:
```suggestion
override def toString: String = s"$key => $value"
```
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/PlanParserSuite.scala:
##########
@@ -1412,6 +1412,31 @@ class PlanParserSuite extends AnalysisTest {
assertEqual("select a, b from db.c; ;; ;", table("db", "c").select($"a",
$"b"))
}
+ test("table valued function with named arguments") {
+ // All named arguments
Review Comment:
Can you add some negative test cases that return errors? You can look here
[1] for some examples, and here are some I can think of:
```
select * from my_tvf(arg1 -> 'value1')
select * from my_tvf(arg1 = => 'value1')
select * from my_tvf(arg1 ==> 'value1')
select * from my_tvf((arg1 => 'value1'))
select * from my_tvf(arg1 'value1')
select * from my_tvf(arg1 => )
select * from my_tvf(arg1 => , 42)
select * from my_tvf(my_tvf.arg1 => 'value1')
select * from my_tvf(arg1 => table t1)
select * from my_tvf(group => 'abc')
```
The last one checks that a reserved keyword like `GROUP` or `ORDER` will
work as a named argument name.
[1]
https://github.com/google/zetasql/blob/master/zetasql/parser/testdata/named_arguments.test
--
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]