Skip to content

Commit 44c6f9d

Browse files
committed
clean up
Signed-off-by: Connor Tsui <connor.tsui20@gmail.com>
1 parent b30ed5b commit 44c6f9d

File tree

4 files changed

+45
-65
lines changed

4 files changed

+45
-65
lines changed

vortex-dtype/src/nullability.rs

Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -15,72 +15,21 @@ pub enum Nullability {
1515
}
1616

1717
impl Nullability {
18-
/// Returns `Some(f())` if the the nullability is [`Nullable`](Self::Nullable), otherwise
19-
/// returns `None`.
18+
/// Returns `true` if the nullability is [`Nullable`](Self::Nullable), otherwise returns
19+
/// `false`.
2020
///
2121
/// # Examples
2222
///
2323
/// ```
2424
/// use vortex_dtype::Nullability::*;
2525
///
26-
/// assert_eq!(NonNullable.is_nullable_then(|| 0), None);
27-
/// assert_eq!(Nullable.is_nullable_then(|| 0), Some(0));
26+
/// assert!(!NonNullable.is_nullable());
27+
/// assert!(Nullable.is_nullable());
2828
/// ```
29-
///
30-
/// ```
31-
/// # use vortex_dtype::Nullability::*;
32-
/// #
33-
/// let mut a = 0;
34-
///
35-
/// Nullable.is_nullable_then(|| { a += 1; });
36-
/// NonNullable.is_nullable_then(|| { a += 1; });
37-
///
38-
/// // `a` is incremented once because the closure is evaluated lazily by `is_nullable_then`.
39-
/// assert_eq!(a, 1);
40-
/// ```
41-
///
42-
/// Inspired by the [`bool::then`] function.
43-
pub fn is_nullable_then<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
44-
match self {
45-
Nullability::NonNullable => None,
46-
Nullability::Nullable => Some(f()),
47-
}
48-
}
49-
50-
/// Returns `Some(t)` if the the nullability is [`Nullable`](Self::Nullable), otherwise returns
51-
/// `None`.
52-
///
53-
/// Arguments passed to `is_nullable_then_some` are eagerly evaluated; if you are passing the
54-
/// result of a function call, it is recommended to use [`then`](bool::then), which is lazily
55-
/// evaluated.
56-
///
57-
/// # Examples
58-
///
59-
/// ```
60-
/// use vortex_dtype::Nullability::*;
61-
///
62-
/// assert_eq!(NonNullable.is_nullable_then_some(0), None);
63-
/// assert_eq!(Nullable.is_nullable_then_some(0), Some(0));
64-
/// ```
65-
///
66-
/// ```
67-
/// # use vortex_dtype::Nullability::*;
68-
/// #
69-
/// let mut a = 0;
70-
/// let mut function_with_side_effects = || { a += 1; };
71-
///
72-
/// Nullable.is_nullable_then_some(function_with_side_effects());
73-
/// NonNullable.is_nullable_then_some(function_with_side_effects());
74-
///
75-
/// // `a` is incremented twice because the value passed to `then_some` is evaluated eagerly.
76-
/// assert_eq!(a, 2);
77-
/// ```
78-
///
79-
/// Inspired by the [`bool::then_some`] function.
80-
pub fn is_nullable_then_some<T>(self, t: T) -> Option<T> {
29+
pub fn is_nullable(&self) -> bool {
8130
match self {
82-
Nullability::NonNullable => None,
83-
Nullability::Nullable => Some(t),
31+
Nullability::NonNullable => false,
32+
Nullability::Nullable => true,
8433
}
8534
}
8635
}

vortex-vector/src/bool/vector_mut.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use vortex_dtype::{DType, Nullability};
88
use vortex_mask::MaskMut;
99

1010
use super::BoolVector;
11-
use crate::VectorMutOps;
11+
use crate::{VectorMutOps, VectorOps};
1212

