Skip to content

Commit

Permalink
[SPARK-23773][SQL] JacksonGenerator does not include keys that have n…
Browse files Browse the repository at this point in the history
…ull value for StructTypes
  • Loading branch information
Sergey Makagonov committed Mar 22, 2018
1 parent 5c9eaa6 commit 9faf853
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,10 @@ private[sql] class JacksonGenerator(
var i = 0
while (i < row.numFields) {
val field = schema(i)
if (!row.isNullAt(i)) {
gen.writeFieldName(field.name)
gen.writeFieldName(field.name)
if (row.isNullAt(i)) {
gen.writeNull()
} else {
fieldWriters(i).apply(row, i)
}
i += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,7 @@ class JsonSuite extends QueryTest with SharedSQLContext with TestJsonData {
val df2 = df1.toDF
val result = df2.toJSON.collect()
// scalastyle:off
assert(result(0) === "{\"f1\":1,\"f2\":\"A1\",\"f3\":true,\"f4\":[\"1\",\" A1\",\" true\",\" null\"]}")
assert(result(0) === "{\"f1\":1,\"f2\":\"A1\",\"f3\":true,\"f4\":[\"1\",\" A1\",\" true\",\" null\"],\"f5\":null}")
assert(result(3) === "{\"f1\":4,\"f2\":\"D4\",\"f3\":true,\"f4\":[\"4\",\" D4\",\" true\",\" 2147483644\"],\"f5\":2147483644}")
// scalastyle:on

Expand Down Expand Up @@ -1265,6 +1265,7 @@ class JsonSuite extends QueryTest with SharedSQLContext with TestJsonData {
1.7976931348623157E308,
10,
21474836470L,
null,
"this is a simple string.")
)

Expand Down Expand Up @@ -2063,4 +2064,11 @@ class JsonSuite extends QueryTest with SharedSQLContext with TestJsonData {
)
}
}

test("SPARK-23773: JacksonGenerator does not include keys " +
"that have null value for StructTypes") {
val df = sql("select NAMED_STRUCT('f1', 10, 'f2', null, 'f3', 15) as my_struct")
val result = df.toJSON.collect()(0)
assert(result === "{\"my_struct\":{\"f1\":10,\"f2\":null,\"f3\":15}}")
}
}

0 comments on commit 9faf853

Please sign in to comment.