• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vortex-data / vortex / 16277416105

14 Jul 2025 08:42PM UTC coverage: 81.534% (+0.001%) from 81.533%
16277416105

Pull #3871

github

web-flow
Merge bcfb1e9a1 into b0be264bf
Pull Request #3871: chore: Fewer ways to invoke display

30 of 31 new or added lines in 9 files covered. (96.77%)

5 existing lines in 1 file now uncovered.

46267 of 56746 relevant lines covered (81.53%)

146559.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.26
/vortex-array/src/array/display/mod.rs
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
//! Convert an array into a human-readable string representation.
4

5
mod tree;
6

7
use std::fmt::Display;
8

9
use itertools::Itertools as _;
10
use tree::TreeDisplayWrapper;
11
use vortex_error::VortexExpect as _;
12

13
use crate::Array;
14

15
/// Describe how to convert an array to a string.
16
///
17
/// See also:
18
/// [Array::display_as](../trait.Array.html#method.display_as)
19
/// and [DisplayArrayAs].
20
pub enum DisplayOptions {
21
    /// Only the top-level encoding id and limited metadata: `vortex.primitive(i16, len=5)`.
22
    ///
23
    /// ```
24
    /// # use vortex_array::display::DisplayOptions;
25
    /// # use vortex_array::IntoArray;
26
    /// # use vortex_buffer::buffer;
27
    /// let array = buffer![0_i16, 1, 2, 3, 4].into_array();
28
    /// assert_eq!(
29
    ///     format!("{}", array.display_as(DisplayOptions::MetadataOnly)),
30
    ///     "vortex.primitive(i16, len=5)",
31
    /// );
32
    /// ```
33
    MetadataOnly,
34
    /// Only the logical values of the array: `[0i16, 1i16, 2i16, 3i16, 4i16]`.
35
    ///
36
    /// ```
37
    /// # use vortex_array::display::DisplayOptions;
38
    /// # use vortex_array::IntoArray;
39
    /// # use vortex_buffer::buffer;
40
    /// let array = buffer![0_i16, 1, 2, 3, 4].into_array();
41
    /// assert_eq!(
42
    ///     format!("{}", array.display_as(DisplayOptions::default())),
43
    ///     "[0i16, 1i16, 2i16, 3i16, 4i16]",
44
    /// );
45
    /// assert_eq!(
46
    ///     format!("{}", array.display_as(DisplayOptions::default())),
47
    ///     format!("{}", array),
48
    /// );
49
    /// ```
50
    CommaSeparatedScalars { space_after_comma: bool },
51
    /// The tree of encodings and all metadata but no values.
52
    ///
53
    /// ```
54
    /// # use vortex_array::display::DisplayOptions;
55
    /// # use vortex_array::IntoArray;
56
    /// # use vortex_buffer::buffer;
57
    /// let array = buffer![0_i16, 1, 2, 3, 4].into_array();
58
    /// let expected = "root: vortex.primitive(i16, len=5) nbytes=10 B (100.00%)
59
    ///   metadata: EmptyMetadata
60
    ///   buffer (align=2): 10 B (100.00%)
61
    /// ";
62
    /// assert_eq!(format!("{}", array.display_as(DisplayOptions::TreeDisplay)), expected);
63
    /// ```
64
    TreeDisplay,
65
}
66

67
impl Default for DisplayOptions {
UNCOV
68
    fn default() -> Self {
×
UNCOV
69
        Self::CommaSeparatedScalars {
×
UNCOV
70
            space_after_comma: true,
×
UNCOV
71
        }
×
UNCOV
72
    }
×
73
}
74

75
/// A shim used to display an array as specified in the options.
76
///
77
/// See also:
78
/// [Array::display_as](../trait.Array.html#method.display_as)
79
/// and [DisplayOptions].
80
pub struct DisplayArrayAs<'a>(pub &'a dyn Array, pub DisplayOptions);
81

82
impl Display for DisplayArrayAs<'_> {
83
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
830✔
84
        self.0.fmt_as(f, &self.1)
830✔
85
    }
830✔
86
}
87

88
/// Display the logical values of this array.
89
///
90
/// # Examples
91
/// ```
92
/// # use vortex_array::IntoArray;
93
/// # use vortex_buffer::buffer;
94
/// let array = buffer![0_i16, 1, 2, 3, 4].into_array();
95
/// assert_eq!(
96
///     format!("{}", array),
97
///     "[0i16, 1i16, 2i16, 3i16, 4i16]",
98
/// );
99
/// ```
100
impl Display for dyn Array + '_ {
101
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
150✔
102
        self.fmt_as(
150✔
103
            f,
150✔
104
            &DisplayOptions::CommaSeparatedScalars {
150✔
105
                space_after_comma: true,
150✔
106
            },
150✔
107
        )
150✔
108
    }
150✔
109
}
110

111
impl dyn Array + '_ {
112
    /// Display the array as specified by the options.
113
    ///
114
    /// See [DisplayOptions] for examples.
115
    pub fn display_as(&self, options: DisplayOptions) -> DisplayArrayAs<'_> {
830✔
116
        DisplayArrayAs(self, options)
830✔
117
    }
