viirya commented on code in PR #48251:
URL: https://github.com/apache/spark/pull/48251#discussion_r1778911247


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1123,33 +1131,42 @@ case class Cast(
         variant.VariantGet.cast(v, to, evalMode != EvalMode.TRY, timeZoneId, 
zoneId)
       })
     } else {
-      to match {
-        case dt if dt == from => identity[Any]
-        case VariantType => input => 
variant.VariantExpressionEvalUtils.castToVariant(input, from)
-        case _: StringType => castToString(from)
-        case BinaryType => castToBinary(from)
-        case DateType => castToDate(from)
-        case decimal: DecimalType => castToDecimal(from, decimal)
-        case TimestampType => castToTimestamp(from)
-        case TimestampNTZType => castToTimestampNTZ(from)
-        case CalendarIntervalType => castToInterval(from)
-        case it: DayTimeIntervalType => castToDayTimeInterval(from, it)
-        case it: YearMonthIntervalType => castToYearMonthInterval(from, it)
-        case BooleanType => castToBoolean(from)
-        case ByteType => castToByte(from)
-        case ShortType => castToShort(from)
-        case IntegerType => castToInt(from)
-        case FloatType => castToFloat(from)
-        case LongType => castToLong(from)
-        case DoubleType => castToDouble(from)
-        case array: ArrayType =>
-          castArray(from.asInstanceOf[ArrayType].elementType, 
array.elementType)
-        case map: MapType => castMap(from.asInstanceOf[MapType], map)
-        case struct: StructType => castStruct(from.asInstanceOf[StructType], 
struct)
-        case udt: UserDefinedType[_] if udt.acceptsType(from) =>
-          identity[Any]
-        case _: UserDefinedType[_] =>
-          throw QueryExecutionErrors.cannotCastError(from, to)
+      from match {
+        // `castToString` has special handling for `UserDefinedType`
+        case udt: UserDefinedType[_] if !to.isInstanceOf[StringType] =>
+          castInternal(udt.sqlType, to)
+        case _ =>
+          to match {
+            case dt if dt == from => identity[Any]
+            case VariantType => input =>
+              variant.VariantExpressionEvalUtils.castToVariant(input, from)
+            case _: StringType => castToString(from)
+            case BinaryType => castToBinary(from)
+            case DateType => castToDate(from)
+            case decimal: DecimalType => castToDecimal(from, decimal)
+            case TimestampType => castToTimestamp(from)
+            case TimestampNTZType => castToTimestampNTZ(from)
+            case CalendarIntervalType => castToInterval(from)
+            case it: DayTimeIntervalType => castToDayTimeInterval(from, it)
+            case it: YearMonthIntervalType => castToYearMonthInterval(from, it)
+            case BooleanType => castToBoolean(from)
+            case ByteType => castToByte(from)
+            case ShortType => castToShort(from)
+            case IntegerType => castToInt(from)
+            case FloatType => castToFloat(from)
+            case LongType => castToLong(from)
+            case DoubleType => castToDouble(from)
+            case array: ArrayType =>
+              castArray(from.asInstanceOf[ArrayType].elementType, 
array.elementType)
+            case map: MapType => castMap(from.asInstanceOf[MapType], map)
+            case struct: StructType => 
castStruct(from.asInstanceOf[StructType], struct)
+            case udt: UserDefinedType[_] if udt.acceptsType(from) =>
+              identity[Any]
+            case udt: UserDefinedType[_] =>
+              castInternal(from, udt.sqlType)

Review Comment:
   Yes. `castToString` has its handling on casting UDT to string. I keep it for 
compatibility.



-- 
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-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to