Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-39339][SQL] Support TimestampNTZ type in JDBC data source #36726

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions docs/sql-data-sources-jdbc.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ logging into the data sources.
<td>(none)</td>
<td>
A prefix that will form the final query together with <code>query</code>.
As the specified <code>query</code> will be parenthesized as a subquery in the <code>FROM</code> clause and some databases do not
As the specified <code>query</code> will be parenthesized as a subquery in the <code>FROM</code> clause and some databases do not
support all clauses in subqueries, the <code>prepareQuery</code> property offers a way to run such complex queries.
As an example, spark will issue a query of the following form to the JDBC Source.<br><br>
<code>&lt;prepareQuery&gt; SELECT &lt;columns&gt; FROM (&lt;user_specified_query&gt;) spark_gen_alias</code><br><br>
Expand Down Expand Up @@ -340,10 +340,19 @@ logging into the data sources.
<td>
The name of the JDBC connection provider to use to connect to this URL, e.g. <code>db2</code>, <code>mssql</code>.
Must be one of the providers loaded with the JDBC data source. Used to disambiguate when more than one provider can handle
the specified driver and options. The selected provider must not be disabled by <code>spark.sql.sources.disabledJdbcConnProviderList</code>.
the specified driver and options. The selected provider must not be disabled by <code>spark.sql.sources.disabledJdbcConnProviderList</code>.
</td>
<td>read/write</td>
</tr>
</tr>
<tr>
<td><code>inferTimestampNTZType</code></td>
<td>false</td>
<td>
When the option is set to <code>true</code>, all timestamps are inferred as TIMESTAMP WITHOUT TIME ZONE.
Otherwise, timestamps are read as TIMESTAMP with local time zone.
</td>
<td>read</td>
</tr>
</table>

Note that kerberos authentication with keytab is not always supported by the JDBC driver.<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ class JDBCOptions(
// The prefix that is added to the query sent to the JDBC database.
// This is required to support some complex queries with some JDBC databases.
val prepareQuery = parameters.get(JDBC_PREPARE_QUERY).map(_ + " ").getOrElse("")

// Infers timestamp values as TimestampNTZ type when reading data.
val inferTimestampNTZType = parameters.getOrElse(JDBC_INFER_TIMESTAMP_NTZ, "false").toBoolean
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we maybe check if spark.sql.timestampType is TIMESTAMP_NTZ if inferTimestampNTZType is not set? That's how CSV type inference and Python type inference do.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I thought about it, let's ask @gengliangwang.

}

