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

vortex-data / vortex / 16331938722

16 Jul 2025 10:49PM UTC coverage: 80.702% (-0.9%) from 81.557%
16331938722

push

github

web-flow
feat: build with stable rust (#3881)

120 of 173 new or added lines in 28 files covered. (69.36%)

174 existing lines in 102 files now uncovered.

41861 of 51871 relevant lines covered (80.7%)

157487.71 hits per line

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

89.16
/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.display_values()),
48
    /// );
49
    /// ```
50
    CommaSeparatedScalars { omit_comma_after_space: 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 {
68
    fn default() -> Self {
×
69
        Self::CommaSeparatedScalars {
×
70
            omit_comma_after_space: false,
×
71
        }
×
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 {
1,341✔
84
        self.0.fmt_as(f, &self.1)
1,341✔
85
    }
1,341✔
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 {
×
102
        self.fmt_as(f, &DisplayOptions::MetadataOnly)
×
103
    }
×
104
}
105

106
impl dyn Array + '_ {
107
    /// Display logical values of the array
108
    ///
109
    /// For example, an `i16` typed array containing the first five non-negative integers is displayed
110
    /// as: `[0i16, 1i16, 2i16, 3i16, 4i16]`.
111
    ///
112
    /// # Examples
113
    ///
114
    /// ```
115
    /// # use vortex_array::IntoArray;
116
    /// # use vortex_buffer::buffer;
117
    /// let array = buffer![0_i16, 1, 2, 3, 4].into_array();
118
    /// assert_eq!(
119
    ///     format!("{}", array.display_values()),
120
    ///     "[0i16, 1i16, 2i16, 3i16, 4i16]",
121
    /// )
122
    /// ```
123
    ///
124
    /// See also:
125
    /// [Array::display_as](..//trait.Array.html#method.display_as),
126
    /// [DisplayArrayAs], and [DisplayOptions].
127
    pub fn display_values(&self) -> DisplayArrayAs<'_> {
150✔
128
        DisplayArrayAs(
150✔
129
            self,
150✔
130
            DisplayOptions::CommaSeparatedScalars {
150✔
131
                omit_comma_after_space: false,
150✔
132
            },
150✔
133
        )
150✔
134
    }
150✔
135

136
    /// Display the array as specified by the options.
137
    ///
138
    /// See [DisplayOptions] for examples.
139
    pub fn display_as(&self, options: DisplayOptions) -> DisplayArrayAs<'_> {
830✔
140
        DisplayArrayAs(self, options)
830✔
141
    }
830✔
142

143
    /// Display the tree of encodings of this array as an indented lists.
144
    ///
145
    /// While some metadata (such as length, bytes and validity-rate) are included, the logical
146
    /// values of the array are not displayed. To view the logical values see
147
    /// [Array::display_as](../trait.Array.html#method.display_as)
148
    /// and [DisplayOptions].
149
    ///
150
    /// # Examples
151
    /// ```
152
    /// # use vortex_array::display::DisplayOptions;
153
    /// # use vortex_array::IntoArray;
154
    /// # use vortex_buffer::buffer;
155
    /// let array = buffer![0_i16, 1, 2, 3, 4].into_array();
156
    /// let expected = "root: vortex.primitive(i16, len=5) nbytes=10 B (100.00%)
157
    ///   metadata: EmptyMetadata
158
    ///   buffer (align=2): 10 B (100.00%)
159
    /// ";
160
    /// assert_eq!(format!("{}", array.display_tree()), expected);
161
    /// ```
162
    pub fn display_tree(&self) -> impl Display {
361✔
163
        DisplayArrayAs(self, DisplayOptions::TreeDisplay)
361✔
164
    }
361✔
165

