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

vortex-data / vortex / 16970635821

14 Aug 2025 04:13PM UTC coverage: 85.882% (-1.8%) from 87.693%
16970635821

Pull #4215

github

web-flow
Merge 5182504a6 into f547cbca5
Pull Request #4215: Ji/vectors

80 of 1729 new or added lines in 38 files covered. (4.63%)

117 existing lines in 25 files now uncovered.

56994 of 66363 relevant lines covered (85.88%)

609331.7 hits per line

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

0.0
/vortex-array/src/pipeline/vector.rs
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3

4
//! Vectors contain owned fixed-size canonical arrays of elements.
5
//!
6

7
// TODO(ngates): Currently, the data in a vector is Arc'd. We should consider whether we want the
8
//  performance hit for as_mut(), or whether we want zero-copy cloning. Not clear that we ever
9
//  need the clone behavior.
10

11
use std::cell::{Ref, RefMut};
12
use std::fmt::Debug;
13
use std::ops::{Deref, DerefMut};
14

15
use vortex_buffer::{Alignment, ByteBuffer, ByteBufferMut};
16

17
use crate::pipeline::N;
18
use crate::pipeline::bits::BitVector;
19
use crate::pipeline::types::{Element, VType};
20
use crate::pipeline::view::{View, ViewMut};
21

22
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
23
pub struct VectorId(pub(crate) usize);
24

25
impl Deref for VectorId {
26
    type Target = usize;
27

NEW
28
    fn deref(&self) -> &Self::Target {
×
NEW
29
        &self.0
×
NEW
30
    }
×
31
}
32

33
/// A vector contains fixed-size owned data in canonical form.
34
#[derive(Debug)]
35
pub struct Vector {
36
    /// The physical type of the vector, which defines how the elements are stored.
37
    vtype: VType,
38
    /// The allocated elements buffer.
39
    /// Alignment is at least the size of the element type.
40
    /// The capacity of the elements buffer is N * size_of::<T>() where T is the element type.
41
    elements: ByteBufferMut,
42
    /// The validity mask for the vector, indicating which elements in the buffer are valid.
43
    validity: BitVector,
44
    // The length of the valid values in the vector.
45
    len: usize,
46

47
    /// Additional buffers of data used by the vector, such as string data.
48
    // TODO(ngates): ideally these buffers are compressed somehow? E.g. using FSST?
49
    #[allow(dead_code)]
50
    data: Vec<ByteBuffer>,
51
}
52

53
impl Vector {
NEW
54
    pub fn new<T: Element>() -> Self {
×
NEW
55
        Self::new_with_vtype(T::vtype())
×
NEW
56
    }
×
57

NEW
58
    pub fn new_with_vtype(vtype: VType) -> Self {
×
NEW
59
        let mut elements = ByteBufferMut::with_capacity_aligned(
×
NEW
60
            vtype.byte_width() * N,
×
NEW
61
            Alignment::new(vtype.byte_width()),
×
62
        );
NEW
63
        unsafe { elements.set_len(vtype.byte_width() * N) };
×
64

NEW
65
        Self {
×
NEW
66
            vtype,
×
NEW
67
            elements,
×
NEW
68
            validity: BitVector::full().clone(),
×
NEW
69
            len: 0,
×
NEW
70
            data: vec![],
×
NEW
71
        }
×
NEW
72
    }
×
73

74
    #[inline(always)]
NEW
75
    pub fn len(&self) -> usize {
×
NEW
76
        self.len
×
NEW
77
    }
×
78

NEW
79
    pub fn set_len(&mut self, len: usize) {
×
NEW
80
        assert!(len <= N, "Length cannot exceed the capacity of the vector");
×
NEW
81
        self.len = len;
×
NEW
82
    }
×
83

NEW
84
    pub fn as_mut<T: Element>(&mut self) -> &mut [T; N] {
×
NEW
85
        assert_eq!(self.vtype, T::vtype());
×
NEW
86
        unsafe { &mut *(self.elements.as_mut_ptr().cast::<T>().cast::<[T; N]>()) }
×
NEW
87
    }
×
88

NEW
89
    pub fn as_view_mut(&mut self) -> ViewMut<'_> {
×
NEW
90
        ViewMut {
×
NEW
91
            vtype: self.vtype,
×
NEW
92
            elements: self.elements.as_mut_ptr().cast(),
×
NEW
93
            validity: Some(self.validity.as_view_mut()),
×
NEW
94
            data: vec![],
×
NEW
95
            _marker: Default::default(),
×
NEW
96
        }
×
NEW
97
    }
×
98

NEW
99
    pub fn as_view(&self) -> View<'_> {
×
NEW
100
        View {
×
NEW
101
            vtype: self.vtype,
×
NEW
102
            elements: self.elements.as_ptr().cast(),
×
NEW
103
            validity: Some(self.validity.as_view()),
×
NEW
104
            len: self.len,
×
NEW
105
            data: vec![],
×
NEW
106
            _marker: Default::default(),
×
NEW
107
        }
×
NEW
108
    }
×
109
}
110

111
/// A [`VectorRef`] provides a small wrapper to allow accessing a [`View`] with the same lifetime
112
/// as the borrowed vector, rather than the lifetime of the [`Ref`].
113
pub struct VectorRef<'a> {
114
    borrow: Ref<'a, Vector>,
115
    view: View<'a>,
116
}
117

118
impl<'a> VectorRef<'a> {
NEW
119
    pub fn new(borrow: Ref<'a, Vector>) -> Self {
×
NEW
120
        let view = borrow.as_view();
×
121
        // SAFETY: we continue to hold onto the [`Ref`], so it is safe to erase the lifetime.
NEW
122
        let view = unsafe { std::mem::transmute::<View<'_>, View<'a>>(view) };
×
NEW
123
        Self { borrow, view }
×
NEW
124
    }
×
125

NEW
126
    pub fn as_view(&self) -> &View<'a> {
×
NEW
127
        &self.view
×
NEW
128
    }
×
129
}
130

131
impl<'a> Deref for VectorRef<'a> {
132
    type Target = View<'a>;
133

NEW
134
    fn deref(&self) -> &Self::Target {
×
NEW
135
        &self.view
×
NEW
136
    }
×
137
}
138

139
/// A [`VectorRefMut`] provides a small wrapper to allow accessing a [`ViewMut`] with the same
140
/// lifetime as the borrowed vector, rather than the lifetime of the [`RefMut`].
141
pub struct VectorRefMut<'a> {
142
    borrow: RefMut<'a, Vector>,
143
    view: ViewMut<'a>,
144
}
145

146
impl<'a> VectorRefMut<'a> {
NEW
147
    pub fn new(mut borrow: RefMut<'a, Vector>) -> Self {
×
NEW
148
        let view = borrow.as_view_mut();
×
149
        // SAFETY: we continue to hold onto the [`Ref`], so it is safe to erase the lifetime.
NEW
150
        let view = unsafe { std::mem::transmute::<ViewMut<'_>, ViewMut<'a>>(view) };
×
NEW
151
        Self { borrow, view }
×
NEW
152
    }
×
153
}
154

155
impl<'a> Deref for VectorRefMut<'a> {
156
    type Target = ViewMut<'a>;
157

NEW
158
    fn deref(&self) -> &Self::Target {
×
NEW
159
        &self.view
×
NEW
160
    }
×
161
}
162

163
impl<'a> DerefMut for VectorRefMut<'a> {
NEW
164
    fn deref_mut(&mut self) -> &mut Self::Target {
×
NEW
165
        &mut self.view
×
NEW
166
    }
×
167
}
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