Skip to content

Commit 32988dc

Browse files
authored
Feature: add into_mut to vectors (#5215)
Signed-off-by: Connor Tsui <[email protected]>
1 parent fae8327 commit 32988dc

File tree

12 files changed

+123
-0
lines changed

12 files changed

+123
-0
lines changed

vortex-vector/src/binaryview/vector.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,21 @@ impl<T: BinaryViewType> VectorOps for BinaryViewVector<T> {
203203
))
204204
}
205205
}
206+
207+
fn into_mut(self) -> BinaryViewVectorMut<T> {
208+
let views_mut = self.views.into_mut();
209+
let validity_mut = self.validity.into_mut();
210+
211+
// If someone else has a strong reference to the `Arc`, clone the underlying data (which is
212+
// just a **different** reference count increment).
213+
let buffers_mut = Arc::try_unwrap(self.buffers)
214+
.unwrap_or_else(|arc| (*arc).clone())
215+
.into_vec();
216+
217+
// SAFETY: The BinaryViewVector maintains the exact same invariants as the immutable
218+
// version, so all invariants are still upheld.
219+
unsafe { BinaryViewVectorMut::new_unchecked(views_mut, validity_mut, buffers_mut) }
220+
}
206221
}
207222

208223
#[cfg(test)]

vortex-vector/src/bool/vector.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,11 @@ impl VectorOps for BoolVector {
103103
}),
104104
}
105105
}
106+
107+
fn into_mut(self) -> BoolVectorMut {
108+
BoolVectorMut {
109+
bits: self.bits.into_mut(),
110+
validity: self.validity.into_mut(),
111+
}
112+
}
106113
}

vortex-vector/src/decimal/generic.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,12 @@ impl<D: NativeDecimalType> VectorOps for DVector<D> {
180180
}),
181181
}
182182
}
183+
184+
fn into_mut(self) -> DVectorMut<D> {
185+
DVectorMut {
186+
ps: self.ps,
187+
elements: self.elements.into_mut(),
188+
validity: self.validity.into_mut(),
189+
}
190+
}
183191
}

vortex-vector/src/decimal/vector.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ impl VectorOps for DecimalVector {
4545
.map_err(Self::from)
4646
})
4747
}
48+
49+
fn into_mut(self) -> DecimalVectorMut {
50+
match_each_dvector!(self, |v| { DecimalVectorMut::from(v.into_mut()) })
51+
}
4852
}
4953

5054
impl DecimalTypeDowncast for DecimalVector {

vortex-vector/src/fixed_size_list/vector.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,23 @@ impl VectorOps for FixedSizeListVector {
188188
}),
189189
}
190190
}
191+
192+
fn into_mut(self) -> FixedSizeListVectorMut {
193+
let len = self.len;
194+
let list_size = self.list_size;
195+
let validity = self.validity.into_mut();
196+
197+
// If someone else has a strong reference to the `Arc`, clone the underlying data (which is
198+
// just a **different** reference count increment).
199+
let elements = Arc::try_unwrap(self.elements).unwrap_or_else(|arc| (*arc).clone());
200+
201+
FixedSizeListVectorMut {
202+
elements: Box::new(elements.into_mut()),
203+
list_size,
204+
validity,
205+
len,
206+
}
207+
}
191208
}
192209

193210
#[cfg(test)]

vortex-vector/src/listview/vector.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,25 @@ impl VectorOps for ListViewVector {
274274
}),
275275
}
276276
}
277+
278+
fn into_mut(self) -> ListViewVectorMut {
279+
let len = self.len;
280+
let validity = self.validity.into_mut();
281+
let offsets = self.offsets.into_mut();
282+
let sizes = self.sizes.into_mut();
283+
284+
// If someone else has a strong reference to the `Arc`, clone the underlying data (which is
285+
// just a **different** reference count increment).
286+
let elements = Arc::try_unwrap(self.elements).unwrap_or_else(|arc| (*arc).clone());
287+
288+
ListViewVectorMut {
289+
offsets,
290+
sizes,
291+
elements: Box::new(elements.into_mut()),
292+
validity,
293+
len,
294+
}
295+
}
277296
}
278297

279298
// TODO(connor): It would be better to separate everything inside the macros into its own function,

vortex-vector/src/null/vector.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,8 @@ impl VectorOps for NullVector {
4747
fn try_into_mut(self) -> Result<NullVectorMut, Self> {
4848
Ok(NullVectorMut::new(self.len))
4949
}
50+
51+
fn into_mut(self) -> NullVectorMut {
52+
NullVectorMut::new(self.len)
53+
}
5054
}

vortex-vector/src/ops.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ pub trait VectorOps: private::Sealed + Into<Vector> + Sized {
3939
///
4040
/// If `self` is not unique, this will fail and return `self` back to the caller.
4141
fn try_into_mut(self) -> Result<Self::Mutable, Self>;
42+
43+
/// Converts `self` into a mutable vector (implementing [`VectorMutOps`]).
44+
///
45+
/// This method uses "clone-on-write" semantics, meaning it will clone any underlying data that
46+
/// has multiple references (preventing mutable access). `into_mut` can be more efficient than
47+
/// [`try_into_mut()`] when mutations are infrequent.
48+
///
49+
/// The semantics of `into_mut` are somewhat similar to that of [`Arc::make_mut()`], but instead
50+
/// of working with references, this works with owned immutable / mutable types.
51+
///
52+
/// [`try_into_mut()`]: Self::try_into_mut
53+
/// [`Arc::make_mut()`]: std::sync::Arc::make_mut
54+
fn into_mut(self) -> Self::Mutable;
4255
}
4356

4457
/// Common operations for mutable vectors (all the variants of [`VectorMut`]).

vortex-vector/src/primitive/generic.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,11 @@ impl<T: NativePType> VectorOps for PVector<T> {
142142
}),
143143
}
144144
}
145+
146+
fn into_mut(self) -> PVectorMut<T> {
147+
let elements = self.elements.into_mut();
148+
let validity = self.validity.into_mut();
149+
150+
PVectorMut { elements, validity }
151+
}
145152
}

vortex-vector/src/primitive/vector.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ impl VectorOps for PrimitiveVector {
8282
.map_err(Self::from)
8383
})
8484
}
85+
86+
fn into_mut(self) -> PrimitiveVectorMut {
87+
match_each_pvector!(self, |v| { v.into_mut().into() })
88+
}
8589
}
8690

8791
impl PTypeUpcast for PrimitiveVector {

0 commit comments

Comments
 (0)