Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 95 additions & 51 deletions vortex-vector/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,16 @@
macro_rules! match_each_vector {
($self:expr, | $vec:ident | $body:block) => {{
match $self {
$crate::Vector::Null(v) => {
let $vec = v;
$body
}
$crate::Vector::Bool(v) => {
let $vec = v;
$body
}
$crate::Vector::Primitive(v) => {
let $vec = v;
$body
}
$crate::Vector::String(v) => {
let $vec = v;
$body
}
$crate::Vector::Binary(v) => {
let $vec = v;
$body
}
$crate::Vector::Struct(v) => {
let $vec = v;
$body
}
$crate::Vector::Null($vec) => $body,
$crate::Vector::Bool($vec) => $body,
$crate::Vector::Primitive($vec) => $body,
$crate::Vector::String($vec) => $body,
$crate::Vector::Binary($vec) => $body,
$crate::Vector::Struct($vec) => $body,
}
}};
}

pub(crate) use match_each_vector;

/// Matches on all variants of [`VectorMut`] and executes the same code for each variant branch.
///
/// This macro eliminates repetitive match statements when implementing operations that need to work
Expand Down Expand Up @@ -104,32 +84,96 @@ pub(crate) use match_each_vector;
macro_rules! match_each_vector_mut {
($self:expr, | $vec:ident | $body:block) => {{
match $self {
$crate::VectorMut::Null(v) => {
let $vec = v;
$body
}
$crate::VectorMut::Bool(v) => {
let $vec = v;
$body
}
$crate::VectorMut::Primitive(v) => {
let $vec = v;
$body
}
$crate::VectorMut::String(v) => {
let $vec = v;
$body
}
$crate::VectorMut::Binary(v) => {
let $vec = v;
$body
}
$crate::VectorMut::Struct(v) => {
let $vec = v;
$body
}
$crate::VectorMut::Null($vec) => $body,
$crate::VectorMut::Bool($vec) => $body,
$crate::VectorMut::Primitive($vec) => $body,
$crate::VectorMut::String($vec) => $body,
$crate::VectorMut::Binary($vec) => $body,
$crate::VectorMut::Struct($vec) => $body,
}
}};
}

/// Internal macro to generate match arms for vector pairs.
#[doc(hidden)]
#[macro_export]
macro_rules! __match_vector_pair_arms {
(
$left:expr,
$right:expr,
$enum_left:ident,
$enum_right:ident,
$a:ident,
$b:ident,
$body:expr
) => {{
match ($left, $right) {
($crate::$enum_left::Null($a), $crate::$enum_right::Null($b)) => $body,
($crate::$enum_left::Bool($a), $crate::$enum_right::Bool($b)) => $body,
($crate::$enum_left::Primitive($a), $crate::$enum_right::Primitive($b)) => $body,
($crate::$enum_left::String($a), $crate::$enum_right::String($b)) => $body,
($crate::$enum_left::Binary($a), $crate::$enum_right::Binary($b)) => $body,
($crate::$enum_left::Struct($a), $crate::$enum_right::Struct($b)) => $body,
_ => ::vortex_error::vortex_panic!("Mismatched vector types"),
}
}};
}

