Skip to content
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

Closed
wants to merge 7 commits into from

Conversation

ayudovin
Copy link
Contributor

@ayudovin ayudovin commented Jan 5, 2019

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

@HyukjinKwon
Copy link
Member

Please also follow the PR template. How did you test this?

@dongjoon-hyun
Copy link
Member

It would be great if we have a test case.

@ayudovin
Copy link
Contributor Author

ayudovin commented Jan 5, 2019

I'm new one of contributing to spark.
I try to add tests to this pull requests but now I have some problem with compiling

@gatorsmile
Copy link
Member

Add a test case in JDBCSuite?

You can run the test case by the following command:

build/sbt "sql/test-only org.apache.spark.sql.jdbc.JDBCSuite"

@gatorsmile
Copy link
Member

ok to test

@SparkQA
Copy link

SparkQA commented Jan 6, 2019

Test build #100812 has finished for PR 23464 at commit 5215f1b.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Jan 6, 2019

Test build #100827 has finished for PR 23464 at commit 2a6243c.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Jan 6, 2019

Test build #100828 has finished for PR 23464 at commit 0989db9.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@@ -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) {
Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

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 ?

Copy link
Member

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) {
Copy link
Member

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?

Copy link
Contributor Author

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)()

Copy link
Member

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) {
Copy link
Member

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",
Copy link
Member

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?

Copy link
Contributor Author

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.

@SparkQA
Copy link

SparkQA commented Jan 6, 2019

Test build #100839 has finished for PR 23464 at commit 34fe718.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@@ -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"
Copy link
Member

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
Copy link
Member

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

@SparkQA
Copy link

SparkQA commented Jan 6, 2019

Test build #100845 has finished for PR 23464 at commit e8fd2b6.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Jan 7, 2019

Test build #100847 has finished for PR 23464 at commit 07f5898.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@srowen
Copy link
Member

srowen commented Jan 7, 2019

Merged to master

@srowen srowen closed this in 868e025 Jan 7, 2019
jackylee-ch pushed a commit to jackylee-ch/spark that referenced this pull request Feb 18, 2019
### 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants