Skip to content

Commit eeba2b1

Browse files
fix to jdbc read to handle null values in array data type column
1 parent 0c92318 commit eeba2b1

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

external/docker-integration-tests/src/test/scala/org/apache/spark/sql/jdbc/PostgresIntegrationSuite.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,16 @@ class PostgresIntegrationSuite extends DockerJDBCIntegrationSuite {
5151
+ "B'1000100101', E'\\\\xDEADBEEF', true, '172.16.0.42', '192.168.0.0/16', "
5252
+ """'{1, 2}', '{"a", null, "b"}', '{0.11, 0.22}', '{0.11, 0.22}', 'd1', 1.01, 1)"""
5353
).executeUpdate()
54+
conn.prepareStatement("INSERT INTO bar VALUES (null, null, null, null, null, "
55+
+ "null, null, null, null, null, "
56+
+ "null, null, null, null, null, null, null)"
57+
).executeUpdate()
5458
}
5559

5660
test("Type mapping for various types") {
5761
val df = sqlContext.read.jdbc(jdbcUrl, "bar", new Properties)
58-
val rows = df.collect()
59-
assert(rows.length == 1)
62+
val rows = df.collect().sortBy(_.toString())
63+
assert(rows.length == 2)
6064
val types = rows(0).toSeq.map(x => x.getClass)
6165
assert(types.length == 17)
6266
assert(classOf[String].isAssignableFrom(types(0)))
@@ -96,6 +100,9 @@ class PostgresIntegrationSuite extends DockerJDBCIntegrationSuite {
96100
assert(rows(0).getString(14) == "d1")
97101
assert(rows(0).getFloat(15) == 1.01f)
98102
assert(rows(0).getShort(16) == 1)
103+
104+
// Test reading null values.
105+
assert(0.until(16).forall(rows(1).isNullAt(_)))
99106
}
100107

101108
test("Basic write test") {

sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,9 @@ object JdbcUtils extends Logging {
465465
}
466466

467467
(rs: ResultSet, row: InternalRow, pos: Int) =>
468-
val array = nullSafeConvert[Object](
469-
rs.getArray(pos + 1).getArray,
470-
array => new GenericArrayData(elementConversion.apply(array)))
468+
val array = nullSafeConvert[java.sql.Array](
469+
rs.getArray(pos + 1),
470+
array => new GenericArrayData(elementConversion.apply(array.getArray)))
471471
row.update(pos, array)
472472

473473
case _ => throw new IllegalArgumentException(s"Unsupported type ${dt.simpleString}")

0 commit comments

Comments
 (0)