You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "Hisoka-X (via GitHub)" <gi...@apache.org> on 2023/08/24 14:25:46 UTC

[GitHub] [spark] Hisoka-X opened a new pull request, #42661: [SPARK-44743][SQL] Fix `reflect` method behavior match with hive

Hisoka-X opened a new pull request, #42661:
URL: https://github.com/apache/spark/pull/42661

   <!--
   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
        'core/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   This PR let `reflect` method return `null` when invoke method throw exception. Add config `spark.sql.legacy.reflectThrowException` to protect the behavior change. If set true (default false), will not return null but throw exception as the way as before this PR.
   
   Hive will return null if invoke method throw exception. https://github.com/apache/hive/blob/1ebef40aba00c4ec5376d8f0623196b42425a589/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFReflect.java#L145C3-L151C17
   
   <!--
   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?
   To correct the behavior of the `reflect` function to make it consistent with hive
   <!--
   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?
   Yes, will return null if invoke method throw exception.
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   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?
   add new test.
   <!--
   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


[GitHub] [spark] cloud-fan commented on a diff in pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/CallMethodViaReflection.scala:
##########
@@ -133,8 +138,13 @@ case class CallMethodViaReflection(children: Seq[Expression])
       }
       i += 1
     }
-    val ret = method.invoke(null, buffer : _*)
-    UTF8String.fromString(String.valueOf(ret))
+    try {
+      val ret = method.invoke(null, buffer : _*)
+      UTF8String.fromString(String.valueOf(ret))
+    } catch {
+      case _: Exception if !failOnError =>

Review Comment:
   shall we catch `NonFatal`?



-- 
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


[GitHub] [spark] Hisoka-X commented on a diff in pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

Posted by "Hisoka-X (via GitHub)" <gi...@apache.org>.
Hisoka-X commented on code in PR #42661:
URL: https://github.com/apache/spark/pull/42661#discussion_r1306308572


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/CallMethodViaReflection.scala:
##########
@@ -55,10 +57,16 @@ import org.apache.spark.util.Utils
   """,
   since = "2.0.0",
   group = "misc_funcs")
-case class CallMethodViaReflection(children: Seq[Expression])
+case class CallMethodViaReflection(
+      children: Seq[Expression],
+      failOnError: Boolean = SQLConf.get.ansiEnabled)

Review Comment:
   Addressed all! Thanks @cloud-fan 



-- 
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


[GitHub] [spark] Hisoka-X commented on pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

Posted by "Hisoka-X (via GitHub)" <gi...@apache.org>.
Hisoka-X commented on PR #42661:
URL: https://github.com/apache/spark/pull/42661#issuecomment-1702666887

   Thanks @cloud-fan for your patience help, and @srielau @dongjoon-hyun


-- 
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


[GitHub] [spark] cloud-fan commented on a diff in pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/CallMethodViaReflection.scala:
##########
@@ -133,8 +141,15 @@ case class CallMethodViaReflection(children: Seq[Expression])
       }
       i += 1
     }
-    val ret = method.invoke(null, buffer : _*)
-    UTF8String.fromString(String.valueOf(ret))
+    try {
+      val ret = method.invoke(null, buffer : _*)
+      UTF8String.fromString(String.valueOf(ret))
+    } catch {
+      case e: Exception if !failOnError =>
+        log.error("Reflect evaluate " + e + " method = " + method + " args = " +

Review Comment:
   why do we log? other functions do not log when error happens and returns null.



-- 
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


[GitHub] [spark] Hisoka-X commented on a diff in pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

Posted by "Hisoka-X (via GitHub)" <gi...@apache.org>.
Hisoka-X commented on code in PR #42661:
URL: https://github.com/apache/spark/pull/42661#discussion_r1306927209


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/CallMethodViaReflection.scala:
##########
@@ -133,8 +138,13 @@ case class CallMethodViaReflection(children: Seq[Expression])
       }
       i += 1
     }
-    val ret = method.invoke(null, buffer : _*)
-    UTF8String.fromString(String.valueOf(ret))
+    try {
+      val ret = method.invoke(null, buffer : _*)
+      UTF8String.fromString(String.valueOf(ret))
+    } catch {
+      case _: Exception if !failOnError =>

Review Comment:
   good point, changed.



-- 
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


[GitHub] [spark] Hisoka-X commented on a diff in pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

Posted by "Hisoka-X (via GitHub)" <gi...@apache.org>.
Hisoka-X commented on code in PR #42661:
URL: https://github.com/apache/spark/pull/42661#discussion_r1306309397


##########
sql/core/src/test/resources/sql-tests/analyzer-results/try_reflect.sql.out:
##########
@@ -0,0 +1,165 @@
+-- Automatically generated by SQLQueryTestSuite
+-- !query
+SELECT try_reflect("java.util.UUID", "fromString", "a5cf6c42-0c85-418f-af6c-3e4e5b1328f2")
+-- !query analysis
+Project [try_reflect(java.util.UUID, fromString, a5cf6c42-0c85-418f-af6c-3e4e5b1328f2) AS try_reflect(java.util.UUID, fromString, a5cf6c42-0c85-418f-af6c-3e4e5b1328f2)#x]
++- OneRowRelation
+
+
+-- !query
+SELECT try_reflect("java.lang.String", "valueOf", 1)
+-- !query analysis
+Project [try_reflect(java.lang.String, valueOf, 1) AS try_reflect(java.lang.String, valueOf, 1)#x]
++- OneRowRelation
+
+
+-- !query
+SELECT try_reflect("java.lang.Math", "max", 2, 3)
+-- !query analysis
+Project [try_reflect(java.lang.Math, max, 2, 3) AS try_reflect(java.lang.Math, max, 2, 3)#x]
++- OneRowRelation
+
+
+-- !query
+SELECT try_reflect("java.lang.Math", "min", 2, 3)
+-- !query analysis
+Project [try_reflect(java.lang.Math, min, 2, 3) AS try_reflect(java.lang.Math, min, 2, 3)#x]
++- OneRowRelation
+
+
+-- !query
+SELECT try_reflect("java.lang.Integer", "valueOf", "10", 16)
+-- !query analysis
+Project [try_reflect(java.lang.Integer, valueOf, 10, 16) AS try_reflect(java.lang.Integer, valueOf, 10, 16)#x]
++- OneRowRelation
+
+
+-- !query
+SELECT try_reflect("java.util.UUID", "fromString", "b")
+-- !query analysis
+Project [try_reflect(java.util.UUID, fromString, b) AS try_reflect(java.util.UUID, fromString, b)#x]
++- OneRowRelation
+
+
+-- !query
+SELECT try_reflect("java.net.URLDecoder", "decode", "%")
+-- !query analysis
+Project [try_reflect(java.net.URLDecoder, decode, %) AS try_reflect(java.net.URLDecoder, decode, %)#x]
++- OneRowRelation
+
+
+-- !query
+SELECT try_reflect("java.lang.Math", "max", "test", 3)
+-- !query analysis
+org.apache.spark.SparkException
+{
+  "errorClass" : "INTERNAL_ERROR",
+  "sqlState" : "XX000",
+  "messageParameters" : {
+    "message" : "Cannot resolve the runtime replaceable expression \"try_reflect(java.lang.Math, max, test, 3)\". The replacement is unresolved: \"reflect(java.lang.Math, max, test, 3)\"."
+  }
+}

Review Comment:
   When replacement expression `checkInputDataTypes` return failed, this error will be reported. It's not `try_reflect`'s bug, it's all `RuntimeReplaceable` will face problem. In fact if we can throw `checkInputDataTypes` failed reason would be better. I'm trying solve it on another PR. cc @cloud-fan 



-- 
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


[GitHub] [spark] cloud-fan commented on pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

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

   thanks, merging to master!


-- 
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


[GitHub] [spark] cloud-fan closed pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function
URL: https://github.com/apache/spark/pull/42661


-- 
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


[GitHub] [spark] Hisoka-X commented on pull request #42661: [SPARK-44743][SQL] Fix `reflect` method behavior match with hive

Posted by "Hisoka-X (via GitHub)" <gi...@apache.org>.
Hisoka-X commented on PR #42661:
URL: https://github.com/apache/spark/pull/42661#issuecomment-1692560437

   > If we are consistent, we add try_reflect.
   > We could hitch the NULL for reflect to ansi_enabled = false.
   
   oh, it make sense. let me add `try_reflect` 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


[GitHub] [spark] Hisoka-X commented on a diff in pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

Posted by "Hisoka-X (via GitHub)" <gi...@apache.org>.
Hisoka-X commented on code in PR #42661:
URL: https://github.com/apache/spark/pull/42661#discussion_r1306925328


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/TryEval.scala:
##########
@@ -236,3 +236,35 @@ case class TryToBinary(
   override protected def withNewChildInternal(newChild: Expression): Expression =
     this.copy(replacement = newChild)
 }
+
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+  usage = "_FUNC_(class, method[, arg1[, arg2 ..]]) - This is a special version of `reflect` that" +
+    " performs the same operation, but returns a NULL value instead of raising an error if the invoke method thrown exception.",
+  examples = """
+    Examples:
+      > SELECT _FUNC_('java.util.UUID', 'randomUUID');
+       c33fb387-8500-4bfa-81d2-6e0e3e930df2
+      > SELECT _FUNC_('java.util.UUID', 'fromString', 'a5cf6c42-0c85-418f-af6c-3e4e5b1328f2');
+       a5cf6c42-0c85-418f-af6c-3e4e5b1328f2
+      > SELECT _FUNC_('java.net.URLDecoder', 'decode', '%');
+       NULL
+  """,
+  since = "4.0.0",
+  group = "misc_funcs")
+// scalastyle:on line.size.limit
+case class TryReflect(params: Seq[Expression], replacement: Expression) extends RuntimeReplaceable
+  with InheritAnalysisRules {

Review Comment:
   Does `checkInputDataTypes` is  analysis rules? Without `InheritAnalysisRules`, the checkInputDataTypes error will not return normally.
   ![image](https://github.com/apache/spark/assets/32387433/73e7a159-53e2-4646-a653-77b618132f30)
   



-- 
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


[GitHub] [spark] cloud-fan commented on pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

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

   > what's the agreed upon behavior for vanilla reflect() now?
   
   I prefer to leave it as it was, which is failing instead of returning null.


-- 
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


[GitHub] [spark] cloud-fan commented on pull request #42661: [SPARK-44743][SQL] Fix `reflect` method behavior match with hive

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

   I don't think we should blindly follow hive, but it seems a reasonable requirement to specify the failure behavior of the `reflect` function.
   
   @srielau any ideas? Shall we add a new parameter to this function or add another function with a different name?


-- 
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


[GitHub] [spark] Hisoka-X commented on a diff in pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

Posted by "Hisoka-X (via GitHub)" <gi...@apache.org>.
Hisoka-X commented on code in PR #42661:
URL: https://github.com/apache/spark/pull/42661#discussion_r1306867550


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/CallMethodViaReflection.scala:
##########
@@ -55,64 +55,73 @@ import org.apache.spark.util.Utils
   """,
   since = "2.0.0",
   group = "misc_funcs")
