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 @@ -55,6 +55,19 @@ class PostgresIntegrationSuite extends DockerJDBCIntegrationSuite {
+ "null, null, null, null, null, "
+ "null, null, null, null, null, null, null)"
).executeUpdate()

conn.prepareStatement("CREATE TABLE st_with_array (c0 uuid, c1 inet, c2 cidr," +
"c3 json, c4 jsonb, c5 uuid[], c6 inet[], c7 cidr[], c8 json[], c9 jsonb[])")
.executeUpdate()
conn.prepareStatement("INSERT INTO st_with_array VALUES ( " +
"'0a532531-cdf1-45e3-963d-5de90b6a30f1', '172.168.22.1', '192.168.100.128/25', " +
"""'{"a": "foo", "b": "bar"}', '{"a": 1, "b": 2}', """ +
"ARRAY['7be8aaf8-650e-4dbb-8186-0a749840ecf2'," +
"'205f9bfc-018c-4452-a605-609c0cfad228']::uuid[], ARRAY['172.16.0.41', " +
"'172.16.0.42']::inet[], ARRAY['192.168.0.0/24', '10.1.0.0/16']::cidr[], " +
"""ARRAY['{"a": "foo", "b": "bar"}', '{"a": 1, "b": 2}']::json[], """ +
"""ARRAY['{"a": 1, "b": 2, "c": 3}']::jsonb[])"""
).executeUpdate()
}

test("Type mapping for various types") {
Expand Down Expand Up @@ -126,4 +139,21 @@ class PostgresIntegrationSuite extends DockerJDBCIntegrationSuite {
assert(schema(0).dataType == FloatType)
assert(schema(1).dataType == ShortType)
}

test("SPARK-22291: Conversion error when transforming array types of " +
"uuid, inet and cidr to StingType in PostgreSQL") {
val df = sqlContext.read.jdbc(jdbcUrl, "st_with_array", new Properties)
val rows = df.collect()
assert(rows(0).getString(0) == "0a532531-cdf1-45e3-963d-5de90b6a30f1")
assert(rows(0).getString(1) == "172.168.22.1")
assert(rows(0).getString(2) == "192.168.100.128/25")
assert(rows(0).getString(3) == "{\"a\": \"foo\", \"b\": \"bar\"}")
assert(rows(0).getString(4) == "{\"a\": 1, \"b\": 2}")
assert(rows(0).getSeq(5) == Seq("7be8aaf8-650e-4dbb-8186-0a749840ecf2",
"205f9bfc-018c-4452-a605-609c0cfad228"))
assert(rows(0).getSeq(6) == Seq("172.16.0.41", "172.16.0.42"))
assert(rows(0).getSeq(7) == Seq("192.168.0.0/24", "10.1.0.0/16"))
assert(rows(0).getSeq(8) == Seq("""{"a": "foo", "b": "bar"}""", """{"a": 1, "b": 2}"""))
assert(rows(0).getSeq(9) == Seq("""{"a": 1, "b": 2, "c": 3}"""))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,9 @@ object JdbcUtils extends Logging {

case StringType =>
(array: Object) =>
array.asInstanceOf[Array[java.lang.String]]
.map(UTF8String.fromString)
// some underlying types are not String such as uuid, inet, cidr, etc.
array.asInstanceOf[Array[java.lang.Object]]
.map(obj => if (obj == null) null else UTF8String.fromString(obj.toString))

case DateType =>
(array: Object) =>
Expand Down