From 471fd40cf5638f25d313c35ecbd0fde1f383cf6d Mon Sep 17 00:00:00 2001 From: Yin Huai Date: Thu, 5 Nov 2015 08:40:10 -0800 Subject: [PATCH] [SPARK-10648] Oracle dialect to handle nonspecific numeric types #9495 Squashed commit of the following: commit a1370a7a93e1bc4e2f908be980165223d0f67d58 Author: Travis Hegner Date: Thu Nov 5 11:10:35 2015 -0500 style cleanup commit 839bcb582e138144b2d0757c9471de3a7cacc2ac Author: Travis Hegner Date: Thu Nov 5 10:06:19 2015 -0500 more attribution commit 3157ed5a71047ed85e8d80d4ef92c34273c38f1e Author: Travis Hegner Date: Thu Nov 5 10:04:09 2015 -0500 adding canHandle override, and registration of OracleDialect commit cc764d47d25eb35ae6c38c1caf1a87d65bdd10fc Author: Travis Hegner Date: Thu Nov 5 09:53:43 2015 -0500 initial attempt Conflicts: sql/core/src/main/scala/org/apache/spark/sql/jdbc/JdbcDialects.scala --- .../apache/spark/sql/jdbc/JdbcDialects.scala | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/sql/core/src/main/scala/org/apache/spark/sql/jdbc/JdbcDialects.scala b/sql/core/src/main/scala/org/apache/spark/sql/jdbc/JdbcDialects.scala index 8849fc2f1f0ef..9bc9ee62e7baf 100644 --- a/sql/core/src/main/scala/org/apache/spark/sql/jdbc/JdbcDialects.scala +++ b/sql/core/src/main/scala/org/apache/spark/sql/jdbc/JdbcDialects.scala @@ -125,6 +125,7 @@ object JdbcDialects { registerDialect(MySQLDialect) registerDialect(PostgresDialect) + registerDialect(OracleDialect) /** * Fetch the JdbcDialect class corresponding to a given database url. @@ -222,3 +223,25 @@ case object MySQLDialect extends JdbcDialect { s"`$colName`" } } + +/** + * :: DeveloperApi :: + * Default Oracle dialect, mapping a nonspecific + * numeric type to a general decimal type. + */ +@DeveloperApi +case object OracleDialect extends JdbcDialect { + override def canHandle(url: String): Boolean = url.startsWith("jdbc:oracle") + override def getCatalystType( + sqlType: Int, typeName: String, size: Int, md: MetadataBuilder): Option[DataType] = { + // Handle NUMBER fields that have no precision/scale in special way + // because JDBC ResultSetMetaData converts this to 0 precision and -127 scale + if (sqlType == Types.NUMERIC && size == 0) { + // This is sub-optimal as we have to pick a precision/scale in advance whereas the data + // in Oracle is allowed to have different precision/scale for each value. + Some(DecimalType(38, 10)) + } else { + None + } + } +}