Skip to content

Commit

Permalink
[SPARK-8831][SQL] Support AbstractDataType in TypeCollection.
Browse files Browse the repository at this point in the history
Otherwise it is impossible to declare an expression supporting DecimalType.

Author: Reynold Xin <rxin@databricks.com>

Closes apache#7232 from rxin/typecollection-adt and squashes the following commits:

934d3d1 [Reynold Xin] [SPARK-8831][SQL] Support AbstractDataType in TypeCollection.
  • Loading branch information
rxin committed Jul 6, 2015
1 parent 6d0411b commit 86768b7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -708,8 +708,6 @@ object HiveTypeCoercion {
case (NullType, target) => Cast(e, target.defaultConcreteType)

// Implicit cast among numeric types
// If input is decimal, and we expect a decimal type, just use the input.
case (_: DecimalType, DecimalType) => e
// If input is a numeric type but not decimal, and we expect a decimal type,
// cast the input to unlimited precision decimal.
case (_: NumericType, DecimalType) if !inType.isInstanceOf[DecimalType] =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ private[sql] abstract class AbstractDataType {
*
* This means that we prefer StringType over BinaryType if it is possible to cast to StringType.
*/
private[sql] class TypeCollection(private val types: Seq[DataType]) extends AbstractDataType {
private[sql] class TypeCollection(private val types: Seq[AbstractDataType])
extends AbstractDataType {

require(types.nonEmpty, s"TypeCollection ($types) cannot be empty")

private[sql] override def defaultConcreteType: DataType = types.head
private[sql] override def defaultConcreteType: DataType = types.head.defaultConcreteType

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

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

private[sql] object TypeCollection {

def apply(types: DataType*): TypeCollection = new TypeCollection(types)
def apply(types: AbstractDataType*): TypeCollection = new TypeCollection(types)

def unapply(typ: AbstractDataType): Option[Seq[DataType]] = typ match {
def unapply(typ: AbstractDataType): Option[Seq[AbstractDataType]] = typ match {
case typ: TypeCollection => Some(typ.types)
case _ => None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ class HiveTypeCoercionSuite extends PlanTest {

shouldCast(IntegerType, TypeCollection(StringType, BinaryType), StringType)
shouldCast(IntegerType, TypeCollection(BinaryType, StringType), StringType)

shouldCast(
DecimalType.Unlimited, TypeCollection(IntegerType, DecimalType), DecimalType.Unlimited)
shouldCast(DecimalType(10, 2), TypeCollection(IntegerType, DecimalType), DecimalType(10, 2))
shouldCast(DecimalType(10, 2), TypeCollection(DecimalType, IntegerType), DecimalType(10, 2))
shouldCast(IntegerType, TypeCollection(DecimalType(10, 2), StringType), DecimalType(10, 2))
}

test("ineligible implicit type cast") {
Expand Down

0 comments on commit 86768b7

Please sign in to comment.