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,19 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
}
}

test("cast struct with a timestamp field") {
val originalSchema = StructType(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we usually use new StructType().add("name", TimestampType, nullable = false)

Seq(StructField( "tsField", TimestampType, nullable = false ))
)
val targetSchema = StructType(
// nine out of ten times I'm casting a struct, it's to normalize its fields nullability
Seq(StructField( "tsField", TimestampType, nullable = true ))
)
val inp = Literal.create(InternalRow(0L), originalSchema)
val expected = InternalRow(0L)
checkEvaluation(cast(inp, targetSchema), expected )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and remove the extra space here too

}

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