Skip to content

Commit 934d3d1

Browse files
committed
[SPARK-8831][SQL] Support AbstractDataType in TypeCollection.
1 parent 6d0411b commit 934d3d1

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/HiveTypeCoercion.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,6 @@ object HiveTypeCoercion {
708708
case (NullType, target) => Cast(e, target.defaultConcreteType)
709709

710710
// Implicit cast among numeric types
711-
// If input is decimal, and we expect a decimal type, just use the input.
712-
case (_: DecimalType, DecimalType) => e
713711
// If input is a numeric type but not decimal, and we expect a decimal type,
714712
// cast the input to unlimited precision decimal.
715713
case (_: NumericType, DecimalType) if !inType.isInstanceOf[DecimalType] =>

sql/catalyst/src/main/scala/org/apache/spark/sql/types/AbstractDataType.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ private[sql] abstract class AbstractDataType {
5353
*
5454
* This means that we prefer StringType over BinaryType if it is possible to cast to StringType.
5555
*/
56-
private[sql] class TypeCollection(private val types: Seq[DataType]) extends AbstractDataType {
56+
private[sql] class TypeCollection(private val types: Seq[AbstractDataType])
57+
extends AbstractDataType {
58+
5759
require(types.nonEmpty, s"TypeCollection ($types) cannot be empty")
5860

59-
private[sql] override def defaultConcreteType: DataType = types.head
61+
private[sql] override def defaultConcreteType: DataType = types.head.defaultConcreteType
6062

6163
private[sql] override def isParentOf(childCandidate: DataType): Boolean = false
6264

@@ -68,9 +70,9 @@ private[sql] class TypeCollection(private val types: Seq[DataType]) extends Abst
6870

6971
private[sql] object TypeCollection {
7072

71-
def apply(types: DataType*): TypeCollection = new TypeCollection(types)
73+
def apply(types: AbstractDataType*): TypeCollection = new TypeCollection(types)
7274

73-
def unapply(typ: AbstractDataType): Option[Seq[DataType]] = typ match {
75+
def unapply(typ: AbstractDataType): Option[Seq[AbstractDataType]] = typ match {
7476
case typ: TypeCollection => Some(typ.types)
7577
case _ => None
7678
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ class HiveTypeCoercionSuite extends PlanTest {
7171

7272
shouldCast(IntegerType, TypeCollection(StringType, BinaryType), StringType)
7373
shouldCast(IntegerType, TypeCollection(BinaryType, StringType), StringType)
74+
75+
shouldCast(
76+
DecimalType.Unlimited, TypeCollection(IntegerType, DecimalType), DecimalType.Unlimited)
77+
shouldCast(DecimalType(10, 2), TypeCollection(IntegerType, DecimalType), DecimalType(10, 2))
78+
shouldCast(DecimalType(10, 2), TypeCollection(DecimalType, IntegerType), DecimalType(10, 2))
79+
shouldCast(IntegerType, TypeCollection(DecimalType(10, 2), StringType), DecimalType(10, 2))
7480
}
7581

7682
test("ineligible implicit type cast") {

0 commit comments

Comments
 (0)