This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/main by this push:
new 20aa5cf80b fix(arrow-json): write null dictionary values as null in
the JSON writer (#12259)
20aa5cf80b is described below
commit 20aa5cf80b126a1a4530efbf7398d85d00613e12
Author: Mohammad Sohail Shaikh <[email protected]>
AuthorDate: Fri Nov 20 09:12:37 2026 +0000
fix(arrow-json): write null dictionary values as null in the JSON writer
(#12259)
# Which issue does this PR close?
No issue; the repro is below.
# Rationale for this change
A dictionary key that points at a null dictionary value is a logical
null, but the JSON writer encodes it as `""`, because the dictionary
encoder only receives the key validity buffer. For keys `[0, 1, null]`
over values `["a", null]`, the writer outputs
`[{"my_dict":"a"},{"my_dict":null},{"my_dict":""}]`. In the #6503
review, alamb pointed out this exact output: "will write a JSON string
with whatever happened to be in that NULL slot (likely an empty string
`""`)". The RunEndEncoded and Null cases in `make_encoder` already use
`logical_nulls()`; Dictionary was the odd one out.
# What changes are included in this PR?
`make_encoder` passes the dictionary's `logical_nulls()` instead of
`nulls()`. `test_writer_null_dict` (added in #6503 to stop a panic)
recorded `""` for this case; it now expects `null`.
# Are these changes tested?
Yes, `test_writer_null_dict` fails on main and passes with the change.
# Are there any significant user-facing changes?
Yes, small: a dictionary key pointing at a null value is now written as
null (or omitted without explicit nulls) instead of `""`.
# AI usage disclosure
The implementation and tests were written with AI assistance.
Thanks for taking a look!
---
arrow-json/src/writer/encoder.rs | 2 +-
arrow-json/src/writer/mod.rs | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/arrow-json/src/writer/encoder.rs b/arrow-json/src/writer/encoder.rs
index d739aa872b..1bead54ff1 100644
--- a/arrow-json/src/writer/encoder.rs
+++ b/arrow-json/src/writer/encoder.rs
@@ -389,7 +389,7 @@ pub fn make_encoder<'a>(
DataType::Dictionary(_, _) => downcast_dictionary_array! {
array => {
-
NullableEncoder::new(Box::new(DictionaryEncoder::try_new(field, array,
options)?), array.nulls().cloned())
+
NullableEncoder::new(Box::new(DictionaryEncoder::try_new(field, array,
options)?), array.logical_nulls())
},
_ => unreachable!()
}
diff --git a/arrow-json/src/writer/mod.rs b/arrow-json/src/writer/mod.rs
index 1201be6e3d..5a6ef1ba6d 100644
--- a/arrow-json/src/writer/mod.rs
+++ b/arrow-json/src/writer/mod.rs
@@ -2075,7 +2075,7 @@ mod tests {
let json_str = str::from_utf8(&json).unwrap();
assert_eq!(
json_str,
- r#"[{"my_dict":"a"},{"my_dict":null},{"my_dict":""}]"#
+ r#"[{"my_dict":"a"},{"my_dict":null},{"my_dict":null}]"#
)
}