spark git commit: [SPARK-19543] from_json fails when the input row is empty

2017-02-10 Thread hvanhovell
Repository: spark
Updated Branches:
  refs/heads/branch-2.1 ff5818b8c -> 7b5ea000e


[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 

Closes #16881 from brkyvz/json-fix.

(cherry picked from commit d5593f7f5794bd0343e783ac4957864fed9d1b38)
Signed-off-by: Herman van Hovell 


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

Branch: refs/heads/branch-2.1
Commit: 7b5ea000e246f7052e7324fd7f2e99f32aaece17
Parents: ff5818b
Author: Burak Yavuz 
Authored: Fri Feb 10 12:55:06 2017 +0100
Committer: Herman van Hovell 
Committed: Fri Feb 10 12:55:26 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/7b5ea000/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 92d0888..abd7696 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
@@ -497,7 +497,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/7b5ea000/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



spark git commit: [SPARK-19543] from_json fails when the input row is empty

2017-02-10 Thread hvanhovell
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 

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 
Authored: Fri Feb 10 12:55:06 2017 +0100
Committer: Herman van Hovell 
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