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

vortex-data / vortex / 16277633154

14 Jul 2025 08:53PM UTC coverage: 81.532% (-0.001%) from 81.533%
16277633154

Pull #3871

github

web-flow
Merge 95f02e8ac 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.

46263 of 56742 relevant lines covered (81.53%)

146621.96 hits per line

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

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

170
#[cfg(test)]
171
mod test {
172
    use vortex_buffer::{Buffer, buffer};
173
    use vortex_dtype::FieldNames;
174

175
    use crate::IntoArray as _;
176
    use crate::arrays::{BoolArray, ListArray, StructArray};
177
    use crate::validity::Validity;
178

179
    #[test]
180
    fn test_primitive() {
1✔
181
        let x = Buffer::<u32>::empty().into_array();
1✔
182
        assert_eq!(x.to_string(), "[]");
1✔
183

184
        let x = buffer![1].into_array();
1✔
185
        assert_eq!(x.to_string(), "[1i32]");
1✔
186

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

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

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

218
    #[test]
219
    fn test_list() {
1✔
220
        let x = ListArray::try_new(
1✔
221
            buffer![1, 2, 3, 4].into_array(),
1✔
222
            buffer![0, 0, 1, 1, 2, 4].into_array(),
1✔
223
            Validity::Array(BoolArray::from_iter([true, true, false, true, true]).into_array()),
1✔
224
        )
1✔
225
        .unwrap()
1✔
226
        .into_array();
1✔
227
        assert_eq!(x.to_string(), "[[], [1i32], null, [2i32], [3i32, 4i32]]");
1✔
228
    }
1✔
229
}
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