Skip to content

Commit 565060f

Browse files
committed
clean up struct vector
Signed-off-by: Connor Tsui <[email protected]>
1 parent 5b91d07 commit 565060f

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

vortex-vector/src/struct_/vector.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ impl VectorOps for StructVector {
137137
Self: Sized,
138138
{
139139
let len = self.len;
140+
140141
let fields = match Arc::try_unwrap(self.fields) {
141142
Ok(fields) => fields,
142143
Err(fields) => return Err(StructVector { fields, ..self }),

vortex-vector/src/struct_/vector_mut.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
use std::sync::Arc;
77

8+
use vortex_dtype::StructFields;
89
use vortex_error::{VortexExpect, VortexResult, vortex_ensure};
910
use vortex_mask::MaskMut;
1011

@@ -174,6 +175,31 @@ impl StructVectorMut {
174175
}
175176
}
176177

178+
/// Creates a new [`StructVectorMut`] with the given fields and capacity.
179+
pub fn with_capacity(struct_fields: &StructFields, capacity: usize) -> Self {
180+
let fields: Vec<VectorMut> = struct_fields
181+
.fields()
182+
.map(|dtype| VectorMut::with_capacity(&dtype, capacity))
183+
.collect();
184+
185+
let validity = MaskMut::with_capacity(capacity);
186+
let len = validity.len();
187+
188+
#[cfg(debug_assertions)]
189+
{
190+
for field in &fields {
191+
debug_assert_eq!(field.len(), 0);
192+
}
193+
debug_assert_eq!(validity.len(), 0);
194+
}
195+
196+
Self {
197+
fields: fields.into_boxed_slice(),
198+
validity,
199+
len,
200+
}
201+
}
202+
177203
/// Decomposes the struct vector into its constituent parts (fields, validity, and length).
178204
pub fn into_parts(self) -> (Box<[VectorMut]>, MaskMut, usize) {
179205
(self.fields, self.validity, self.len)
@@ -662,7 +688,7 @@ mod tests {
662688
);
663689

664690
// Create a VectorMut with capacity using the struct dtype.
665-
let vector_mut = VectorMut::with_capacity(100, &struct_dtype);
691+
let vector_mut = VectorMut::with_capacity(&struct_dtype, 100);
666692

667693
// Verify it's a struct vector.
668694
match vector_mut {

vortex-vector/src/vector_mut.rs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
99
use vortex_dtype::DType;
1010
use vortex_error::vortex_panic;
11-
use vortex_mask::MaskMut;
1211

1312
use crate::varbin::{BinaryVectorMut, StringVectorMut};
1413
use crate::{
@@ -46,34 +45,22 @@ pub enum VectorMut {
4645

4746
impl VectorMut {
4847
/// Create a new mutable vector with the given capacity and dtype.
49-
pub fn with_capacity(capacity: usize, dtype: &DType) -> Self {
48+
pub fn with_capacity(dtype: &DType, capacity: usize) -> Self {
5049
match dtype {
51-
DType::Null => NullVectorMut::new(0).into(), // `NullVector` has `usize::MAX` capacity.
50+
DType::Null => NullVectorMut::new(0).into(),
5251
DType::Bool(_) => BoolVectorMut::with_capacity(capacity).into(),
5352
DType::Primitive(ptype, _) => {
5453
PrimitiveVectorMut::with_capacity(*ptype, capacity).into()
5554
}
5655
DType::Struct(struct_fields, _) => {
57-
let fields: Vec<VectorMut> = struct_fields
58-
.fields()
59-
.map(|dtype| Self::with_capacity(capacity, &dtype))
60-
.collect();
61-
let validity = MaskMut::with_capacity(capacity);
62-
63-
#[cfg(debug_assertions)]
64-
{
65-
for field in &fields {
66-
debug_assert_eq!(field.len(), 0);
67-
}
68-
debug_assert_eq!(validity.len(), 0);
69-
}
70-
71-
// SAFETY: All fields and validity have length 0, so they all have the same length.
72-
Self::Struct(unsafe {
73-
StructVectorMut::new_unchecked(fields.into_boxed_slice(), validity)
74-
})
56+
StructVectorMut::with_capacity(struct_fields, capacity).into()
7557
}
76-
_ => vortex_panic!("Unsupported dtype for VectorMut"),
58+
DType::Decimal(..)
59+
| DType::Utf8(_)
60+
| DType::Binary(_)
61+
| DType::List(..)
62+
| DType::FixedSizeList(..)
63+
| DType::Extension(_) => vortex_panic!("Unsupported dtype for VectorMut"),
7764
}
7865
}
7966
}
@@ -226,24 +213,24 @@ mod tests {
226213
#[test]
227214
fn test_with_capacity() {
228215
// Test capacity allocation for different types.
229-
let null_vec = VectorMut::with_capacity(10, &DType::Null);
216+
let null_vec = VectorMut::with_capacity(&DType::Null, 10);
230217
assert_eq!(null_vec.capacity(), usize::MAX); // Null vectors have unlimited capacity.
231218

232-
let bool_vec = VectorMut::with_capacity(100, &DType::Bool(Nullability::Nullable));
219+
let bool_vec = VectorMut::with_capacity(&DType::Bool(Nullability::Nullable), 100);
233220
assert!(bool_vec.capacity() >= 100);
234221

235222
let prim_vec =
236-
VectorMut::with_capacity(50, &DType::Primitive(PType::I32, Nullability::Nullable));
223+
VectorMut::with_capacity(&DType::Primitive(PType::I32, Nullability::Nullable), 50);
237224
assert!(prim_vec.capacity() >= 50);
238225
}
239226

240227
#[test]
241228
#[should_panic(expected = "Mismatched vector types")]
242229
fn test_type_mismatch_panics() {
243230
// Test that operations between mismatched types panic.
244-
let mut vec1 = VectorMut::with_capacity(10, &DType::Bool(Nullability::Nullable));
231+
let mut vec1 = VectorMut::with_capacity(&DType::Bool(Nullability::Nullable), 10);
245232
let vec2 =
246-
VectorMut::with_capacity(10, &DType::Primitive(PType::I32, Nullability::Nullable));
233+
VectorMut::with_capacity(&DType::Primitive(PType::I32, Nullability::Nullable), 10);
247234

248235
vec1.unsplit(vec2); // Should panic.
249236
}

0 commit comments

Comments
 (0)