@@ -19,6 +19,7 @@ package org.apache.spark.sql.catalyst.expressions
1919
2020import java .math .{BigDecimal => JavaBigDecimal }
2121
22+ import org .apache .spark .SparkException
2223import org .apache .spark .sql .catalyst .InternalRow
2324import org .apache .spark .sql .catalyst .analysis .TypeCheckResult
2425import org .apache .spark .sql .catalyst .expressions .codegen ._
@@ -434,6 +435,11 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression {
434435 case array : ArrayType => castArray(from.asInstanceOf [ArrayType ].elementType, array.elementType)
435436 case map : MapType => castMap(from.asInstanceOf [MapType ], map)
436437 case struct : StructType => castStruct(from.asInstanceOf [StructType ], struct)
438+ case udt : UserDefinedType [_]
439+ if udt.userClass == from.asInstanceOf [UserDefinedType [_]].userClass =>
440+ identity[Any ]
441+ case _ : UserDefinedType [_] =>
442+ throw new SparkException (s " Cannot cast $from to $to. " )
437443 }
438444
439445 private [this ] lazy val cast : Any => Any = cast(child.dataType, dataType)
@@ -479,6 +485,8 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression {
479485 case udt : UserDefinedType [_]
480486 if udt.userClass == from.asInstanceOf [UserDefinedType [_]].userClass =>
481487 (c, evPrim, evNull) => s " $evPrim = $c; "
488+ case _ : UserDefinedType [_] =>
489+ throw new SparkException (s " Cannot cast $from to $to. " )
482490 }
483491
484492 // Since we need to cast child expressions recursively inside ComplexTypes, such as Map's
0 commit comments