-
Notifications
You must be signed in to change notification settings - Fork 28.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-43922] Add named parameter support in parser for function calls #41429
Changes from 18 commits
c7215fb
5e168de
e044d14
6e44c55
246d5c1
20b9b93
12c814b
95b3f20
8312980
30a72e9
515d153
7a8f4ad
23f9c98
f53dab7
b26fd43
3ae0ee6
4eff719
4038773
3209f60
23075d5
c879012
c60f1a4
208d005
90dafcb
dc868e7
25775f7
cb0d011
9a5f74e
566c994
65d9d95
7653225
f071073
7d99b58
345b96d
704dcad
01c3af9
c6ca3bd
da49eed
5735732
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -789,7 +789,7 @@ inlineTable | |
; | ||
|
||
functionTable | ||
: funcName=functionName LEFT_PAREN (expression (COMMA expression)*)? RIGHT_PAREN tableAlias | ||
: funcName=functionName LEFT_PAREN (functionArgument (COMMA functionArgument)*)? RIGHT_PAREN tableAlias | ||
; | ||
|
||
tableAlias | ||
|
@@ -862,6 +862,15 @@ expression | |
: booleanExpression | ||
; | ||
|
||
namedArgumentExpression | ||
: key=identifier FAT_ARROW value=expression | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. identifier can be quoted and unquoted There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gengliangwang After looking at the design doc, would it be fine to keep it like this? |
||
; | ||
|
||
functionArgument | ||
: expression | ||
| namedArgumentExpression | ||
; | ||
|
||
expressionSeq | ||
: expression (COMMA expression)* | ||
; | ||
|
@@ -921,7 +930,8 @@ primaryExpression | |
| LEFT_PAREN namedExpression (COMMA namedExpression)+ RIGHT_PAREN #rowConstructor | ||
| LEFT_PAREN query RIGHT_PAREN #subqueryExpression | ||
| IDENTIFIER_KW LEFT_PAREN expression RIGHT_PAREN #identifierClause | ||
| functionName LEFT_PAREN (setQuantifier? argument+=expression (COMMA argument+=expression)*)? RIGHT_PAREN | ||
| functionName LEFT_PAREN (setQuantifier? argument+=functionArgument | ||
(COMMA argument+=functionArgument)*)? RIGHT_PAREN | ||
(FILTER LEFT_PAREN WHERE where=booleanExpression RIGHT_PAREN)? | ||
(nullsOption=(IGNORE | RESPECT) NULLS)? ( OVER windowSpec)? #functionCall | ||
| identifier ARROW expression #lambda | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
learningchess2003 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* 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 { | ||
learningchess2003 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
override def nullable: Boolean = value.nullable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: we can remove this line as |
||
|
||
override def dataType: DataType = value.dataType | ||
|
||
override def toString: String = s"$key => $value" | ||
|
||
// NamedArgumentExpression has a single child, which is its value expression, | ||
// so the value expression can be resolved by Analyzer rules recursively. | ||
// For example, when the value is a built-in function expression, | ||
// it must be resolved by [[ResolveFunctions]] | ||
override def child: Expression = value | ||
|
||
override protected def withNewChildInternal(newChild: Expression): Expression = | ||
copy(value = newChild) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ import org.apache.spark._ | |
import org.apache.spark.sql.{AnalysisException, DataFrame, Dataset, QueryTest, Row, SaveMode} | ||
import org.apache.spark.sql.catalyst.FunctionIdentifier | ||
import org.apache.spark.sql.catalyst.analysis.{Parameter, UnresolvedGenerator} | ||
import org.apache.spark.sql.catalyst.expressions.{Grouping, Literal, RowNumber} | ||
import org.apache.spark.sql.catalyst.expressions.{Grouping, Literal, NamedArgumentExpression, RowNumber} | ||
import org.apache.spark.sql.catalyst.expressions.CodegenObjectFactoryMode._ | ||
import org.apache.spark.sql.catalyst.expressions.codegen.CodegenContext | ||
import org.apache.spark.sql.catalyst.expressions.objects.InitializeJavaBean | ||
|
@@ -837,6 +837,18 @@ class QueryExecutionErrorsSuite | |
sqlState = "XX000") | ||
} | ||
|
||
test("INTERNAL_ERROR: Calling eval on Unevaluable NamedArgumentExpression") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need to test it. |
||
val e = intercept[SparkException] { | ||
NamedArgumentExpression("arg0", Literal("value0")).eval() | ||
} | ||
checkError( | ||
exception = e, | ||
errorClass = "INTERNAL_ERROR", | ||
parameters = Map("message" -> "Cannot evaluate expression: arg0 => value0"), | ||
sqlState = "XX000" | ||
) | ||
} | ||
|
||
test("INTERNAL_ERROR: Calling doGenCode on unresolved") { | ||
val e = intercept[SparkException] { | ||
val ctx = new CodegenContext | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like it can be enabled. If so, could you describe how in the error message.
If it is just not supported, could you add a sub-class to
UNSUPPORTED_FEATURE