zhengruifeng commented on code in PR #45796:
URL: https://github.com/apache/spark/pull/45796#discussion_r1547367346
##########
sql/core/src/main/scala/org/apache/spark/sql/Column.scala:
##########
@@ -1222,6 +1222,43 @@ class Column(val expr: Expression) extends Logging {
*/
def cast(to: String): Column = cast(CatalystSqlParser.parseDataType(to))
+ /**
+ * Casts the column to a different data type and the result is null on
failure.
+ * {{{
+ * // Casts colA to IntegerType.
+ * import org.apache.spark.sql.types.IntegerType
+ * df.select(df("colA").try_cast(IntegerType))
+ *
+ * // equivalent to
+ * df.select(df("colA").try_cast("int"))
+ * }}}
+ *
+ * @group expr_ops
+ * @since 4.0.0
+ */
+ def try_cast(to: DataType): Column = withExpr {
+ val cast = Cast(
+ child = expr,
+ dataType = CharVarcharUtils.replaceCharVarcharWithStringForCast(to),
+ evalMode = EvalMode.TRY)
+ cast.setTagValue(Cast.USER_SPECIFIED_CAST, ())
Review Comment:
this tag was introduced in
https://github.com/apache/spark/commit/92e5cfd58dc7dd5a5b1d3e2b6e93a03af5a07b67,
seems it only affects the `sql` string
--
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]