Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@ object JdbcUtils extends Logging {
(rs: ResultSet, row: InternalRow, pos: Int) =>
row.setShort(pos, rs.getShort(pos + 1))

case ByteType =>
(rs: ResultSet, row: InternalRow, pos: Int) =>
row.update(pos, rs.getByte(pos + 1))

case StringType =>
(rs: ResultSet, row: InternalRow, pos: Int) =>
// TODO(davies): use getBytes for better performance, if the encoding is UTF-8
Expand Down
25 changes: 25 additions & 0 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 @@ -56,6 +56,20 @@ class JDBCSuite extends QueryTest
Some(StringType)
}

val testH2DialectTinyInt = new JdbcDialect {
override def canHandle(url: String): Boolean = url.startsWith("jdbc:h2")
override def getCatalystType(
sqlType: Int,
typeName: String,
size: Int,
md: MetadataBuilder): Option[DataType] = {
sqlType match {
case java.sql.Types.TINYINT => Some(ByteType)
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if this is okay in this H2 dialect context.
We made testH2DialectTinyInt like this, but theoretically, TINYINT is still unsigned and Spark ByteType is signed.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I think we found similar issue in another PR. @twdsilva can you check and fix it if needed?

case _ => None
}
}
}

before {
Utils.classForName("org.h2.Driver")
// Extra properties that will be specified for our database. We need these to test
Expand Down Expand Up @@ -693,6 +707,17 @@ class JDBCSuite extends QueryTest
JdbcDialects.unregisterDialect(testH2Dialect)
}

test("Map TINYINT to ByteType via JdbcDialects") {
JdbcDialects.registerDialect(testH2DialectTinyInt)
Copy link
Member

Choose a reason for hiding this comment

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

Oh, @dongjoon-hyun, actually this test seems fine. The test dialect seems only scoped for this specific case.

Copy link
Member

Choose a reason for hiding this comment

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

although it's a bit odd given #23400 (comment), at least this test is scoped and won't affect other tests about unsigned TINYINT cases.

Copy link
Member

Choose a reason for hiding this comment

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

Originally, I suspected this dialect is valid for H2 database's type.

Luckily, TINYINT of H2 database seems to be also -128 to 127.

In this case, I'm okay~

Copy link
Member

Choose a reason for hiding this comment

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

Thank you for verifying this case @dongjoon-hyun

val df = spark.read.jdbc(urlWithUserAndPass, "test.inttypes", new Properties())
val rows = df.collect()
assert(rows.length === 2)
assert(rows(0).get(2).isInstanceOf[Byte])
assert(rows(0).getByte(2) === 3)
assert(rows(1).isNullAt(2))
JdbcDialects.unregisterDialect(testH2DialectTinyInt)
}

test("Default jdbc dialect registration") {
assert(JdbcDialects.get("jdbc:mysql://127.0.0.1/db") == MySQLDialect)
assert(JdbcDialects.get("jdbc:postgresql://127.0.0.1/db") == PostgresDialect)
Expand Down