Skip to content

Commit

Permalink
[SPARK-28706][SQL] Allow cast null type to any types
Browse files Browse the repository at this point in the history
## What changes were proposed in this pull request?

apache#25242 proposed to disallow upcasting complex data types to string type, however, upcasting from null type to any types should still be safe.

## How was this patch tested?

Add corresponding case in `CastSuite`.

Closes apache#25425 from jiangxb1987/nullToString.

Authored-by: Xingbo Jiang <[email protected]>
Signed-off-by: Wenchen Fan <[email protected]>
  • Loading branch information
jiangxb1987 authored and cloud-fan committed Aug 13, 2019
1 parent 247bebc commit 3249c7a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ object Cast {
case (DateType, TimestampType) => true
case (_: AtomicType, StringType) => true
case (_: CalendarIntervalType, StringType) => true
case (NullType, _) => true

// Spark supports casting between long and timestamp, please see `longToTimestamp` and
// `timestampToLong` for details.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
)
}

import DataTypeTestUtils.numericTypes
import DataTypeTestUtils._
numericTypes.foreach { from =>
val (safeTargetTypes, unsafeTargetTypes) = numericTypes.partition(to => isCastSafe(from, to))

Expand Down Expand Up @@ -1008,6 +1008,10 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
assert(!Cast.canUpCast(complexType, StringType))
}
}

atomicTypes.foreach { atomicType =>
assert(Cast.canUpCast(NullType, atomicType))
}
}

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

0 comments on commit 3249c7a

Please sign in to comment.