Skip to content

Commit dad4d6a

Browse files
committed
[SPARK-31229][SQL][TESTS] Add unit tests TypeCoercion.findTypeForComplex and Cast.canCast in null <> complex types
### What changes were proposed in this pull request? This PR (SPARK-31229) is rather a followup of #27926 (SPARK-31166). It adds unittests for `TypeCoercion.findTypeForComplex` and `Cast.canCast` about struct, map and array with the respect to null types. ### Why are the changes needed? To detect which scope was broken in the future easily. ### Does this PR introduce any user-facing change? No, it's a test-only. ### How was this patch tested? Unittests were added. Closes #27990 from HyukjinKwon/SPARK-31166-followup. Authored-by: HyukjinKwon <[email protected]> Signed-off-by: HyukjinKwon <[email protected]> (cherry picked from commit bd32400) Signed-off-by: HyukjinKwon <[email protected]>
1 parent 5c1e78f commit dad4d6a

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercionSuite.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,18 +467,36 @@ class TypeCoercionSuite extends AnalysisTest {
467467
ArrayType(IntegerType, containsNull = false),
468468
Some(ArrayType(IntegerType, containsNull = true)))
469469

470+
widenTest(
471+
ArrayType(NullType, containsNull = true),
472+
ArrayType(IntegerType, containsNull = false),
473+
Some(ArrayType(IntegerType, containsNull = true)))
474+
470475
widenTest(
471476
MapType(IntegerType, StringType, valueContainsNull = true),
472477
MapType(IntegerType, StringType, valueContainsNull = false),
473478
Some(MapType(IntegerType, StringType, valueContainsNull = true)))
474479

480+
widenTest(
481+
MapType(NullType, NullType, true),
482+
MapType(IntegerType, StringType, false),
483+
Some(MapType(IntegerType, StringType, true)))
484+
475485
widenTest(
476486
new StructType()
477487
.add("arr", ArrayType(IntegerType, containsNull = true), nullable = false),
478488
new StructType()
479489
.add("arr", ArrayType(IntegerType, containsNull = false), nullable = true),
480490
Some(new StructType()
481491
.add("arr", ArrayType(IntegerType, containsNull = true), nullable = true)))
492+
493+
widenTest(
494+
new StructType()
495+
.add("null", NullType, nullable = true),
496+
new StructType()
497+
.add("null", IntegerType, nullable = false),
498+
Some(new StructType()
499+
.add("null", IntegerType, nullable = true)))
482500
}
483501

484502
test("wider common type for decimal and array") {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,23 @@ class CastSuite extends CastSuiteBase {
11401140
assert(Cast.canCast(set.dataType, ArrayType(StringType, false)))
11411141
}
11421142

1143+
test("NullTypes should be able to cast to any complex types") {
1144+
assert(Cast.canCast(ArrayType(NullType, true), ArrayType(IntegerType, true)))
1145+
assert(Cast.canCast(ArrayType(NullType, false), ArrayType(IntegerType, true)))
1146+
1147+
assert(Cast.canCast(
1148+
MapType(NullType, NullType, true), MapType(IntegerType, IntegerType, true)))
1149+
assert(Cast.canCast(
1150+
MapType(NullType, NullType, false), MapType(IntegerType, IntegerType, true)))
1151+
1152+
assert(Cast.canCast(
1153+
StructType(StructField("a", NullType, true) :: Nil),
1154+
StructType(StructField("a", IntegerType, true) :: Nil)))
1155+
assert(Cast.canCast(
1156+
StructType(StructField("a", NullType, false) :: Nil),
1157+
StructType(StructField("a", IntegerType, true) :: Nil)))
1158+
}
1159+
11431160
test("Cast should output null for invalid strings when ANSI is not enabled.") {
11441161
withSQLConf(SQLConf.ANSI_ENABLED.key -> "false") {
11451162
checkEvaluation(cast("abdef", DecimalType.USER_DEFAULT), null)

0 commit comments

Comments
 (0)