alamb commented on code in PR #27432:
URL: https://github.com/apache/datafusion/pull/27432#discussion_r4774912824
##########
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:
why wouldn't we always enable this flag? Why only when MAP is set?
##########
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:
can you please also add tests for Structs and Lists (that also have NULLs )?
--
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]