Skip to content

Commit 3e1e03d

Browse files
travishegneryhuai
authored andcommitted
[SPARK-10648] Oracle dialect to handle nonspecific numeric types
This is the alternative/agreed upon solution to PR #8780. Creating an OracleDialect to handle the nonspecific numeric types that can be defined in oracle. Author: Travis Hegner <[email protected]> Closes #9495 from travishegner/OracleDialect. (cherry picked from commit 14ee0f5) Signed-off-by: Yin Huai <[email protected]>
1 parent f80f7b6 commit 3e1e03d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ object JdbcDialects {
139139
registerDialect(DB2Dialect)
140140
registerDialect(MsSqlServerDialect)
141141
registerDialect(DerbyDialect)
142+
registerDialect(OracleDialect)
142143

143144

144145
/**
@@ -315,3 +316,27 @@ case object DerbyDialect extends JdbcDialect {
315316

316317
}
317318

319+
/**
320+
* :: DeveloperApi ::
321+
* Default Oracle dialect, mapping a nonspecific numeric type to a general decimal type.
322+
*/
323+
@DeveloperApi
324+
case object OracleDialect extends JdbcDialect {
325+
override def canHandle(url: String): Boolean = url.startsWith("jdbc:oracle")
326+
override def getCatalystType(
327+
sqlType: Int, typeName: String, size: Int, md: MetadataBuilder): Option[DataType] = {
328+
// Handle NUMBER fields that have no precision/scale in special way
329+
// because JDBC ResultSetMetaData converts this to 0 procision and -127 scale
330+
// For more details, please see
331+
// https://github.com/apache/spark/pull/8780#issuecomment-145598968
332+
// and
333+
// https://github.com/apache/spark/pull/8780#issuecomment-144541760
334+
if (sqlType == Types.NUMERIC && size == 0) {
335+
// This is sub-optimal as we have to pick a precision/scale in advance whereas the data
336+
// in Oracle is allowed to have different precision/scale for each value.
337+
Some(DecimalType(DecimalType.MAX_PRECISION, 10))
338+
} else {
339+
None
340+
}
341+
}
342+
}

0 commit comments

Comments
 (0)