-
Notifications
You must be signed in to change notification settings - Fork 28.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-26383][Core] - NPE when use DataFrameReader.jdbc with wrong URL #23464
Conversation
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JDBCRDD.scala
Outdated
Show resolved
Hide resolved
Please also follow the PR template. How did you test this? |
It would be great if we have a test case. |
I'm new one of contributing to spark. |
Add a test case in JDBCSuite? You can run the test case by the following command:
|
ok to test |
Test build #100812 has finished for PR 23464 at commit
|
Test build #100827 has finished for PR 23464 at commit
|
Test build #100828 has finished for PR 23464 at commit
|
@@ -54,6 +54,11 @@ object JDBCRDD extends Logging { | |||
val table = options.tableOrQuery | |||
val dialect = JdbcDialects.get(url) | |||
val conn: Connection = JdbcUtils.createConnectionFactory(options)() | |||
|
|||
if (null == conn) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: conn == null
. Yes, I think this check should be in the function that createConnectionFactory
returns. I doubt it's ever reasonable to return a null connection.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea. It should be in that function. We can use require
function to throw illegal argument exception. Looks it can potentially return null
assuming from it's doc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it mean that I should to replace this check to createConnectionFactory
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, to the lambda it returns
@@ -68,7 +73,9 @@ object JDBCRDD extends Logging { | |||
statement.close() | |||
} | |||
} finally { | |||
conn.close() | |||
if (null != conn) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can't be null here right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, it can't be null now because we throw exception before.
I just forgot to remove it
@@ -54,6 +54,7 @@ object JDBCRDD extends Logging { | |||
val table = options.tableOrQuery | |||
val dialect = JdbcDialects.get(url) | |||
val conn: Connection = JdbcUtils.createConnectionFactory(options)() | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Go ahead and revert this
driver.connect(options.url, options.asConnectionProperties) | ||
val connection: Connection = driver.connect(options.url, options.asConnectionProperties) | ||
|
||
if (connection == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use require(connection != null, ...)
. I'd also change the message and scaladoc to be a little more general, like here: s"The driver could not open a JDBC connection. Check the URL: ${options.url}"
test("SPARK-26383 throw IllegalArgumentException if url is wrong") { | ||
val e = intercept[IllegalArgumentException] { | ||
val opts = Map( | ||
"url" -> "jdbc:mysql://localhost/db", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to use a more obviously invalid URL here? or does it only trigger this scenario if the URL is valid but can't be opened?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The driver returns null
if it realizes it is the wrong kind of driver to connect to the given URL. I used mysql
url with postgresql
driver.
Test build #100839 has finished for PR 23464 at commit
|
@@ -45,6 +45,7 @@ class JDBCSuite extends QueryTest | |||
|
|||
val url = "jdbc:h2:mem:testdb0" | |||
val urlWithUserAndPass = "jdbc:h2:mem:testdb0;user=testUser;password=testPass" | |||
val wrongUrl = "jdbc:h2:mem:testdbx" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is unused?
@@ -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 JDBC options has wrong url |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe edit this description too per the exception message
Test build #100845 has finished for PR 23464 at commit
|
Test build #100847 has finished for PR 23464 at commit
|
Merged to master |
### 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 apache#23464 from ayudovin/fixing-npe. Authored-by: ayudovin <a.yudovin6695@gmail.com> Signed-off-by: Sean Owen <sean.owen@databricks.com>
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