Skip to content

Commit

Permalink
[SPARK-7196][SQL] Support precision and scale of decimal type for JDBC
Browse files Browse the repository at this point in the history
JIRA: https://issues.apache.org/jira/browse/SPARK-7196

Author: Liang-Chi Hsieh <[email protected]>

Closes apache#5777 from viirya/jdbc_precision and squashes the following commits:

f40f5e6 [Liang-Chi Hsieh] Support precision and scale for NUMERIC type.
49acbf9 [Liang-Chi Hsieh] Add unit test.
a509e19 [Liang-Chi Hsieh] Support precision and scale of decimal type for JDBC.
  • Loading branch information
viirya authored and jeanlyn committed Jun 12, 2015
1 parent 2648c4d commit 2183a40
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 8 additions & 2 deletions sql/core/src/main/scala/org/apache/spark/sql/jdbc/JDBCRDD.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private[sql] object JDBCRDD extends Logging {
* @param sqlType - A field of java.sql.Types
* @return The Catalyst type corresponding to sqlType.
*/
private def getCatalystType(sqlType: Int): DataType = {
private def getCatalystType(sqlType: Int, precision: Int, scale: Int): DataType = {
val answer = sqlType match {
case java.sql.Types.ARRAY => null
case java.sql.Types.BIGINT => LongType
Expand All @@ -49,6 +49,8 @@ private[sql] object JDBCRDD extends Logging {
case java.sql.Types.CLOB => StringType
case java.sql.Types.DATALINK => null
case java.sql.Types.DATE => DateType
case java.sql.Types.DECIMAL
if precision != 0 || scale != 0 => DecimalType(precision, scale)
case java.sql.Types.DECIMAL => DecimalType.Unlimited
case java.sql.Types.DISTINCT => null
case java.sql.Types.DOUBLE => DoubleType
Expand All @@ -61,6 +63,8 @@ private[sql] object JDBCRDD extends Logging {
case java.sql.Types.NCHAR => StringType
case java.sql.Types.NCLOB => StringType
case java.sql.Types.NULL => null
case java.sql.Types.NUMERIC
if precision != 0 || scale != 0 => DecimalType(precision, scale)
case java.sql.Types.NUMERIC => DecimalType.Unlimited
case java.sql.Types.NVARCHAR => StringType
case java.sql.Types.OTHER => null
Expand Down Expand Up @@ -109,10 +113,11 @@ private[sql] object JDBCRDD extends Logging {
val dataType = rsmd.getColumnType(i + 1)
val typeName = rsmd.getColumnTypeName(i + 1)
val fieldSize = rsmd.getPrecision(i + 1)
val fieldScale = rsmd.getScale(i + 1)
val nullable = rsmd.isNullable(i + 1) != ResultSetMetaData.columnNoNulls
val metadata = new MetadataBuilder().putString("name", columnName)
var columnType = quirks.getCatalystType(dataType, typeName, fieldSize, metadata)
if (columnType == null) columnType = getCatalystType(dataType)
if (columnType == null) columnType = getCatalystType(dataType, fieldSize, fieldScale)
fields(i) = StructField(columnName, columnType, nullable, metadata.build())
i = i + 1
}
Expand Down Expand Up @@ -307,6 +312,7 @@ private[sql] class JDBCRDD(
case BooleanType => BooleanConversion
case DateType => DateConversion
case DecimalType.Unlimited => DecimalConversion
case DecimalType.Fixed(d) => DecimalConversion
case DoubleType => DoubleConversion
case FloatType => FloatConversion
case IntegerType => IntegerConversion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import java.sql.DriverManager
import java.util.{Calendar, GregorianCalendar, Properties}

import org.apache.spark.sql.test._
import org.apache.spark.sql.types._
import org.h2.jdbc.JdbcSQLException
import org.scalatest.{FunSuite, BeforeAndAfter}
import TestSQLContext._
Expand Down Expand Up @@ -271,6 +272,7 @@ class JDBCSuite extends FunSuite with BeforeAndAfter {
assert(rows(0).getDouble(1) === 1.00000011920928955) // Yes, I meant ==.
assert(rows(0).getAs[BigDecimal](2)
.equals(new BigDecimal("123456789012345.54321543215432100000")))
assert(rows(0).schema.fields(2).dataType === DecimalType(40, 20))
}

test("SQL query as table name") {
Expand Down

0 comments on commit 2183a40

Please sign in to comment.