use vortex_array::aggregate_fn::AggregateFnVTableExt;
use vortex_array::aggregate_fn::EmptyOptions as AggregateEmptyOptions;
use vortex_array::aggregate_fn::NumericalAggregateOpts;
use vortex_array::aggregate_fn::fns::all_nan::AllNan;
use vortex_array::aggregate_fn::fns::all_non_nan::AllNonNan;
use vortex_array::aggregate_fn::fns::all_non_null::AllNonNull;
use vortex_array::aggregate_fn::fns::all_null::AllNull;
use vortex_array::aggregate_fn::fns::max::Max;
use vortex_array::aggregate_fn::fns::min::Min;
use vortex_array::aggregate_fn::fns::nan_count::NanCount;
use vortex_array::aggregate_fn::fns::null_count::NullCount;
use vortex_array::dtype::DType;
use vortex_array::scalar_fn::EmptyOptions;
use vortex_array::scalar_fn::builtin::cast::Cast;
use vortex_array::scalar_fn::builtin::literal::Literal;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_session::VortexSession;
use crate::BoundExpression;
use crate::ScalarFnExprExt;
use crate::bound::and;
use crate::bound::and_collect;
use crate::bound::eq;
use crate::bound::lit;
use crate::lambda::row_count::RowCount;
use crate::stats::derive::stat_expr;
pub fn row_count() -> BoundExpression {
RowCount
.try_new_bound_expr(EmptyOptions, [])
.vortex_expect("row-count expressions are always well-typed")
}
pub fn min(expr: &BoundExpression, session: &VortexSession) -> Option<BoundExpression> {
stat_expr(
expr,
&Min.bind(NumericalAggregateOpts::skip_nans()),
session,
)
}
pub fn max(expr: &BoundExpression, session: &VortexSession) -> Option<BoundExpression> {
stat_expr(
expr,
&Max.bind(NumericalAggregateOpts::skip_nans()),
session,
)
}
pub fn null_count(expr: &BoundExpression, session: &VortexSession) -> Option<BoundExpression> {
stat_expr(expr, &NullCount.bind(AggregateEmptyOptions), session)
}
pub fn nan_count(expr: &BoundExpression, session: &VortexSession) -> Option<BoundExpression> {
stat_expr(expr, &NanCount.bind(AggregateEmptyOptions), session)
}
pub fn all_null(expr: &BoundExpression, session: &VortexSession) -> Option<BoundExpression> {
stat_expr(expr, &AllNull.bind(AggregateEmptyOptions), session)
}
pub fn all_non_null(expr: &BoundExpression, session: &VortexSession) -> Option<BoundExpression> {
stat_expr(expr, &AllNonNull.bind(AggregateEmptyOptions), session)
}
pub fn all_nan(expr: &BoundExpression, session: &VortexSession) -> Option<BoundExpression> {
stat_expr(expr, &AllNan.bind(AggregateEmptyOptions), session)
}
pub fn all_non_nan(expr: &BoundExpression, session: &VortexSession) -> Option<BoundExpression> {
stat_expr(expr, &AllNonNan.bind(AggregateEmptyOptions), session)
}
pub enum NanCheck {
NotNeeded,
Check(BoundExpression),
Unavailable,
}
pub trait NonNanProof {
const EMIT_UNGUARDED_REWRITES: bool;
fn check(expr: &BoundExpression, session: &VortexSession) -> VortexResult<NanCheck>;
}
pub struct NanCountProof;
impl NonNanProof for NanCountProof {
const EMIT_UNGUARDED_REWRITES: bool = true;
fn check(expr: &BoundExpression, session: &VortexSession) -> VortexResult<NanCheck> {
non_nan_check(expr, |expr| match nan_count(expr, session) {
Some(nan_count) => NanCheck::Check(eq(nan_count, lit(0u64))),
None => NanCheck::Unavailable,
})
}
}
pub struct AllNonNanProof;
impl NonNanProof for AllNonNanProof {
const EMIT_UNGUARDED_REWRITES: bool = false;
fn check(expr: &BoundExpression, session: &VortexSession) -> VortexResult<NanCheck> {
non_nan_check(expr, |expr| match all_non_nan(expr, session) {
Some(all_non_nan) => NanCheck::Check(all_non_nan),
None => NanCheck::Unavailable,
})
}
}
pub fn non_nan_check(
expr: &BoundExpression,
proof: impl FnOnce(&BoundExpression) -> NanCheck,
) -> VortexResult<NanCheck> {
if let Some(scalar) = expr.as_opt::<Literal>() {
if !scalar.dtype().is_float() {
return Ok(NanCheck::NotNeeded);
}
return Ok(if scalar.as_primitive().is_nan() {
NanCheck::Check(lit(false))
} else {
NanCheck::NotNeeded
});
}
if expr.is::<Cast>() {
if !has_nans(expr.child(0).dtype()) {
return Ok(NanCheck::NotNeeded);
}
return non_nan_check(expr.child(0), proof);
}
if !has_nans(expr.dtype()) {
return Ok(NanCheck::NotNeeded);
}
Ok(proof(expr))
}
fn has_nans(dtype: &DType) -> bool {
dtype.is_float()
}
pub fn with_non_nan_guards<'a, P: NonNanProof>(
exprs: impl IntoIterator<Item = &'a BoundExpression>,
value_predicate: BoundExpression,
session: &VortexSession,
) -> VortexResult<Option<BoundExpression>> {
let mut nan_checks = Vec::new();
for expr in exprs {
match P::check(expr, session)? {
NanCheck::NotNeeded => {}
NanCheck::Check(check) => nan_checks.push(check),
NanCheck::Unavailable => return Ok(None),
}
}
let nan_predicate = and_collect(nan_checks);
Ok(match nan_predicate {
Some(nan_check) => Some(and(nan_check, value_predicate)),
None if P::EMIT_UNGUARDED_REWRITES => Some(value_predicate),
None => None,
})
}