You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "ueshin (via GitHub)" <gi...@apache.org> on 2024/04/02 05:02:24 UTC
Re: [PR] [SPARK-47669][SQL][CONNECT][PYTHON] Add `Column.try_cast` [spark]
ueshin commented on code in PR #45796:
URL: https://github.com/apache/spark/pull/45796#discussion_r1547142056
##########
connector/connect/common/src/main/protobuf/spark/connect/expressions.proto:
##########
@@ -137,6 +137,13 @@ message Expression {
}
}
+ enum EvalMode {
+ EVAL_MODE_UNSPECIFIED = 0;
+ EVAL_MODE_LEGACY = 1;
+ EVAL_MODE_ANSI = 2;
+ EVAL_MODE_TRY = 3;
+ }
Review Comment:
I'm wondering whether we should put this here or in Cast message?
##########
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:
Just curious, what is this tag doing?
--
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