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

vortex-data / vortex / 16276835702

14 Jul 2025 08:12PM UTC coverage: 81.509% (-0.02%) from 81.533%
16276835702

Pull #3871

github

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

26 of 29 new or added lines in 8 files covered. (89.66%)

14 existing lines in 3 files now uncovered.

46253 of 56746 relevant lines covered (81.51%)

3489259.99 hits per line

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

78.65
/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 {
×
NEW
84
        self.0.fmt_as(f, &self.1)
×
85
    }
×
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 {
980✔
102
        self.fmt_as(
980✔
103
            f,
980✔
104
            &DisplayOptions::CommaSeparatedScalars {
980✔
105
                space_after_comma: true,
980✔
106
            },
980✔
107
        )
980✔
108
    }
980✔
109
}
110

111
impl dyn Array + '_ {
112
    /// Display the array as specified by the options.
113
    ///
114
    /// See [DisplayOptions] for examples.
NEW
115
    pub fn display_as(&self, options: DisplayOptions) -> DisplayArrayAs<'_> {
×
116
        DisplayArrayAs(self, options)
×
117
    }
×
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
    /// Format an array as specified by the options.
143
    ///
144
    /// See [DisplayOptions] for examples.
145
    pub fn fmt_as(
980✔
146
        &self,
980✔
147
        f: &mut std::fmt::Formatter,
980✔
148
        options: &DisplayOptions,
980✔
149
    ) -> std::fmt::Result {
980✔
150
        match options {
980✔
151
            DisplayOptions::MetadataOnly => {
UNCOV
152
                write!(
×
UNCOV
153
                    f,
×
UNCOV
154
                    "{}({}, len={})",
×
UNCOV
155
                    self.encoding_id(),
×
UNCOV
156
                    self.dtype(),
×
UNCOV
157
                    self.len()
×
UNCOV
158
                )
×
159
            }
160
            DisplayOptions::CommaSeparatedScalars { space_after_comma } => {
980✔
161
                write!(f, "[")?;
980✔
162
                let sep = if *space_after_comma { ", " } else { "," };
980✔
163
                write!(
980✔
164
                    f,
980✔
165
                    "{}",
980✔
166
                    (0..self.len())
980✔
167
                        .map(|i| self.scalar_at(i).vortex_expect("index is in bounds"))
36,699,399✔
168
                        .format(sep)
980✔
169
                )?;
980✔
170
                write!(f, "]")
980✔
171
            }
172
            DisplayOptions::TreeDisplay => write!(f, "{}", TreeDisplayWrapper(self.to_array())),
×
173
        }
174
    }
980✔
175
}
176

177
#[cfg(test)]
178
mod test {
179
    use vortex_buffer::{Buffer, buffer};
180
    use vortex_dtype::FieldNames;
181

182
    use crate::IntoArray as _;
183
    use crate::arrays::{BoolArray, ListArray, StructArray};
184
    use crate::validity::Validity;
185

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

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

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

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

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

225
    #[test]
226
    fn test_list() {
1✔
227
        let x = ListArray::try_new(
1✔
228
            buffer![1, 2, 3, 4].into_array(),
1✔
229
            buffer![0, 0, 1, 1, 2, 4].into_array(),
1✔
230
            Validity::Array(BoolArray::from_iter([true, true, false, true, true]).into_array()),
1✔
231
        )
1✔
232
        .unwrap()
1✔
233
        .into_array();
1✔
234
        assert_eq!(x.to_string(), "[[], [1i32], null, [2i32], [3i32, 4i32]]");
1✔
235
    }
1✔
236
}
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