Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-wang committed Apr 22, 2015
1 parent 5eed4bc commit f3f7baf
Showing 1 changed file with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ trait HiveTypeCoercion {
val typeCoercionRules =
PropagateTypes ::
ConvertNaNs ::
PromoteToStrings ::
WidenTypes ::
PromoteStrings ::
DecimalPrecision ::
Expand Down Expand Up @@ -233,7 +234,7 @@ trait HiveTypeCoercion {
case Some(dt) => findTightestCommonType(dt, c)
}) match {
// If there is no applicable conversion, leave expression unchanged.
case None => i.makeCopy(Array(a, b))
case None => i
case Some(dt) => i.makeCopy(Array(Cast(a, dt), b.map(Cast(_, dt))))
}
}
Expand Down Expand Up @@ -286,14 +287,6 @@ trait HiveTypeCoercion {
i.makeCopy(Array(Cast(a, StringType), b.map(Cast(_, StringType))))
case i @ In(a, b) if a.dataType == TimestampType && b.forall(_.dataType == DateType) =>
i.makeCopy(Array(Cast(a, StringType), b.map(Cast(_, StringType))))
case i @ In(a, b) if a.dataType == StringType
&& b.exists(_.dataType.isInstanceOf[NumericType]) =>
i.makeCopy(Array(Cast(a, DoubleType), b))
case i @ In(a, b) if b.exists(_.dataType == StringType)
&& a.dataType.isInstanceOf[NumericType] =>
i.makeCopy(Array(a, b.map(_.dataType match{
case StringType => Cast(a, DoubleType)
})))

case Sum(e) if e.dataType == StringType =>
Sum(Cast(e, DoubleType))
Expand All @@ -304,6 +297,27 @@ trait HiveTypeCoercion {
}
}

/**
* Promotes strings that appear in arithmetic expressions.
*/
object PromoteToStrings extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan transformAllExpressions {
// For In we need to promote numeric as strings
case i @ In(a, b) if a.dataType == StringType
&& b.exists(_.dataType.isInstanceOf[NumericType]) =>
i.makeCopy(Array(a, b.map(exp => exp.dataType match {
case n: NumericType => Cast(exp, StringType)
case _ =>
})))
case i @ In(a, b) if b.exists(_.dataType == StringType)
&& a.dataType.isInstanceOf[NumericType] =>
i.makeCopy(Array(Cast(a, StringType), b.map(exp => exp.dataType match {
case n: NumericType => Cast(exp, StringType)
case _ =>
})))
}
}

// scalastyle:off
/**
* Calculates and propagates precision for fixed-precision decimals. Hive has a number of
Expand Down

0 comments on commit f3f7baf

Please sign in to comment.