Repository: spark
Updated Branches:
  refs/heads/master fd6c3a0b1 -> d5593f7f5


[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 <brk...@gmail.com>

Closes #16881 from brkyvz/json-fix.


Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/d5593f7f
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/d5593f7f
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/d5593f7f

Branch: refs/heads/master
Commit: d5593f7f5794bd0343e783ac4957864fed9d1b38
Parents: fd6c3a0
Author: Burak Yavuz <brk...@gmail.com>
Authored: Fri Feb 10 12:55:06 2017 +0100
Committer: Herman van Hovell <hvanhov...@databricks.com>
Committed: Fri Feb 10 12:55:06 2017 +0100

----------------------------------------------------------------------
 .../spark/sql/catalyst/expressions/jsonExpressions.scala     | 2 +-
 .../sql/catalyst/expressions/JsonExpressionsSuite.scala      | 8 ++++++++
 2 files changed, 9 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/d5593f7f/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
----------------------------------------------------------------------
diff --git 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
index d55f85d..c410e79 100644
--- 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
+++ 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
@@ -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
     }
   }

http://git-wip-us.apache.org/repos/asf/spark/blob/d5593f7f/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/JsonExpressionsSuite.scala
----------------------------------------------------------------------
diff --git 
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/JsonExpressionsSuite.scala
 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/JsonExpressionsSuite.scala
index 618b8b2..8e20bd1 100644
--- 
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/JsonExpressionsSuite.scala
+++ 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/JsonExpressionsSuite.scala
@@ -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)


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org
For additional commands, e-mail: commits-h...@spark.apache.org

Reply via email to