[SPARK-7299][SQL] Set precision and scale for Decimal according to JDBC metadata instead of returned BigDecimal

JIRA: https://issues.apache.org/jira/browse/SPARK-7299

When connecting with oracle db through jdbc, the precision and scale of `BigDecimal` object returned by `ResultSet.getBigDecimal` is not correctly matched to the table schema reported by `ResultSetMetaData.getPrecision` and `ResultSetMetaData.getScale`.

So in case you insert a value like `19999` into a column with `NUMBER(12, 2)` type, you get through a `BigDecimal` object with scale as 0. But the dataframe schema has correct type as `DecimalType(12, 2)`. Thus, after you save the dataframe into parquet file and then retrieve it, you will get wrong result `199.99`.

Because it is reported to be problematic on jdbc connection with oracle db. It might be difficult to add test case for it. But according to the user's test on JIRA, it solves this problem.

Author: Liang-Chi Hsieh <viirya@gmail.com>

Closes #5833 from viirya/jdbc_decimal_precision and squashes the following commits:

69bc2b5 [Liang-Chi Hsieh] Merge remote-tracking branch 'upstream/master' into jdbc_decimal_precision
928f864 [Liang-Chi Hsieh] Add comments.
5f9da94 [Liang-Chi Hsieh] Set up Decimal's precision and scale according to table schema instead of returned BigDecimal.

(cherry picked from commit e32c0f69f3)
Signed-off-by: Reynold Xin <rxin@databricks.com>
This commit is contained in:
Liang-Chi Hsieh 2015-05-18 01:10:55 -07:00 committed by Reynold Xin
parent 0b6bc8a239
commit 0e7cd8ff82

View file

@ -300,7 +300,7 @@ private[sql] class JDBCRDD(
abstract class JDBCConversion abstract class JDBCConversion
case object BooleanConversion extends JDBCConversion case object BooleanConversion extends JDBCConversion
case object DateConversion extends JDBCConversion case object DateConversion extends JDBCConversion
case object DecimalConversion extends JDBCConversion case class DecimalConversion(precisionInfo: Option[(Int, Int)]) extends JDBCConversion
case object DoubleConversion extends JDBCConversion case object DoubleConversion extends JDBCConversion
case object FloatConversion extends JDBCConversion case object FloatConversion extends JDBCConversion
case object IntegerConversion extends JDBCConversion case object IntegerConversion extends JDBCConversion
@ -317,8 +317,8 @@ private[sql] class JDBCRDD(
schema.fields.map(sf => sf.dataType match { schema.fields.map(sf => sf.dataType match {
case BooleanType => BooleanConversion case BooleanType => BooleanConversion
case DateType => DateConversion case DateType => DateConversion
case DecimalType.Unlimited => DecimalConversion case DecimalType.Unlimited => DecimalConversion(None)
case DecimalType.Fixed(d) => DecimalConversion case DecimalType.Fixed(d) => DecimalConversion(Some(d))
case DoubleType => DoubleConversion case DoubleType => DoubleConversion
case FloatType => FloatConversion case FloatType => FloatConversion
case IntegerType => IntegerConversion case IntegerType => IntegerConversion
@ -375,7 +375,22 @@ private[sql] class JDBCRDD(
} else { } else {
mutableRow.update(i, null) mutableRow.update(i, null)
} }
case DecimalConversion => // When connecting with Oracle DB through JDBC, the precision and scale of BigDecimal
// object returned by ResultSet.getBigDecimal is not correctly matched to the table
// schema reported by ResultSetMetaData.getPrecision and ResultSetMetaData.getScale.
// If inserting values like 19999 into a column with NUMBER(12, 2) type, you get through
// a BigDecimal object with scale as 0. But the dataframe schema has correct type as
// DecimalType(12, 2). Thus, after saving the dataframe into parquet file and then
// retrieve it, you will get wrong result 199.99.
// So it is needed to set precision and scale for Decimal based on JDBC metadata.
case DecimalConversion(Some((p, s))) =>
val decimalVal = rs.getBigDecimal(pos)
if (decimalVal == null) {
mutableRow.update(i, null)
} else {
mutableRow.update(i, Decimal(decimalVal, p, s))
}
case DecimalConversion(None) =>
val decimalVal = rs.getBigDecimal(pos) val decimalVal = rs.getBigDecimal(pos)
if (decimalVal == null) { if (decimalVal == null) {
mutableRow.update(i, null) mutableRow.update(i, null)