Skip to content
Snippets Groups Projects
Commit 149cd692 authored by gatorsmile's avatar gatorsmile Committed by Davies Liu
Browse files

[SPARK-12028] [SQL] get_json_object returns an incorrect result when the value is null literals

When calling `get_json_object` for the following two cases, both results are `"null"`:

```scala
    val tuple: Seq[(String, String)] = ("5", """{"f1": null}""") :: Nil
    val df: DataFrame = tuple.toDF("key", "jstring")
    val res = df.select(functions.get_json_object($"jstring", "$.f1")).collect()
```
```scala
    val tuple2: Seq[(String, String)] = ("5", """{"f1": "null"}""") :: Nil
    val df2: DataFrame = tuple2.toDF("key", "jstring")
    val res3 = df2.select(functions.get_json_object($"jstring", "$.f1")).collect()
```

Fixed the problem and also added a test case.

Author: gatorsmile <gatorsmile@gmail.com>

Closes #10018 from gatorsmile/get_json_object.
parent b9921524
No related branches found
No related tags found
No related merge requests found
......@@ -298,8 +298,11 @@ case class GetJsonObject(json: Expression, path: Expression)
case (FIELD_NAME, Named(name) :: xs) if p.getCurrentName == name =>
// exact field match
p.nextToken()
evaluatePath(p, g, style, xs)
if (p.nextToken() != JsonToken.VALUE_NULL) {
evaluatePath(p, g, style, xs)
} else {
false
}
case (FIELD_NAME, Wildcard :: xs) =>
// wildcard field match
......
......@@ -39,6 +39,26 @@ class JsonFunctionsSuite extends QueryTest with SharedSQLContext {
("6", "[invalid JSON string]") ::
Nil
test("function get_json_object - null") {
val df: DataFrame = tuples.toDF("key", "jstring")
val expected =
Row("1", "value1", "value2", "3", null, "5.23") ::
Row("2", "value12", "2", "value3", "4.01", null) ::
Row("3", "value13", "2", "value33", "value44", "5.01") ::
Row("4", null, null, null, null, null) ::
Row("5", "", null, null, null, null) ::
Row("6", null, null, null, null, null) ::
Nil
checkAnswer(
df.select($"key", functions.get_json_object($"jstring", "$.f1"),
functions.get_json_object($"jstring", "$.f2"),
functions.get_json_object($"jstring", "$.f3"),
functions.get_json_object($"jstring", "$.f4"),
functions.get_json_object($"jstring", "$.f5")),
expected)
}
test("json_tuple select") {
val df: DataFrame = tuples.toDF("key", "jstring")
val expected =
......
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