Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Spark-16791] [SQL] cast struct with timestamp field fails #14400

Closed
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression w
}

private[this] def cast(from: DataType, to: DataType): Any => Any = to match {
case dt if dt == child.dataType => identity[Any]
case dt if dt == from => identity[Any]
case StringType => castToString(from)
case BinaryType => castToBinary(from)
case DateType => castToDate(from)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,16 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
}
}

test("cast struct with a timestamp field") {
val originalSchema = new StructType().add("tsField", TimestampType, nullable = false)
// nine out of ten times I'm casting a struct, it's to normalize its fields nullability
val targetSchema = new StructType().add("tsField", TimestampType, nullable = true)

val inp = Literal.create(InternalRow(0L), originalSchema)
val expected = InternalRow(0L)
checkEvaluation(cast(inp, targetSchema), expected)
}

test("complex casting") {
val complex = Literal.create(
Row(
Expand Down