Skip to content

Commit

Permalink
[SPARK-18260] Make from_json null safe
Browse files Browse the repository at this point in the history
## What changes were proposed in this pull request?

`from_json` is currently not safe against `null` rows. This PR adds a fix and a regression test for it.

## How was this patch tested?

Regression test

Author: Burak Yavuz <[email protected]>

Closes apache#15771 from brkyvz/json_fix.
  • Loading branch information
brkyvz authored and rxin committed Nov 5, 2016
1 parent 8a9ca19 commit 6e27018
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,9 @@ case class JsonToStruct(schema: StructType, options: Map[String, String], child:
override def children: Seq[Expression] = child :: Nil

override def eval(input: InternalRow): Any = {
try parser.parse(child.eval(input).toString).head catch {
val json = child.eval(input)
if (json == null) return null
try parser.parse(json.toString).head catch {
case _: SparkSQLJsonProcessingException => null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,14 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
)
}

test("from_json null input column") {
val schema = StructType(StructField("a", IntegerType) :: Nil)
checkEvaluation(
JsonToStruct(schema, Map.empty, Literal(null)),
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 6e27018

Please sign in to comment.