Skip to content

Commit d5593f7

Browse files
brkyvzhvanhovell
authored andcommitted
[SPARK-19543] from_json fails when the input row is empty
## 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 <[email protected]> Closes #16881 from brkyvz/json-fix.
1 parent fd6c3a0 commit d5593f7

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ case class JsonToStruct(schema: StructType, options: Map[String, String], child:
496496
override def dataType: DataType = schema
497497

498498
override def nullSafeEval(json: Any): Any = {
499-
try parser.parse(json.toString).head catch {
499+
try parser.parse(json.toString).headOption.orNull catch {
500500
case _: SparkSQLJsonProcessingException => null
501501
}
502502
}

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/JsonExpressionsSuite.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,14 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
376376
)
377377
}
378378

379+
test("SPARK-19543: from_json empty input column") {
380+
val schema = StructType(StructField("a", IntegerType) :: Nil)
381+
checkEvaluation(
382+
JsonToStruct(schema, Map.empty, Literal.create(" ", StringType)),
383+
null
384+
)
385+
}
386+
379387
test("to_json") {
380388
val schema = StructType(StructField("a", IntegerType) :: Nil)
381389
val struct = Literal.create(create_row(1), schema)

0 commit comments

Comments
 (0)