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

vortex-data / vortex / 17040114484

18 Aug 2025 12:07PM UTC coverage: 86.042% (-1.9%) from 87.913%
17040114484

Pull #4215

github

web-flow
Merge 4600ca5c4 into cb1a92920
Pull Request #4215: Ji/vectors

132 of 1817 new or added lines in 42 files covered. (7.26%)

125 existing lines in 26 files now uncovered.

56932 of 66168 relevant lines covered (86.04%)

611735.65 hits per line

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

0.0
/vortex-vector/src/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_STEP_COUNT;
18
use crate::bits::BitVector;
19
use crate::types::{Element, VType};
20
use crate::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() * PIPELINE_STEP_COUNT,
×
NEW
61
            Alignment::new(vtype.byte_width()),
×
62
        );
NEW
63
        unsafe { elements.set_len(vtype.byte_width() * PIPELINE_STEP_COUNT) };
×
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!(
×
NEW
81
            len <= PIPELINE_STEP_COUNT,
×
NEW
82
            "Length cannot exceed the capacity of the vector"
×
83
        );
NEW
84
        self.len = len;
×
NEW
85
    }
×
86

NEW
87
    pub fn as_mut_array<T: Element>(&mut self) -> &mut [T; PIPELINE_STEP_COUNT] {
×
NEW
88
        assert_eq!(self.vtype, T::vtype());
×
NEW
89
        unsafe {
×
NEW
90
            &mut *(self
×
NEW
91
                .elements
×
NEW
92
                .as_mut_ptr()
×
NEW
93
                .cast::<T>()
×
NEW
94
                .cast::<[T; PIPELINE_STEP_COUNT]>())
×
NEW
95
        }
×
NEW
96
    }
×
97

NEW
98
    pub fn as_view_mut(&mut self) -> ViewMut<'_> {
×
NEW
99
        ViewMut {
×
NEW
100
            vtype: self.vtype,
×
NEW
101
            elements: self.elements.as_mut_ptr().cast(),
×
NEW
102
            validity: Some(self.validity.as_view_mut()),
×
NEW
103
            data: vec![],
×
NEW
104
            _marker: Default::default(),
×
NEW
105
        }
×
NEW
106
    }
×
107

NEW
108
    pub fn as_view(&self) -> View<'_> {
×
NEW
109
        View {
×
NEW
110
            vtype: self.vtype,
×
NEW
111
            elements: self.elements.as_ptr().cast(),
×
NEW
112
            validity: Some(self.validity.as_view()),
×
NEW
113
            len: self.len,
×
NEW
114
            data: vec![],
×
NEW
115
            _marker: Default::default(),
×
NEW
116
        }
×
NEW
117
    }
×
118
}
119

120
/// A [`VectorRef`] provides a small wrapper to allow accessing a [`View`] with the same lifetime
121
/// as the borrowed vector, rather than the lifetime of the [`Ref`].
122
pub struct VectorRef<'a> {
123
    // Use to ensure that view and borrow have the same lifetime.
124
    #[allow(dead_code)]
125
    borrow: Ref<'a, Vector>,
126
    view: View<'a>,
127
}
128

129
impl<'a> VectorRef<'a> {
NEW
130
    pub fn new(borrow: Ref<'a, Vector>) -> Self {
×
NEW
131
        let view = borrow.as_view();
×
132
        // SAFETY: we continue to hold onto the [`Ref`], so it is safe to erase the lifetime.
NEW
133
        let view = unsafe { std::mem::transmute::<View<'_>, View<'a>>(view) };
×
NEW
134
        Self { borrow, view }
×
NEW
135
    }
×
136

NEW
137
    pub fn as_view(&self) -> &View<'a> {
×
NEW
138
        &self.view
×
NEW
139
    }
×
140
}
141

142
impl<'a> Deref for VectorRef<'a> {
143
    type Target = View<'a>;
144

NEW
145
    fn deref(&self) -> &Self::Target {
×
NEW
146
        &self.view
×
NEW
147
    }
×
148
}
149

150
/// A [`VectorRefMut`] provides a small wrapper to allow accessing a [`ViewMut`] with the same
151
/// lifetime as the borrowed vector, rather than the lifetime of the [`RefMut`].
152
pub struct VectorRefMut<'a> {
153
    // Use to ensure that view and borrow have the same lifetime.
154
    #[allow(dead_code)]
155
    borrow: RefMut<'a, Vector>,
156
    view: ViewMut<'a>,
157
}
158

159
impl<'a> VectorRefMut<'a> {
NEW
160
    pub fn new(mut borrow: RefMut<'a, Vector>) -> Self {
×
NEW
161
        let view = borrow.as_view_mut();
×
162
        // SAFETY: we continue to hold onto the [`Ref`], so it is safe to erase the lifetime.
NEW
163
        let view = unsafe { std::mem::transmute::<ViewMut<'_>, ViewMut<'a>>(view) };
×
NEW
164
        Self { borrow, view }
×
NEW
165
    }
×
166
}
167

168
impl<'a> Deref for VectorRefMut<'a> {
169
    type Target = ViewMut<'a>;
170

NEW
171
    fn deref(&self) -> &Self::Target {
×
NEW
172
        &self.view
×
NEW
173
    }
×
174
}
175

176
impl<'a> DerefMut for VectorRefMut<'a> {
NEW
177
    fn deref_mut(&mut self) -> &mut Self::Target {
×
NEW
178
        &mut self.view
×
NEW
179
    }
×
180
}
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