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

vortex-data / vortex / 16273663911

14 Jul 2025 05:31PM UTC coverage: 81.554% (+0.02%) from 81.537%
16273663911

Pull #3871

github

web-flow
Merge aeb0f7b4d into 9b0d852a1
Pull Request #3871: chore: Fewer ways to invoke display

83 of 86 new or added lines in 8 files covered. (96.51%)

6 existing lines in 2 files now uncovered.

46320 of 56797 relevant lines covered (81.55%)

146672.82 hits per line

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

95.45
/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
/// [DisplayArray], 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.display()),
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
/// [DisplayArray], 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 {
511✔
84
        self.0.fmt_as(f, &self.1)
511✔
85
    }
511✔
86
}
87

88
/// Display the encoding and limited metadata 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
///     "vortex.primitive(i16, len=5)",
98
/// );
99
/// ```
100
impl Display for dyn Array + '_ {
101
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
830✔
102
        self.fmt_as(f, &DisplayOptions::MetadataOnly)
830✔
103
    }
830✔
104
}
105

106
impl dyn Array + '_ {
107
    /// Display the array as specified by the options.
108
    ///
109
    /// See [DisplayOptions] for examples.
110
    pub fn display_as(&self, options: DisplayOptions) -> DisplayArrayAs<'_> {
511✔
111
        DisplayArrayAs(self, options)
511✔
112
    }
511✔
113

114
    fn fmt_as(&self, f: &mut std::fmt::Formatter, options: &DisplayOptions) -> std::fmt::Result {
1,341✔
115
        match options {
1,341✔
116
            DisplayOptions::MetadataOnly => {
117
                write!(
830✔
118
                    f,
830✔
119
                    "{}({}, len={})",
830✔
120
                    self.encoding_id(),
830✔
121
                    self.dtype(),
830✔
122
                    self.len()
830✔
123
                )
830✔
124
            }
125
            DisplayOptions::CommaSeparatedScalars { space_after_comma } => {
150✔
126
                write!(f, "[")?;
150✔
127
                let sep = if *space_after_comma { ", " } else { "," };
150✔
128
                write!(
150✔
129
                    f,
150✔
130
                    "{}",
150✔
131
                    (0..self.len())
150✔
132
                        .map(|i| self.scalar_at(i).vortex_expect("index is in bounds"))
989✔
133
                        .format(sep)
150✔
134
                )?;
150✔
135
                write!(f, "]")
150✔
136
            }
137
            DisplayOptions::TreeDisplay => write!(f, "{}", TreeDisplayWrapper(self.to_array())),
361✔
138
        }
139
    }
1,341✔
140
}
141

142
#[cfg(test)]
143
mod test {
144
    use vortex_buffer::{Buffer, buffer};
145
    use vortex_dtype::FieldNames;
146

147
    use crate::IntoArray as _;
148
    use crate::arrays::{BoolArray, ListArray, StructArray};
149
    use crate::display::DisplayOptions;
150
    use crate::validity::Validity;
151

152
    #[test]
153
    fn test_primitive() {
1✔
154
        let x = Buffer::<u32>::empty().into_array();
1✔
155
        assert_eq!(
1✔
156
            x.display_as(DisplayOptions::CommaSeparatedScalars {
1✔
157
                space_after_comma: true
1✔
158
            })
1✔
159
            .to_string(),
1✔
160
            "[]"
1✔
161
        );
1✔
162

163
        let x = buffer![1].into_array();
1✔
164
        assert_eq!(
1✔
165
            x.display_as(DisplayOptions::CommaSeparatedScalars {
1✔
166
                space_after_comma: true
1✔
167
            })
1✔
168
            .to_string(),
1✔
169
            "[1i32]"
1✔
170
        );
1✔
171

172
        let x = buffer![1, 2, 3, 4].into_array();
1✔
173
        assert_eq!(
1✔
174
            x.display_as(DisplayOptions::CommaSeparatedScalars {
1✔
175
                space_after_comma: true
1✔
176
            })
1✔
177
            .to_string(),
1✔
178
            "[1i32, 2i32, 3i32, 4i32]"
1✔
179
        );
1✔
180
    }
1✔
181

182
    #[test]
183
    fn test_empty_struct() {
1✔
184
        let s = StructArray::try_new(
1✔
185
            FieldNames::from(vec![]),
1✔
186
            vec![],
1✔
187
            3,
1✔
188
            Validity::Array(BoolArray::from_iter([true, false, true]).into_array()),
1✔
189
        )
1✔
190
        .unwrap()
1✔
191
        .into_array();
1✔
192
        assert_eq!(
1✔
193
            s.display_as(DisplayOptions::CommaSeparatedScalars {
1✔
194
                space_after_comma: true
1✔
195
            })
1✔
196
            .to_string(),
1✔
197
            "[{}, null, {}]"
1✔
198
        );
1✔
199
    }
1✔
200

201
    #[test]
202
    fn test_simple_struct() {
1✔
203
        let s = StructArray::from_fields(&[
1✔
204
            ("x", buffer![1, 2, 3, 4].into_array()),
1✔
205
            ("y", buffer![-1, -2, -3, -4].into_array()),
1✔
206
        ])
1✔
207
        .unwrap()
1✔
208
        .into_array();
1✔
209
        assert_eq!(
1✔
210
            s.display_as(DisplayOptions::CommaSeparatedScalars {
1✔
211
                space_after_comma: true
1✔
212
            })
1✔
213
            .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!(
1✔
228
            x.display_as(DisplayOptions::CommaSeparatedScalars {
1✔
229
                space_after_comma: true
1✔
230
            })
1✔
231
            .to_string(),
1✔
232
            "[[], [1i32], null, [2i32], [3i32, 4i32]]"
1✔
233
        );
1✔
234
    }
1✔
235
}
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