-
Notifications
You must be signed in to change notification settings - Fork 1k
refactor: improve display formatting for Union #8529
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
Changes from 2 commits
9b64973
586236d
ca49a25
3d5af15
8b4cfe6
5a92730
92868e4
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 |
|---|---|---|
|
|
@@ -132,7 +132,24 @@ impl fmt::Display for DataType { | |
| Ok(()) | ||
| } | ||
| Self::Union(union_fields, union_mode) => { | ||
| write!(f, "Union({union_fields:?}, {union_mode:?})") | ||
| write!(f, "Union({union_mode:?}, ")?; | ||
| if !union_fields.is_empty() { | ||
| let fields_str = union_fields | ||
| .iter() | ||
| .map(|v| { | ||
| let type_id = v.0; | ||
| let field = v.1; | ||
| let maybe_nullable = if field.is_nullable() { "nullable " } else { "" }; | ||
| let data_type = field.data_type(); | ||
| let metadata_str = format_metadata(field.metadata()); | ||
| format!("{type_id:?}: {maybe_nullable}{data_type}{metadata_str}") | ||
| }) | ||
| .collect::<Vec<_>>() | ||
| .join(", "); | ||
|
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. Would it be better to use 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. nice catch 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. After some investigation, I think we should continue using commas to separate different fields, following the DuckDB SQL style. For metadata, since it is displayed with brackets, it should not be too confusing. |
||
| write!(f, "{fields_str}")?; | ||
| } | ||
| write!(f, ")")?; | ||
| Ok(()) | ||
| } | ||
| Self::Dictionary(data_type, data_type1) => { | ||
| write!(f, "Dictionary({data_type}, {data_type1})") | ||
|
|
@@ -248,4 +265,22 @@ mod tests { | |
| "FixedSizeList(4 x nullable Int32, metadata: {\"key2\": \"value2\"})"; | ||
| assert_eq!(fixed_size_metadata_string, expected_metadata_string); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_display_union() { | ||
| let fields = vec![ | ||
| Field::new("a", DataType::Int32, false), | ||
| Field::new("b", DataType::Utf8, true), | ||
| ]; | ||
| let type_ids = vec![0, 1]; | ||
| let union_fields = type_ids | ||
| .into_iter() | ||
| .zip(fields.into_iter().map(Arc::new)) | ||
| .collect(); | ||
|
|
||
| let union_data_type = DataType::Union(union_fields, crate::UnionMode::Sparse); | ||
| let union_data_type_string = union_data_type.to_string(); | ||
| let expected_string = "Union(Sparse, 0: Int32, 1: nullable Utf8)"; | ||
Weijun-H marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert_eq!(union_data_type_string, expected_string); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.