-case class CallMethodViaReflection(children: Seq[Expression])
+case class CallMethodViaReflection(
+      children: Seq[Expression],
+      failOnError: Boolean = true)
   extends Nondeterministic
   with CodegenFallback
   with QueryErrorsBase {
 
+  def this(children: Seq[Expression]) =
+    this(children, true)
+
   override def prettyName: String = getTagValue(FunctionRegistry.FUNC_ALIAS).getOrElse("reflect")
 
   override def checkInputDataTypes(): TypeCheckResult = {
-    if (children.size < 2) {
-      throw QueryCompilationErrors.wrongNumArgsError(
-        toSQLId(prettyName), Seq("> 1"), children.length
-      )
+    if (!failOnError) {

Review Comment:
   I am also a little uncertain about this. In the `try_reflect` function, if the method cannot be found or the parameter type does not match, do we need to report an error, or return null? Or should we report an error for the parameter size mismatch, and return null for other errors ?



-- 
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


[GitHub] [spark] cloud-fan commented on a diff in pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/CallMethodViaReflection.scala:
##########
@@ -55,64 +55,73 @@ import org.apache.spark.util.Utils
   """,
   since = "2.0.0",
   group = "misc_funcs")
-case class CallMethodViaReflection(children: Seq[Expression])
+case class CallMethodViaReflection(
+      children: Seq[Expression],
+      failOnError: Boolean = true)
   extends Nondeterministic
   with CodegenFallback
   with QueryErrorsBase {
 
+  def this(children: Seq[Expression]) =
+    this(children, true)
+
   override def prettyName: String = getTagValue(FunctionRegistry.FUNC_ALIAS).getOrElse("reflect")
 
   override def checkInputDataTypes(): TypeCheckResult = {
-    if (children.size < 2) {
-      throw QueryCompilationErrors.wrongNumArgsError(
-        toSQLId(prettyName), Seq("> 1"), children.length
-      )
+    if (!failOnError) {

Review Comment:
   I don't think this flag should change the analysis check. Look at the `lazy val`s in this expression, how can it work with less than 2 children?



-- 
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


[GitHub] [spark] cloud-fan commented on a diff in pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

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


##########
sql/core/src/test/resources/sql-tests/inputs/try_reflect.sql:
##########
@@ -0,0 +1,19 @@
+-- positive
+SELECT try_reflect("java.util.UUID", "fromString", "a5cf6c42-0c85-418f-af6c-3e4e5b1328f2");
+SELECT try_reflect("java.lang.String", "valueOf", 1);
+SELECT try_reflect("java.lang.Math", "max", 2, 3);
+SELECT try_reflect("java.lang.Math", "min", 2, 3);
+SELECT try_reflect("java.lang.Integer", "valueOf", "10", 16);
+
+-- negative

Review Comment:
   does it really matter to put so many similar tests? I'd like to think more about the test coverage:
   - runtime errors
   - non-existing methods
   - non-existing class
   - invalid invocation (`try_reflect()` with no arg), since I caught https://github.com/apache/spark/pull/42661/files#r1306863750



-- 
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


[GitHub] [spark] cloud-fan commented on a diff in pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/CallMethodViaReflection.scala:
##########
@@ -55,10 +57,16 @@ import org.apache.spark.util.Utils
   """,
   since = "2.0.0",
   group = "misc_funcs")
-case class CallMethodViaReflection(children: Seq[Expression])
+case class CallMethodViaReflection(
+      children: Seq[Expression],
+      failOnError: Boolean = SQLConf.get.ansiEnabled)

Review Comment:
   let's not change the behavior of `CallMethodViaReflection`. The `failOnError` should be true by default.



-- 
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


[GitHub] [spark] dongjoon-hyun commented on pull request #42661: [SPARK-44743][SQL] Fix `reflect` method behavior match with hive

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #42661:
URL: https://github.com/apache/spark/pull/42661#issuecomment-1692216639

   cc @sunchao , too.


-- 
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


[GitHub] [spark] Hisoka-X commented on pull request #42661: [SPARK-44743][SQL] Fix `reflect` method behavior match with hive

Posted by "Hisoka-X (via GitHub)" <gi...@apache.org>.
Hisoka-X commented on PR #42661:
URL: https://github.com/apache/spark/pull/42661#issuecomment-1691788396

   cc @cloud-fan @MaxGekk @dongjoon-hyun 


-- 
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


[GitHub] [spark] Hisoka-X commented on pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

Posted by "Hisoka-X (via GitHub)" <gi...@apache.org>.
Hisoka-X commented on PR #42661:
URL: https://github.com/apache/spark/pull/42661#issuecomment-1692710926

   Hi @cloud-fan @srielau , sorry to bother you, I have changed this PR to add `try_reflect` function, could you review again? Thanks.


-- 
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


[GitHub] [spark] Hisoka-X commented on a diff in pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

Posted by "Hisoka-X (via GitHub)" <gi...@apache.org>.
Hisoka-X commented on code in PR #42661:
URL: https://github.com/apache/spark/pull/42661#discussion_r1306888014


##########
sql/core/src/test/resources/sql-tests/inputs/try_reflect.sql:
##########
@@ -0,0 +1,19 @@
+-- positive
+SELECT try_reflect("java.util.UUID", "fromString", "a5cf6c42-0c85-418f-af6c-3e4e5b1328f2");
+SELECT try_reflect("java.lang.String", "valueOf", 1);
+SELECT try_reflect("java.lang.Math", "max", 2, 3);
+SELECT try_reflect("java.lang.Math", "min", 2, 3);
+SELECT try_reflect("java.lang.Integer", "valueOf", "10", 16);
+
+-- negative

Review Comment:
   Addressed all. Thanks for advise!



-- 
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


[GitHub] [spark] cloud-fan commented on a diff in pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/CallMethodViaReflection.scala:
##########
@@ -55,64 +55,73 @@ import org.apache.spark.util.Utils
   """,
   since = "2.0.0",
   group = "misc_funcs")
-case class CallMethodViaReflection(children: Seq[Expression])
+case class CallMethodViaReflection(
+      children: Seq[Expression],
+      failOnError: Boolean = true)
   extends Nondeterministic
   with CodegenFallback
   with QueryErrorsBase {
 
+  def this(children: Seq[Expression]) =
+    this(children, true)
+
   override def prettyName: String = getTagValue(FunctionRegistry.FUNC_ALIAS).getOrElse("reflect")
 
   override def checkInputDataTypes(): TypeCheckResult = {
-    if (children.size < 2) {
-      throw QueryCompilationErrors.wrongNumArgsError(
-        toSQLId(prettyName), Seq("> 1"), children.length
-      )
+    if (!failOnError) {

Review Comment:
   I think failing makes more sense as non-existing class/method is mostly a user error. It happens before submitting the job, so failing earlier is more useful to the users.
   



-- 
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


[GitHub] [spark] srielau commented on pull request #42661: [SPARK-44743][SQL] Fix `reflect` method behavior match with hive

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

   > I don't think we should blindly follow hive, but it seems a reasonable requirement to specify the failure behavior of the `reflect` function.
   > 
   > @srielau any ideas? Shall we add a new parameter to this function or add another function with a different name?
   
   If we are consistent, we add try_reflect.
   We could hitch the NULL for reflect to ansi_enabled = false.
   Isn't it in the users best interest to know when a function raises an error? suppressing it leads to silent, undetected errors.
   And NULL may well be part of expected results, so it wouldn't raise an eyebrow. 


-- 
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


[GitHub] [spark] cloud-fan commented on a diff in pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/TryEval.scala:
##########
@@ -236,3 +236,31 @@ case class TryToBinary(
   override protected def withNewChildInternal(newChild: Expression): Expression =
     this.copy(replacement = newChild)
 }
+
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+  usage = "_FUNC_(class, method[, arg1[, arg2 ..]]) - Calls a method with reflection.",

Review Comment:
   let's explicitly document the error behavior.



-- 
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


[GitHub] [spark] cloud-fan commented on a diff in pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

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


##########
sql/core/src/test/scala/org/apache/spark/sql/MiscFunctionsSuite.scala:
##########
@@ -242,6 +242,19 @@ class MiscFunctionsSuite extends QueryTest with SharedSparkSession {
       Seq(Row("a5cf6c42-0c85-418f-af6c-3e4e5b1328f2")))
   }
 
+  test("try_reflect") {

Review Comment:
   can we move the tests to the golden files?



-- 
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


[GitHub] [spark] srielau commented on pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

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

   Maybe I didn't see the forest for the trees, but what's the agreed upon behavior for vanilla reflect() now?


-- 
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


[GitHub] [spark] Hisoka-X commented on a diff in pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

Posted by "Hisoka-X (via GitHub)" <gi...@apache.org>.
Hisoka-X commented on code in PR #42661:
URL: https://github.com/apache/spark/pull/42661#discussion_r1306309397


##########
sql/core/src/test/resources/sql-tests/analyzer-results/try_reflect.sql.out:
##########
@@ -0,0 +1,165 @@
+-- Automatically generated by SQLQueryTestSuite
+-- !query
+SELECT try_reflect("java.util.UUID", "fromString", "a5cf6c42-0c85-418f-af6c-3e4e5b1328f2")
+-- !query analysis
+Project [try_reflect(java.util.UUID, fromString, a5cf6c42-0c85-418f-af6c-3e4e5b1328f2) AS try_reflect(java.util.UUID, fromString, a5cf6c42-0c85-418f-af6c-3e4e5b1328f2)#x]
++- OneRowRelation
+
+
+-- !query
+SELECT try_reflect("java.lang.String", "valueOf", 1)
+-- !query analysis
+Project [try_reflect(java.lang.String, valueOf, 1) AS try_reflect(java.lang.String, valueOf, 1)#x]
++- OneRowRelation
+
+
+-- !query
+SELECT try_reflect("java.lang.Math", "max", 2, 3)
+-- !query analysis
+Project [try_reflect(java.lang.Math, max, 2, 3) AS try_reflect(java.lang.Math, max, 2, 3)#x]
++- OneRowRelation
+
+
+-- !query
+SELECT try_reflect("java.lang.Math", "min", 2, 3)
+-- !query analysis
+Project [try_reflect(java.lang.Math, min, 2, 3) AS try_reflect(java.lang.Math, min, 2, 3)#x]
++- OneRowRelation
+
+
+-- !query
+SELECT try_reflect("java.lang.Integer", "valueOf", "10", 16)
+-- !query analysis
+Project [try_reflect(java.lang.Integer, valueOf, 10, 16) AS try_reflect(java.lang.Integer, valueOf, 10, 16)#x]
++- OneRowRelation
+
+
+-- !query
+SELECT try_reflect("java.util.UUID", "fromString", "b")
+-- !query analysis
+Project [try_reflect(java.util.UUID, fromString, b) AS try_reflect(java.util.UUID, fromString, b)#x]
++- OneRowRelation
+
+
+-- !query
+SELECT try_reflect("java.net.URLDecoder", "decode", "%")
+-- !query analysis
+Project [try_reflect(java.net.URLDecoder, decode, %) AS try_reflect(java.net.URLDecoder, decode, %)#x]
++- OneRowRelation
+
+
+-- !query
+SELECT try_reflect("java.lang.Math", "max", "test", 3)
+-- !query analysis
+org.apache.spark.SparkException
+{
+  "errorClass" : "INTERNAL_ERROR",
+  "sqlState" : "XX000",
+  "messageParameters" : {
+    "message" : "Cannot resolve the runtime replaceable expression \"try_reflect(java.lang.Math, max, test, 3)\". The replacement is unresolved: \"reflect(java.lang.Math, max, test, 3)\"."
+  }
+}

Review Comment:
   When replacement expression `checkInputDataTypes` return failed, this error will be reported. It's not `try_reflect`'s bug, it's all `RuntimeReplaceable` will face problem. In fact if we can throw `checkInputDataTypes` failed reason would be better. I'm trying solve it on another PR. cc @cloud-fan 



-- 
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


[GitHub] [spark] cloud-fan commented on a diff in pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/TryEval.scala:
##########
@@ -236,3 +236,35 @@ case class TryToBinary(
   override protected def withNewChildInternal(newChild: Expression): Expression =
     this.copy(replacement = newChild)
 }
+
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+  usage = "_FUNC_(class, method[, arg1[, arg2 ..]]) - This is a special version of `reflect` that" +
+    " performs the same operation, but returns a NULL value instead of raising an error if the invoke method thrown exception.",
+  examples = """
+    Examples:
+      > SELECT _FUNC_('java.util.UUID', 'randomUUID');
+       c33fb387-8500-4bfa-81d2-6e0e3e930df2
+      > SELECT _FUNC_('java.util.UUID', 'fromString', 'a5cf6c42-0c85-418f-af6c-3e4e5b1328f2');
+       a5cf6c42-0c85-418f-af6c-3e4e5b1328f2
+      > SELECT _FUNC_('java.net.URLDecoder', 'decode', '%');
+       NULL
+  """,
+  since = "4.0.0",
+  group = "misc_funcs")
+// scalastyle:on line.size.limit
+case class TryReflect(params: Seq[Expression], replacement: Expression) extends RuntimeReplaceable
+  with InheritAnalysisRules {

Review Comment:
   `CallMethodViaReflection` does not define any analysis rules, I think we can make it a simple RuntimeReplaceable with
   ```
   lazy val replacement = CallMethodViaReflection(params, failOnError = false)
   ```



-- 
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


[GitHub] [spark] cloud-fan commented on a diff in pull request #42661: [SPARK-44743][SQL] Add `try_reflect` function

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/TryEval.scala:
##########
@@ -236,3 +236,35 @@ case class TryToBinary(
   override protected def withNewChildInternal(newChild: Expression): Expression =
     this.copy(replacement = newChild)
 }
+
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+  usage = "_FUNC_(class, method[, arg1[, arg2 ..]]) - This is a special version of `reflect` that" +
+    " performs the same operation, but returns a NULL value instead of raising an error if the invoke method thrown exception.",
+  examples = """
+    Examples:
+      > SELECT _FUNC_('java.util.UUID', 'randomUUID');
+       c33fb387-8500-4bfa-81d2-6e0e3e930df2
+      > SELECT _FUNC_('java.util.UUID', 'fromString', 'a5cf6c42-0c85-418f-af6c-3e4e5b1328f2');
+       a5cf6c42-0c85-418f-af6c-3e4e5b1328f2
+      > SELECT _FUNC_('java.net.URLDecoder', 'decode', '%');
+       NULL
+  """,
+  since = "4.0.0",
+  group = "misc_funcs")
+// scalastyle:on line.size.limit
+case class TryReflect(params: Seq[Expression], replacement: Expression) extends RuntimeReplaceable
+  with InheritAnalysisRules {

Review Comment:
   oh I see!



-- 
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