Skip to content
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 @@ -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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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