GitHub user HyukjinKwon opened a pull request:
https://github.com/apache/spark/pull/17468
[SPARK-20143][SQL] DataType.fromJson should throw an exception with better
message
## What changes were proposed in this pull request?
Currently, `DataType.fromJson` throws `scala.MatchError` or
`java.util.NoSuchElementException` in some cases when the JSON input is invalid
as below:
```scala
DataType.fromJson(""""abcd"""")
```
```
java.util.NoSuchElementException: key not found: abcd
at ...
```
```scala
DataType.fromJson("""{"abcd":"a"}""")
```
```
scala.MatchError: JObject(List((abcd,JString(a)))) (of class
org.json4s.JsonAST$JObject)
at ...
```
```scala
DataType.fromJson("""{"fields": [{"a":123}], "type": "struct"}""")
```
```
scala.MatchError: JObject(List((a,JInt(123)))) (of class
org.json4s.JsonAST$JObject)
at ...
```
After this PR,
```scala
DataType.fromJson(""""abcd"""")
```
```
java.lang.IllegalArgumentException: Failed to convert the JSON string
'abcd' to a data type.
at ...
```
```scala
DataType.fromJson("""{"abcd":"a"}""")
```
```
java.lang.IllegalArgumentException: Failed to convert the JSON string
'{"abcd":"a"}' to a data type.
at ...
```
```scala
DataType.fromJson("""{"fields": [{"a":123}], "type": "struct"}""")
at ...
```
```
java.lang.IllegalArgumentException: Failed to convert the JSON string
'{"a":123}' to a field.
```
## How was this patch tested?
Unit test added in `DataTypeSuite`.
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/HyukjinKwon/spark fromjson_exception
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/spark/pull/17468.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #17468
----
commit 75ebd181950a556b264267a73a452f9e75601439
Author: hyukjinkwon <[email protected]>
Date: 2017-03-29T14:17:35Z
DataType.fromJson should throw an exception with better message
----
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]