You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "mihailotim-db (via GitHub)" <gi...@apache.org> on 2025/03/21 09:02:14 UTC

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

mihailotim-db opened a new pull request, #50345:
URL: https://github.com/apache/spark/pull/50345

   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'common/utils/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction`.
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   
   
   ### Why are the changes needed?
   Currently, the analysis fails with `invalid call to dataType`.
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   
   ### Does this PR introduce _any_ user-facing change?
   No.
   <!--
   Note that it means *any* user-facing change including all aspects such as new features, bug fixes, or other behavior changes. Documentation-only updates are not considered user-facing changes.
   
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   
   ### How was this patch tested?
   Added new test cases with expected error message.
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   
   
   ### Was this patch authored or co-authored using generative AI tooling?
   No
   <!--
   If generative AI tooling has been used in the process of authoring this patch, please include the
   phrase: 'Generated-by: ' followed by the name of the tool and its version.
   If no, write 'No'.
   Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
   -->
   


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #50345:
URL: https://github.com/apache/spark/pull/50345#discussion_r2007366871


##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -2954,6 +2954,11 @@
         "message" : [
           "A higher order function expects <expectedNumArgs> arguments, but got <actualNumArgs>."
         ]
+      },
+      "LAMBDA_AS_HIGHER_ORDER_FUNCTION_ARGUMENT" : {
+        "message" : [
+          "A lambda function can only be used as a transformation in higher order functions, not as an argument."

Review Comment:
   This is a bit confusing. Lambda function is also an argument of a high order function.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "mihailotim-db (via GitHub)" <gi...@apache.org>.
mihailotim-db commented on code in PR #50345:
URL: https://github.com/apache/spark/pull/50345#discussion_r2007375023


##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -2954,6 +2954,11 @@
         "message" : [
           "A higher order function expects <expectedNumArgs> arguments, but got <actualNumArgs>."
         ]
+      },
+      "LAMBDA_AS_HIGHER_ORDER_FUNCTION_ARGUMENT" : {
+        "message" : [
+          "A lambda function can only be used as a transformation in higher order functions, not as an argument."

Review Comment:
   This is a good point, I was thinking of removing the second part of the message. Do yuo have a preference/suggestion as to what the message should be?



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "mihailotim-db (via GitHub)" <gi...@apache.org>.
mihailotim-db commented on code in PR #50345:
URL: https://github.com/apache/spark/pull/50345#discussion_r2010253497


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala:
##########
@@ -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

Review Comment:
   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 



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "mihailotim-db (via GitHub)" <gi...@apache.org>.
mihailotim-db commented on code in PR #50345:
URL: https://github.com/apache/spark/pull/50345#discussion_r2007370044


##########
sql/core/src/test/scala/org/apache/spark/sql/errors/QueryCompilationErrorsSuite.scala:
##########
@@ -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))")

Review Comment:
   The error would be: `[INVALID_LAMBDA_FUNCTION_CALL.NON_HIGHER_ORDER_FUNCTION] Invalid lambda function call. A lambda function should only be used in a higher order function.`



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on code in PR #50345:
URL: https://github.com/apache/spark/pull/50345#discussion_r2009442556


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala:
##########
@@ -345,6 +345,15 @@ trait CheckAnalysis extends LookupCatalog with QueryErrorsBase with PlanToString
 
       case operator: LogicalPlan =>
         operator transformExpressionsDown {
+          case hof: HigherOrderFunction if hof.arguments.exists {
+            case LambdaFunction(_, _, _) => true
+            case _ => false
+          } =>
+            throw new AnalysisException(
+              errorClass = s"INVALID_LAMBDA_FUNCTION_CALL.LAMBDA_AS_HIGHER_ORDER_FUNCTION_ARGUMENT",

Review Comment:
   ```suggestion
                 errorClass = "INVALID_LAMBDA_FUNCTION_CALL.LAMBDA_AS_HIGHER_ORDER_FUNCTION_ARGUMENT",
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "mihailotim-db (via GitHub)" <gi...@apache.org>.
mihailotim-db commented on code in PR #50345:
URL: https://github.com/apache/spark/pull/50345#discussion_r2010016659


##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -2945,6 +2945,11 @@
           "The lambda function has duplicate arguments <args>. Please, consider to rename the argument names or set <caseSensitiveConfig> to \"true\"."
         ]
       },
+      "LAMBDA_FUNCTION_AS_ARGUMENT_IS_OUT_OF_POSITION" : {

Review Comment:
   Hm, how strong is your preference? I prefer the current message 



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #50345:
URL: https://github.com/apache/spark/pull/50345#discussion_r2010204787


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala:
##########
@@ -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

Review Comment:
   So after binding, `LambdaFunction` becomes something different?



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #50345:
URL: https://github.com/apache/spark/pull/50345#discussion_r2010000747


##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -2945,6 +2945,11 @@
           "The lambda function has duplicate arguments <args>. Please, consider to rename the argument names or set <caseSensitiveConfig> to \"true\"."
         ]
       },
+      "LAMBDA_FUNCTION_AS_ARGUMENT_IS_OUT_OF_POSITION" : {

Review Comment:
   how about `PARAMETER_NOT_ACCEPT_LAMBDA_FUNCTION`? The message can be
   `You passed the lambda function to a parameter that does not accept. Please check if lambda function argument is in the correct position.`



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "mihailotim-db (via GitHub)" <gi...@apache.org>.
mihailotim-db commented on code in PR #50345:
URL: https://github.com/apache/spark/pull/50345#discussion_r2007373072


##########
sql/core/src/test/scala/org/apache/spark/sql/errors/QueryCompilationErrorsSuite.scala:
##########
@@ -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.LAMBDA_AS_HIGHER_ORDER_FUNCTION_ARGUMENT",
+      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)")

Review Comment:
   Oh you mean another test case. The reason I added it is because the first one is `SimpleHigherOrderFunction` which has 1 arg and 1 function and this one is a bit more complex. We can remove it if you don't think it provides any value



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #50345:
URL: https://github.com/apache/spark/pull/50345#discussion_r2007365737


##########
sql/core/src/test/scala/org/apache/spark/sql/errors/QueryCompilationErrorsSuite.scala:
##########
@@ -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.LAMBDA_AS_HIGHER_ORDER_FUNCTION_ARGUMENT",
+      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)")

Review Comment:
   Does it provide more test coverage?



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #50345:
URL: https://github.com/apache/spark/pull/50345#discussion_r2007365282


##########
sql/core/src/test/scala/org/apache/spark/sql/errors/QueryCompilationErrorsSuite.scala:
##########
@@ -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))")

Review Comment:
   what error people get if they use lambda function in normal functions? like `select abs(x -> x + 1)`



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #50345: [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction`
URL: https://github.com/apache/spark/pull/50345


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "mihailotim-db (via GitHub)" <gi...@apache.org>.
mihailotim-db commented on code in PR #50345:
URL: https://github.com/apache/spark/pull/50345#discussion_r2009574657


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala:
##########
@@ -345,6 +345,15 @@ trait CheckAnalysis extends LookupCatalog with QueryErrorsBase with PlanToString
 
       case operator: LogicalPlan =>
         operator transformExpressionsDown {
+          case hof: HigherOrderFunction if hof.arguments.exists {
+            case LambdaFunction(_, _, _) => true
+            case _ => false
+          } =>
+            throw new AnalysisException(
+              errorClass = s"INVALID_LAMBDA_FUNCTION_CALL.LAMBDA_AS_HIGHER_ORDER_FUNCTION_ARGUMENT",

Review Comment:
   Nice catch, done!



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "mihailotim-db (via GitHub)" <gi...@apache.org>.
mihailotim-db commented on code in PR #50345:
URL: https://github.com/apache/spark/pull/50345#discussion_r2010013971


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala:
##########
@@ -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

Review Comment:
   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. 



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #50345:
URL: https://github.com/apache/spark/pull/50345#discussion_r2010001342


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala:
##########
@@ -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

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



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #50345:
URL: https://github.com/apache/spark/pull/50345#discussion_r2010206567


##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -2945,6 +2945,11 @@
           "The lambda function has duplicate arguments <args>. Please, consider to rename the argument names or set <caseSensitiveConfig> to \"true\"."
         ]
       },
+      "LAMBDA_FUNCTION_AS_ARGUMENT_IS_OUT_OF_POSITION" : {

Review Comment:
   I think OUT_OF_POSITION is confusing. e.g. `select transform(x -> x + 1, x -> x + 1)`, the user does not swap the position but simply pass a lambda function to a parameter that does not accept it.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "mihailotim-db (via GitHub)" <gi...@apache.org>.
mihailotim-db commented on code in PR #50345:
URL: https://github.com/apache/spark/pull/50345#discussion_r2010273365


##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -2945,6 +2945,11 @@
           "The lambda function has duplicate arguments <args>. Please, consider to rename the argument names or set <caseSensitiveConfig> to \"true\"."
         ]
       },
+      "LAMBDA_FUNCTION_AS_ARGUMENT_IS_OUT_OF_POSITION" : {

Review Comment:
   Okay, makes sense. Done!



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #50345:
URL: https://github.com/apache/spark/pull/50345#issuecomment-2750314250

   The spark connect test failure is unrelated, thanks, merging to master/4.0!


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #50345:
URL: https://github.com/apache/spark/pull/50345#discussion_r2010000747


##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -2945,6 +2945,11 @@
           "The lambda function has duplicate arguments <args>. Please, consider to rename the argument names or set <caseSensitiveConfig> to \"true\"."
         ]
       },
+      "LAMBDA_FUNCTION_AS_ARGUMENT_IS_OUT_OF_POSITION" : {

Review Comment:
   how about `PARAMETER_NOT_ACCEPT_LAMBDA_FUNCTION`? The message can be
   `You passed the lambda function to a parameter that does not accept it. Please check if lambda function argument is in the correct position.`



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "mihailotim-db (via GitHub)" <gi...@apache.org>.
mihailotim-db commented on code in PR #50345:
URL: https://github.com/apache/spark/pull/50345#discussion_r2007371277


##########
sql/core/src/test/scala/org/apache/spark/sql/errors/QueryCompilationErrorsSuite.scala:
##########
@@ -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.LAMBDA_AS_HIGHER_ORDER_FUNCTION_ARGUMENT",
+      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)")

Review Comment:
   We don't have a test for this atm, so I guess yes?



##########
sql/core/src/test/scala/org/apache/spark/sql/errors/QueryCompilationErrorsSuite.scala:
##########
@@ -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.LAMBDA_AS_HIGHER_ORDER_FUNCTION_ARGUMENT",
+      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)")

Review Comment:
   We don't have a test for this atm, so I guess yes?



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51580][SQL] Throw proper user facing error message when lambda function is out of place in `HigherOrderFunction` [spark]

Posted by "mihailotim-db (via GitHub)" <gi...@apache.org>.
mihailotim-db commented on code in PR #50345:
URL: https://github.com/apache/spark/pull/50345#discussion_r2007403138


##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -2954,6 +2954,11 @@
         "message" : [
           "A higher order function expects <expectedNumArgs> arguments, but got <actualNumArgs>."
         ]
+      },
+      "LAMBDA_AS_HIGHER_ORDER_FUNCTION_ARGUMENT" : {
+        "message" : [
+          "A lambda function can only be used as a transformation in higher order functions, not as an argument."

Review Comment:
   Updated. Think this is better but you might be able to come up with an even better one



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org