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.setByte(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 @@ -57,6 +57,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)
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 @@ -685,6 +699,17 @@ class JDBCSuite extends QueryTest
JdbcDialects.unregisterDialect(testH2Dialect)
}

test("SPARK-26499: Map TINYINT to ByteType via JdbcDialects") {
JdbcDialects.registerDialect(testH2DialectTinyInt)
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