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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to