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-25096][SQL] Loosen nullability if the cast is force-nullable. #22086

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,26 @@ object TypeCoercion {
t2: DataType,
findTypeFunc: (DataType, DataType) => Option[DataType]): Option[DataType] = (t1, t2) match {
case (ArrayType(et1, containsNull1), ArrayType(et2, containsNull2)) =>
findTypeFunc(et1, et2).map(ArrayType(_, containsNull1 || containsNull2))
findTypeFunc(et1, et2).map { et =>
ArrayType(et, containsNull1 || containsNull2 ||
Cast.forceNullable(et1, et) || Cast.forceNullable(et2, et))
}
case (MapType(kt1, vt1, valueContainsNull1), MapType(kt2, vt2, valueContainsNull2)) =>
findTypeFunc(kt1, kt2).flatMap { kt =>
findTypeFunc(vt1, vt2).map { vt =>
MapType(kt, vt, valueContainsNull1 || valueContainsNull2)
}
findTypeFunc(kt1, kt2)
.filter { kt => !Cast.forceNullable(kt1, kt) && !Cast.forceNullable(kt2, kt) }
.flatMap { kt =>
findTypeFunc(vt1, vt2).map { vt =>
MapType(kt, vt, valueContainsNull1 || valueContainsNull2 ||
Cast.forceNullable(vt1, vt) || Cast.forceNullable(vt2, vt))
}
}
case (StructType(fields1), StructType(fields2)) if fields1.length == fields2.length =>
val resolver = SQLConf.get.resolver
fields1.zip(fields2).foldLeft(Option(new StructType())) {
case (Some(struct), (field1, field2)) if resolver(field1.name, field2.name) =>
findTypeFunc(field1.dataType, field2.dataType).map {
dt => struct.add(field1.name, dt, field1.nullable || field2.nullable)
findTypeFunc(field1.dataType, field2.dataType).map { dt =>
struct.add(field1.name, dt, field1.nullable || field2.nullable ||
Cast.forceNullable(field1.dataType, dt) || Cast.forceNullable(field2.dataType, dt))
}
case _ => None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,10 @@ class TypeCoercionSuite extends AnalysisTest {
ArrayType(new StructType().add("num", ShortType), containsNull = false),
ArrayType(new StructType().add("num", LongType), containsNull = false),
Some(ArrayType(new StructType().add("num", LongType), containsNull = false)))
widenTestWithStringPromotion(
ArrayType(IntegerType, containsNull = false),
ArrayType(DecimalType.IntDecimal, containsNull = false),
Some(ArrayType(DecimalType.IntDecimal, containsNull = true)))

// MapType
widenTestWithStringPromotion(
Expand All @@ -517,6 +521,14 @@ class TypeCoercionSuite extends AnalysisTest {
MapType(IntegerType, new StructType().add("num", ShortType), valueContainsNull = false),
MapType(LongType, new StructType().add("num", LongType), valueContainsNull = false),
Some(MapType(LongType, new StructType().add("num", LongType), valueContainsNull = false)))
widenTestWithStringPromotion(
MapType(StringType, IntegerType, valueContainsNull = false),
MapType(StringType, DecimalType.IntDecimal, valueContainsNull = false),
Some(MapType(StringType, DecimalType.IntDecimal, valueContainsNull = true)))
widenTestWithStringPromotion(
MapType(IntegerType, StringType, valueContainsNull = false),
MapType(DecimalType.IntDecimal, StringType, valueContainsNull = false),
None)

// StructType
widenTestWithStringPromotion(
Expand All @@ -540,6 +552,10 @@ class TypeCoercionSuite extends AnalysisTest {
.add("map", MapType(DoubleType, StringType, valueContainsNull = false), nullable = false),
Some(new StructType()
.add("map", MapType(DoubleType, StringType, valueContainsNull = true), nullable = false)))
widenTestWithStringPromotion(
new StructType().add("num", IntegerType, nullable = false),
new StructType().add("num", DecimalType.IntDecimal, nullable = false),
Some(new StructType().add("num", DecimalType.IntDecimal, nullable = true)))

widenTestWithStringPromotion(
new StructType().add("num", IntegerType),
Expand Down