Pastoray commented on code in PR #27432:
URL: https://github.com/apache/datafusion/pull/27432#discussion_r4776796623
##########
datafusion/sqllogictest/test_files/json.slt:
##########
@@ -161,3 +161,36 @@ statement error Not valid JSON
CREATE EXTERNAL TABLE json_array_as_ndjson
STORED AS JSON
LOCATION '../core/tests/data/json_array.json';
+
+##########
Review Comment:
Done, thanks. I added round-trip tests to `json.slt` (COPY to JSON, read
back through an external table, compare with the source) covering:
- a `STRUCT` with NULL fields, an all-NULL-fields struct, and a NULL struct
- a `LIST` with NULL elements, a list containing only a NULL, and a NULL list
- a table with a `MAP`, `STRUCT` and `LIST` together, to check that the
explicit NULLs written because of the `MAP` don't change what the other columns
read back as
These pass both with and without the `MAP`-only condition, since a
struct/list NULL can be recovered from the schema, so they act as regression
tests rather than reproducing a bug.
##########
datafusion/datasource-json/src/file_format.rs:
##########
@@ -373,6 +373,53 @@ impl Default for JsonSerializer {
}
}
+/// Returns `true` if `data_type` is, or contains at any nesting level, a
+/// [`DataType::Map`].
+fn contains_map(data_type: &DataType) -> bool {
+ match data_type {
+ DataType::Map(_, _) => true,
+ DataType::List(field)
+ | DataType::LargeList(field)
+ | DataType::ListView(field)
+ | DataType::LargeListView(field)
+ | DataType::FixedSizeList(field, _) => contains_map(field.data_type()),
+ DataType::Struct(fields) => {
+ fields.iter().any(|field| contains_map(field.data_type()))
+ }
+ DataType::Union(fields, _) => fields
+ .iter()
+ .any(|(_, field)| contains_map(field.data_type())),
+ DataType::Dictionary(_, value_type) => contains_map(value_type),
+ DataType::RunEndEncoded(_, values) => contains_map(values.data_type()),
+ _ => false,
+ }
+}
+
+/// Creates a newline-delimited JSON writer suited to `schema`.
+///
+/// By default, `arrow-json` omits any NULL value from the output. For a
+/// struct field or a top level column that is lossless because a reader fills
+/// the missing key back in from the schema. A `MAP` is different: the entry
+/// key *is* the data, so omitting an entry whose value is NULL silently drops
+/// the key and changes the cardinality of the map
+/// (`{color: red, size: NULL}` would be written as `{"color":"red"}`).
+///
+/// Therefore, when the schema contains a `MAP` (at any nesting level), NULLs
+/// are written explicitly, e.g. `{"color":"red","size":null}`. Schemas
+/// without a `MAP` keep the previous, more compact output.
+pub(crate) fn line_delimited_writer<W: std::io::Write>(
+ writer: W,
+ schema: &Schema,
+) -> json::LineDelimitedWriter<W> {
+ let explicit_nulls = schema
+ .fields()
+ .iter()
+ .any(|field| contains_map(field.data_type()));
+ json::WriterBuilder::new()
+ .with_explicit_nulls(explicit_nulls)
Review Comment:
Good question. I only enabled it for schemas containing a `MAP` to keep this
change as small as possible and avoid changing the output for existing users:
- For a struct field, or a top-level column, omitting a NULL is lossless
when reading with a schema, because the missing key is filled back in as NULL.
A `MAP` is different: the entry key *is* the data, so omitting `{size: NULL}`
changes the map's contents and cardinality. That is the bug in #26493.
- Always enabling it would change the bytes written for every existing JSON
sink (more `null`s, larger files), which didn't seem necessary to fix the bug.
That said, I don't feel strongly about this. The downsides of the
conditional approach are that it is a bit more code (`contains_map`) and the
output for a given column depends on the other columns in the schema. When a
table has a `MAP`, its other NULL columns also become explicit `null`s, so the
same column is written differently depending on what sits next to it. Always
enabling it would be simpler and consistent with Parquet/DuckDB-style output,
and it would also keep the key visible for readers that infer the schema from
the file.
I tried the always-on variant locally: `json.slt` and `copy.slt` pass
unchanged, and the only test that fails is my own unit test asserting that
non-map schemas still omit NULLs. Happy to switch to always-on (it would remove
`contains_map` and that test) if you prefer, or keep it scoped to `MAP` to
avoid a behavior change for existing users. Let me know which you'd like.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]