tobixdev commented on code in PR #8829:
URL: https://github.com/apache/arrow-rs/pull/8829#discussion_r2520071255
##########
arrow-cast/src/pretty.rs:
##########
@@ -1283,4 +1428,247 @@ mod tests {
let actual: Vec<&str> = iso.lines().collect();
assert_eq!(expected_iso, actual, "Actual result:\n{iso}");
}
+
+ //
+ // Custom Formatting
+ //
+
+ /// The factory that will create the [`ArrayFormatter`]s.
+ struct TestFormatters {}
+
+ impl ArrayFormatterFactory for TestFormatters {
+ fn create_display_index<'formatter>(
+ &self,
+ array: &'formatter dyn Array,
+ options: &'formatter FormatOptions<'formatter>,
+ field: Option<&'formatter Field>,
+ ) -> Result<Option<ArrayFormatter<'formatter>>, ArrowError> {
+ 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)));
+ }
+
+ if array.data_type() == &DataType::Int32 {
+ // We assume that my_money always is an Int32.
+ let array = array.as_primitive();
+ let display_index = Box::new(MyInt32Formatter { array, options
});
+ return Ok(Some(ArrayFormatter::new(display_index,
options.safe)));
+ }
+
+ Ok(None)
+ }
+ }
+
+ /// The actual formatter
+ struct MyMoneyFormatter<'a> {
+ array: &'a Int32Array,
+ options: &'a FormatOptions<'a>,
+ }
+
+ impl<'a> DisplayIndex for MyMoneyFormatter<'a> {
+ fn write(&self, idx: usize, f: &mut dyn Write) ->
crate::display::FormatResult {
+ match self.array.is_valid(idx) {
+ true => write!(f, "{} €", self.array.value(idx))?,
+ false => write!(f, "{}", self.options.null)?,
+ }
+
+ Ok(())
+ }
+ }
+
+ /// The actual formatter
+ struct MyInt32Formatter<'a> {
+ array: &'a Int32Array,
+ options: &'a FormatOptions<'a>,
+ }
+
+ impl<'a> DisplayIndex for MyInt32Formatter<'a> {
+ fn write(&self, idx: usize, f: &mut dyn Write) ->
crate::display::FormatResult {
+ match self.array.is_valid(idx) {
+ true => write!(f, "{} (32-Bit)", self.array.value(idx))?,
+ false => write!(f, "{}", self.options.null)?,
+ }
+
+ Ok(())
+ }
+ }
+
+ #[test]
+ fn test_format_batches_with_custom_formatters() {
+ // define a schema.
+ let options = FormatOptions::new().with_null("<NULL>");
+ let money_metadata = HashMap::from([(
+ extension::EXTENSION_TYPE_NAME_KEY.to_owned(),
+ "my_money".to_owned(),
+ )]);
+ let schema = Arc::new(Schema::new(vec![
+ Field::new("income", DataType::Int32,
true).with_metadata(money_metadata.clone()),
+ ]));
+
+ // define data.
+ let batch = RecordBatch::try_new(
+ schema,
+ vec![Arc::new(array::Int32Array::from(vec![
+ Some(1),
+ None,
+ Some(10),
+ Some(100),
+ ]))],
+ )
+ .unwrap();
+
+ let mut buf = String::new();
+ write!(
+ &mut buf,
+ "{}",
+ pretty_format_batches_with_options_and_formatters(
+ &[batch],
+ &options,
+ Some(&TestFormatters {})
+ )
+ .unwrap()
+ )
+ .unwrap();
+
+ let s = [
+ "+--------+",
Review Comment:
Pretty-printing result with a custom extension type
--
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]