830✔
118

119
    /// Display the tree of encodings of this array as an indented lists.
120
    ///
121
    /// While some metadata (such as length, bytes and validity-rate) are included, the logical
122
    /// values of the array are not displayed. To view the logical values see
123
    /// [Array::display_as](../trait.Array.html#method.display_as)
124
    /// and [DisplayOptions].
125
    ///
126
    /// # Examples
127
    /// ```
128
    /// # use vortex_array::display::DisplayOptions;
129
    /// # use vortex_array::IntoArray;
130
    /// # use vortex_buffer::buffer;
131
    /// let array = buffer![0_i16, 1, 2, 3, 4].into_array();
132
    /// let expected = "root: vortex.primitive(i16, len=5) nbytes=10 B (100.00%)
133
    ///   metadata: EmptyMetadata
134
    ///   buffer (align=2): 10 B (100.00%)
135
    /// ";
136
    /// assert_eq!(format!("{}", array.display_tree()), expected);
137
    /// ```
138
    pub fn display_tree(&self) -> impl Display {
361✔
139
        TreeDisplayWrapper(self.to_array())
361✔
140
    }
361✔
141

142
    fn fmt_as(
980✔
143
        &self,
980✔
144
        f: &mut std::fmt::Formatter,
980✔
145
        options: &DisplayOptions,
980✔
146
    ) -> std::fmt::Result {
980✔
147
        match options {
980✔
148
            DisplayOptions::MetadataOnly => {
149
                write!(
830✔
150
                    f,
830✔
151
                    "{}({}, len={})",
830✔
152
                    self.encoding_id(),
830✔
153
                    self.dtype(),
830✔
154
                    self.len()
830✔
155
                )
830✔
156
            }
157
            DisplayOptions::CommaSeparatedScalars { space_after_comma } => {
150✔
158
                write!(f, "[")?;
150✔
159
                let sep = if *space_after_comma { ", " } else { "," };
150✔
160
                write!(
150✔
161
                    f,
150✔
162
                    "{}",
150✔
163
                    (0..self.len())
150✔
164
                        .map(|i| self.scalar_at(i).vortex_expect("index is in bounds"))
989✔
165
                        .format(sep)
150✔
166
                )?;
150✔
167
                write!(f, "]")
150✔
168
            }
169
            DisplayOptions::TreeDisplay => write!(f, "{}", TreeDisplayWrapper(self.to_array())),
×
170
        }
171
    }
980✔
172
}
173

174
#[cfg(test)]
175
mod test {
176
    use vortex_buffer::{Buffer, buffer};
177
    use vortex_dtype::FieldNames;
178

179
    use crate::IntoArray as _;
180
    use crate::arrays::{BoolArray, ListArray, StructArray};
181
    use crate::validity::Validity;
182

183
    #[test]
184
    fn test_primitive() {
1✔
185
        let x = Buffer::<u32>::empty().into_array();
1✔
186
        assert_eq!(x.to_string(), "[]");
1✔
187

188
        let x = buffer![1].into_array();
1✔
189
        assert_eq!(x.to_string(), "[1i32]");
1✔
190

191
        let x = buffer![1, 2, 3, 4].into_array();
1✔
192
        assert_eq!(x.to_string(), "[1i32, 2i32, 3i32, 4i32]");
1✔
193
    }
1✔
194

195
    #[test]
196
    fn test_empty_struct() {
1✔
197
        let s = StructArray::try_new(
1✔
198
            FieldNames::from(vec![]),
1✔
199
            vec![],
1✔
200
            3,
1✔
201
            Validity::Array(BoolArray::from_iter([true, false, true]).into_array()),
1✔
202
        )
1✔
203
        .unwrap()
1✔
204
        .into_array();
1✔
205
        assert_eq!(s.to_string(), "[{}, null, {}]");
1✔
206
    }
1✔
207

208
    #[test]
209
    fn test_simple_struct() {
1✔
210
        let s = StructArray::from_fields(&[
1✔
211
            ("x", buffer![1, 2, 3, 4].into_array()),
1✔
212
            ("y", buffer![-1, -2, -3, -4].into_array()),
1✔
213
        ])
1✔
214
        .unwrap()
1✔
215
        .into_array();
1✔
216
        assert_eq!(
1✔
217
            s.to_string(),
1✔
218
            "[{x: 1i32, y: -1i32}, {x: 2i32, y: -2i32}, {x: 3i32, y: -3i32}, {x: 4i32, y: -4i32}]"
1✔
219
        );
1✔
220
    }
1✔
221

222
    #[test]
223
    fn test_list() {
1✔
224
        let x = ListArray::try_new(
1✔
225
            buffer![1, 2, 3, 4].into_array(),
1✔
226
            buffer![0, 0, 1, 1, 2, 4].into_array(),
1✔
227
            Validity::Array(BoolArray::from_iter([true, true, false, true, true]).into_array()),
1✔
228
        )
1✔
229
        .unwrap()
1✔
230
        .into_array();
1✔
231
        assert_eq!(x.to_string(), "[[], [1i32], null, [2i32], [3i32, 4i32]]");
1✔
232
    }
1✔
233
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc