Skip to content

Commit

Permalink
[SPARK-19543] from_json fails when the input row is empty
Browse files Browse the repository at this point in the history
## What changes were proposed in this pull request?

Using from_json on a column with an empty string results in: java.util.NoSuchElementException: head of empty list.

This is because `parser.parse(input)` may return `Nil` when `input.trim.isEmpty`

## How was this patch tested?

Regression test in `JsonExpressionsSuite`

Author: Burak Yavuz <brkyvz@gmail.com>

Closes #16881 from brkyvz/json-fix.
  • Loading branch information
brkyvz authored and hvanhovell committed Feb 10, 2017
1 parent fd6c3a0 commit d5593f7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Expand Up @@ -496,7 +496,7 @@ case class JsonToStruct(schema: StructType, options: Map[String, String], child:
override def dataType: DataType = schema

override def nullSafeEval(json: Any): Any = {
try parser.parse(json.toString).head catch {
try parser.parse(json.toString).headOption.orNull catch {
case _: SparkSQLJsonProcessingException => null
}
}
Expand Down
Expand Up @@ -376,6 +376,14 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
)
}

test("SPARK-19543: from_json empty input column") {
val schema = StructType(StructField("a", IntegerType) :: Nil)
checkEvaluation(
JsonToStruct(schema, Map.empty, Literal.create(" ", StringType)),
null
)
}

test("to_json") {
val schema = StructType(StructField("a", IntegerType) :: Nil)
val struct = Literal.create(create_row(1), schema)
Expand Down

0 comments on commit d5593f7

Please sign in to comment.