Skip to content

Commit 826570f

Browse files
committed
Disallow upcasting complex data types to string type
1 parent 022667c commit 826570f

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

  • sql/catalyst/src
    • main/scala/org/apache/spark/sql/catalyst/expressions
    • test/scala/org/apache/spark/sql/catalyst/expressions

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ object Cast {
131131
case (from: DecimalType, to: NumericType) if from.isTighterThan(to) => true
132132
case (f, t) if legalNumericPrecedence(f, t) => true
133133
case (DateType, TimestampType) => true
134-
case (_, StringType) => true
134+
case (_: AtomicType, StringType) => true
135+
case (_: CalendarIntervalType, StringType) => true
135136

136137
// Spark supports casting between long and timestamp, please see `longToTimestamp` and
137138
// `timestampToLong` for details.

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,11 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
10021002
}
10031003
}
10041004
}
1005+
numericTypes.foreach { dt =>
1006+
makeComplexTypes(dt, true).foreach { complexType =>
1007+
assert(!Cast.canUpCast(dt, StringType))
1008+
}
1009+
}
10051010
}
10061011

10071012
test("SPARK-27671: cast from nested null type in struct") {

0 commit comments

Comments
 (0)