166
    fn fmt_as(&self, f: &mut std::fmt::Formatter, options: &DisplayOptions) -> std::fmt::Result {
1,341✔
167
        match options {
1,341✔
168
            DisplayOptions::MetadataOnly => {
169
                write!(
830✔
170
                    f,
830✔
171
                    "{}({}, len={})",
830✔
172
                    self.encoding_id(),
830✔
173
                    self.dtype(),
830✔
174
                    self.len()
830✔
175
                )
176
            }
177
            DisplayOptions::CommaSeparatedScalars {
178
                omit_comma_after_space,
150✔
179
            } => {
180
                write!(f, "[")?;
150✔
181
                let sep = if *omit_comma_after_space { "," } else { ", " };
150✔
182
                write!(
150✔
183
                    f,
150✔
184
                    "{}",
150✔
185
                    (0..self.len())
150✔
186
                        .map(|i| self.scalar_at(i).vortex_expect("index is in bounds"))
989✔
187
                        .format(sep)
150✔
UNCOV
188
                )?;
×
189
                write!(f, "]")
150✔
190
            }
191
            DisplayOptions::TreeDisplay => write!(f, "{}", TreeDisplayWrapper(self.to_array())),
361✔
192
        }
193
    }
1,341✔
194
}
195

196
#[cfg(test)]
197
mod test {
198
    use vortex_buffer::{Buffer, buffer};
199
    use vortex_dtype::FieldNames;
200

201
    use crate::IntoArray as _;
202
    use crate::arrays::{BoolArray, ListArray, StructArray};
203
    use crate::validity::Validity;
204

205
    #[test]
206
    fn test_primitive() {
1✔
207
        let x = Buffer::<u32>::empty().into_array();
1✔
208
        assert_eq!(x.display_values().to_string(), "[]");
1✔
209

210
        let x = buffer![1].into_array();
1✔
211
        assert_eq!(x.display_values().to_string(), "[1i32]");
1✔
212

213
        let x = buffer![1, 2, 3, 4].into_array();
1✔
214
        assert_eq!(x.display_values().to_string(), "[1i32, 2i32, 3i32, 4i32]");
1✔
215
    }
1✔
216

217
    #[test]
218
    fn test_empty_struct() {
1✔
219
        let s = StructArray::try_new(
1✔
220
            FieldNames::from(vec![]),
1✔
221
            vec![],
1✔
222
            3,
223
            Validity::Array(BoolArray::from_iter([true, false, true]).into_array()),
1✔
224
        )
225
        .unwrap()
1✔
226
        .into_array();
1✔
227
        assert_eq!(s.display_values().to_string(), "[{}, null, {}]");
1✔
228
    }
1✔
229

230
    #[test]
231
    fn test_simple_struct() {
1✔
232
        let s = StructArray::from_fields(&[
1✔
233
            ("x", buffer![1, 2, 3, 4].into_array()),
1✔
234
            ("y", buffer![-1, -2, -3, -4].into_array()),
1✔
235
        ])
1✔
236
        .unwrap()
1✔
237
        .into_array();
1✔
238
        assert_eq!(
1✔
239
            s.display_values().to_string(),
1✔
240
            "[{x: 1i32, y: -1i32}, {x: 2i32, y: -2i32}, {x: 3i32, y: -3i32}, {x: 4i32, y: -4i32}]"
241
        );
242
    }
1✔
243

244
    #[test]
245
    fn test_list() {
1✔
246
        let x = ListArray::try_new(
1✔
247
            buffer![1, 2, 3, 4].into_array(),
1✔
248
            buffer![0, 0, 1, 1, 2, 4].into_array(),
1✔
249
            Validity::Array(BoolArray::from_iter([true, true, false, true, true]).into_array()),
1✔
250
        )
251
        .unwrap()
1✔
252
        .into_array();
1✔
253
        assert_eq!(
1✔
254
            x.display_values().to_string(),
1✔
255
            "[[], [1i32], null, [2i32], [3i32, 4i32]]"
256
        );
257
    }
1✔
258
}
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

© 2025 Coveralls, Inc