|
1 | 1 | // SPDX-License-Identifier: Apache-2.0 |
2 | 2 | // SPDX-FileCopyrightText: Copyright the Vortex contributors |
3 | 3 |
|
| 4 | +use std::fmt::Write; |
4 | 5 | use std::fmt::{self}; |
5 | 6 |
|
6 | 7 | use humansize::DECIMAL; |
7 | 8 | use humansize::format_size; |
8 | 9 |
|
| 10 | +use crate::Array; |
9 | 11 | use crate::ArrayRef; |
10 | 12 | use crate::ArrayVisitor; |
11 | 13 | use crate::arrays::ChunkedVTable; |
12 | 14 | use crate::display::DisplayOptions; |
| 15 | +use crate::expr::stats::Stat; |
| 16 | +use crate::expr::stats::StatsProvider; |
| 17 | + |
| 18 | +/// Display wrapper for array statistics in compact format. |
| 19 | +struct StatsDisplay<'a>(&'a dyn Array); |
| 20 | + |
| 21 | +impl fmt::Display for StatsDisplay<'_> { |
| 22 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 23 | + let stats = self.0.statistics(); |
| 24 | + let mut first = true; |
| 25 | + |
| 26 | + // Helper to write separator |
| 27 | + let mut sep = |f: &mut fmt::Formatter<'_>| -> fmt::Result { |
| 28 | + if first { |
| 29 | + first = false; |
| 30 | + f.write_str(" [") |
| 31 | + } else { |
| 32 | + f.write_str(", ") |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + // Null count or validity fallback |
| 37 | + if let Some(nc) = stats.get(Stat::NullCount) { |
| 38 | + if let Ok(n) = usize::try_from(&nc.clone().into_inner()) { |
| 39 | + sep(f)?; |
| 40 | + write!(f, "nulls={}", n)?; |
| 41 | + } else { |
| 42 | + sep(f)?; |
| 43 | + write!(f, "nulls={}", nc)?; |
| 44 | + } |
| 45 | + } else if self.0.dtype().is_nullable() { |
| 46 | + if self.0.all_valid() { |
| 47 | + sep(f)?; |
| 48 | + f.write_str("all_valid")?; |
| 49 | + } else if self.0.all_invalid() { |
| 50 | + sep(f)?; |
| 51 | + f.write_str("all_invalid")?; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // NaN count (only if > 0) |
| 56 | + if let Some(nan) = stats.get(Stat::NaNCount) |
| 57 | + && let Ok(n) = usize::try_from(&nan.into_inner()) |
| 58 | + && n > 0 |
| 59 | + { |
| 60 | + sep(f)?; |
| 61 | + write!(f, "nan={}", n)?; |
| 62 | + } |
| 63 | + |
| 64 | + // Min/Max |
| 65 | + if let Some(min) = stats.get(Stat::Min) { |
| 66 | + sep(f)?; |
| 67 | + write!(f, "min={}", min)?; |
| 68 | + } |
| 69 | + if let Some(max) = stats.get(Stat::Max) { |
| 70 | + sep(f)?; |
| 71 | + write!(f, "max={}", max)?; |
| 72 | + } |
| 73 | + |
| 74 | + // Sum |
| 75 | + if let Some(sum) = stats.get(Stat::Sum) { |
| 76 | + sep(f)?; |
| 77 | + write!(f, "sum={}", sum)?; |
| 78 | + } |
| 79 | + |
| 80 | + // Boolean flags (compact) |
| 81 | + if let Some(c) = stats.get(Stat::IsConstant) |
| 82 | + && bool::try_from(&c.into_inner()).unwrap_or(false) |
| 83 | + { |
| 84 | + sep(f)?; |
| 85 | + f.write_str("const")?; |
| 86 | + } |
| 87 | + if let Some(s) = stats.get(Stat::IsStrictSorted) { |
| 88 | + if bool::try_from(&s.into_inner()).unwrap_or(false) { |
| 89 | + sep(f)?; |
| 90 | + f.write_str("strict")?; |
| 91 | + } |
| 92 | + } else if let Some(s) = stats.get(Stat::IsSorted) |
| 93 | + && bool::try_from(&s.into_inner()).unwrap_or(false) |
| 94 | + { |
| 95 | + sep(f)?; |
| 96 | + f.write_str("sorted")?; |
| 97 | + } |
| 98 | + |
| 99 | + // Close bracket if we wrote anything |
| 100 | + if !first { |
| 101 | + f.write_char(']')?; |
| 102 | + } |
| 103 | + |
| 104 | + Ok(()) |
| 105 | + } |
| 106 | +} |
13 | 107 |
|
14 | 108 | pub(crate) struct TreeDisplayWrapper(pub(crate) ArrayRef); |
15 | 109 |
|
@@ -39,13 +133,15 @@ impl<'a, 'b: 'a> TreeFormatter<'a, 'b> { |
39 | 133 | } else { |
40 | 134 | 100_f64 * nbytes as f64 / total_size as f64 |
41 | 135 | }; |
| 136 | + |
42 | 137 | writeln!( |
43 | 138 | self, |
44 | | - "{}: {} nbytes={} ({:.2}%)", |
| 139 | + "{}: {} nbytes={} ({:.2}%){}", |
45 | 140 | name, |
46 | 141 | array.display_as(DisplayOptions::MetadataOnly), |
47 | 142 | format_size(nbytes, DECIMAL), |
48 | | - percent |
| 143 | + percent, |
| 144 | + StatsDisplay(array.as_ref()), |
49 | 145 | )?; |
50 | 146 |
|
51 | 147 | self.indent(|i| { |
|
0 commit comments