pub(crate) use match_each_vector_mut;
/// Matches on pairs of vector variants and executes the same code for matching variant pairs.
///
/// This macro eliminates repetitive match statements when implementing operations that need to work
/// with pairs of vectors where the variants must match.
///
/// Specify the types of the left and right vectors (either `Vector` or `VectorMut`) and the macro
/// generates the appropriate match arms.
///
/// The macro binds the matched inner values to identifiers in the closure that can be used in the
/// body expression.
///
/// # Examples
///
/// ```
/// use vortex_vector::{
/// BoolVector, BoolVectorMut, Vector, VectorMut, VectorMutOps, match_vector_pair
/// };
///
/// fn extend_vector(left: &mut VectorMut, right: &Vector) {
/// match_vector_pair!(left, right, |a: VectorMut, b: Vector| {
/// a.extend_from_vector(b);
/// })
/// }
///
/// let mut mut_vec: VectorMut = BoolVectorMut::from_iter([true, false, true]).into();
/// let vec: Vector = BoolVectorMut::from_iter([false, true]).freeze().into();
///
/// extend_vector(&mut mut_vec, &vec);
/// assert_eq!(mut_vec.len(), 5);
/// ```
///
/// Note that the vectors can also be owned:
///
/// ```
/// use vortex_vector::{
/// BoolVector, BoolVectorMut, Vector, VectorMut, VectorMutOps, match_vector_pair
/// };
///
/// fn extend_vector_owned(mut dest: VectorMut, src: Vector) -> VectorMut {
/// match_vector_pair!(&mut dest, src, |a: VectorMut, b: Vector| {
/// a.extend_from_vector(&b);
/// dest
/// })
/// }
///
/// let mut_vec: VectorMut = BoolVectorMut::from_iter([true, false, true]).into();
/// let vec: Vector = BoolVectorMut::from_iter([false, true]).freeze().into();
///
/// let new_bool_mut = extend_vector_owned(mut_vec, vec);
/// assert_eq!(new_bool_mut.len(), 5);
/// ```
#[macro_export] // DO NOT ADD `#[rustfmt::skip]`!!! https://github.com/rust-lang/rust/pull/52234#issuecomment-903419099
macro_rules! match_vector_pair {
($left:expr, $right:expr, | $a:ident : Vector, $b:ident : Vector | $body:expr) => {{ $crate::__match_vector_pair_arms!($left, $right, Vector, Vector, $a, $b, $body) }};
($left:expr, $right:expr, | $a:ident : Vector, $b:ident : VectorMut | $body:expr) => {{ $crate::__match_vector_pair_arms!($left, $right, Vector, VectorMut, $a, $b, $body) }};
($left:expr, $right:expr, | $a:ident : VectorMut, $b:ident : Vector | $body:expr) => {{ $crate::__match_vector_pair_arms!($left, $right, VectorMut, Vector, $a, $b, $body) }};
($left:expr, $right:expr, | $a:ident : VectorMut, $b:ident : VectorMut | $body:expr) => {{ $crate::__match_vector_pair_arms!($left, $right, VectorMut, VectorMut, $a, $b, $body) }};
}
114 changes: 22 additions & 92 deletions vortex-vector/src/primitive/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,56 +44,21 @@
macro_rules! match_each_pvector {
($self:expr, | $vec:ident | $body:block) => {{
match $self {
$crate::PrimitiveVector::U8(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVector::U16(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVector::U32(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVector::U64(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVector::I8(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVector::I16(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVector::I32(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVector::I64(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVector::F16(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVector::F32(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVector::F64(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVector::U8($vec) => $body,
$crate::PrimitiveVector::U16($vec) => $body,
$crate::PrimitiveVector::U32($vec) => $body,
$crate::PrimitiveVector::U64($vec) => $body,
$crate::PrimitiveVector::I8($vec) => $body,
$crate::PrimitiveVector::I16($vec) => $body,
$crate::PrimitiveVector::I32($vec) => $body,
$crate::PrimitiveVector::I64($vec) => $body,
$crate::PrimitiveVector::F16($vec) => $body,
$crate::PrimitiveVector::F32($vec) => $body,
$crate::PrimitiveVector::F64($vec) => $body,
}
}};
}

pub(crate) use match_each_pvector;

/// Matches on all primitive type variants of [`PrimitiveVectorMut`] and executes the same code
/// for each variant branch.
///
Expand Down Expand Up @@ -129,52 +94,17 @@ pub(crate) use match_each_pvector;
macro_rules! match_each_pvector_mut {
($self:expr, | $vec:ident | $body:block) => {{
match $self {
$crate::PrimitiveVectorMut::U8(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVectorMut::U16(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVectorMut::U32(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVectorMut::U64(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVectorMut::I8(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVectorMut::I16(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVectorMut::I32(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVectorMut::I64(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVectorMut::F16(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVectorMut::F32(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVectorMut::F64(v) => {
let $vec = v;
$body
}
$crate::PrimitiveVectorMut::U8($vec) => $body,
$crate::PrimitiveVectorMut::U16($vec) => $body,
$crate::PrimitiveVectorMut::U32($vec) => $body,
$crate::PrimitiveVectorMut::U64($vec) => $body,
$crate::PrimitiveVectorMut::I8($vec) => $body,
$crate::PrimitiveVectorMut::I16($vec) => $body,
$crate::PrimitiveVectorMut::I32($vec) => $body,
$crate::PrimitiveVectorMut::I64($vec) => $body,
$crate::PrimitiveVectorMut::F16($vec) => $body,
$crate::PrimitiveVectorMut::F32($vec) => $body,
$crate::PrimitiveVectorMut::F64($vec) => $body,
}
}};
}

pub(crate) use match_each_pvector_mut;
3 changes: 1 addition & 2 deletions vortex-vector/src/primitive/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use vortex_dtype::half::f16;
use vortex_dtype::{NativePType, PType, PTypeDowncast, PTypeUpcast};
use vortex_error::vortex_panic;

use super::macros::match_each_pvector;
use crate::{PVector, PrimitiveVectorMut, VectorOps};
use crate::{PVector, PrimitiveVectorMut, VectorOps, match_each_pvector};

/// An immutable vector of primitive values.
///
Expand Down
3 changes: 1 addition & 2 deletions vortex-vector/src/primitive/vector_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use vortex_dtype::half::f16;
use vortex_dtype::{NativePType, PType, PTypeDowncast, PTypeUpcast};
use vortex_error::vortex_panic;

use super::macros::match_each_pvector_mut;
use crate::{PVectorMut, PrimitiveVector, VectorMutOps};
use crate::{PVectorMut, PrimitiveVector, VectorMutOps, match_each_pvector_mut};

/// A mutable vector of primitive values.
///
Expand Down
9 changes: 4 additions & 5 deletions vortex-vector/src/struct_/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ pub struct StructVector {

/// The length of the vector (which is the same as all field vectors).
///
/// This is stored here as a convenience, and also helps in the case that the `StructVector` has
/// no fields.
/// This is stored here as a convenience, as the validity also tracks this information.
pub(super) len: usize,
}

Expand Down Expand Up @@ -111,14 +110,14 @@ impl StructVector {
}
}

/// Decomposes the struct vector into its constituent parts (fields, validity, and length).
/// Decomposes the struct vector into its constituent parts (fields and validity).
pub fn into_parts(self) -> (Arc<Box<[Vector]>>, Mask) {
(self.fields, self.validity)
}

/// Returns the fields of the `StructVector`, each stored column-wise as a [`Vector`].
pub fn fields(&self) -> &[Vector] {
self.fields.as_ref()
pub fn fields(&self) -> &Arc<Box<[Vector]>> {
&self.fields
}
}

Expand Down
Loading
Loading