Skip to content

Commit

Permalink
DType variant traits (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
gatesn authored Jul 17, 2024
1 parent 93da8f1 commit 5f72446
Show file tree
Hide file tree
Showing 35 changed files with 705 additions and 43 deletions.
1 change: 1 addition & 0 deletions bench-vortex/src/vortex_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::path::PathBuf;

use vortex::array::chunked::ChunkedArray;
use vortex::array::struct_::StructArray;
use vortex::variants::StructArrayTrait;
use vortex::ArrayDType;
use vortex_dtype::DType;
use vortex_error::VortexResult;
Expand Down
9 changes: 9 additions & 0 deletions encodings/alp/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize};
use vortex::array::primitive::PrimitiveArray;
use vortex::stats::ArrayStatisticsCompute;
use vortex::validity::{ArrayValidity, LogicalValidity};
use vortex::variants::{ArrayVariants, PrimitiveArrayTrait};
use vortex::visitor::{AcceptArrayVisitor, ArrayVisitor};
use vortex::{impl_encoding, ArrayDType, Canonical, IntoCanonical};
use vortex_dtype::PType;
Expand Down Expand Up @@ -92,6 +93,14 @@ impl ALPArray {

impl ArrayTrait for ALPArray {}

impl ArrayVariants for ALPArray {
fn as_primitive_array(&self) -> Option<&dyn PrimitiveArrayTrait> {
Some(self)
}
}

impl PrimitiveArrayTrait for ALPArray {}

impl ArrayValidity for ALPArray {
fn is_valid(&self, index: usize) -> bool {
self.encoded().with_dyn(|a| a.is_valid(index))
Expand Down
9 changes: 9 additions & 0 deletions encodings/byte_bool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::mem::ManuallyDrop;
use arrow_buffer::BooleanBuffer;
use serde::{Deserialize, Serialize};
use vortex::array::bool::BoolArray;
use vortex::variants::{ArrayVariants, BoolArrayTrait};
use vortex::{
impl_encoding,
validity::{ArrayValidity, LogicalValidity, Validity, ValidityMetadata},
Expand Down Expand Up @@ -73,6 +74,14 @@ impl ByteBoolArray {

impl ArrayTrait for ByteBoolArray {}

impl ArrayVariants for ByteBoolArray {
fn as_bool_array(&self) -> Option<&dyn BoolArrayTrait> {
Some(self)
}
}

impl BoolArrayTrait for ByteBoolArray {}

impl From<Vec<bool>> for ByteBoolArray {
fn from(value: Vec<bool>) -> Self {
Self::try_from_vec(value, Validity::AllValid).unwrap()
Expand Down
9 changes: 9 additions & 0 deletions encodings/datetime-parts/src/array.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use serde::{Deserialize, Serialize};
use vortex::stats::ArrayStatisticsCompute;
use vortex::validity::{ArrayValidity, LogicalValidity};
use vortex::variants::{ArrayVariants, ExtensionArrayTrait};
use vortex::visitor::{AcceptArrayVisitor, ArrayVisitor};
use vortex::{impl_encoding, ArrayDType, Canonical, IntoCanonical};
use vortex_error::vortex_bail;
Expand Down Expand Up @@ -79,6 +80,14 @@ impl DateTimePartsArray {

impl ArrayTrait for DateTimePartsArray {}

impl ArrayVariants for DateTimePartsArray {
fn as_extension_array(&self) -> Option<&dyn ExtensionArrayTrait> {
Some(self)
}
}

impl ExtensionArrayTrait for DateTimePartsArray {}

impl IntoCanonical for DateTimePartsArray {
fn into_canonical(self) -> VortexResult<Canonical> {
Ok(Canonical::Extension(
Expand Down
1 change: 1 addition & 0 deletions encodings/dict/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ mod compress;
mod compute;
mod dict;
mod stats;
mod variants;
37 changes: 37 additions & 0 deletions encodings/dict/src/variants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use vortex::variants::{ArrayVariants, BinaryArrayTrait, PrimitiveArrayTrait, Utf8ArrayTrait};
use vortex::ArrayDType;
use vortex_dtype::DType;

use crate::DictArray;

impl ArrayVariants for DictArray {
fn as_primitive_array(&self) -> Option<&dyn PrimitiveArrayTrait> {
if matches!(self.dtype(), DType::Primitive(..)) {
Some(self)
} else {
None
}
}

fn as_utf8_array(&self) -> Option<&dyn Utf8ArrayTrait> {
if matches!(self.dtype(), DType::Utf8(..)) {
Some(self)
} else {
None
}
}

fn as_binary_array(&self) -> Option<&dyn BinaryArrayTrait> {
if matches!(self.dtype(), DType::Binary(..)) {
Some(self)
} else {
None
}
}
}

impl PrimitiveArrayTrait for DictArray {}

impl Utf8ArrayTrait for DictArray {}

impl BinaryArrayTrait for DictArray {}
9 changes: 9 additions & 0 deletions encodings/fastlanes/src/bitpacking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub use compress::*;
use vortex::array::primitive::{Primitive, PrimitiveArray};
use vortex::stats::ArrayStatisticsCompute;
use vortex::validity::{ArrayValidity, LogicalValidity, Validity, ValidityMetadata};
use vortex::variants::{ArrayVariants, PrimitiveArrayTrait};
use vortex::visitor::{AcceptArrayVisitor, ArrayVisitor};
use vortex::{impl_encoding, ArrayDType, Canonical, IntoCanonical};
use vortex_dtype::{Nullability, PType};
Expand Down Expand Up @@ -201,6 +202,14 @@ impl ArrayTrait for BitPackedArray {
}
}

impl ArrayVariants for BitPackedArray {
fn as_primitive_array(&self) -> Option<&dyn PrimitiveArrayTrait> {
Some(self)
}
}

impl PrimitiveArrayTrait for BitPackedArray {}

#[cfg(test)]
mod test {
use vortex::array::primitive::PrimitiveArray;
Expand Down
9 changes: 9 additions & 0 deletions encodings/fastlanes/src/delta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
use vortex::stats::ArrayStatisticsCompute;
use vortex::validity::ValidityMetadata;
use vortex::validity::{ArrayValidity, LogicalValidity, Validity};
use vortex::variants::{ArrayVariants, PrimitiveArrayTrait};
use vortex::visitor::{AcceptArrayVisitor, ArrayVisitor};
use vortex::{impl_encoding, ArrayDType, Canonical, IntoCanonical};
use vortex_dtype::match_each_unsigned_integer_ptype;
Expand Down Expand Up @@ -92,6 +93,14 @@ impl DeltaArray {

impl ArrayTrait for DeltaArray {}

impl ArrayVariants for DeltaArray {
fn as_primitive_array(&self) -> Option<&dyn PrimitiveArrayTrait> {
Some(self)
}
}

impl PrimitiveArrayTrait for DeltaArray {}

impl IntoCanonical for DeltaArray {
fn into_canonical(self) -> VortexResult<Canonical> {
delta_decompress(self).map(Canonical::Primitive)
Expand Down
9 changes: 9 additions & 0 deletions encodings/fastlanes/src/for/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub use compress::*;
use serde::{Deserialize, Serialize};
use vortex::stats::ArrayStatisticsCompute;
use vortex::validity::{ArrayValidity, LogicalValidity};
use vortex::variants::{ArrayVariants, PrimitiveArrayTrait};
use vortex::visitor::{AcceptArrayVisitor, ArrayVisitor};
use vortex::{impl_encoding, ArrayDType, Canonical, IntoCanonical};
use vortex_dtype::PType;
Expand Down Expand Up @@ -95,3 +96,11 @@ impl ArrayTrait for FoRArray {
self.encoded().nbytes()
}
}

impl ArrayVariants for FoRArray {
fn as_primitive_array(&self) -> Option<&dyn PrimitiveArrayTrait> {
Some(self)
}
}

impl PrimitiveArrayTrait for FoRArray {}
9 changes: 9 additions & 0 deletions encodings/roaring/src/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize};
use vortex::array::bool::{Bool, BoolArray};
use vortex::stats::ArrayStatisticsCompute;
use vortex::validity::{ArrayValidity, LogicalValidity, Validity};
use vortex::variants::{ArrayVariants, BoolArrayTrait};
use vortex::visitor::{AcceptArrayVisitor, ArrayVisitor};
use vortex::{impl_encoding, ArrayDType, Canonical, IntoCanonical};
use vortex_buffer::Buffer;
Expand Down Expand Up @@ -62,6 +63,14 @@ impl RoaringBoolArray {

impl ArrayTrait for RoaringBoolArray {}

impl ArrayVariants for RoaringBoolArray {
fn as_bool_array(&self) -> Option<&dyn BoolArrayTrait> {
Some(self)
}
}

impl BoolArrayTrait for RoaringBoolArray {}

impl AcceptArrayVisitor for RoaringBoolArray {
fn accept(&self, _visitor: &mut dyn ArrayVisitor) -> VortexResult<()> {
// TODO(ngates): should we store a buffer in memory? Or delay serialization?
Expand Down
11 changes: 10 additions & 1 deletion encodings/roaring/src/integer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
use vortex::array::primitive::{Primitive, PrimitiveArray};
use vortex::stats::ArrayStatisticsCompute;
use vortex::validity::{ArrayValidity, LogicalValidity};
use vortex::variants::{ArrayVariants, PrimitiveArrayTrait};
use vortex::visitor::{AcceptArrayVisitor, ArrayVisitor};
use vortex::{impl_encoding, Canonical, IntoCanonical};
use vortex_buffer::Buffer;
Expand All @@ -29,7 +30,7 @@ impl RoaringIntArray {
let length = bitmap.statistics().cardinality as usize;
Ok(Self {
typed: TypedArray::try_from_parts(
DType::Bool(NonNullable),
DType::Primitive(ptype, NonNullable),
length,
RoaringIntMetadata { ptype },
Some(Buffer::from(bitmap.serialize::<Portable>())),
Expand Down Expand Up @@ -64,6 +65,14 @@ impl RoaringIntArray {

impl ArrayTrait for RoaringIntArray {}

impl ArrayVariants for RoaringIntArray {
fn as_primitive_array(&self) -> Option<&dyn PrimitiveArrayTrait> {
Some(self)
}
}

impl PrimitiveArrayTrait for RoaringIntArray {}

impl ArrayValidity for RoaringIntArray {
fn is_valid(&self, _index: usize) -> bool {
true
Expand Down
9 changes: 9 additions & 0 deletions encodings/runend/src/runend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use vortex::compute::unary::scalar_at::scalar_at;
use vortex::compute::{search_sorted, SearchSortedSide};
use vortex::stats::{ArrayStatistics, ArrayStatisticsCompute};
use vortex::validity::{ArrayValidity, LogicalValidity, Validity, ValidityMetadata};
use vortex::variants::{ArrayVariants, PrimitiveArrayTrait};
use vortex::visitor::{AcceptArrayVisitor, ArrayVisitor};
use vortex::{impl_encoding, ArrayDType, Canonical, IntoArrayVariant, IntoCanonical};
use vortex_error::vortex_bail;
Expand Down Expand Up @@ -107,6 +108,14 @@ impl RunEndArray {

impl ArrayTrait for RunEndArray {}

impl ArrayVariants for RunEndArray {
fn as_primitive_array(&self) -> Option<&dyn PrimitiveArrayTrait> {
Some(self)
}
}

impl PrimitiveArrayTrait for RunEndArray {}

impl ArrayValidity for RunEndArray {
fn is_valid(&self, index: usize) -> bool {
self.validity().is_valid(index)
Expand Down
9 changes: 9 additions & 0 deletions encodings/zigzag/src/zigzag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize};
use vortex::array::primitive::PrimitiveArray;
use vortex::stats::ArrayStatisticsCompute;
use vortex::validity::{ArrayValidity, LogicalValidity};
use vortex::variants::{ArrayVariants, PrimitiveArrayTrait};
use vortex::visitor::{AcceptArrayVisitor, ArrayVisitor};
use vortex::{impl_encoding, ArrayDType, Canonical, IntoCanonical};
use vortex_dtype::PType;
Expand Down Expand Up @@ -52,6 +53,14 @@ impl ZigZagArray {

impl ArrayTrait for ZigZagArray {}

impl ArrayVariants for ZigZagArray {
fn as_primitive_array(&self) -> Option<&dyn PrimitiveArrayTrait> {
Some(self)
}
}

impl PrimitiveArrayTrait for ZigZagArray {}

impl ArrayValidity for ZigZagArray {
fn is_valid(&self, index: usize) -> bool {
self.encoded().with_dyn(|a| a.is_valid(index))
Expand Down
9 changes: 9 additions & 0 deletions vortex-array/src/array/bool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use vortex_buffer::Buffer;

use crate::validity::{ArrayValidity, ValidityMetadata};
use crate::validity::{LogicalValidity, Validity};
use crate::variants::{ArrayVariants, BoolArrayTrait};
use crate::visitor::{AcceptArrayVisitor, ArrayVisitor};
use crate::{impl_encoding, Canonical, IntoCanonical};

Expand Down Expand Up @@ -74,6 +75,14 @@ impl BoolArray {

impl ArrayTrait for BoolArray {}

impl ArrayVariants for BoolArray {
fn as_bool_array(&self) -> Option<&dyn BoolArrayTrait> {
Some(self)
}
}

impl BoolArrayTrait for BoolArray {}

impl From<BooleanBuffer> for BoolArray {
fn from(value: BooleanBuffer) -> Self {
Self::try_new(value, Validity::NonNullable).unwrap()
Expand Down
1 change: 1 addition & 0 deletions vortex-array/src/array/chunked/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::array::struct_::StructArray;
use crate::array::varbin::builder::VarBinBuilder;
use crate::array::varbin::VarBinArray;
use crate::validity::Validity;
use crate::variants::StructArrayTrait;
use crate::{
Array, ArrayDType, ArrayValidity, Canonical, IntoArray, IntoArrayVariant, IntoCanonical,
};
Expand Down
1 change: 1 addition & 0 deletions vortex-array/src/array/chunked/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::{impl_encoding, ArrayDType};
mod canonical;
mod compute;
mod stats;
mod variants;

impl_encoding!("vortex.chunked", 11u16, Chunked);

Expand Down
Loading

0 comments on commit 5f72446

Please sign in to comment.