-
Notifications
You must be signed in to change notification settings - Fork 29.1k
[SPARK-34379][SQL] Map JDBC RowID to StringType rather than LongType #31491
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
Changes from all commits
222be6e
e47690e
10e070d
899706f
0d4d19e
315c7e5
73f53d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -226,7 +226,7 @@ object JdbcUtils extends Logging { | |
| case java.sql.Types.REAL => DoubleType | ||
| case java.sql.Types.REF => StringType | ||
| case java.sql.Types.REF_CURSOR => null | ||
| case java.sql.Types.ROWID => LongType | ||
| case java.sql.Types.ROWID => StringType | ||
HyukjinKwon marked this conversation as resolved.
Show resolved
Hide resolved
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So basically we can't assume the row ID is <= 8 bytes? if that's true then I agree.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not only we can't assume the length of the ROWID but also it's not required to be represented as integer. |
||
| case java.sql.Types.SMALLINT => IntegerType | ||
| case java.sql.Types.SQLXML => StringType | ||
| case java.sql.Types.STRUCT => StringType | ||
|
|
@@ -310,11 +310,15 @@ object JdbcUtils extends Logging { | |
| val metadata = new MetadataBuilder() | ||
| metadata.putLong("scale", fieldScale) | ||
|
|
||
| // SPARK-33888 | ||
| // - include TIME type metadata | ||
| // - always build the metadata | ||
| if (dataType == java.sql.Types.TIME) { | ||
| metadata.putBoolean("logical_time_type", true) | ||
| dataType match { | ||
| case java.sql.Types.TIME => | ||
| // SPARK-33888 | ||
| // - include TIME type metadata | ||
| // - always build the metadata | ||
| metadata.putBoolean("logical_time_type", true) | ||
| case java.sql.Types.ROWID => | ||
| metadata.putBoolean("rowid", true) | ||
| case _ => | ||
| } | ||
|
|
||
| val columnType = | ||
|
|
@@ -448,6 +452,10 @@ object JdbcUtils extends Logging { | |
| (rs: ResultSet, row: InternalRow, pos: Int) => | ||
| row.setByte(pos, rs.getByte(pos + 1)) | ||
|
|
||
| case StringType if metadata.contains("rowid") => | ||
| (rs: ResultSet, row: InternalRow, pos: Int) => | ||
| row.update(pos, UTF8String.fromString(rs.getRowId(pos + 1).toString)) | ||
|
|
||
| case StringType => | ||
| (rs: ResultSet, row: InternalRow, pos: Int) => | ||
| // TODO(davies): use getBytes for better performance, if the encoding is UTF-8 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,8 @@ import java.util.{Calendar, GregorianCalendar, Properties} | |
| import scala.collection.JavaConverters._ | ||
|
|
||
| import org.h2.jdbc.JdbcSQLException | ||
| import org.mockito.ArgumentMatchers._ | ||
| import org.mockito.Mockito._ | ||
| import org.scalatest.{BeforeAndAfter, PrivateMethodTester} | ||
|
|
||
| import org.apache.spark.SparkException | ||
|
|
@@ -1781,4 +1783,28 @@ class JDBCSuite extends QueryTest | |
| assert(options.asProperties.get("url") == url) | ||
| assert(options.asProperties.get("dbtable") == "table3") | ||
| } | ||
|
|
||
| test("SPARK-34379: Map JDBC RowID to StringType rather than LongType") { | ||
| val mockRsmd = mock(classOf[java.sql.ResultSetMetaData]) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. h2 database cannot generate rowid-typed data?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It cannot. |
||
| when(mockRsmd.getColumnCount).thenReturn(1) | ||
| when(mockRsmd.getColumnLabel(anyInt())).thenReturn("rowid") | ||
| when(mockRsmd.getColumnType(anyInt())).thenReturn(java.sql.Types.ROWID) | ||
| when(mockRsmd.getColumnTypeName(anyInt())).thenReturn("rowid") | ||
| when(mockRsmd.getPrecision(anyInt())).thenReturn(0) | ||
| when(mockRsmd.getScale(anyInt())).thenReturn(0) | ||
| when(mockRsmd.isSigned(anyInt())).thenReturn(false) | ||
| when(mockRsmd.isNullable(anyInt())).thenReturn(java.sql.ResultSetMetaData.columnNoNulls) | ||
|
|
||
| val mockRs = mock(classOf[java.sql.ResultSet]) | ||
| when(mockRs.getMetaData).thenReturn(mockRsmd) | ||
|
|
||
| val mockDialect = mock(classOf[JdbcDialect]) | ||
| when(mockDialect.getCatalystType(anyInt(), anyString(), anyInt(), any[MetadataBuilder])) | ||
| .thenReturn(None) | ||
|
|
||
| val schema = JdbcUtils.getSchema(mockRs, mockDialect) | ||
| val fields = schema.fields | ||
| assert(fields.length === 1) | ||
| assert(fields(0).dataType === StringType) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dialectsis an internal word? If so, how about saying "Since Spark 3.2, `java.sql.ROWID` is mapped to `StringType` when reading data from other databases via JDBC"?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least, I use
dialectshere as a general word, not represents specific implementations likePostgresDialect.dialectanddialectshave been used from before in the migration guide.