Skip to main content

vortex_expr/stats/
derive.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Session-registered derivations of a scalar function's result statistics from the statistics
5//! of its inputs.
6//!
7//! Stats rewrite rules bound sub-expressions of a predicate, such as the min and max of
8//! `cast(col)` in `cast(col) > 5`. Statistics are stored only for columns, so a statistic of
9//! `cast(col)` must be expressed in terms of the statistics of `col`. A [`StatDerivation`]
10//! registered for a scalar function says how.
11
12use std::fmt::Debug;
13use std::sync::Arc;
14
15use vortex_array::aggregate_fn::AggregateFnRef;
16use vortex_array::expr::BoundExpression;
17use vortex_array::scalar_fn::ScalarFnId;
18use vortex_session::VortexSession;
19
20use crate::stats::bound;
21use crate::stats::session::StatsSessionExt;
22
23/// Shared reference to a statistic derivation.
24pub type StatDerivationRef = Arc<dyn StatDerivation>;
25
26/// Expresses the statistics of a scalar function's result in terms of the statistics of its
27/// inputs.
28///
29/// At most one derivation is registered per scalar function. When one is registered, it decides
30/// every statistic of expressions rooted at that function: stats rewrites never read a statistic
31/// stored for such an expression itself.
32pub trait StatDerivation: Debug + Send + Sync + 'static {
33    /// Returns the scalar function id whose statistics this derivation describes.
34    fn scalar_fn_id(&self) -> ScalarFnId;
35
36    /// Expresses the statistic `aggregate_fn` of `expr`, whose root scalar function id is
37    /// [`Self::scalar_fn_id`], as an expression over statistics of other expressions.
38    ///
39    /// Returns `None` when the statistic cannot be known from statistics.
40    fn derive_stat(
41        &self,
42        expr: &BoundExpression,
43        aggregate_fn: &AggregateFnRef,
44        session: &VortexSession,
45    ) -> Option<BoundExpression>;
46}
47
48/// Expresses the statistic `aggregate_fn` of `expr` as an expression over the statistics that
49/// can be read for the current scope.
50///
51/// If a [`StatDerivation`] is registered for the root scalar function of `expr`, it decides the
52/// statistic. Otherwise this is a reference to `aggregate_fn` of `expr` itself. Returns `None`
53/// when the statistic cannot be known, for example when the aggregate does not support the dtype
54/// of `expr`.
55pub fn stat_expr(
56    expr: &BoundExpression,
57    aggregate_fn: &AggregateFnRef,
58    session: &VortexSession,
59) -> Option<BoundExpression> {
60    if let Some(scalar_fn) = expr.as_scalar()
61        && let Some(derivation) = session.stats().stat_derivation_for(scalar_fn.id())
62    {
63        return derivation.derive_stat(expr, aggregate_fn, session);
64    }
65
66    // The aggregate may not support the expression's dtype, e.g. min/max over structs,
67    // even when the predicate itself is well-typed. Such stats cannot be lowered later,
68    // so do not reference them in the rewrite.
69    aggregate_fn
70        .return_dtype(expr.dtype())
71        .is_some()
72        .then(|| bound::stat(expr.clone(), aggregate_fn.clone()))
73}