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
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ object DataType {
name match {
case "decimal" => DecimalType.USER_DEFAULT
case FIXED_DECIMAL(precision, scale) => DecimalType(precision.toInt, scale.toInt)
case other => nonDecimalNameToType(other)
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

}
}

Expand Down Expand Up @@ -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 =>

throw new IllegalArgumentException(
s"Failed to convert the JSON string '${compact(render(other))}' to a data type.")
}

private def parseStructField(json: JValue): StructField = json match {
Expand All @@ -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.

throw new IllegalArgumentException(
s"Failed to convert the JSON string '${compact(render(other))}' to a field.")
}

protected[types] def buildFormattedString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.spark.sql.types

import com.fasterxml.jackson.core.JsonParseException

import org.apache.spark.{SparkException, SparkFunSuite}
import org.apache.spark.sql.catalyst.parser.CatalystSqlParser

Expand Down Expand Up @@ -203,6 +205,32 @@ class DataTypeSuite extends SparkFunSuite {
StructField("c", DoubleType, nullable = false, metadata)))
checkDataTypeJsonRepr(structType)

test("fromJson throws an exception when given type string is invalid") {
var message = intercept[IllegalArgumentException] {
DataType.fromJson(""""abcd"""")
}.getMessage
assert(message.contains(
"Failed to convert the JSON string 'abcd' to a data type."))

message = intercept[IllegalArgumentException] {
DataType.fromJson("""{"abcd":"a"}""")
}.getMessage
assert(message.contains(
"""Failed to convert the JSON string '{"abcd":"a"}' to a data type"""))

message = intercept[IllegalArgumentException] {
DataType.fromJson("""{"fields": [{"a":123}], "type": "struct"}""")
}.getMessage
assert(message.contains(
"""Failed to convert the JSON string '{"a":123}' to a field."""))

// Malformed JSON string
message = intercept[JsonParseException] {
DataType.fromJson("abcd")
}.getMessage
assert(message.contains("Unrecognized token 'abcd'"))
}

def checkDefaultSize(dataType: DataType, expectedDefaultSize: Int): Unit = {
test(s"Check the default size of $dataType") {
assert(dataType.defaultSize === expectedDefaultSize)
Expand Down