Skip to content

Commit

Permalink
[SPARK-26383][CORE] NPE when use DataFrameReader.jdbc with wrong URL
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?
When passing wrong url to jdbc then It would throw IllegalArgumentException instead of NPE.
### How was this patch tested?
Adding test case to Existing tests in JDBCSuite

Closes #23464 from ayudovin/fixing-npe.

Authored-by: ayudovin <a.yudovin6695@gmail.com>
Signed-off-by: Sean Owen <sean.owen@databricks.com>
  • Loading branch information
ayudovin authored and srowen committed Jan 7, 2019
1 parent a927c76 commit 868e025
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}

Expand Down
13 changes: 13 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 @@ -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"))
}
}

0 comments on commit 868e025

Please sign in to comment.