class JdbcOptionsInWrite(
Expand Down Expand Up @@ -287,4 +290,5 @@ object JDBCOptions {
val JDBC_REFRESH_KRB5_CONFIG = newOption("refreshKrb5Config")
val JDBC_CONNECTION_PROVIDER = newOption("connectionProvider")
val JDBC_PREPARE_QUERY = newOption("prepareQuery")
val JDBC_INFER_TIMESTAMP_NTZ = newOption("inferTimestampNTZType")
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ object JDBCRDD extends Logging {
statement.setQueryTimeout(options.queryTimeout)
val rs = statement.executeQuery()
try {
JdbcUtils.getSchema(rs, dialect, alwaysNullable = true)
JdbcUtils.getSchema(rs, dialect, alwaysNullable = true,
isTimestampNTZ = options.inferTimestampNTZType)
} finally {
rs.close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import org.apache.spark.sql.catalyst.encoders.RowEncoder
import org.apache.spark.sql.catalyst.expressions.SpecificInternalRow
import org.apache.spark.sql.catalyst.parser.CatalystSqlParser
import org.apache.spark.sql.catalyst.util.{CaseInsensitiveMap, DateTimeUtils, GenericArrayData}
import org.apache.spark.sql.catalyst.util.DateTimeUtils.{instantToMicros, localDateToDays, toJavaDate, toJavaTimestamp}
import org.apache.spark.sql.catalyst.util.DateTimeUtils.{instantToMicros, localDateTimeToMicros, localDateToDays, toJavaDate, toJavaTimestamp}
import org.apache.spark.sql.connector.catalog.TableChange
import org.apache.spark.sql.connector.catalog.index.{SupportsIndex, TableIndex}
import org.apache.spark.sql.connector.expressions.NamedReference
Expand Down Expand Up @@ -150,6 +150,9 @@ object JdbcUtils extends Logging with SQLConfHelper {
case StringType => Option(JdbcType("TEXT", java.sql.Types.CLOB))
case BinaryType => Option(JdbcType("BLOB", java.sql.Types.BLOB))
case TimestampType => Option(JdbcType("TIMESTAMP", java.sql.Types.TIMESTAMP))
// Most of the databases either don't support TIMESTAMP WITHOUT TIME ZONE or map it to
// TIMESTAMP type. This will be overwritten in dialects.
case TimestampNTZType => Option(JdbcType("TIMESTAMP", java.sql.Types.TIMESTAMP))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot do this. We should let JDBC dialect decide how to do the mapping.

Copy link
Contributor Author

@sadikovi sadikovi Jun 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a common use case of treating TIMESTAMP as timestamp without time zone. JDBC dialects can override this setting if need be. For example, SQL Server uses DATETIME instead. I have verified that most of the jdbc data sources work fine with TIMESTAMP.

I am going to update the comment to elaborate in more detail.

case DateType => Option(JdbcType("DATE", java.sql.Types.DATE))
case t: DecimalType => Option(
JdbcType(s"DECIMAL(${t.precision},${t.scale})", java.sql.Types.DECIMAL))
Expand All @@ -173,7 +176,8 @@ object JdbcUtils extends Logging with SQLConfHelper {
sqlType: Int,
precision: Int,
scale: Int,
signed: Boolean): DataType = {
signed: Boolean,
isTimestampNTZ: Boolean): DataType = {
val answer = sqlType match {
// scalastyle:off
case java.sql.Types.ARRAY => null
Expand Down Expand Up @@ -215,6 +219,8 @@ object JdbcUtils extends Logging with SQLConfHelper {
case java.sql.Types.TIME => TimestampType
case java.sql.Types.TIME_WITH_TIMEZONE
=> null
case java.sql.Types.TIMESTAMP
if isTimestampNTZ => TimestampNTZType
case java.sql.Types.TIMESTAMP => TimestampType
case java.sql.Types.TIMESTAMP_WITH_TIMEZONE
=> null
Expand Down Expand Up @@ -243,7 +249,8 @@ object JdbcUtils extends Logging with SQLConfHelper {
conn.prepareStatement(options.prepareQuery + dialect.getSchemaQuery(options.tableOrQuery))
try {
statement.setQueryTimeout(options.queryTimeout)
Some(getSchema(statement.executeQuery(), dialect))
Some(getSchema(statement.executeQuery(), dialect,
isTimestampNTZ = options.inferTimestampNTZType))
} catch {
case _: SQLException => None
} finally {
Expand All @@ -258,13 +265,15 @@ object JdbcUtils extends Logging with SQLConfHelper {
* Takes a [[ResultSet]] and returns its Catalyst schema.
*
* @param alwaysNullable If true, all the columns are nullable.
* @param isTimestampNTZ If true, all timestamp columns are interpreted as TIMESTAMP_NTZ.
* @return A [[StructType]] giving the Catalyst schema.
* @throws SQLException if the schema contains an unsupported type.
*/
def getSchema(
resultSet: ResultSet,
dialect: JdbcDialect,
alwaysNullable: Boolean = false): StructType = {
alwaysNullable: Boolean = false,
isTimestampNTZ: Boolean = false): StructType = {
val rsmd = resultSet.getMetaData
val ncols = rsmd.getColumnCount
val fields = new Array[StructField](ncols)
Expand Down Expand Up @@ -306,7 +315,7 @@ object JdbcUtils extends Logging with SQLConfHelper {

val columnType =
dialect.getCatalystType(dataType, typeName, fieldSize, metadata).getOrElse(
getCatalystType(dataType, fieldSize, fieldScale, isSigned))
getCatalystType(dataType, fieldSize, fieldScale, isSigned, isTimestampNTZ))
fields(i) = StructField(columnName, columnType, nullable, metadata.build())
i = i + 1
}
Expand Down Expand Up @@ -472,6 +481,15 @@ object JdbcUtils extends Logging with SQLConfHelper {
row.update(pos, null)
}

case TimestampNTZType =>
(rs: ResultSet, row: InternalRow, pos: Int) =>
val t = rs.getTimestamp(pos + 1)
if (t != null) {
row.setLong(pos, DateTimeUtils.fromJavaTimestamp(t))
} else {
row.update(pos, null)
}
Copy link
Contributor

@LuciferYang LuciferYang May 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as TimestampType branch, should we merge them?

case TimestampType =>
(rs: ResultSet, row: InternalRow, pos: Int) =>
val t = rs.getTimestamp(pos + 1)
if (t != null) {
row.setLong(pos, DateTimeUtils.fromJavaTimestamp(t))
} else {
row.update(pos, null)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can merge. I will do that, thanks.


case BinaryType =>
(rs: ResultSet, row: InternalRow, pos: Int) =>
row.update(pos, rs.getBytes(pos + 1))
Expand Down Expand Up @@ -583,6 +601,18 @@ object JdbcUtils extends Logging with SQLConfHelper {
stmt.setTimestamp(pos + 1, row.getAs[java.sql.Timestamp](pos))
}

case TimestampNTZType =>
if (conf.datetimeJava8ApiEnabled) {
(stmt: PreparedStatement, row: Row, pos: Int) =>
stmt.setTimestamp(pos + 1, toJavaTimestamp(instantToMicros(row.getAs[Instant](pos))))
} else {
(stmt: PreparedStatement, row: Row, pos: Int) =>
stmt.setTimestamp(
pos + 1,
toJavaTimestamp(localDateTimeToMicros(row.getAs[java.time.LocalDateTime](pos)))
)
}

case DateType =>
if (conf.datetimeJava8ApiEnabled) {
(stmt: PreparedStatement, row: Row, pos: Int) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ private object MsSqlServerDialect extends JdbcDialect {

override def getJDBCType(dt: DataType): Option[JdbcType] = dt match {
case TimestampType => Some(JdbcType("DATETIME", java.sql.Types.TIMESTAMP))
case TimestampNTZType => Some(JdbcType("DATETIME", java.sql.Types.TIMESTAMP))
case StringType => Some(JdbcType("NVARCHAR(MAX)", java.sql.Types.NVARCHAR))
case BooleanType => Some(JdbcType("BIT", java.sql.Types.BIT))
case BinaryType => Some(JdbcType("VARBINARY(MAX)", java.sql.Types.VARBINARY))
Expand Down
27 changes: 25 additions & 2 deletions sql/core/src/test/scala/org/apache/spark/sql/jdbc/JDBCSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package org.apache.spark.sql.jdbc

import java.math.BigDecimal
import java.sql.{Date, DriverManager, SQLException, Timestamp}
import java.time.{Instant, LocalDate}
import java.time.{Instant, LocalDate, LocalDateTime}
import java.util.{Calendar, GregorianCalendar, Properties, TimeZone}

import scala.collection.JavaConverters._
Expand Down Expand Up @@ -1230,6 +1230,7 @@ class JDBCSuite extends QueryTest
assert(getJdbcType(oracleDialect, BinaryType) == "BLOB")
assert(getJdbcType(oracleDialect, DateType) == "DATE")
assert(getJdbcType(oracleDialect, TimestampType) == "TIMESTAMP")
assert(getJdbcType(oracleDialect, TimestampNTZType) == "TIMESTAMP")
}

private def assertEmptyQuery(sqlString: String): Unit = {
Expand Down Expand Up @@ -1879,5 +1880,27 @@ class JDBCSuite extends QueryTest
val fields = schema.fields
assert(fields.length === 1)
assert(fields(0).dataType === StringType)
}
}

test("SPARK-39339: TimestampNTZType support") {
val tableName = "timestamp_ntz_table"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add tests write/read timestamp_ntz with different time zone ? I doubt the result is not correct.

Copy link
Contributor Author

@sadikovi sadikovi Jun 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Timestamp NTZ type is independent of the time zone with timestamp rebased to UTC. Sure, I can add a test to confirm.


val df = Seq(
(LocalDateTime.parse("2021-12-03T10:50:12"), false),
(null, true)).toDF("col1", "col2")

df.write.format("jdbc")
.option("url", urlWithUserAndPass)
.option("dbtable", tableName).save()

val resWithoutTZ = spark.read.format("jdbc")
.option("inferTimestampNTZType", "true")
.option("url", urlWithUserAndPass)
.option("dbtable", tableName)
.load()

checkAnswer(resWithoutTZ, Seq(
Row(LocalDateTime.parse("2021-12-03T10:50:12"), false),
Row(null, true)))
}
}