beliefer commented on code in PR #36531:
URL: https://github.com/apache/spark/pull/36531#discussion_r873277888
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -275,6 +376,53 @@ object Cast {
case _ => null
}
}
+
+ // Show suggestion on how to complete the disallowed explicit casting with
built-in type
+ // conversion functions.
+ private def suggestionOnConversionFunctions (
+ from: DataType,
+ to: DataType,
+ functionNames: String): String = {
+ // scalastyle:off line.size.limit
+ s"""cannot cast ${from.catalogString} to ${to.catalogString}.
+ |To convert values from ${from.catalogString} to ${to.catalogString},
you can use $functionNames instead.
+ |""".stripMargin
+ // scalastyle:on line.size.limit
+ }
+
+ def typeCheckFailureMessage(
+ from: DataType,
+ to: DataType,
+ fallbackConfKey: Option[String],
+ fallbackConfValue: Option[String]): String =
+ (from, to) match {
+ case (_: NumericType, TimestampType) =>
+ suggestionOnConversionFunctions(from, to,
+ "functions TIMESTAMP_SECONDS/TIMESTAMP_MILLIS/TIMESTAMP_MICROS")
+
+ case (TimestampType, _: NumericType) =>
+ suggestionOnConversionFunctions(from, to, "functions
UNIX_SECONDS/UNIX_MILLIS/UNIX_MICROS")
+
+ case (_: NumericType, DateType) =>
+ suggestionOnConversionFunctions(from, to, "function
DATE_FROM_UNIX_DATE")
+
+ case (DateType, _: NumericType) =>
+ suggestionOnConversionFunctions(from, to, "function UNIX_DATE")
+
+ // scalastyle:off line.size.limit
+ case _ if fallbackConfKey.isDefined && fallbackConfValue.isDefined &&
Cast.canCast(from, to) =>
+ s"""
+ | cannot cast ${from.catalogString} to ${to.catalogString} with
ANSI mode on.
+ | If you have to cast ${from.catalogString} to ${to.catalogString},
you can set ${fallbackConfKey.get} as ${fallbackConfValue.get}.
+ |""".stripMargin
+ // scalastyle:on line.size.limit
+
+ case _ => s"cannot cast ${from.catalogString} to ${to.catalogString}"
+ }
+
+ def ansiCast(child: Expression, dataType: DataType, timeZoneId:
Option[String] = None): Cast =
+ Cast(child, dataType, timeZoneId, true,
+ SQLConf.STORE_ASSIGNMENT_POLICY.key,
SQLConf.StoreAssignmentPolicy.LEGACY.toString)
}
abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression
with NullIntolerant {
Review Comment:
Yes. `TryCast` extends this parent class 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: [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]