You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/08/10 06:09:49 UTC

[GitHub] [spark] gengliangwang opened a new pull request, #37460: [SPARK-40031][SQL] Remove unnecessary TryEval in TryCast

gengliangwang opened a new pull request, #37460:
URL: https://github.com/apache/spark/pull/37460

   <!--
   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?
   <!--
   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.
   -->
   
   When the try_cast() syntax is equivalent to the non-ansi cast,  convert TryCast as Cast without wrapping with "TryEval" expression.
   
   
   ### Why are the changes needed?
   <!--
   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.
   -->
   1. For better performance. Taking `try_cast(string_col as int)` as example, Spark will have to contact error messages from [SparkThrowableHelper. getMessage ](https://github.com/apache/spark/blob/master/core/src/main/scala/org/apache/spark/ErrorInfo.scala#L86) for every invalid casting. This is actually not necessary in the try_cast function.
   2. More accurate nullability. If the child of try_cast is non-nullable and the casting itself is up-cast, the result should be non-nullable too.
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   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'.
   -->
   No
   
   ### How was this patch tested?
   <!--
   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.
   -->
   Existing UT


-- 
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] gengliangwang commented on a diff in pull request #37460: [SPARK-40031][SQL] Remove unnecessary TryEval in TryCast

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on code in PR #37460:
URL: https://github.com/apache/spark/pull/37460#discussion_r942770985


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/TryEval.scala:
##########
@@ -91,7 +91,17 @@ case class TryCast(child: Expression, toType: DataType, timeZoneId: Option[Strin
     (!Cast.needsTimeZone(child.dataType, toType) || timeZoneId.isDefined)
 
   override lazy val replacement = {
-    TryEval(Cast(child, toType, timeZoneId = timeZoneId, ansiEnabled = true))
+    if (equivalentToNonAnsiCast) {
+      Cast(child, toType, timeZoneId = timeZoneId, ansiEnabled = false)
+    } else {
+      TryEval(Cast(child, toType, timeZoneId = timeZoneId, ansiEnabled = true))
+    }
+  }
+
+  private def equivalentToNonAnsiCast: Boolean = {

Review Comment:
   > Maybe we could keep the TryEval in place but still set ansiEnabled = false when we know that the Cast itself is equivalent? This would still give us performance benefits since we still avoid the cost of throwing and catching Cast exceptions.
   
   That's a really good point. Thank you @JoshRosen 



-- 
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] gengliangwang commented on a diff in pull request #37460: [SPARK-40031][SQL] Remove unnecessary TryEval in TryCast

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on code in PR #37460:
URL: https://github.com/apache/spark/pull/37460#discussion_r942685063


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/TryEval.scala:
##########
@@ -91,7 +91,17 @@ case class TryCast(child: Expression, toType: DataType, timeZoneId: Option[Strin
     (!Cast.needsTimeZone(child.dataType, toType) || timeZoneId.isDefined)
 
   override lazy val replacement = {
-    TryEval(Cast(child, toType, timeZoneId = timeZoneId, ansiEnabled = true))
+    if (equivalentToNonAnsiCast) {
+      Cast(child, toType, timeZoneId = timeZoneId, ansiEnabled = false)
+    } else {
+      TryEval(Cast(child, toType, timeZoneId = timeZoneId, ansiEnabled = true))
+    }
+  }
+
+  private def equivalentToNonAnsiCast: Boolean = {
+    val fromType = child.dataType
+    canUpCast(fromType, toType) ||
+      (fromType == StringType && !toType.isInstanceOf[AnsiIntervalType])

Review Comment:
   try_cast(string as numeric) is always the same as the non-ansi cast



-- 
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] gengliangwang commented on a diff in pull request #37460: [SPARK-40031][SQL] Remove unnecessary TryEval in TryCast

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on code in PR #37460:
URL: https://github.com/apache/spark/pull/37460#discussion_r942154970


##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/TryCastSuite.scala:
##########
@@ -39,8 +39,8 @@ class TryCastSuite extends SparkFunSuite {
   }
 
   test("nullability") {
-    assert(cast("abcdef", StringType).nullable)
-    assert(cast("abcdef", BinaryType).nullable)
+    assert(!cast("abcdef", StringType).nullable)
+    assert(!cast("abcdef", BinaryType).nullable)

Review Comment:
   Sure, updated



-- 
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] JoshRosen commented on a diff in pull request #37460: [SPARK-40031][SQL] Remove unnecessary TryEval in TryCast

Posted by GitBox <gi...@apache.org>.
JoshRosen commented on code in PR #37460:
URL: https://github.com/apache/spark/pull/37460#discussion_r942750140


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/TryEval.scala:
##########
@@ -91,7 +91,17 @@ case class TryCast(child: Expression, toType: DataType, timeZoneId: Option[Strin
     (!Cast.needsTimeZone(child.dataType, toType) || timeZoneId.isDefined)
 
   override lazy val replacement = {
-    TryEval(Cast(child, toType, timeZoneId = timeZoneId, ansiEnabled = true))
+    if (equivalentToNonAnsiCast) {
+      Cast(child, toType, timeZoneId = timeZoneId, ansiEnabled = false)
+    } else {
+      TryEval(Cast(child, toType, timeZoneId = timeZoneId, ansiEnabled = true))
+    }
+  }
+
+  private def equivalentToNonAnsiCast: Boolean = {

Review Comment:
   In order to preserve existing behaviors, I think that we must consider more than just the types of the Cast because `TryEval` will catch and suppress exceptions that occur during the evaluation of the Cast's child.
   
   For example, let's say that I'm casting the result of extracting a field from a map, something like
   
   ```
   cast(element_at(myMap, 'key') as integer)
   ```
   
   This could fail due because either (a) the map doesn't have a value for that key, or (b) the cast fails because the value isn't an integer. `TryEval` catches both types of exceptions, so removing it can change behavior:
   
   ```python
   >>> from pyspark.sql.functions import *
   
   >>> df = spark.range(10).withColumn("mapCol", create_map(col('id').cast('string'), 'id'))
   
   >>> df.selectExpr("try_cast(element_at(mapCol, 'missingKey') as string)").first()
   Row(TRY_CAST(element_at(mapCol, missingKey) AS STRING)=None)
   
   >>> df.selectExpr("cast(element_at(mapCol, 'missingKey') as string)").first()
   22/08/10 10:56:12 ERROR Executor: Exception in task 2.0 in stage 3.0 (TID 8)
   org.apache.spark.SparkNoSuchElementException: [MAP_KEY_DOES_NOT_EXIST] Key 'missingKey' does not exist. Use `try_element_at` to tolerate non-existent key and return NULL instead. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.
   ```
   
   It looks like the other `try_` expressions have similar behavior. For example, `try_add` will also suppress exceptions from its grandchildren, etc.
   
   ---
   
   As a result, removing the `TryEval` might only be safe in cases where we know that the Cast's child cannot possibly throw.
   
   Maybe we could keep the `TryEval` in place but still set `ansiEnabled = false` when we know that the Cast itself is equivalent? This would still give us performance benefits since we still avoid the cost of throwing and catching Cast exceptions.



-- 
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] github-actions[bot] closed pull request #37460: [WIP][SPARK-40031][SQL] Remove unnecessary TryEval in TryCast

Posted by GitBox <gi...@apache.org>.
github-actions[bot] closed pull request #37460: [WIP][SPARK-40031][SQL] Remove unnecessary TryEval in TryCast
URL: https://github.com/apache/spark/pull/37460


-- 
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 #37460: [SPARK-40031][SQL] Remove unnecessary TryEval in TryCast

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #37460:
URL: https://github.com/apache/spark/pull/37460#discussion_r942508517


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/TryEval.scala:
##########
@@ -91,7 +91,17 @@ case class TryCast(child: Expression, toType: DataType, timeZoneId: Option[Strin
     (!Cast.needsTimeZone(child.dataType, toType) || timeZoneId.isDefined)
 
   override lazy val replacement = {
-    TryEval(Cast(child, toType, timeZoneId = timeZoneId, ansiEnabled = true))
+    if (equivalentToNonAnsiCast) {
+      Cast(child, toType, timeZoneId = timeZoneId, ansiEnabled = false)
+    } else {
+      TryEval(Cast(child, toType, timeZoneId = timeZoneId, ansiEnabled = true))
+    }
+  }
+
+  private def equivalentToNonAnsiCast: Boolean = {
+    val fromType = child.dataType
+    canUpCast(fromType, toType) ||
+      (fromType == StringType && !toType.isInstanceOf[AnsiIntervalType])

Review Comment:
   this looks a bit risky. how about `toType.isInstanceOf[NumericType]`?



-- 
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] github-actions[bot] commented on pull request #37460: [WIP][SPARK-40031][SQL] Remove unnecessary TryEval in TryCast

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on PR #37460:
URL: https://github.com/apache/spark/pull/37460#issuecomment-1320692056

   We're closing this PR because it hasn't been updated in a while. This isn't a judgement on the merit of the PR in any way. It's just a way of keeping the PR queue manageable.
   If you'd like to revive this PR, please reopen it and ask a committer to remove the Stale tag!


-- 
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] JoshRosen commented on a diff in pull request #37460: [SPARK-40031][SQL] Remove unnecessary TryEval in TryCast

Posted by GitBox <gi...@apache.org>.
JoshRosen commented on code in PR #37460:
URL: https://github.com/apache/spark/pull/37460#discussion_r942069508


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/TryEval.scala:
##########
@@ -91,7 +91,16 @@ case class TryCast(child: Expression, toType: DataType, timeZoneId: Option[Strin
     (!Cast.needsTimeZone(child.dataType, toType) || timeZoneId.isDefined)
 
   override lazy val replacement = {
-    TryEval(Cast(child, toType, timeZoneId = timeZoneId, ansiEnabled = true))
+    if (equivalentToNonAnsiCast) {
+      Cast(child, toType, timeZoneId = timeZoneId, ansiEnabled = false)
+    } else {
+      TryEval(Cast(child, toType, timeZoneId = timeZoneId, ansiEnabled = true))
+    }
+  }
+
+  private def equivalentToNonAnsiCast: Boolean = {
+    val fromType = child.dataType
+    canUpCast(fromType, toType) || fromType == StringType

Review Comment:
   Should this be 
   
   ```suggestion
       canUpCast(fromType, toType) || toType == StringType
   ```
   ?
   
   We can cast anything to a string but we can't cast arbitrary strings to arbitrary types.



##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/TryCastSuite.scala:
##########
@@ -39,8 +39,8 @@ class TryCastSuite extends SparkFunSuite {
   }
 
   test("nullability") {
-    assert(cast("abcdef", StringType).nullable)
-    assert(cast("abcdef", BinaryType).nullable)
+    assert(!cast("abcdef", StringType).nullable)
+    assert(!cast("abcdef", BinaryType).nullable)

Review Comment:
   Should we also add a test case which _is_ expected to be nullable? Maybe something like `cast("abcdef", IntegerType)`?



-- 
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] gengliangwang commented on a diff in pull request #37460: [SPARK-40031][SQL] Remove unnecessary TryEval in TryCast

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on code in PR #37460:
URL: https://github.com/apache/spark/pull/37460#discussion_r942158908


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/TryEval.scala:
##########
@@ -91,7 +91,16 @@ case class TryCast(child: Expression, toType: DataType, timeZoneId: Option[Strin
     (!Cast.needsTimeZone(child.dataType, toType) || timeZoneId.isDefined)
 
   override lazy val replacement = {
-    TryEval(Cast(child, toType, timeZoneId = timeZoneId, ansiEnabled = true))
+    if (equivalentToNonAnsiCast) {
+      Cast(child, toType, timeZoneId = timeZoneId, ansiEnabled = false)
+    } else {
+      TryEval(Cast(child, toType, timeZoneId = timeZoneId, ansiEnabled = true))
+    }
+  }
+
+  private def equivalentToNonAnsiCast: Boolean = {
+    val fromType = child.dataType
+    canUpCast(fromType, toType) || fromType == StringType

Review Comment:
   On converting a string column to other data types(except ANSI interval types) under the non-ANSI mode, Spark SQL returns null on invalid casts. 
   Thus, when try_cast a string column to other data types(except ANSI interval types), we can rewrite it as CAST with ANSI off.



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