Skip to content

Commit a75dd83

Browse files
guowei2marmbrus
authored andcommitted
[SPARK-4928][SQL] Fix: Operator '>,<,>=,<=' with decimal between different precision report error
case operator with decimal between different precision, we need change them to unlimited Author: guowei2 <[email protected]> Closes #3767 from guowei2/SPARK-4928 and squashes the following commits: c6a6e3e [guowei2] fix code style 3214e0a [guowei2] add test case b4985a2 [guowei2] fix code style 27adf42 [guowei2] Fix: Operation '>,<,>=,<=' with Decimal report error
1 parent 2deac74 commit a75dd83

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,22 @@ trait HiveTypeCoercion {
361361
DecimalType(min(p1 - s1, p2 - s2) + max(s1, s2), max(s1, s2))
362362
)
363363

364+
case LessThan(e1 @ DecimalType.Expression(p1, s1),
365+
e2 @ DecimalType.Expression(p2, s2)) if p1 != p2 || s1 != s2 =>
366+
LessThan(Cast(e1, DecimalType.Unlimited), Cast(e2, DecimalType.Unlimited))
367+
368+
case LessThanOrEqual(e1 @ DecimalType.Expression(p1, s1),
369+
e2 @ DecimalType.Expression(p2, s2)) if p1 != p2 || s1 != s2 =>
370+
LessThanOrEqual(Cast(e1, DecimalType.Unlimited), Cast(e2, DecimalType.Unlimited))
371+
372+
case GreaterThan(e1 @ DecimalType.Expression(p1, s1),
373+
e2 @ DecimalType.Expression(p2, s2)) if p1 != p2 || s1 != s2 =>
374+
GreaterThan(Cast(e1, DecimalType.Unlimited), Cast(e2, DecimalType.Unlimited))
375+
376+
case GreaterThanOrEqual(e1 @ DecimalType.Expression(p1, s1),
377+
e2 @ DecimalType.Expression(p2, s2)) if p1 != p2 || s1 != s2 =>
378+
GreaterThanOrEqual(Cast(e1, DecimalType.Unlimited), Cast(e2, DecimalType.Unlimited))
379+
364380
// Promote integers inside a binary expression with fixed-precision decimals to decimals,
365381
// and fixed-precision decimals in an expression with floats / doubles to doubles
366382
case b: BinaryExpression if b.left.dataType != b.right.dataType =>

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/DecimalPrecisionSuite.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ class DecimalPrecisionSuite extends FunSuite with BeforeAndAfter {
4949
assert(analyzer(plan).schema.fields(0).dataType === expectedType)
5050
}
5151

52+
private def checkComparison(expression: Expression, expectedType: DataType): Unit = {
53+
val plan = Project(Alias(expression, "c")() :: Nil, relation)
54+
val comparison = analyzer(plan).collect {
55+
case Project(Alias(e: BinaryComparison, _) :: Nil, _) => e
56+
}.head
57+
assert(comparison.left.dataType === expectedType)
58+
assert(comparison.right.dataType === expectedType)
59+
}
60+
5261
test("basic operations") {
5362
checkType(Add(d1, d2), DecimalType(6, 2))
5463
checkType(Subtract(d1, d2), DecimalType(6, 2))
@@ -65,6 +74,14 @@ class DecimalPrecisionSuite extends FunSuite with BeforeAndAfter {
6574
checkType(Add(Add(d1, d2), Add(d1, d2)), DecimalType(7, 2))
6675
}
6776

77+
test("Comparison operations") {
78+
checkComparison(LessThan(i, d1), DecimalType.Unlimited)
79+
checkComparison(LessThanOrEqual(d1, d2), DecimalType.Unlimited)
80+
checkComparison(GreaterThan(d2, u), DecimalType.Unlimited)
81+
checkComparison(GreaterThanOrEqual(d1, f), DoubleType)
82+
checkComparison(GreaterThan(d2, d2), DecimalType(5, 2))
83+
}
84+
6885
test("bringing in primitive types") {
6986
checkType(Add(d1, i), DecimalType(12, 1))
7087
checkType(Add(d1, f), DoubleType)

0 commit comments

Comments
 (0)