Skip to content

Commit 9faf853

Browse files
author
Sergey Makagonov
committed
[SPARK-23773][SQL] JacksonGenerator does not include keys that have null value for StructTypes
1 parent 5c9eaa6 commit 9faf853

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/json/JacksonGenerator.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,10 @@ private[sql] class JacksonGenerator(
171171
var i = 0
172172
while (i < row.numFields) {
173173
val field = schema(i)
174-
if (!row.isNullAt(i)) {
175-
gen.writeFieldName(field.name)
174+
gen.writeFieldName(field.name)
175+
if (row.isNullAt(i)) {
176+
gen.writeNull()
177+
} else {
176178
fieldWriters(i).apply(row, i)
177179
}
178180
i += 1

sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/json/JsonSuite.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ class JsonSuite extends QueryTest with SharedSQLContext with TestJsonData {
12291229
val df2 = df1.toDF
12301230
val result = df2.toJSON.collect()
12311231
// scalastyle:off
1232-
assert(result(0) === "{\"f1\":1,\"f2\":\"A1\",\"f3\":true,\"f4\":[\"1\",\" A1\",\" true\",\" null\"]}")
1232+
assert(result(0) === "{\"f1\":1,\"f2\":\"A1\",\"f3\":true,\"f4\":[\"1\",\" A1\",\" true\",\" null\"],\"f5\":null}")
12331233
assert(result(3) === "{\"f1\":4,\"f2\":\"D4\",\"f3\":true,\"f4\":[\"4\",\" D4\",\" true\",\" 2147483644\"],\"f5\":2147483644}")
12341234
// scalastyle:on
12351235

@@ -1265,6 +1265,7 @@ class JsonSuite extends QueryTest with SharedSQLContext with TestJsonData {
12651265
1.7976931348623157E308,
12661266
10,
12671267
21474836470L,
1268+
null,
12681269
"this is a simple string.")
12691270
)
12701271

@@ -2063,4 +2064,11 @@ class JsonSuite extends QueryTest with SharedSQLContext with TestJsonData {
20632064
)
20642065
}
20652066
}
2067+
2068+
test("SPARK-23773: JacksonGenerator does not include keys " +
2069+
"that have null value for StructTypes") {
2070+
val df = sql("select NAMED_STRUCT('f1', 10, 'f2', null, 'f3', 15) as my_struct")
2071+
val result = df.toJSON.collect()(0)
2072+
assert(result === "{\"my_struct\":{\"f1\":10,\"f2\":null,\"f3\":15}}")
2073+
}
20662074
}

0 commit comments

Comments
 (0)