Skip to content

Commit 33b9171

Browse files
adrian-wangjeanlyn
authored andcommitted
[SPARK-5456] [SQL] fix decimal compare for jdbc rdd
Author: Daoyuan Wang <[email protected]> Closes apache#5803 from adrian-wang/decimalcompare and squashes the following commits: aef0e96 [Daoyuan Wang] add null handle ec455b9 [Daoyuan Wang] fix decimal compare for jdbc rdd
1 parent d001700 commit 33b9171

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

sql/core/src/main/scala/org/apache/spark/sql/jdbc/JDBCRDD.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,13 @@ private[sql] class JDBCRDD(
363363
case BooleanConversion => mutableRow.setBoolean(i, rs.getBoolean(pos))
364364
case DateConversion =>
365365
mutableRow.update(i, DateUtils.fromJavaDate(rs.getDate(pos)))
366-
case DecimalConversion => mutableRow.update(i, rs.getBigDecimal(pos))
366+
case DecimalConversion =>
367+
val decimalVal = rs.getBigDecimal(pos)
368+
if (decimalVal == null) {
369+
mutableRow.update(i, null)
370+
} else {
371+
mutableRow.update(i, Decimal(decimalVal))
372+
}
367373
case DoubleConversion => mutableRow.setDouble(i, rs.getDouble(pos))
368374
case FloatConversion => mutableRow.setFloat(i, rs.getFloat(pos))
369375
case IntegerConversion => mutableRow.setInt(i, rs.getInt(pos))

sql/core/src/test/scala/org/apache/spark/sql/jdbc/JDBCSuite.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,11 @@ class JDBCSuite extends FunSuite with BeforeAndAfter {
271271
assert(rows(0).getDouble(0) === 1.00000000000000022) // Yes, I meant ==.
272272
assert(rows(0).getDouble(1) === 1.00000011920928955) // Yes, I meant ==.
273273
assert(rows(0).getAs[BigDecimal](2)
274-
.equals(new BigDecimal("123456789012345.54321543215432100000")))
274+
.equals(new BigDecimal("123456789012345.54321543215432100000")))
275275
assert(rows(0).schema.fields(2).dataType === DecimalType(40, 20))
276+
val compareDecimal = sql("SELECT C FROM flttypes where C > C - 1").collect()
277+
assert(compareDecimal(0).getAs[BigDecimal](0)
278+
.equals(new BigDecimal("123456789012345.54321543215432100000")))
276279
}
277280

278281
test("SQL query as table name") {

0 commit comments

Comments
 (0)