srowen closed pull request #23464: [SPARK-26383][Core] - NPE when use
DataFrameReader.jdbc with wrong URL
URL: https://github.com/apache/spark/pull/23464
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala
b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala
index 922bef284c98e..86a27b5afc250 100644
---
a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala
+++
b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala
@@ -48,6 +48,7 @@ object JdbcUtils extends Logging {
* Returns a factory for creating connections to the given JDBC URL.
*
* @param options - JDBC options that contains url, table and other
information.
+ * @throws IllegalArgumentException if the driver could not open a JDBC
connection.
*/
def createConnectionFactory(options: JDBCOptions): () => Connection = {
val driverClass: String = options.driverClass
@@ -60,7 +61,11 @@ object JdbcUtils extends Logging {
throw new IllegalStateException(
s"Did not find registered driver with class $driverClass")
}
- driver.connect(options.url, options.asConnectionProperties)
+ val connection: Connection = driver.connect(options.url,
options.asConnectionProperties)
+ require(connection != null,
+ s"The driver could not open a JDBC connection. Check the URL:
${options.url}")
+
+ connection
}
}
diff --git a/sql/core/src/test/scala/org/apache/spark/sql/jdbc/JDBCSuite.scala
b/sql/core/src/test/scala/org/apache/spark/sql/jdbc/JDBCSuite.scala
index e4641631e607d..aefa5da94481b 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/jdbc/JDBCSuite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/jdbc/JDBCSuite.scala
@@ -1507,4 +1507,17 @@ class JDBCSuite extends QueryTest
checkNotPushdown(sql("SELECT name, theid FROM predicateOption WHERE
theid = 1")),
Row("fred", 1) :: Nil)
}
+
+ test("SPARK-26383 throw IllegalArgumentException if wrong kind of driver to
the given url") {
+ val e = intercept[IllegalArgumentException] {
+ val opts = Map(
+ "url" -> "jdbc:mysql://localhost/db",
+ "dbtable" -> "table",
+ "driver" -> "org.postgresql.Driver"
+ )
+ spark.read.format("jdbc").options(opts).load
+ }.getMessage
+ assert(e.contains("The driver could not open a JDBC connection. " +
+ "Check the URL: jdbc:mysql://localhost/db"))
+ }
}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]