Skip to content
Snippets Groups Projects
Commit 6e270181 authored by Burak Yavuz's avatar Burak Yavuz Committed by Reynold Xin
Browse files

[SPARK-18260] Make from_json null safe

## 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 <brkyvz@gmail.com>

Closes #15771 from brkyvz/json_fix.
parent 8a9ca192
No related branches found
No related tags found
No related merge requests found
......@@ -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
}
}
......
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment