-
Notifications
You must be signed in to change notification settings - Fork 867
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
support struct array in pretty display #579
Changes from all commits
2cbf918
3d620f6
5491ef8
119c013
da79ba9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,9 +106,9 @@ mod tests { | |
use crate::{ | ||
array::{ | ||
self, new_null_array, Array, Date32Array, Date64Array, PrimitiveBuilder, | ||
StringBuilder, StringDictionaryBuilder, Time32MillisecondArray, | ||
Time32SecondArray, Time64MicrosecondArray, Time64NanosecondArray, | ||
TimestampMicrosecondArray, TimestampMillisecondArray, | ||
StringArray, StringBuilder, StringDictionaryBuilder, StructArray, | ||
Time32MillisecondArray, Time32SecondArray, Time64MicrosecondArray, | ||
Time64NanosecondArray, TimestampMicrosecondArray, TimestampMillisecondArray, | ||
TimestampNanosecondArray, TimestampSecondArray, | ||
}, | ||
datatypes::{DataType, Field, Int32Type, Schema}, | ||
|
@@ -507,4 +507,63 @@ mod tests { | |
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_pretty_format_struct() -> Result<()> { | ||
let schema = Schema::new(vec![ | ||
Field::new( | ||
"c1", | ||
DataType::Struct(vec![ | ||
Field::new("c11", DataType::Int32, false), | ||
Field::new( | ||
"c12", | ||
DataType::Struct(vec![Field::new("c121", DataType::Utf8, false)]), | ||
false, | ||
), | ||
]), | ||
false, | ||
), | ||
Field::new("c2", DataType::Utf8, false), | ||
]); | ||
|
||
let c1 = StructArray::from(vec![ | ||
( | ||
Field::new("c11", DataType::Int32, false), | ||
Arc::new(Int32Array::from(vec![Some(1), None, Some(5)])) as ArrayRef, | ||
), | ||
( | ||
Field::new( | ||
"c12", | ||
DataType::Struct(vec![Field::new("c121", DataType::Utf8, false)]), | ||
false, | ||
), | ||
Arc::new(StructArray::from(vec![( | ||
Field::new("c121", DataType::Utf8, false), | ||
Arc::new(StringArray::from(vec![Some("e"), Some("f"), Some("g")])) | ||
as ArrayRef, | ||
)])) as ArrayRef, | ||
), | ||
]); | ||
let c2 = StringArray::from(vec![Some("a"), Some("b"), Some("c")]); | ||
|
||
let batch = | ||
RecordBatch::try_new(Arc::new(schema), vec![Arc::new(c1), Arc::new(c2)]) | ||
.unwrap(); | ||
|
||
let table = pretty_format_batches(&[batch])?; | ||
let expected = vec![ | ||
r#"+-------------------------------------+----+"#, | ||
r#"| c1 | c2 |"#, | ||
r#"+-------------------------------------+----+"#, | ||
r#"| {"c11": 1, "c12": {"c121": "e"}} | a |"#, | ||
r#"| {"c11": null, "c12": {"c121": "f"}} | b |"#, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it could have been useful to have a struct with a validity tested, to make sure we do not miss that case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will send a follow up PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't have a strong opinion on this. I went with this way because I feel like it looks more clear than |
||
r#"| {"c11": 5, "c12": {"c121": "g"}} | c |"#, | ||
r#"+-------------------------------------+----+"#, | ||
]; | ||
|
||
let actual: Vec<&str> = table.lines().collect(); | ||
assert_eq!(expected, actual, "Actual result:\n{}", table); | ||
|
||
Ok(()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is cool -- it is almost like JSON