Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamGS committed Aug 21, 2024
1 parent 0000f74 commit 17899b3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 28 deletions.
55 changes: 46 additions & 9 deletions vortex-array/src/array/chunked/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@ use crate::{

impl IntoCanonical for ChunkedArray {
fn into_canonical(self) -> VortexResult<Canonical> {
try_canonicalize_chunks(
self.chunks().collect(),
if self.dtype().is_nullable() {
self.logical_validity().into_validity()
} else {
Validity::NonNullable
},
self.dtype(),
)
let validity = if self.dtype().is_nullable() {
self.logical_validity().into_validity()
} else {
Validity::NonNullable
};
try_canonicalize_chunks(self.chunks().collect(), validity, self.dtype())
}
}

Expand Down Expand Up @@ -240,3 +237,43 @@ fn pack_varbin(chunks: &[Array], validity: Validity, dtype: &DType) -> VortexRes
validity,
)
}

#[cfg(test)]
mod tests {
use rstest::{fixture, rstest};
use vortex_dtype::Nullability;

use super::*;

#[fixture]
fn binary_array() -> Array {
let values = PrimitiveArray::from(
"hello worldhello world this is a long string"
.as_bytes()
.to_vec(),
);
let offsets = PrimitiveArray::from(vec![0, 11, 44]);

VarBinArray::try_new(
offsets.into_array(),
values.into_array(),
DType::Utf8(Nullability::NonNullable),
Validity::NonNullable,
)
.unwrap()
.into_array()
}

#[rstest]
fn test_pack_varbin(binary_array: Array) {
let arrays = vec![binary_array.clone(), binary_array.clone()];
let packed_array = pack_varbin(
&arrays,
Validity::NonNullable,
&DType::Utf8(Nullability::NonNullable),
)
.unwrap();

assert_eq!(packed_array.len(), binary_array.len() * arrays.len());
}
}
19 changes: 10 additions & 9 deletions vortex-array/src/array/varbin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ pub fn varbin_scalar(value: Buffer, dtype: &DType) -> Scalar {

#[cfg(test)]
mod test {
use rstest::{fixture, rstest};
use vortex_dtype::{DType, Nullability};

use crate::array::primitive::PrimitiveArray;
Expand All @@ -236,6 +237,7 @@ mod test {
use crate::validity::Validity;
use crate::{Array, IntoArray};

#[fixture]
fn binary_array() -> Array {
let values = PrimitiveArray::from(
"hello worldhello world this is a long string"
Expand All @@ -254,20 +256,19 @@ mod test {
.into_array()
}

#[test]
pub fn test_scalar_at() {
let binary_arr = binary_array();
assert_eq!(binary_arr.len(), 2);
assert_eq!(scalar_at(&binary_arr, 0).unwrap(), "hello world".into());
#[rstest]
pub fn test_scalar_at(binary_array: Array) {
assert_eq!(binary_array.len(), 2);
assert_eq!(scalar_at(&binary_array, 0).unwrap(), "hello world".into());
assert_eq!(
scalar_at(&binary_arr, 1).unwrap(),
scalar_at(&binary_array, 1).unwrap(),
"hello world this is a long string".into()
)
}

#[test]
pub fn slice_array() {
let binary_arr = slice(&binary_array(), 1, 2).unwrap();
#[rstest]
pub fn slice_array(binary_array: Array) {
let binary_arr = slice(&binary_array, 1, 2).unwrap();
assert_eq!(
scalar_at(&binary_arr, 0).unwrap(),
"hello world this is a long string".into()
Expand Down
21 changes: 11 additions & 10 deletions vortex-array/src/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,19 @@ impl FromIterator<LogicalValidity> for Validity {
// Else, construct the boolean buffer
let mut buffer = BooleanBufferBuilder::new(validities.iter().map(|v| v.len()).sum());
for validity in validities {
let present = match validity {
LogicalValidity::AllValid(count) => BooleanBuffer::new_set(count),
LogicalValidity::AllInvalid(count) => BooleanBuffer::new_unset(count),
LogicalValidity::Array(array) => array
.into_bool()
.expect("validity must flatten to BoolArray")
.boolean_buffer(),
match validity {
LogicalValidity::AllValid(count) => buffer.append_n(count, true),
LogicalValidity::AllInvalid(count) => buffer.append_n(count, false),
LogicalValidity::Array(array) => {
let array_buffer = array
.into_bool()
.expect("validity must flatten to BoolArray")
.boolean_buffer();
buffer.append_buffer(&array_buffer);
}
};
buffer.append_buffer(&present);
}
let bool_array = BoolArray::try_new(buffer.finish(), Validity::NonNullable)
.expect("BoolArray::try_new from BooleanBuffer should always succeed");
let bool_array = BoolArray::from(buffer.finish());
Self::Array(bool_array.into_array())
}
}
Expand Down

0 comments on commit 17899b3

Please sign in to comment.