diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercionSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercionSuite.scala index 0d6f9bcedb6a..95005fd3f5a5 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercionSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercionSuite.scala @@ -467,11 +467,21 @@ class TypeCoercionSuite extends AnalysisTest { ArrayType(IntegerType, containsNull = false), Some(ArrayType(IntegerType, containsNull = true))) + widenTest( + ArrayType(NullType, containsNull = true), + ArrayType(IntegerType, containsNull = false), + Some(ArrayType(IntegerType, containsNull = true))) + widenTest( MapType(IntegerType, StringType, valueContainsNull = true), MapType(IntegerType, StringType, valueContainsNull = false), Some(MapType(IntegerType, StringType, valueContainsNull = true))) + widenTest( + MapType(NullType, NullType, true), + MapType(IntegerType, StringType, false), + Some(MapType(IntegerType, StringType, true))) + widenTest( new StructType() .add("arr", ArrayType(IntegerType, containsNull = true), nullable = false), @@ -479,6 +489,14 @@ class TypeCoercionSuite extends AnalysisTest { .add("arr", ArrayType(IntegerType, containsNull = false), nullable = true), Some(new StructType() .add("arr", ArrayType(IntegerType, containsNull = true), nullable = true))) + + widenTest( + new StructType() + .add("null", NullType, nullable = true), + new StructType() + .add("null", IntegerType, nullable = false), + Some(new StructType() + .add("null", IntegerType, nullable = true))) } test("wider common type for decimal and array") { diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala index 58ce5a4ea5af..8296562ac739 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala @@ -1140,6 +1140,23 @@ class CastSuite extends CastSuiteBase { assert(Cast.canCast(set.dataType, ArrayType(StringType, false))) } + test("NullTypes should be able to cast to any complex types") { + assert(Cast.canCast(ArrayType(NullType, true), ArrayType(IntegerType, true))) + assert(Cast.canCast(ArrayType(NullType, false), ArrayType(IntegerType, true))) + + assert(Cast.canCast( + MapType(NullType, NullType, true), MapType(IntegerType, IntegerType, true))) + assert(Cast.canCast( + MapType(NullType, NullType, false), MapType(IntegerType, IntegerType, true))) + + assert(Cast.canCast( + StructType(StructField("a", NullType, true) :: Nil), + StructType(StructField("a", IntegerType, true) :: Nil))) + assert(Cast.canCast( + StructType(StructField("a", NullType, false) :: Nil), + StructType(StructField("a", IntegerType, true) :: Nil))) + } + test("Cast should output null for invalid strings when ANSI is not enabled.") { withSQLConf(SQLConf.ANSI_ENABLED.key -> "false") { checkEvaluation(cast("abdef", DecimalType.USER_DEFAULT), null)