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:41:06 UTC

[GitHub] [spark] JoshRosen commented on a diff in pull request #37460: [SPARK-40031][SQL] Remove unnecessary TryEval in TryCast

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