Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 04C94200C17 for ; Fri, 10 Feb 2017 12:55:36 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 03529160B5C; Fri, 10 Feb 2017 11:55:36 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 4B481160B5B for ; Fri, 10 Feb 2017 12:55:35 +0100 (CET) Received: (qmail 94735 invoked by uid 500); 10 Feb 2017 11:55:34 -0000 Mailing-List: contact commits-help@spark.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Delivered-To: mailing list commits@spark.apache.org Received: (qmail 94726 invoked by uid 99); 10 Feb 2017 11:55:34 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 10 Feb 2017 11:55:34 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 4965BDFBDB; Fri, 10 Feb 2017 11:55:34 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: hvanhovell@apache.org To: commits@spark.apache.org Message-Id: <749c11e322e94eb0b00345d98822708a@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: spark git commit: [SPARK-19543] from_json fails when the input row is empty Date: Fri, 10 Feb 2017 11:55:34 +0000 (UTC) archived-at: Fri, 10 Feb 2017 11:55:36 -0000 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-unsubscribe@spark.apache.org For additional commands, e-mail: commits-help@spark.apache.org