tobixdev commented on code in PR #8829:
URL: https://github.com/apache/arrow-rs/pull/8829#discussion_r2520069266


##########
arrow-cast/src/pretty.rs:
##########
@@ -22,14 +22,99 @@
 //! [`RecordBatch`]: arrow_array::RecordBatch
 //! [`Array`]: arrow_array::Array
 
-use std::fmt::Display;
-
 use comfy_table::{Cell, Table};
+use std::fmt::{Display, Write};
+use std::sync::Arc;
 
-use arrow_array::{Array, ArrayRef, RecordBatch};
-use arrow_schema::{ArrowError, SchemaRef};
+use arrow_array::cast::AsArray;
+use arrow_array::{Array, ArrayRef, Int32Array, RecordBatch, array};
+use arrow_schema::{ArrowError, Field, SchemaRef};
 
-use crate::display::{ArrayFormatter, FormatOptions};
+use crate::display::{ArrayFormatter, DisplayIndex, FormatOptions};
+
+/// Allows creating a new [`ArrayFormatter`] for a given [`Array`] and an 
optional [`Field`].
+///
+/// # Example
+///
+/// The example below shows how to create a custom formatter for a custom type 
`my_money`.
+///
+/// ```rust
+/// use std::fmt::Write;
+/// use arrow_array::{Array, Int32Array, cast::AsArray};
+/// use arrow_cast::display::{ArrayFormatter, DisplayIndex, FormatOptions, 
FormatResult};
+/// use 
arrow_cast::pretty::{pretty_format_batches_with_options_and_formatters, 
ArrayFormatterFactory};
+/// use arrow_schema::{ArrowError, Field};
+///
+/// /// A custom formatter factory that can create a formatter for the special 
type `my_money`.
+/// ///
+/// /// This struct could have access to some kind of extension type registry 
that can lookup the
+/// /// correct formatter for an extension type on-demand.
+/// struct MyFormatters {}
+///
+/// impl ArrayFormatterFactory for MyFormatters {
+///     fn create_display_index<'formatter>(
+///         &self,
+///         array: &'formatter dyn Array,
+///         options: &'formatter FormatOptions<'formatter>,
+///         field: Option<&'formatter Field>,
+///     ) -> Result<Option<ArrayFormatter<'formatter>>, ArrowError> {
+///         // check if this is the money type
+///         if field
+///             .map(|f| f.extension_type_name() == Some("my_money"))
+///             .unwrap_or(false)
+///         {
+///             // We assume that my_money always is an Int32.
+///             let array = array.as_primitive();
+///             let display_index = Box::new(MyMoneyFormatter { array, options 
});
+///             return Ok(Some(ArrayFormatter::new(display_index, 
options.safe)));
+///         }
+///
+///         Ok(None) // None indicates that the default formatter should be 
used.
+///     }
+/// }
+///
+/// /// A formatter for the type `my_money` that wraps a specific array and 
has access to the
+/// /// formatting options.
+/// struct MyMoneyFormatter<'a> {

Review Comment:
   From the DataFusion perspective, this would be a struct that users implement 
such that they can pretty-print their custom types.
   
   We would also do something similar for canonical data types (e.g., 
formatting UUIDs).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to