MaxGekk commented on code in PR #41429:
URL: https://github.com/apache/spark/pull/41429#discussion_r1234075810
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala:
##########
@@ -1513,6 +1513,19 @@ class AstBuilder extends
SqlBaseParserBaseVisitor[AnyRef] with SQLConfHelper wit
}
}
+ private def extractExpression(expr: FunctionArgumentContext, funcName:
String) : Expression = {
+ Option(expr.namedArgumentExpression).map { n =>
+ if (SQLConf.get.getConf(SQLConf.ALLOW_NAMED_FUNCTION_ARGUMENTS)) {
Review Comment:
```suggestion
if (conf.getConf(SQLConf.ALLOW_NAMED_FUNCTION_ARGUMENTS)) {
```
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala:
##########
@@ -193,6 +193,14 @@ private[sql] object QueryCompilationErrors extends
QueryErrorsBase {
messageParameters = Map("configKey" -> configKey))
}
+ def namedArgumentsNotEnabledError(functionName: String, argumentName:
String): Throwable = {
+ new AnalysisException(
+ errorClass = "NAMED_ARGUMENTS_SUPPORT_DISABLED",
+ messageParameters = Map("functionName" -> toSQLId(functionName),
+ "argument" -> toSQLId(argumentName))
Review Comment:
nit:
```suggestion
messageParameters = Map(
"functionName" -> toSQLId(functionName),
"argument" -> toSQLId(argumentName))
```
##########
core/src/main/resources/error/error-classes.json:
##########
@@ -1580,6 +1580,11 @@
"Not allowed to implement multiple UDF interfaces, UDF class
<className>."
]
},
+ "NAMED_ARGUMENTS_SUPPORT_DISABLED" : {
Review Comment:
Sounds like it can be enabled. If so, could you describe how in the error
message.
> ... are not supported here
If it is just not supported, could you add a sub-class to
`UNSUPPORTED_FEATURE`
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala:
##########
@@ -1513,6 +1513,19 @@ class AstBuilder extends
SqlBaseParserBaseVisitor[AnyRef] with SQLConfHelper wit
}
}
+ private def extractExpression(expr: FunctionArgumentContext, funcName:
String) : Expression = {
+ Option(expr.namedArgumentExpression).map { n =>
+ if (SQLConf.get.getConf(SQLConf.ALLOW_NAMED_FUNCTION_ARGUMENTS)) {
+ NamedArgumentExpression(n.key.getText, expression(n.value))
+ } else {
+ throw QueryCompilationErrors.namedArgumentsNotEnabledError(
+ funcName, n.key.getText)
Review Comment:
```suggestion
throw QueryCompilationErrors.namedArgumentsNotEnabledError(funcName,
n.key.getText)
```
##########
sql/core/src/test/scala/org/apache/spark/sql/errors/QueryParsingErrorsSuite.scala:
##########
@@ -20,16 +20,33 @@ package org.apache.spark.sql.errors
import org.apache.spark.SparkThrowable
import org.apache.spark.sql.QueryTest
import org.apache.spark.sql.catalyst.parser.ParseException
+import org.apache.spark.sql.catalyst.plans.SQLHelper
+import org.apache.spark.sql.catalyst.util.TypeUtils.toSQLId
import org.apache.spark.sql.test.SharedSparkSession
// Turn of the length check because most of the tests check entire error
messages
// scalastyle:off line.size.limit
-class QueryParsingErrorsSuite extends QueryTest with SharedSparkSession {
+class QueryParsingErrorsSuite extends QueryTest with SharedSparkSession with
SQLHelper {
private def parseException(sqlText: String): SparkThrowable = {
intercept[ParseException](sql(sqlText).collect())
}
+ test("NAMED_ARGUMENTS_SUPPORT_DISABLED: named arguments not turned on") {
+ withSQLConf("spark.sql.allowNamedFunctionArguments" -> "false") {
+ checkError(
+ exception = parseException("SELECT * FROM encode(value => 'abc',
charset => 'utf-8')"),
+ errorClass = "NAMED_ARGUMENTS_SUPPORT_DISABLED",
+ parameters = Map("functionName" -> toSQLId("encode"), "argument" ->
toSQLId("value"))
Review Comment:
nit: just enough to use the expected values instead of `toSQLId`
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/NamedArgumentExpression.scala:
##########
@@ -0,0 +1,59 @@
+/*
+ * 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
+
+/**
+ * This represents an argument expression to a function call accompanied with
an
+ * explicit reference to the corresponding argument name as a string. In this
way,
+ * the analyzer can make sure that the provided values match up to the
arguments
+ * as intended, and the arguments may appear in any order.
+ * This unary expression is unevaluable because we intend to replace it with
+ * the provided value itself during query analysis (after possibly rearranging
+ * the parsed argument list to match up the names to the expected function
+ * signature).
+ *
+ * SQL Syntax: key => value
+ * SQL grammar: key=identifier FAT_ARROW value=expression
+ *
+ * Example usage with the "encode" scalar function:
+ * 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 {
Review Comment:
see https://github.com/databricks/scala-style-guide#spacing-and-indentation
```suggestion
extends UnaryExpression with Unevaluable {
```
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/ExpressionParserSuite.scala:
##########
@@ -329,6 +330,27 @@ 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')",
+ $"encode".function(NamedArgumentExpression("value",
Literal("abc")),
+ NamedArgumentExpression("charset", Literal("utf-8"))))
Review Comment:
Please, fix indentation here and below according to
https://github.com/databricks/scala-style-guide
```suggestion
$"encode".function(NamedArgumentExpression("value", Literal("abc")),
NamedArgumentExpression("charset", Literal("utf-8"))))
```
--
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]