Skip to content
Open
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
14 changes: 1 addition & 13 deletions fuzz/fuzz_targets/file_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fuzz_target!(|fuzz: FuzzFileAction| -> Corpus {
} = fuzz;
let array_data = array;

if has_nullable_struct(array_data.dtype()) || has_duplicate_field_names(array_data.dtype()) {
if has_duplicate_field_names(array_data.dtype()) {
return Corpus::Reject;
}

Expand Down Expand Up @@ -115,18 +115,6 @@ fuzz_target!(|fuzz: FuzzFileAction| -> Corpus {
Corpus::Keep
});

fn has_nullable_struct(dtype: &DType) -> bool {
dtype.is_struct() && dtype.is_nullable()
|| dtype
.as_struct_fields_opt()
.map(|sdt| sdt.fields().any(|dtype| has_nullable_struct(&dtype)))
.unwrap_or(false)
|| dtype
.as_list_element_opt()
.map(|e| has_nullable_struct(e.as_ref()))
.unwrap_or(false)
}

fn has_duplicate_field_names(dtype: &DType) -> bool {
if let Some(struct_dtype) = dtype.as_struct_fields_opt() {
struct_has_duplicate_names(struct_dtype)
Expand Down
4 changes: 4 additions & 0 deletions vortex-array/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ harness = false
name = "aggregate_grouped"
harness = false

[[bench]]
name = "aggregate_definedness"
harness = false

[[bench]]
name = "cast_primitive"
harness = false
Expand Down
200 changes: 200 additions & 0 deletions vortex-array/benches/aggregate_definedness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

//! Accumulating a nested field with the definedness of its nullable parent, compared with accumulating the
//! logical field, whose validity already excludes the rows the parent nulls out.

#![allow(clippy::unwrap_used)]

use std::sync::LazyLock;

use divan::Bencher;
use mimalloc::MiMalloc;
use rand::prelude::*;
use vortex_array::ArrayRef;
use vortex_array::IntoArray;
use vortex_array::VortexSessionExecute;
use vortex_array::aggregate_fn::AggregateFnRef;
use vortex_array::aggregate_fn::AggregateFnVTableExt;
use vortex_array::aggregate_fn::EmptyOptions;
use vortex_array::aggregate_fn::NumericalAggregateOpts;
use vortex_array::aggregate_fn::definedness::list_elements_definedness;
use vortex_array::aggregate_fn::fns::null_count::NullCount;
use vortex_array::aggregate_fn::fns::sum::Sum;
use vortex_array::arrays::ListArray;
use vortex_array::arrays::PrimitiveArray;
use vortex_array::dtype::Nullability;
use vortex_array::scalar::Scalar;
use vortex_array::validity::Validity;
use vortex_buffer::Buffer;
use vortex_mask::Mask;
use vortex_session::VortexSession;

#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;

fn main() {
LazyLock::force(&SESSION);
check_results();
divan::main();
}

// Sized to keep the CodSpeed simulation under 1ms per benchmark.
const N: usize = 15_000;

static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);

/// An aggregate that skips nulls and one that counts them.
const AGGREGATES: &[&str] = &["sum", "null_count"];

fn aggregate(name: &str) -> AggregateFnRef {
match name {
"sum" => Sum.bind(NumericalAggregateOpts::default()),
"null_count" => NullCount.bind(EmptyOptions),
_ => unreachable!("unknown aggregate {name}"),
}
}

fn values() -> Buffer<i32> {
let mut rng = StdRng::seed_from_u64(1);
(0..N).map(|_| rng.random_range(-1000..1000)).collect()
}

/// The field's own validity: a tenth of its values are null.
fn field_validity() -> Mask {
let mut rng = StdRng::seed_from_u64(5);
Mask::from_iter((0..N).map(|_| rng.random_bool(0.9)))
}

/// A nullable struct field, holding values in every row including those the struct nulls out.
fn field() -> ArrayRef {
PrimitiveArray::new(
values(),
Validity::from_mask(field_validity(), Nullability::Nullable),
)
.into_array()
}

/// Nine in ten rows, chosen at random, as the struct validity of a nested field would be.
fn definedness() -> Mask {
let mut rng = StdRng::seed_from_u64(2);
Mask::from_iter((0..N).map(|_| rng.random_bool(0.9)))
}

/// The logical field: null wherever the struct or the field is null.
fn masked_field() -> ArrayRef {
PrimitiveArray::new(
values(),
Validity::from_mask(&field_validity() & &definedness(), Nullability::Nullable),
)
.into_array()
}

fn run(aggregate_fn: &AggregateFnRef, batch: &ArrayRef, defined: Option<&Mask>) -> Scalar {
let mut ctx = SESSION.create_execution_ctx();
let mut acc = aggregate_fn.accumulator(batch.dtype()).unwrap();
match defined {
Some(defined) => acc.accumulate_defined(batch, defined, &mut ctx).unwrap(),
None => acc.accumulate(batch, &mut ctx).unwrap(),
}
acc.finish().unwrap()
}

/// Check that every benchmark computes the right result before timing it.
///
/// Undefined rows are null, so the defined field gives the same result as the logical field for
/// every aggregate. The null count counts the field's own nulls and the rows the struct nulls out.
fn check_results() {
let (field, defined) = (field(), definedness());
for name in AGGREGATES {
let aggregate_fn = aggregate(name);
assert_eq!(
run(&aggregate_fn, &field, Some(&defined)),
run(&aggregate_fn, &masked_field(), None),
"{name}"
);
}

let expected_nulls = (!&(&field_validity() & &defined)).true_count() as u64;
let null_count = run(&aggregate("null_count"), &field, Some(&defined));
assert_eq!(
null_count.as_primitive().typed_value::<u64>(),
Some(expected_nulls)
);
}

/// Accumulate the field with the definedness of its parent.
///
/// Both this and [`nullable`] build their input outside the timed region, a fresh batch per
/// iteration so that no statistic cached by a previous iteration is reused, and run the same
/// accumulator over the same logical values. The defined side also pays for merging the definedness
/// into the field's validity, which the nullable side receives already merged.
#[divan::bench(args = AGGREGATES)]
fn defined(bencher: Bencher, name: &str) {
let aggregate_fn = aggregate(name);
let defined = definedness();
bencher
.with_inputs(|| {
let batch = field();
(
aggregate_fn.accumulator(batch.dtype()).unwrap(),
batch,
SESSION.create_execution_ctx(),
)
})
.bench_values(|(mut acc, batch, mut ctx)| {
acc.accumulate_defined(&batch, &defined, &mut ctx).unwrap();
acc.flush().unwrap()
});
}

/// Accumulate the logical field, whose validity already excludes the undefined rows.
#[divan::bench(args = AGGREGATES)]
fn nullable(bencher: Bencher, name: &str) {
let aggregate_fn = aggregate(name);
bencher
.with_inputs(|| {
let batch = masked_field();
(
aggregate_fn.accumulator(batch.dtype()).unwrap(),
batch,
SESSION.create_execution_ctx(),
)
})
.bench_values(|(mut acc, batch, mut ctx)| {
acc.accumulate(&batch, &mut ctx).unwrap();
acc.flush().unwrap()
});
}

const VALID_DENSITIES: &[f64] = &[0.5, 0.99];

/// A list column of `N` lists of up to eight elements, whose given fraction of lists is valid.
fn lists_with_validity(density: f64) -> ListArray {
let mut rng = StdRng::seed_from_u64(7);
let mut offsets = Vec::with_capacity(N + 1);
offsets.push(0u32);
for _ in 0..N {
let last = *offsets.last().unwrap();
offsets.push(last + rng.random_range(0..8u32));
}
let elements: Buffer<u32> = (0..*offsets.last().unwrap()).collect();
let validity = Mask::from_iter((0..N).map(|_| rng.random_bool(density)));
ListArray::try_new(
elements.into_array(),
Buffer::from(offsets).into_array(),
Validity::from_mask(validity, Nullability::Nullable),
&mut SESSION.create_execution_ctx(),
)
.unwrap()
}

/// Derive the definedness on the elements of a list column from the lists' validity.
#[divan::bench(args = VALID_DENSITIES)]
fn list_elements_definedness_by_validity(bencher: Bencher, density: f64) {
let lists = lists_with_validity(density);
let defined = Mask::new_true(N);
bencher
.with_inputs(|| SESSION.create_execution_ctx())
.bench_values(|mut ctx| list_elements_definedness(&lists, &defined, &mut ctx).unwrap());
}
75 changes: 75 additions & 0 deletions vortex-array/src/aggregate_fn/accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_ensure;
use vortex_error::vortex_ensure_eq;
use vortex_mask::Mask;

use crate::ArrayRef;
use crate::Columnar;
use crate::ExecutionCtx;
use crate::IntoArray;
use crate::aggregate_fn::AggregateDTypes;
use crate::aggregate_fn::AggregateFn;
use crate::aggregate_fn::AggregateFnRef;
use crate::aggregate_fn::AggregateFnVTable;
use crate::builtins::ArrayBuiltins;
use crate::columnar::AnyColumnar;
use crate::dtype::DType;
use crate::executor::max_iterations;
Expand Down Expand Up @@ -181,6 +184,20 @@ pub trait DynAccumulator: 'static + Send {
/// Accumulate a new array into the accumulator's state.
fn accumulate(&mut self, batch: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<()>;

/// Accumulate the logical column that `defined` describes over `batch`.
///
/// `defined` must have the length of `batch`. See the [`definedness`](crate::aggregate_fn::definedness)
/// module for what definedness means and the semantics aggregates give it. An aggregate defines
/// its own semantics with [`AggregateFnVTable::try_accumulate_defined`]; by default the
/// undefined rows are null whatever they hold, so the result equals accumulating
/// `batch.mask(defined)`.
fn accumulate_defined(
&mut self,
batch: &ArrayRef,
defined: &Mask,
ctx: &mut ExecutionCtx,
) -> VortexResult<()>;

/// Drain another accumulator's state into this one, resetting `other`.
///
/// The other accumulator must have been constructed for the same aggregate function,
Expand Down Expand Up @@ -283,6 +300,64 @@ impl<V: AggregateFnVTable> DynAccumulator for Accumulator<V> {
self.fold_partial(batch_partial)
}

fn accumulate_defined(
&mut self,
batch: &ArrayRef,
defined: &Mask,
ctx: &mut ExecutionCtx,
) -> VortexResult<()> {
vortex_ensure_eq!(
defined.len(),
batch.len(),
"Definedness mask length must match the batch length",
);
if defined.all_true() {
return self.accumulate(batch, ctx);
}
if self.is_saturated() {
return Ok(());
}
vortex_ensure_eq!(batch.dtype(), &self.dtypes.dtype, "Input DType mismatch");

self.ensure_partial()?;
let partial = self.partial.as_mut().vortex_expect("partial materialized");
if self.vtable.try_accumulate_defined(
self.dtypes.args(&self.options),
partial,
batch,
defined,
ctx,
)? {
return Ok(());
}

// The default semantics: undefined rows read as null. The masked batch is a temporary
// array, so no statistics are read from or cached on it.
let masked = batch.clone().mask(defined.clone().into_array())?;
if masked.dtype() == &self.dtypes.dtype {
return self.accumulate_batch(&masked, ctx);
}

// Masking widened a non-nullable input to nullable. Accumulate it as the nullable input,
// whose partial state must be the same, and fold the result into this state.
let mut nullable = Accumulator::try_new(
self.vtable.clone(),
self.options.clone(),
masked.dtype().clone(),
)?;
vortex_ensure_eq!(
nullable.dtypes.partial_dtype,
self.dtypes.partial_dtype,
"Aggregate {} has a different partial dtype for a nullable input",
self.aggregate_fn,
);
nullable.accumulate_batch(&masked, ctx)?;
match nullable.take_partial() {
Some(partial) => self.fold_partial(partial),
None => Ok(()),
}
}

fn merge_from(&mut self, other: &mut dyn DynAccumulator) -> VortexResult<()> {
let Some(other) = other.downcast_mut::<V>() else {
vortex_bail!(
Expand Down
16 changes: 16 additions & 0 deletions vortex-array/src/aggregate_fn/combined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::hash::Hash;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_err;
use vortex_mask::Mask;
use vortex_session::VortexSession;

use crate::ArrayRef;
Expand Down Expand Up @@ -316,6 +317,21 @@ impl<T: BinaryCombined> AggregateFnVTable for Combined<T> {
Ok(true)
}

/// Hand the definedness to each child, which gives it the child's own semantics. The children
/// keep the parent's input dtype, so their states merge with those of undefined batches.
fn try_accumulate_defined(
&self,
_args: AggregateArgs<'_, Self::Options>,
state: &mut Self::Partial,
batch: &ArrayRef,
defined: &Mask,
ctx: &mut ExecutionCtx,
) -> VortexResult<bool> {
state.0.accumulate_defined(batch, defined, ctx)?;
state.1.accumulate_defined(batch, defined, ctx)?;
Ok(true)
}

fn accumulate(
&self,
_args: AggregateArgs<'_, Self::Options>,
Expand Down
Loading
Loading