Skip to content

Commit b5c831a

Browse files
authored
Allow Users to Provide Custom ArrayFormatters when Pretty-Printing Record Batches (#8829)
# Which issue does this PR close? - Closes #8821. # Rationale for this change Allows users that require custom pretty-printing logic for batches to supply this implementation. # What changes are included in this PR? Changes to existing code: - Make fields in `FormatOptions` public. This is necessary as the custom `ArrayFormatter` must also have access to the formatting options. (see `<NULL>` in the test) - Deprecate `types_info()` method as the field is now public - Allow directly creating `ArrayFormatter` with a `DisplayIndex` implementation - Make `FormatError`, `FormatResult`, and `DisplayIndex` public. I do have some second thoughts about `DisplayIndex` not having any concept of length even though its taking an index as input. However, it may be fine for now. New code: - `ArrayFormatterFactory`: Allows creating `ArrayFormatters` with custom behavior - `pretty_format_batches_with_options_and_formatters` pretty printing with custom formatters - Similar thing for format column # Are these changes tested? Yes, existing tests cover the default formatting path. Three new tests: - Format record batch with custom type (append € sign) - Format column with custom formatter (append (32-Bit) for `Int32`) - Allow overriding the custom types with a custom schema (AFAIK this is not possible with the current API but might make sense). - Added a sanity check that the number of fields in a custom schema must match the number of columns in the record batch. # Are there any user-facing changes? Yes, multiple things become public, `types_info()` becomes deprecated, and there are new APIs for custom pretty printing of batches.
1 parent 389f404 commit b5c831a

File tree

3 files changed

+781
-62
lines changed

3 files changed

+781
-62
lines changed

arrow-array/src/array/map_array.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ impl MapArray {
173173
&self.entries
174174
}
175175

176+
/// Returns a reference to the fields of the [`StructArray`] that backs this map.
177+
pub fn entries_fields(&self) -> (&Field, &Field) {
178+
let fields = self.entries.fields().iter().collect::<Vec<_>>();
179+
let fields = TryInto::<[&FieldRef; 2]>::try_into(fields)
180+
.expect("Every map has a key and value field");
181+
182+
(fields[0].as_ref(), fields[1].as_ref())
183+
}
184+
176185
/// Returns the data type of the map's keys.
177186
pub fn key_type(&self) -> &DataType {
178187
self.keys().data_type()

0 commit comments

Comments
 (0)