vortex_expr/
analysis.rs

1use vortex_array::stats::Stat;
2
3use crate::{AccessPath, ExprRef};
4
5pub trait StatsCatalog {
6    /// Given an id, field and stat return an expression that when evaluated will return that stat
7    /// this would be a column reference or a literal value, if the value is known at planning time.
8    fn stats_ref(&mut self, _access_path: &AccessPath, _stat: Stat) -> Option<ExprRef> {
9        None
10    }
11}
12
13/// This can be used by expression to plug into vortex expression analysis, such as
14/// pruning or expression simplification
15pub trait AnalysisExpr {
16    /// An expression over zone-statistics which implies all records in the zone evaluate to false.
17    ///
18    /// Given an expression, `e`, if `e.stat_falsification(..)` evaluates to true, it is guaranteed
19    /// that `e` evaluates to false on all records in the zone. However, the inverse is not
20    /// necessarily true: even if the falsification evaluates to false, `e` need not evaluate to
21    /// true on all records.
22    ///
23    /// The `StatsCatalog` can be used to constrain or rename stats used in the final expr.
24    ///
25    /// # Examples
26    ///
27    /// - An expression over one variable: `x > 0` is false for all records in a zone if the maximum
28    ///   value of the column `x` in that zone is less than or equal to zero: `max(x) <= 0`.
29    /// - An expression over two variables: `x > y` becomes `max(x) <= min(y)`.
30    /// - A conjunctive expression: `x > y AND z < x` becomes `max(x) <= min(y) OR min(z) >= max(x).
31    ///
32    /// Some expressions, in theory, have falsifications but this function does not support them
33    /// such as `x < (y < z)` or `x LIKE "needle%"`.
34    fn stat_falsification(&self, _catalog: &mut dyn StatsCatalog) -> Option<ExprRef> {
35        None
36    }
37
38    /// An expression for the upper non-null bound of this expression, if available.
39    ///
40    /// This function returns None if there is no upper bound or it is difficult to compute.
41    ///
42    /// The returned expression evaluates to null if the maximum value is unknown. In that case, you
43    /// _must not_ assume the array is empty _nor_ may you assume the array only contains non-null
44    /// values.
45    fn max(&self, _catalog: &mut dyn StatsCatalog) -> Option<ExprRef> {
46        None
47    }
48
49    /// An expression for the lower non-null bound of this expression, if available.
50    ///
51    /// See [AnalysisExpr::max] for important details.
52    fn min(&self, _catalog: &mut dyn StatsCatalog) -> Option<ExprRef> {
53        None
54    }
55
56    fn field_path(&self) -> Option<AccessPath> {
57        None
58    }
59
60    // TODO: add containment
61}