Skip to content

Commit

Permalink
[SPARK-29675][SQL] Add exception when isolationLevel is Illegal
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

Now we use JDBC api and set an Illegal isolationLevel option, spark will throw a `scala.MatchError`, it's not friendly to user. So we should add an IllegalArgumentException.

### Why are the changes needed?

Make exception friendly to user.

### Does this PR introduce any user-facing change?

No.

### How was this patch tested?

Add UT.

Closes #26334 from ulysses-you/SPARK-29675.

Authored-by: ulysses <youxiduo@weidian.com>
Signed-off-by: Dongjoon Hyun <dhyun@apple.com>
  • Loading branch information
ulysses-you authored and dongjoon-hyun committed Oct 31, 2019
1 parent 121510c commit 888cc46
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ class JDBCOptions(
case "READ_COMMITTED" => Connection.TRANSACTION_READ_COMMITTED
case "REPEATABLE_READ" => Connection.TRANSACTION_REPEATABLE_READ
case "SERIALIZABLE" => Connection.TRANSACTION_SERIALIZABLE
case other => throw new IllegalArgumentException(
s"Invalid value `$other` for parameter `$JDBC_TXN_ISOLATION_LEVEL`. This can be " +
"`NONE`, `READ_UNCOMMITTED`, `READ_COMMITTED`, `REPEATABLE_READ` or `SERIALIZABLE`."
)
}
// An option to execute custom SQL before fetching data from the remote DB
val sessionInitStatement = parameters.get(JDBC_SESSION_INIT_STATEMENT)
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 @@ -1649,4 +1649,17 @@ class JDBCSuite extends QueryTest
}
}
}

test("Add exception when isolationLevel is Illegal") {
val e = intercept[IllegalArgumentException] {
spark.read.format("jdbc")
.option("Url", urlWithUserAndPass)
.option("dbTable", "test.people")
.option("isolationLevel", "test")
.load()
}.getMessage
assert(e.contains(
"Invalid value `test` for parameter `isolationLevel`. This can be " +
"`NONE`, `READ_UNCOMMITTED`, `READ_COMMITTED`, `REPEATABLE_READ` or `SERIALIZABLE`."))
}
}

0 comments on commit 888cc46

Please sign in to comment.