cloud-fan commented on code in PR #48596:
URL: https://github.com/apache/spark/pull/48596#discussion_r1814453637
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/AnsiTypeCoercion.scala:
##########
@@ -223,47 +205,13 @@ object AnsiTypeCoercion extends TypeCoercionBase {
override def canCast(from: DataType, to: DataType): Boolean =
Cast.canAnsiCast(from, to)
object PromoteStrings extends TypeCoercionRule {
- private def castExpr(expr: Expression, targetType: DataType): Expression =
{
- expr.dataType match {
- case NullType => Literal.create(null, targetType)
- case l if l != targetType => Cast(expr, targetType)
- case _ => expr
- }
- }
override def transform: PartialFunction[Expression, Expression] = {
// Skip nodes who's children have not been resolved yet.
case e if !e.childrenResolved => e
-
- case b @ BinaryOperator(left, right)
- if findWiderTypeForString(left.dataType, right.dataType).isDefined =>
- val promoteType = findWiderTypeForString(left.dataType,
right.dataType).get
- b.withNewChildren(Seq(castExpr(left, promoteType), castExpr(right,
promoteType)))
-
- case Abs(e @ StringTypeExpression(), failOnError) => Abs(Cast(e,
DoubleType), failOnError)
- case m @ UnaryMinus(e @ StringTypeExpression(), _) =>
- m.withNewChildren(Seq(Cast(e, DoubleType)))
- case UnaryPositive(e @ StringTypeExpression()) => UnaryPositive(Cast(e,
DoubleType))
-
- case d @ DateAdd(left @ StringTypeExpression(), _) =>
- d.copy(startDate = Cast(d.startDate, DateType))
- case d @ DateAdd(_, right @ StringTypeExpression()) =>
- d.copy(days = Cast(right, IntegerType))
- case d @ DateSub(left @ StringTypeExpression(), _) =>
- d.copy(startDate = Cast(d.startDate, DateType))
- case d @ DateSub(_, right @ StringTypeExpression()) =>
- d.copy(days = Cast(right, IntegerType))
-
- case s @ SubtractDates(left @ StringTypeExpression(), _, _) =>
- s.copy(left = Cast(s.left, DateType))
- case s @ SubtractDates(_, right @ StringTypeExpression(), _) =>
- s.copy(right = Cast(s.right, DateType))
- case t @ TimeAdd(left @ StringTypeExpression(), _, _) =>
- t.copy(start = Cast(t.start, TimestampType))
- case t @ SubtractTimestamps(left @ StringTypeExpression(), _, _, _) =>
- t.copy(left = Cast(t.left, t.right.dataType))
- case t @ SubtractTimestamps(_, right @ StringTypeExpression(), _, _) =>
- t.copy(right = Cast(right, t.left.dataType))
+ case withChildrenResolved
+ if
AnsiStringPromotionTypeCoercion.apply.isDefinedAt(withChildrenResolved) =>
Review Comment:
why do we use `PartialFunction`? I think it's simpler if we just use
`Expression => Expression`, then the code here can be `case
withChildrenResolved => AnsiStringPromotionTypeCoercion(withChildrenResolved)`.
`AnsiStringPromotionTypeCoercion` should leave the input expression unchanged
if the pattern match does not hit.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]