Skip to content

[SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in HigherOrderFunction #50345

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions common/utils/src/main/resources/error/error-conditions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2954,6 +2954,11 @@
"message" : [
"A higher order function expects <expectedNumArgs> arguments, but got <actualNumArgs>."
]
},
"PARAMETER_DOES_NOT_ACCEPT_LAMBDA_FUNCTION" : {
"message" : [
"You passed a lambda function to a parameter that does not accept it. Please check if lambda function argument is in the correct position."
]
}
},
"sqlState" : "42K0D"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,16 @@ trait CheckAnalysis extends LookupCatalog with QueryErrorsBase with PlanToString

case operator: LogicalPlan =>
operator transformExpressionsDown {
case hof: HigherOrderFunction if hof.arguments.exists {
case LambdaFunction(_, _, _) => true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need a bit more context. So ideally the analyzer should rewrite LambdaFunction if it's in the right place?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is in the following query pattern: select transform(x -> x + 1, array(1,2,3)). By reversing the parameter order the user sees the error: call to dataType on unresolved object, because the lambda function will not be bound properly. This is because HigherOrderFunction expects a lambda in a certain place but we never enforce this check anywhere. We can't really place it in checkInputDataTypes because we are not going to hit that codepath before erroring out, so we do it in CheckAnalysis instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So after binding, LambdaFunction becomes something different?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we are never going to bind LambdaFunction because in order to bind it, we need to have arguments resolved, but because lambda is an argument we get into a recursive cycle of it never being resolved: https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/higherOrderFunctions.scala#L116

case _ => false
} =>
throw new AnalysisException(
errorClass =
"INVALID_LAMBDA_FUNCTION_CALL.PARAMETER_DOES_NOT_ACCEPT_LAMBDA_FUNCTION",
messageParameters = Map.empty,
origin = hof.origin
)
// Check argument data types of higher-order functions downwards first.
// If the arguments of the higher-order functions are resolved but the type check fails,
// the argument functions will not get resolved, but we should report the argument type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,29 @@ class QueryCompilationErrorsSuite
context = ExpectedContext(fragment = "in (select map(1,2))", start = 16, stop = 35)
)
}

test("SPARK-51580: Throw proper user facing error message when lambda function is out of " +
"place in HigherOrderFunction") {
checkError(
exception = intercept[AnalysisException] {
sql("select transform(x -> x + 1, array(1,2,3))")
},
condition = "INVALID_LAMBDA_FUNCTION_CALL.PARAMETER_DOES_NOT_ACCEPT_LAMBDA_FUNCTION",
parameters = Map(),
context =
ExpectedContext(fragment = "transform(x -> x + 1, array(1,2,3))", start = 7, stop = 41)
)

checkError(
exception = intercept[AnalysisException] {
sql("select aggregate(array(1,2,3), x -> x + 1, 0)")
},
condition = "INVALID_LAMBDA_FUNCTION_CALL.PARAMETER_DOES_NOT_ACCEPT_LAMBDA_FUNCTION",
parameters = Map(),
context =
ExpectedContext(fragment = "aggregate(array(1,2,3), x -> x + 1, 0)", start = 7, stop = 44)
)
}
}

class MyCastToString extends SparkUserDefinedFunction(
Expand Down