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-20143][SQL] DataType.fromJson should throw an exception with better message #17468

Closed
wants to merge 2 commits into from

Conversation

HyukjinKwon
Copy link
Member

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:

DataType.fromJson(""""abcd"""")
java.util.NoSuchElementException: key not found: abcd
  at ...
DataType.fromJson("""{"abcd":"a"}""")
scala.MatchError: JObject(List((abcd,JString(a)))) (of class org.json4s.JsonAST$JObject)
  at ...
DataType.fromJson("""{"fields": [{"a":123}], "type": "struct"}""")
scala.MatchError: JObject(List((a,JInt(123)))) (of class org.json4s.JsonAST$JObject)
  at ...

After this PR,

DataType.fromJson(""""abcd"""")
java.lang.IllegalArgumentException: Failed to convert the JSON string 'abcd' to a data type.
  at ...
DataType.fromJson("""{"abcd":"a"}""")
java.lang.IllegalArgumentException: Failed to convert the JSON string '{"abcd":"a"}' to a data type.
  at ...
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.

@HyukjinKwon
Copy link
Member Author

(BTW, I believe this does not make a conflict with PR 17406)

@SparkQA
Copy link

SparkQA commented Mar 29, 2017

Test build #75357 has finished for PR 17468 at commit 75ebd18.

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

@HyukjinKwon
Copy link
Member Author

@gatorsmile, could you take a look please?

case other => nonDecimalNameToType.getOrElse(other, {
throw new IllegalArgumentException(
s"Failed to convert the JSON string '$name' to a data type.")
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ and } are not needed

@@ -164,6 +167,10 @@ object DataType {
("sqlType", v: JValue),
("type", JString("udt"))) =>
new PythonUserDefinedType(parseDataType(v), pyClass, serialized)

case other: JValue =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case other =>

@@ -179,6 +186,9 @@ object DataType {
("nullable", JBool(nullable)),
("type", dataType: JValue)) =>
StructField(name, parseDataType(dataType), nullable)
case other: JValue =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same here.

Copy link
Member

@gatorsmile gatorsmile Mar 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or case _ =>. Then, you can directly use json in the following message.

@gatorsmile
Copy link
Member

LGTM except a few minor comments.

@HyukjinKwon
Copy link
Member Author

Thank you @gatorsmile.

@SparkQA
Copy link

SparkQA commented Mar 31, 2017

Test build #75405 has finished for PR 17468 at commit 756825d.

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

@HyukjinKwon
Copy link
Member Author

@gatorsmile, could this get merged maybe?

@gatorsmile
Copy link
Member

retest this please

@SparkQA
Copy link

SparkQA commented Apr 2, 2017

Test build #75456 has finished for PR 17468 at commit 756825d.

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

@gatorsmile
Copy link
Member

Thanks! Merging to master.

@asfgit asfgit closed this in d40cbb8 Apr 2, 2017
@HyukjinKwon HyukjinKwon deleted the fromjson_exception branch January 2, 2018 03:38
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.

3 participants