Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix arrow-json encoding with dictionary including nulls #6503

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion arrow-json/src/writer/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ impl<'a, K: ArrowDictionaryKeyType> DictionaryEncoder<'a, K> {
array: &'a DictionaryArray<K>,
options: &EncoderOptions,
) -> Result<Self, ArrowError> {
let encoder = make_encoder(array.values().as_ref(), options)?;
let (encoder, _) = make_encoder_impl(array.values().as_ref(), options)?;
alamb marked this conversation as resolved.
Show resolved Hide resolved

Ok(Self {
keys: array.keys().values().clone(),
Expand Down
31 changes: 29 additions & 2 deletions arrow-json/src/writer.rs → arrow-json/src/writer/mod.rs
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can revert this move if you prefer, but I think the previous naming was weird.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like a nice improvement to me

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually seems mod.rs naming is no longer the preferred convention: https://doc.rust-lang.org/reference/items/modules.html#module-source-filenames

Note: Prior to rustc 1.30, using mod.rs files was the way to load a module with nested children. It is encouraged to use the new naming convention as it is more consistent, and avoids having many files named mod.rs within a project.

I don't have any particular strong feelings about this, as it would be nice to keep consistent (as reader already has mod.rs) 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree keeping consistency is a good thing

Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ use std::{fmt::Debug, io::Write};
use arrow_array::*;
use arrow_schema::*;

use crate::writer::encoder::EncoderOptions;
use encoder::make_encoder;
use encoder::{make_encoder, EncoderOptions};

/// This trait defines how to format a sequence of JSON objects to a
/// byte stream.
Expand Down Expand Up @@ -1806,4 +1805,32 @@ mod tests {
);
}
}

#[test]
fn test_writer_null_dict() {
let keys = Int32Array::from_iter(vec![Some(0), None, Some(1)]);
let values = Arc::new(StringArray::from_iter(vec![Some("a"), None]));
let dict = DictionaryArray::new(keys, values);

let schema = SchemaRef::new(Schema::new(vec![Field::new(
"my_dict",
DataType::Dictionary(DataType::Int32.into(), DataType::Utf8.into()),
true,
)]));

let array = Arc::new(dict) as ArrayRef;
let batch = RecordBatch::try_new(schema, vec![array]).unwrap();

let mut json = Vec::new();
let write_builder = WriterBuilder::new().with_explicit_nulls(true);
let mut writer = write_builder.build::<_, JsonArray>(&mut json);
writer.write(&batch).unwrap();
writer.close().unwrap();

let json_str = str::from_utf8(&json).unwrap();
assert_eq!(
json_str,
r#"[{"my_dict":"a"},{"my_dict":null},{"my_dict":null}]"#
)
}
Comment on lines +1809 to +1835
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
Loading