polars_arrow/array/primitive/
fmt.rs

1#![allow(clippy::redundant_closure_call)]
2use std::fmt::{Debug, Formatter, Result, Write};
3
4use super::PrimitiveArray;
5use crate::array::Array;
6use crate::array::fmt::write_vec;
7use crate::datatypes::{IntervalUnit, TimeUnit};
8use crate::temporal_conversions;
9use crate::types::{NativeType, days_ms, i256, months_days_ns};
10
11macro_rules! dyn_primitive {
12    ($array:expr, $ty:ty, $expr:expr) => {{
13        let array = ($array as &dyn Array)
14            .as_any()
15            .downcast_ref::<PrimitiveArray<$ty>>()
16            .unwrap();
17        Box::new(move |f, index| write!(f, "{}", $expr(array.value(index))))
18    }};
19}
20
21pub fn get_write_value<'a, T: NativeType, F: Write>(
22    array: &'a PrimitiveArray<T>,
23) -> Box<dyn Fn(&mut F, usize) -> Result + 'a> {
24    use crate::datatypes::ArrowDataType::*;
25    match array.dtype().to_logical_type() {
26        Int8 => Box::new(|f, index| write!(f, "{}", array.value(index))),
27        Int16 => Box::new(|f, index| write!(f, "{}", array.value(index))),
28        Int32 => Box::new(|f, index| write!(f, "{}", array.value(index))),
29        Int64 => Box::new(|f, index| write!(f, "{}", array.value(index))),
30        Int128 => Box::new(|f, index| write!(f, "{}", array.value(index))),
31        UInt8 => Box::new(|f, index| write!(f, "{}", array.value(index))),
32        UInt16 => Box::new(|f, index| write!(f, "{}", array.value(index))),
33        UInt32 => Box::new(|f, index| write!(f, "{}", array.value(index))),
34        UInt64 => Box::new(|f, index| write!(f, "{}", array.value(index))),
35        Float16 => unreachable!(),
36        Float32 => Box::new(|f, index| write!(f, "{}", array.value(index))),
37        Float64 => Box::new(|f, index| write!(f, "{}", array.value(index))),
38        Date32 => {
39            dyn_primitive!(array, i32, temporal_conversions::date32_to_date)
40        },
41        Date64 => {
42            dyn_primitive!(array, i64, temporal_conversions::date64_to_date)
43        },
44        Time32(TimeUnit::Second) => {
45            dyn_primitive!(array, i32, temporal_conversions::time32s_to_time)
46        },
47        Time32(TimeUnit::Millisecond) => {
48            dyn_primitive!(array, i32, temporal_conversions::time32ms_to_time)
49        },
50        Time32(_) => unreachable!(), // remaining are not valid
51        Time64(TimeUnit::Microsecond) => {
52            dyn_primitive!(array, i64, temporal_conversions::time64us_to_time)
53        },
54        Time64(TimeUnit::Nanosecond) => {
55            dyn_primitive!(array, i64, temporal_conversions::time64ns_to_time)
56        },
57        Time64(_) => unreachable!(), // remaining are not valid
58        Timestamp(time_unit, tz) => {
59            if let Some(tz) = tz {
60                let timezone = temporal_conversions::parse_offset(tz.as_str());
61                match timezone {
62                    Ok(timezone) => {
63                        dyn_primitive!(array, i64, |time| {
64                            temporal_conversions::timestamp_to_datetime(time, *time_unit, &timezone)
65                        })
66                    },
67                    #[cfg(feature = "chrono-tz")]
68                    Err(_) => {
69                        let timezone = temporal_conversions::parse_offset_tz(tz.as_str());
70                        match timezone {
71                            Ok(timezone) => dyn_primitive!(array, i64, |time| {
72                                temporal_conversions::timestamp_to_datetime(
73                                    time, *time_unit, &timezone,
74                                )
75                            }),
76                            Err(_) => {
77                                let tz = tz.clone();
78                                Box::new(move |f, index| {
79                                    write!(f, "{} ({})", array.value(index), tz)
80                                })
81                            },
82                        }
83                    },
84                    #[cfg(not(feature = "chrono-tz"))]
85                    _ => {
86                        let tz = tz.clone();
87                        Box::new(move |f, index| write!(f, "{} ({})", array.value(index), tz))
88                    },
89                }
90            } else {
91                dyn_primitive!(array, i64, |time| {
92                    temporal_conversions::timestamp_to_naive_datetime(time, *time_unit)
93                })
94            }
95        },
96        Interval(IntervalUnit::YearMonth) => {
97            dyn_primitive!(array, i32, |x| format!("{x}m"))
98        },
99        Interval(IntervalUnit::DayTime) => {
100            dyn_primitive!(array, days_ms, |x: days_ms| format!(
101                "{}d{}ms",
102                x.days(),
103                x.milliseconds()
104            ))
105        },
106        Interval(IntervalUnit::MonthDayNano) => {
107            dyn_primitive!(array, months_days_ns, |x: months_days_ns| format!(
108                "{}m{}d{}ns",
109                x.months(),
110                x.days(),
111                x.ns()
112            ))
113        },
114        Duration(TimeUnit::Second) => dyn_primitive!(array, i64, |x| format!("{x}s")),
115        Duration(TimeUnit::Millisecond) => dyn_primitive!(array, i64, |x| format!("{x}ms")),
116        Duration(TimeUnit::Microsecond) => dyn_primitive!(array, i64, |x| format!("{x}us")),
117        Duration(TimeUnit::Nanosecond) => dyn_primitive!(array, i64, |x| format!("{x}ns")),
118        Decimal(_, scale) => {
119            // The number 999.99 has a precision of 5 and scale of 2
120            let scale = *scale as u32;
121            let factor = 10i128.pow(scale);
122            let display = move |x: i128| {
123                let base = x / factor;
124                let decimals = (x - base * factor).abs();
125                format!("{base}.{decimals}")
126            };
127            dyn_primitive!(array, i128, display)
128        },
129        Decimal32(_, scale) => {
130            let scale = *scale as u32;
131            let factor = 10i32.pow(scale);
132            let display = move |x: i32| {
133                let base = x / factor;
134                let decimals = (x - base * factor).abs();
135                format!("{base}.{decimals}")
136            };
137            dyn_primitive!(array, i32, display)
138        },
139        Decimal64(_, scale) => {
140            let scale = *scale as u32;
141            let factor = 10i64.pow(scale);
142            let display = move |x: i64| {
143                let base = x / factor;
144                let decimals = (x - base * factor).abs();
145                format!("{base}.{decimals}")
146            };
147            dyn_primitive!(array, i64, display)
148        },
149        Decimal256(_, scale) => {
150            let scale = *scale as u32;
151            let factor = (ethnum::I256::ONE * 10).pow(scale);
152            let display = move |x: i256| {
153                let base = x.0 / factor;
154                let decimals = (x.0 - base * factor).abs();
155                format!("{base}.{decimals}")
156            };
157            dyn_primitive!(array, i256, display)
158        },
159        _ => unreachable!(),
160    }
161}
162
163impl<T: NativeType> Debug for PrimitiveArray<T> {
164    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
165        let writer = get_write_value(self);
166
167        write!(f, "{:?}", self.dtype())?;
168        write_vec(f, &*writer, self.validity(), self.len(), "None", false)
169    }
170}