1313
/// A mutable vector of boolean values.
1414
///
@@ -25,7 +25,9 @@ pub struct BoolVectorMut {
2525
impl BoolVectorMut {
2626
/// Creates a new mutable boolean vector with the given `capacity` and `nullability`.
2727
pub fn with_capacity(capacity: usize, nullability: Nullability) -> Self {
28-
let validity = nullability.is_nullable_then(|| MaskMut::with_capacity(capacity));
28+
let validity = nullability
29+
.is_nullable()
30+
.then(|| MaskMut::with_capacity(capacity));
2931

3032
Self {
3133
bits: BitBufferMut::with_capacity(capacity),
@@ -68,9 +70,13 @@ impl VectorMutOps for BoolVectorMut {
6870
}
6971

7072
fn extend_from_vector(&mut self, other: &BoolVector) {
73+
assert!(
74+
self.is_nullable() || !other.is_nullable(),
75+
"tried to extend a non-nullable `BoolVector` with a nullable vector"
76+
);
77+
7178
self.bits.append_buffer(&other.bits);
7279

73-
// TODO(connor): We must `other`'s nullability in relation to `self`.
7480
match (&mut self.validity, &other.validity) {
7581
(Some(self_v), Some(other_v)) => self_v.append_mask(other_v),
7682
(Some(self_v), None) => self_v.append_n(true, other.bits.len()),

vortex-vector/src/ops.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ pub trait VectorOps: private::Sealed + Into<Vector> {
1313
/// Returns the [`Nullability`] of the vector.
1414
fn nullability(&self) -> Nullability;
1515

16+
/// Returns `true` if the nullability of this vector is [`Nullable`].
17+
///
18+
/// [`Nullable`]: Nullability::Nullable
19+
fn is_nullable(&self) -> bool {
20+
self.nullability().is_nullable()
21+
}
22+
1623
/// Returns the [`DType`] (or data type) of the vector.
1724
fn dtype(&self) -> DType;
1825

@@ -46,6 +53,13 @@ pub trait VectorMutOps: private::Sealed + Into<VectorMut> {
4653
/// Returns the [`Nullability`] of the vector.
4754
fn nullability(&self) -> Nullability;
4855

56+
/// Returns `true` if the nullability of this vector is [`Nullable`].
57+
///
58+
/// [`Nullable`]: Nullability::Nullable
59+
fn is_nullable(&self) -> bool {
60+
self.nullability().is_nullable()
61+
}
62+
4963
/// Returns the [`DType`] (or data type) of the vector.
5064
fn dtype(&self) -> DType;
5165

@@ -72,8 +86,10 @@ pub trait VectorMutOps: private::Sealed + Into<VectorMut> {
7286

7387
/// Extends the vector by appending elements from another vector.
7488
///
75-
/// TODO(connor): Document semantics of what happens if `self` is non-nullable and `other` is
76-
/// nullable (should panic?).
89+
/// # Panics
90+
///
91+
/// If `self` is a non-nullable vector, and `other` is a nullable vector, implementors should
92+
/// ensure that this function panics.
7793
fn extend_from_vector(&mut self, other: &Self::Immutable);
7894

7995
/// Converts `self` into an immutable vector.
@@ -93,6 +109,7 @@ pub trait VectorMutOps: private::Sealed + Into<VectorMut> {
93109
/// `at > capacity`).
94110
fn split_off(&mut self, at: usize) -> Self;
95111

112+
// TODO(connor): Should this panic if other has a different nullability?
96113
/// Absorbs a mutable vector that was previously split off.
97114
///
98115
/// If the two vectors were previously contiguous and not mutated in a way that causes

vortex-vector/src/primitive/generic_mut.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use vortex_buffer::BufferMut;
77
use vortex_dtype::{DType, NativePType, Nullability};
88
use vortex_mask::MaskMut;
99

10-
use crate::{GenericPVector, VectorMutOps};
10+
use crate::{GenericPVector, VectorMutOps, VectorOps};
1111

1212
/// A mutable vector of generic primitive values.
1313
///
@@ -25,7 +25,9 @@ pub struct GenericPVectorMut<T> {
2525
impl<T: NativePType> GenericPVectorMut<T> {
2626
/// Create a new mutable primitive vector with the given capacity and nullability.
2727
pub fn with_capacity(capacity: usize, nullability: Nullability) -> Self {
28-
let validity = nullability.is_nullable_then(|| MaskMut::with_capacity(capacity));
28+
let validity = nullability
29+
.is_nullable()
30+
.then(|| MaskMut::with_capacity(capacity));
2931

3032
Self {
3133
elements: BufferMut::with_capacity(capacity),
@@ -62,7 +64,13 @@ impl<T: NativePType> VectorMutOps for GenericPVectorMut<T> {
6264

6365
/// Extends the vector by appending elements from another vector.
6466
fn extend_from_vector(&mut self, other: &GenericPVector<T>) {
67+
assert!(
68+
self.is_nullable() || !other.is_nullable(),
69+
"tried to extend a non-nullable `GenericPVector` with a nullable vector"
70+
);
71+
6572
self.elements.extend_from_slice(other.elements.as_slice());
73+
6674
match (&mut self.validity, &other.validity) {
6775
(Some(self_v), Some(other_v)) => self_v.append_mask(other_v),
6876
(Some(self_v), None) => self_v.append_n(true, other.elements.len()),

0 commit comments

Comments
 (0)