Skip to content

Commit 3bee157

Browse files
committed
and decimal type coercion rule for binary comparison
1 parent 8883025 commit 3bee157

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/HiveTypeCoercion.scala

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ trait HiveTypeCoercion {
323323
* e1 union e2 max(s1, s2) + max(p1-s1, p2-s2) max(s1, s2)
324324
* sum(e1) p1 + 10 s1
325325
* avg(e1) p1 + 4 s1 + 4
326+
* compare max(p1, p2) max(s1, s2)
326327
*
327328
* Catalyst also has unlimited-precision decimals. For those, all ops return unlimited precision.
328329
*
@@ -441,21 +442,10 @@ trait HiveTypeCoercion {
441442
DecimalType(min(p1 - s1, p2 - s2) + max(s1, s2), max(s1, s2))
442443
)
443444

444-
case LessThan(e1 @ DecimalType.Expression(p1, s1),
445-
e2 @ DecimalType.Expression(p2, s2)) if p1 != p2 || s1 != s2 =>
446-
LessThan(Cast(e1, DecimalType.Unlimited), Cast(e2, DecimalType.Unlimited))
447-
448-
case LessThanOrEqual(e1 @ DecimalType.Expression(p1, s1),
449-
e2 @ DecimalType.Expression(p2, s2)) if p1 != p2 || s1 != s2 =>
450-
LessThanOrEqual(Cast(e1, DecimalType.Unlimited), Cast(e2, DecimalType.Unlimited))
451-
452-
case GreaterThan(e1 @ DecimalType.Expression(p1, s1),
453-
e2 @ DecimalType.Expression(p2, s2)) if p1 != p2 || s1 != s2 =>
454-
GreaterThan(Cast(e1, DecimalType.Unlimited), Cast(e2, DecimalType.Unlimited))
455-
456-
case GreaterThanOrEqual(e1 @ DecimalType.Expression(p1, s1),
457-
e2 @ DecimalType.Expression(p2, s2)) if p1 != p2 || s1 != s2 =>
458-
GreaterThanOrEqual(Cast(e1, DecimalType.Unlimited), Cast(e2, DecimalType.Unlimited))
445+
case b @ BinaryComparison(e1 @ DecimalType.Expression(p1, s1),
446+
e2 @ DecimalType.Expression(p2, s2)) if p1 != p2 || s1 != s2 =>
447+
val resultType = DecimalType(max(p1, p2), max(s1, s2))
448+
b.makeCopy(Array(Cast(e1, resultType), Cast(e2, resultType)))
459449

460450
// Promote integers inside a binary expression with fixed-precision decimals to decimals,
461451
// and fixed-precision decimals in an expression with floats / doubles to doubles

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ abstract class BinaryComparison extends BinaryExpression with Predicate {
202202
sys.error(s"BinaryComparisons must either override eval or evalInternal")
203203
}
204204

205+
object BinaryComparison {
206+
def unapply(b: BinaryComparison): Option[(Expression, Expression)] =
207+
Some((b.left, b.right))
208+
}
209+
205210
case class EqualTo(left: Expression, right: Expression) extends BinaryComparison {
206211
override def symbol: String = "="
207212

0 commit comments

Comments
 (0)