Skip to content
Snippets Groups Projects
Commit 781b21ba authored by Ewan Leith's avatar Ewan Leith Committed by Reynold Xin
Browse files

[SPARK-10419] [SQL] Adding SQLServer support for datetimeoffset types to JdbcDialects

Reading from Microsoft SQL Server over jdbc fails when the table contains datetimeoffset types.

This patch registers a SQLServer JDBC Dialect that maps datetimeoffset to a String, as Microsoft suggest.

Author: Ewan Leith <ewan.leith@realitymine.com>

Closes #8575 from realitymine-coordinator/sqlserver.
parent 0180b849
No related branches found
No related tags found
No related merge requests found
......@@ -137,6 +137,8 @@ object JdbcDialects {
registerDialect(MySQLDialect)
registerDialect(PostgresDialect)
registerDialect(DB2Dialect)
registerDialect(MsSqlServerDialect)
/**
* Fetch the JdbcDialect class corresponding to a given database url.
......@@ -260,3 +262,19 @@ case object DB2Dialect extends JdbcDialect {
case _ => None
}
}
/**
* :: DeveloperApi ::
* Default Microsoft SQL Server dialect, mapping the datetimeoffset types to a String on read.
*/
@DeveloperApi
case object MsSqlServerDialect extends JdbcDialect {
override def canHandle(url: String): Boolean = url.startsWith("jdbc:sqlserver")
override def getCatalystType(
sqlType: Int, typeName: String, size: Int, md: MetadataBuilder): Option[DataType] = {
if (typeName.contains("datetimeoffset")) {
// String is recommend by Microsoft SQL Server for datetimeoffset types in non-MS clients
Some(StringType)
} else None
}
}
......@@ -408,6 +408,7 @@ class JDBCSuite extends SparkFunSuite with BeforeAndAfter with SharedSQLContext
assert(JdbcDialects.get("jdbc:mysql://127.0.0.1/db") == MySQLDialect)
assert(JdbcDialects.get("jdbc:postgresql://127.0.0.1/db") == PostgresDialect)
assert(JdbcDialects.get("jdbc:db2://127.0.0.1/db") == DB2Dialect)
assert(JdbcDialects.get("jdbc:sqlserver://127.0.0.1/db") == MsSqlServerDialect)
assert(JdbcDialects.get("test.invalid") == NoopDialect)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment