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

vortex-data / vortex / 16270705780

14 Jul 2025 03:13PM UTC coverage: 81.519% (+0.06%) from 81.464%
16270705780

Pull #3866

github

web-flow
Merge c925e4a0b into b1a8d1d4f
Pull Request #3866: feat: display

141 of 148 new or added lines in 2 files covered. (95.27%)

1 existing line in 1 file now uncovered.

46252 of 56738 relevant lines covered (81.52%)

146118.57 hits per line

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

92.47
/vortex-array/src/array/display/mod.rs
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3

4
mod tree;
5

6
use std::fmt::Display;
7

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

12
use crate::Array;
13

14
pub struct DisplayArray<'a>(pub &'a dyn Array);
15

16
impl Display for DisplayArray<'_> {
17
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
150✔
18
        self.0.fmt_as(f, &DisplayOptions::default())
150✔
19
    }
150✔
20
}
21

22
pub enum DisplayOptions {
23
    /// ```
24
    /// use vortex_array::display::DisplayOptions;
25
    /// use vortex_array::IntoArray;
26
    /// use vortex_buffer::buffer;
27
    ///
28
    /// let array = buffer![0_i16, 1, 2, 3, 4].into_array();
29
    /// let opts = DisplayOptions::MetadataOnly;
30
    /// assert_eq!(
31
    ///     format!("{}", array.display_as(&opts)),
32
    ///     "vortex.primitive(i16, len=5)",
33
    /// );
34
    /// ```
35
    MetadataOnly,
36
    /// ```
37
    /// use vortex_array::display::DisplayOptions;
38
    /// use vortex_array::IntoArray;
39
    /// use vortex_buffer::buffer;
40
    ///
41
    /// let array = buffer![0_i16, 1, 2, 3, 4].into_array();
42
    /// let opts = DisplayOptions::default();
43
    /// assert_eq!(
44
    ///     format!("{}", array.display_as(&opts)),
45
    ///     "[0i16, 1i16, 2i16, 3i16, 4i16]",
46
    /// );
47
    /// assert_eq!(
48
    ///     format!("{}", array.display_as(&opts)),
49
    ///     format!("{}", array.display()),
50
    /// );
51
    /// ```
52
    CommaSeparatedScalars { space_after_comma: bool },
53
    /// ```
54
    /// use vortex_array::display::DisplayOptions;
55
    /// use vortex_array::IntoArray;
56
    /// use vortex_buffer::buffer;
57
    ///
58
    /// let array = buffer![0_i16, 1, 2, 3, 4].into_array();
59
    /// let opts = DisplayOptions::TreeDisplay;
60
    /// let expected = "root: vortex.primitive(i16, len=5) nbytes=10 B (100.00%)
61
    ///   metadata: EmptyMetadata
62
    ///   buffer (align=2): 10 B (100.00%)
63
    /// ";
64
    /// assert_eq!(format!("{}", array.display_as(&opts)), expected);
65
    /// ```
66
    TreeDisplay,
67
}
68

69
impl Default for DisplayOptions {
70
    fn default() -> Self {
150✔
71
        Self::CommaSeparatedScalars {
150✔
72
            space_after_comma: true,
150✔
73
        }
150✔
74
    }
150✔
75
}
76

77
pub struct DisplayArrayAs<'a>(pub &'a dyn Array, pub &'a DisplayOptions);
78

79
impl Display for DisplayArrayAs<'_> {
NEW
80
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
×
NEW
81
        self.0.fmt_as(f, self.1)
×
NEW
82
    }
×
83
}
84

85
impl Display for dyn Array + '_ {
86
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
830✔
87
        self.fmt_as(f, &DisplayOptions::MetadataOnly)
830✔
88
    }
830✔
89
}
90

91
impl dyn Array + '_ {
92
    pub fn tree_display(&self) -> impl Display {
361✔
93
        TreeDisplayWrapper(self.to_array())
361✔
94
    }
361✔
95

96
    /// Display the logical values of the array.
97
    ///
98
    /// # Examples
99
    ///
100
    /// ```
101
    /// use vortex_array::display::DisplayOptions;
102
    /// use vortex_array::IntoArray;
103
    /// use vortex_buffer::buffer;
104
    ///
105
    /// let array = buffer![0_i16, 1, 2, 3, 4].into_array();
106
    /// let opts = DisplayOptions::default();
107
    /// assert_eq!(
108
    ///     format!("{}", array.display()),
109
    ///     "[0i16, 1i16, 2i16, 3i16, 4i16]",
110
    /// )
111
    /// ```
112
    pub fn display(&self) -> DisplayArray {
150✔
113
        DisplayArray(self)
150✔
114
    }
150✔
115

116
    /// Display with options.
NEW
117
    pub fn display_as<'a>(&'a self, options: &'a DisplayOptions) -> DisplayArrayAs<'a> {
×
NEW
118
        DisplayArrayAs(self, options)
×
NEW
119
    }
×
120
    pub fn fmt_as(
980✔
121
        &self,
980✔
122
        f: &mut std::fmt::Formatter,
980✔
123
        options: &DisplayOptions,
980✔
124
    ) -> std::fmt::Result {
980✔
125
        match options {
980✔
126
            DisplayOptions::MetadataOnly => {
127
                write!(
830✔
128
                    f,
830✔
129
                    "{}({}, len={})",
830✔
130
                    self.encoding_id(),
830✔
131
                    self.dtype(),
830✔
132
                    self.len()
830✔
133
                )
830✔
134
            }
135
            DisplayOptions::CommaSeparatedScalars { space_after_comma } => {
150✔
136
                write!(f, "[")?;
150✔
137
                let sep = if *space_after_comma { ", " } else { "," };
150✔
138
                write!(
150✔
139
                    f,
150✔
140
                    "{}",
150✔
141
                    (0..self.len())
150✔
142
                        .map(|i| self.scalar_at(i).vortex_expect("index is in bounds"))
989✔
143
                        .format(sep)
150✔
144
                )?;
150✔
145
                write!(f, "]")
150✔
146
            }
NEW
147
            DisplayOptions::TreeDisplay => write!(f, "{}", TreeDisplayWrapper(self.to_array())),
×
148
        }
149
    }
980✔
150
}
151

152
#[cfg(test)]
153
mod test {
154
    use vortex_buffer::{Buffer, buffer};
155
    use vortex_dtype::FieldNames;
156

157
    use crate::IntoArray as _;
158
    use crate::arrays::{BoolArray, ListArray, StructArray};
159
    use crate::validity::Validity;
160

161
    #[test]
162
    fn test_primitive() {
1✔
163
        let x = Buffer::<u32>::empty().into_array();
1✔
164
        assert_eq!(x.display().to_string(), "[]");
1✔
165

166
        let x = buffer![1].into_array();
1✔
167
        assert_eq!(x.display().to_string(), "[1i32]");
1✔
168

169
        let x = buffer![1, 2, 3, 4].into_array();
1✔
170
        assert_eq!(x.display().to_string(), "[1i32, 2i32, 3i32, 4i32]");
1✔
171
    }
1✔
172

173
    #[test]
174
    fn test_empty_struct() {
1✔
175
        let s = StructArray::try_new(
1✔
176
            FieldNames::from(vec![]),
1✔
177
            vec![],
1✔
178
            3,
1✔
179
            Validity::Array(BoolArray::from_iter([true, false, true]).into_array()),
1✔
180
        )
1✔
181
        .unwrap()
1✔
182
        .into_array();
1✔
183
        assert_eq!(s.display().to_string(), "[{}, null, {}]");
1✔
184
    }
1✔
185

186
    #[test]
187
    fn test_simple_struct() {
1✔
188
        let s = StructArray::from_fields(&[
1✔
189
            ("x", buffer![1, 2, 3, 4].into_array()),
1✔
190
            ("y", buffer![-1, -2, -3, -4].into_array()),
1✔
191
        ])
1✔
192
        .unwrap()
1✔
193
        .into_array();
1✔
194
        assert_eq!(
1✔
195
            s.display().to_string(),
1✔
196
            "[{x: 1i32, y: -1i32}, {x: 2i32, y: -2i32}, {x: 3i32, y: -3i32}, {x: 4i32, y: -4i32}]"
1✔
197
        );
1✔
198
    }
1✔
199

200
    #[test]
201
    fn test_list() {
1✔
202
        let x = ListArray::try_new(
1✔
203
            buffer![1, 2, 3, 4].into_array(),
1✔
204
            buffer![0, 0, 1, 1, 2, 4].into_array(),
1✔
205
            Validity::Array(BoolArray::from_iter([true, true, false, true, true]).into_array()),
1✔
206
        )
1✔
207
        .unwrap()
1✔
208
        .into_array();
1✔
209
        assert_eq!(
1✔
210
            x.display().to_string(),
1✔
211
            "[[], [1i32], null, [2i32], [3i32, 4i32]]"
1✔
212
        );
1✔
213
    }
1✔
214
}
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