Skip to content

Commit

Permalink
Validity
Browse files Browse the repository at this point in the history
  • Loading branch information
gatesn committed Apr 6, 2024
1 parent 8af1865 commit 68fc26e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 149 deletions.
1 change: 0 additions & 1 deletion vortex-array2/src/array/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod bool;
pub mod primitive;
pub mod ree;
pub mod validity;
61 changes: 33 additions & 28 deletions vortex-array2/src/array/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ mod compute;
use arrow_buffer::Buffer;
use vortex::ptype::{NativePType, PType};
use vortex_error::VortexResult;
use vortex_schema::{DType, Nullability};
use vortex_schema::DType;

use crate::array::validity::Validity;
use crate::compute::scalar_at;
use crate::impl_encoding;
use crate::validity::ArrayValidity;
use crate::{Array, IntoArray};
use crate::validity::{ArrayValidity, Validity, ValidityMetadata};
use crate::{impl_encoding, IntoArray};
use crate::{ArrayData, TypedArrayData};
use crate::{ArrayMetadata, TryFromArrayMetadata};
use crate::{ArrayView, ToArrayData};
Expand All @@ -18,26 +15,39 @@ use crate::{ToArray, TypedArrayView};
impl_encoding!("vortex.primitive", Primitive);

#[derive(Clone, Debug)]
pub struct PrimitiveMetadata(PType);
pub struct PrimitiveMetadata {
ptype: PType,
validity: ValidityMetadata,
}

impl PrimitiveMetadata {
pub fn ptype(&self) -> PType {
self.0
self.ptype
}
pub fn validity(&self) -> &ValidityMetadata {
&self.validity
}
}

pub trait PrimitiveArray {
fn dtype(&self) -> &DType;
fn ptype(&self) -> PType;
fn buffer(&self) -> &Buffer;
fn validity(&self) -> Option<Array>;
fn len(&self) -> usize {
self.buffer().len() / self.ptype().byte_width()
}
fn validity(&self) -> Option<Validity>;
}

impl PrimitiveData {
pub fn from_vec<T: NativePType>(values: Vec<T>) -> Self {
ArrayData::try_new(
&PrimitiveEncoding,
DType::from(T::PTYPE),
Arc::new(PrimitiveMetadata(T::PTYPE)),
Arc::new(PrimitiveMetadata {
ptype: T::PTYPE,
validity: ValidityMetadata::NonNullable,
}),
vec![Buffer::from_vec(values)].into(),
vec![].into(),
)
Expand All @@ -60,11 +70,10 @@ impl PrimitiveArray for PrimitiveData {
self.data().buffers().first().unwrap()
}

fn validity(&self) -> Option<Array> {
match self.dtype().nullability() {
Nullability::NonNullable => None,
Nullability::Nullable => Some(self.data().child(0).unwrap().to_array()),
}
fn validity(&self) -> Option<Validity> {
self.metadata()
.validity()
.to_validity(self.len(), self.data().child(0).map(|data| data.to_array()))
}
}

Expand All @@ -84,13 +93,13 @@ impl PrimitiveArray for PrimitiveView<'_> {
.expect("PrimitiveView must have a single buffer")
}

fn validity(&self) -> Option<Array> {
match self.dtype().nullability() {
Nullability::NonNullable => None,
Nullability::Nullable => {
Some(self.view().child(0, &Validity::DTYPE).unwrap().into_array())
}
}
fn validity(&self) -> Option<Validity> {
self.metadata().validity().to_validity(
self.len(),
self.view()
.child(0, &Validity::DTYPE)
.map(|view| view.into_array()),
)
}
}

Expand Down Expand Up @@ -119,17 +128,13 @@ impl TryFromArrayData for PrimitiveData {

impl ArrayTrait for &dyn PrimitiveArray {
fn len(&self) -> usize {
self.buffer().len() / self.ptype().byte_width()
(**self).len()
}
}

impl ArrayValidity for &dyn PrimitiveArray {
fn is_valid(&self, index: usize) -> bool {
if let Some(v) = self.validity() {
scalar_at(&v, index).unwrap().try_into().unwrap()
} else {
true
}
self.validity().map(|v| v.is_valid(index)).unwrap_or(true)
}
}

Expand Down
113 changes: 0 additions & 113 deletions vortex-array2/src/array/validity/mod.rs

This file was deleted.

7 changes: 0 additions & 7 deletions vortex-array2/src/validity.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use vortex_error::{vortex_bail, VortexResult};
use vortex_schema::{DType, Nullability};

use crate::array::validity::ValidityArray;
use crate::compute::scalar_at;
use crate::{Array, ArrayData, ToArrayData, WithArray};

Expand All @@ -10,12 +9,6 @@ pub trait ArrayValidity {
// Maybe add to_bool_array() here?
}

impl ArrayValidity for &dyn ValidityArray {
fn is_valid(&self, _index: usize) -> bool {
todo!()
}
}

#[derive(Clone, Debug)]
pub enum ValidityMetadata {
NonNullable,
Expand Down

0 comments on commit 68fc26e

Please sign in to comment.