cfmcgrady commented on code in PR #37439:
URL: https://github.com/apache/spark/pull/37439#discussion_r959099576
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala:
##########
@@ -316,55 +316,25 @@ object UnwrapCastInBinaryComparison extends
Rule[LogicalPlan] {
// note that 3.14 will be rounded to 3.14000010... after casting to
float
val (nullList, canCastList) = (ArrayBuffer[Literal](),
ArrayBuffer[Literal]())
- var containsCannotCastLiteral = false
val fromType = fromExp.dataType
val ordering = toType.ordering.asInstanceOf[Ordering[Any]]
- val minMaxInToType = getRange(fromType).map {
- case (min, max) =>
- (Cast(Literal(min), toType).eval(), Cast(Literal(max), toType).eval())
- }
list.foreach {
case lit @ Literal(null, _) => nullList += lit
case NonNullLiteral(value, _) =>
- val minMaxCmp = minMaxInToType.map {
- case (minInToType, maxInToType) =>
- (ordering.compare(value, minInToType), ordering.compare(value,
maxInToType))
- }
- minMaxCmp match {
- // the literal value is out of fromType range
- case Some((minCmp, maxCmp)) if maxCmp > 0 || minCmp < 0 =>
- containsCannotCastLiteral = true
- case _ =>
- val newValue = Cast(Literal(value), fromType, ansiEnabled =
false).eval()
- if (newValue == null) {
- // the literal cannot cast to fromExp.dataType, and there is no
min/max for the
- // fromType
- containsCannotCastLiteral = true
- } else {
- val valueRoundTrip = Cast(Literal(newValue, fromType),
toType).eval()
- val cmp = ordering.compare(value, valueRoundTrip)
- if (cmp == 0) {
- canCastList += Literal(newValue, fromType)
- } else {
- // the literal value is rounded up/down after casting to
`fromType`
- containsCannotCastLiteral = true
- }
- }
+ val newValue = Cast(Literal(value), fromType, ansiEnabled =
false).eval()
Review Comment:
Yes, I think it's enough.
1. we rely on equality of cast values when there is no min/max for the
`fromType` (e.g. decimal type), or that the literal `value` is within range
`(min, max)`
https://github.com/apache/spark/blob/c2d4c4ed4c5d08bb1ee8e91207aef5eccfc50470/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala#L305-L346
2. if the literal value is out of `fromExp.dataType` range, the `newValue`
cannot converted back to `toType` without precision loss.
--
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]