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

vortex-data / vortex / 17045413678

18 Aug 2025 03:43PM UTC coverage: 86.065% (-1.8%) from 87.913%
17045413678

Pull #4215

github

web-flow
Merge 2657b4c8e into cb2220961
Pull Request #4215: Ji/vectors

136 of 1803 new or added lines in 42 files covered. (7.54%)

127 existing lines in 26 files now uncovered.

56918 of 66134 relevant lines covered (86.06%)

612050.14 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::SC;
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() * SC,
×
NEW
61
            Alignment::new(vtype.byte_width()),
×
62
        );
NEW
63
        unsafe { elements.set_len(vtype.byte_width() * SC) };
×
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 is_empty(&self) -> bool {
×
NEW
80
        self.len == 0
×
NEW
81
    }
×
82

NEW
83
    pub fn set_len(&mut self, len: usize) {
×
NEW
84
        assert!(len <= SC, "Length cannot exceed the capacity of the vector");
×
NEW
85
        self.len = len;
×
NEW
86
    }
×
87

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

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

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

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

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

NEW
132
    pub fn as_view(&self) -> &View<'a> {
×
NEW
133
        &self.view
×
NEW
134
    }
×
135
}
136

137
impl<'a> Deref for VectorRef<'a> {
138
    type Target = View<'a>;
139

NEW
140
    fn deref(&self) -> &Self::Target {
×
NEW
141
        &self.view
×
NEW
142
    }
×
143
}
144

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

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

163
impl<'a> Deref for VectorRefMut<'a> {
164
    type Target = ViewMut<'a>;
165

NEW
166
    fn deref(&self) -> &Self::Target {
×
NEW
167
        &self.view
×
NEW
168
    }
×
169
}
170

171
impl<'a> DerefMut for VectorRefMut<'a> {
NEW
172
    fn deref_mut(&mut self) -> &mut Self::Target {
×
NEW
173
        &mut self.view
×
NEW
174
    }
×
175
}
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