Skip to content

Commit

Permalink
Fix: fuzzer reference take implementation respects generated values n…
Browse files Browse the repository at this point in the history
…ullability (#1586)
  • Loading branch information
robert3005 authored Dec 6, 2024
1 parent 2958870 commit 3a27f4a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 36 deletions.
53 changes: 19 additions & 34 deletions fuzz/src/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@ use vortex_dtype::{match_each_native_ptype, DType};
use vortex_error::VortexExpect;

pub fn take_canonical_array(array: &ArrayData, indices: &[usize]) -> ArrayData {
let validity = if array.dtype().is_nullable() {
let validity_idx = array
.logical_validity()
.into_array()
.into_bool()
.unwrap()
.boolean_buffer()
.iter()
.collect::<Vec<_>>();

Validity::from_iter(indices.iter().map(|i| validity_idx[*i]))
} else {
Validity::NonNullable
};

match array.dtype() {
DType::Bool(_) => {
let bool_array = array.clone().into_bool().unwrap();
let vec_values = bool_array.boolean_buffer().iter().collect::<Vec<_>>();
let vec_validity = bool_array
.logical_validity()
BoolArray::try_new(indices.iter().map(|i| vec_values[*i]).collect(), validity)
.vortex_expect("Validity length cannot mismatch")
.into_array()
.into_bool()
.unwrap()
.boolean_buffer()
.iter()
.collect::<Vec<_>>();
BoolArray::try_new(
indices.iter().map(|i| vec_values[*i]).collect(),
Validity::from_iter(indices.iter().map(|i| vec_validity[*i])),
)
.vortex_expect("Validity length cannot mismatch")
.into_array()
}
DType::Primitive(p, _) => match_each_native_ptype!(p, |$P| {
let primitive_array = array.clone().into_primitive().unwrap();
Expand All @@ -33,19 +37,8 @@ pub fn take_canonical_array(array: &ArrayData, indices: &[usize]) -> ArrayData {
.iter()
.copied()
.collect::<Vec<_>>();
let vec_validity = primitive_array
.logical_validity()
PrimitiveArray::from_vec(indices.iter().map(|i| vec_values[*i]).collect(),validity)
.into_array()
.into_bool()
.unwrap()
.boolean_buffer()
.iter()
.collect::<Vec<_>>();
PrimitiveArray::from_vec(
indices.iter().map(|i| vec_values[*i]).collect(),
Validity::from_iter(indices.iter().map(|i| vec_validity[*i]))
)
.into_array()
}),
DType::Utf8(_) | DType::Binary(_) => {
let utf8 = array.clone().into_varbinview().unwrap();
Expand All @@ -64,20 +57,12 @@ pub fn take_canonical_array(array: &ArrayData, indices: &[usize]) -> ArrayData {
.children()
.map(|c| take_canonical_array(&c, indices))
.collect::<Vec<_>>();
let vec_validity = struct_array
.logical_validity()
.into_array()
.into_bool()
.unwrap()
.boolean_buffer()
.iter()
.collect::<Vec<_>>();

StructArray::try_new(
struct_array.names().clone(),
taken_children,
indices.len(),
Validity::from_iter(indices.iter().map(|i| vec_validity[*i])),
validity,
)
.unwrap()
.into_array()
Expand Down
9 changes: 7 additions & 2 deletions vortex-dtype/src/arbitrary.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::sync::Arc;

use arbitrary::{Arbitrary, Result, Unstructured};

use crate::{DType, FieldNames, Nullability, PType, StructDType};
use crate::{DType, FieldName, FieldNames, Nullability, PType, StructDType};

impl<'a> Arbitrary<'a> for DType {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
Expand Down Expand Up @@ -59,7 +61,10 @@ impl<'a> Arbitrary<'a> for StructDType {
}

fn random_struct_dtype(u: &mut Unstructured<'_>, depth: u8) -> Result<StructDType> {
let names: FieldNames = u.arbitrary()?;
let field_count = u.choose_index(3)?;
let names: FieldNames = (0..field_count)
.map(|_| FieldName::arbitrary(u))
.collect::<Result<Arc<_>>>()?;
let dtypes = (0..names.len())
.map(|_| random_dtype(u, depth))
.collect::<Result<Vec<_>>>()?;
Expand Down

0 comments on commit 3a27f4a

Please sign in to comment.