This is an automated email from the ASF dual-hosted git repository.
alamb 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 30b58d5bdc feat: Enhance `Map` display formatting in DataType (#8570)
30b58d5bdc is described below
commit 30b58d5bdcad1ca07ce7ce22ef3fae79f49ff87e
Author: Alex Huang <[email protected]>
AuthorDate: Sat Oct 11 15:50:09 2025 +0300
feat: Enhance `Map` display formatting in DataType (#8570)
# Which issue does this PR close?
- Part of #8351
# Rationale for this change
# What changes are included in this PR?
# Are these changes tested?
Yes
# Are there any user-facing changes?
No
---
arrow-cast/src/cast/mod.rs | 4 +--
arrow-schema/src/datatype_display.rs | 57 +++++++++++++++++++++++++++++++++++-
2 files changed, 58 insertions(+), 3 deletions(-)
diff --git a/arrow-cast/src/cast/mod.rs b/arrow-cast/src/cast/mod.rs
index aa9d4b021f..aa26d0c2f9 100644
--- a/arrow-cast/src/cast/mod.rs
+++ b/arrow-cast/src/cast/mod.rs
@@ -8772,7 +8772,7 @@ mod tests {
};
assert_eq!(
t,
- r#"Casting from Map(Field { "entries": Struct("key": Utf8,
"value": nullable Utf8) }, false) to Map(Field { "entries": Struct("key": Utf8,
"value": Utf8) }, true) not supported"#
+ r#"Casting from Map("entries": Struct("key": Utf8, "value":
nullable Utf8), unsorted) to Map("entries": Struct("key": Utf8, "value": Utf8),
sorted) not supported"#
);
}
@@ -8823,7 +8823,7 @@ mod tests {
};
assert_eq!(
t,
- r#"Casting from Map(Field { "entries": Struct("key": Utf8,
"value": nullable Interval(DayTime)) }, false) to Map(Field { "entries":
Struct("key": Utf8, "value": Duration(s)) }, true) not supported"#
+ r#"Casting from Map("entries": Struct("key": Utf8, "value":
nullable Interval(DayTime)), unsorted) to Map("entries": Struct("key": Utf8,
"value": Duration(s)), sorted) not supported"#
);
}
diff --git a/arrow-schema/src/datatype_display.rs
b/arrow-schema/src/datatype_display.rs
index 6e9501777a..ea50e3634c 100644
--- a/arrow-schema/src/datatype_display.rs
+++ b/arrow-schema/src/datatype_display.rs
@@ -158,7 +158,20 @@ impl fmt::Display for DataType {
Self::Decimal64(precision, scale) => write!(f,
"Decimal64({precision}, {scale})"),
Self::Decimal128(precision, scale) => write!(f,
"Decimal128({precision}, {scale})"),
Self::Decimal256(precision, scale) => write!(f,
"Decimal256({precision}, {scale})"),
- Self::Map(field, keys_are_sorted) => write!(f, "Map({field},
{keys_are_sorted})"),
+ Self::Map(field, sorted) => {
+ write!(f, "Map(")?;
+ let name = field.name();
+ let maybe_nullable = if field.is_nullable() { "nullable " }
else { "" };
+ let data_type = field.data_type();
+ let metadata_str = format_metadata(field.metadata());
+ let keys_are_sorted = if *sorted { "sorted" } else {
"unsorted" };
+
+ write!(
+ f,
+ "\"{name}\": {maybe_nullable}{data_type}{metadata_str},
{keys_are_sorted})"
+ )?;
+ Ok(())
+ }
Self::RunEndEncoded(run_ends_field, values_field) => {
write!(f, "RunEndEncoded({run_ends_field}, {values_field})")
}
@@ -330,4 +343,46 @@ mod tests {
expected_string_with_metadata
);
}
+
+ #[test]
+ fn test_display_map() {
+ let entry_field = Field::new(
+ "entries",
+ DataType::Struct(
+ vec![
+ Field::new("key", DataType::Utf8, false),
+ Field::new("value", DataType::Int32, true),
+ ]
+ .into(),
+ ),
+ false,
+ );
+ let map_data_type = DataType::Map(Arc::new(entry_field), true);
+ let map_data_type_string = map_data_type.to_string();
+ let expected_string =
+ "Map(\"entries\": Struct(\"key\": Utf8, \"value\": nullable
Int32), sorted)";
+ assert_eq!(map_data_type_string, expected_string);
+
+ // Test with metadata
+ let mut entry_field_with_metadata = Field::new(
+ "entries",
+ DataType::Struct(
+ vec![
+ Field::new("key", DataType::Utf8, false),
+ Field::new("value", DataType::Int32, true),
+ ]
+ .into(),
+ ),
+ false,
+ );
+ let metadata = HashMap::from([("key".to_string(),
"value".to_string())]);
+ entry_field_with_metadata.set_metadata(metadata);
+ let map_data_type_with_metadata =
DataType::Map(Arc::new(entry_field_with_metadata), true);
+ let map_data_type_with_metadata_string =
map_data_type_with_metadata.to_string();
+ let expected_string_with_metadata = "Map(\"entries\": Struct(\"key\":
Utf8, \"value\": nullable Int32), metadata: {\"key\": \"value\"}, sorted)";
+ assert_eq!(
+ map_data_type_with_metadata_string,
+ expected_string_with_metadata
+ );
+ }
}