|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +//! Session-registered rewrite rules for aggregate-backed stats expressions. |
| 5 | +
|
| 6 | +use std::fmt::Debug; |
| 7 | +use std::sync::Arc; |
| 8 | + |
| 9 | +use vortex_error::VortexResult; |
| 10 | +use vortex_session::VortexSession; |
| 11 | + |
| 12 | +use crate::expr::Expression; |
| 13 | +use crate::expr::or_collect; |
| 14 | +use crate::scalar_fn::ScalarFnId; |
| 15 | +use crate::stats::session::StatsRewriteSessionExt; |
| 16 | + |
| 17 | +/// Shared reference to a stats rewrite rule. |
| 18 | +pub(crate) type StatsRewriteRuleRef = Arc<dyn StatsRewriteRule>; |
| 19 | + |
| 20 | +/// A plugin-provided rule that rewrites predicates into stats-backed proof expressions. |
| 21 | +/// |
| 22 | +/// A falsifier evaluates to `true` only when the original predicate is definitely false for the |
| 23 | +/// current stats scope. A satisfier evaluates to `true` only when the original predicate is |
| 24 | +/// definitely true for the current stats scope. Returning `None` means the rule cannot prove |
| 25 | +/// anything for the expression. |
| 26 | +#[allow(dead_code)] |
| 27 | +pub(crate) trait StatsRewriteRule: Debug + Send + Sync + 'static { |
| 28 | + /// The scalar function ID this rule applies to. |
| 29 | + fn scalar_fn_id(&self) -> ScalarFnId; |
| 30 | + |
| 31 | + /// Rewrite an expression into a stats-backed falsifier. |
| 32 | + fn falsify( |
| 33 | + &self, |
| 34 | + expr: &Expression, |
| 35 | + ctx: &StatsRewriteCtx<'_>, |
| 36 | + ) -> VortexResult<Option<Expression>> { |
| 37 | + _ = expr; |
| 38 | + _ = ctx; |
| 39 | + Ok(None) |
| 40 | + } |
| 41 | + |
| 42 | + /// Rewrite an expression into a stats-backed satisfier. |
| 43 | + fn satisfy( |
| 44 | + &self, |
| 45 | + expr: &Expression, |
| 46 | + ctx: &StatsRewriteCtx<'_>, |
| 47 | + ) -> VortexResult<Option<Expression>> { |
| 48 | + _ = expr; |
| 49 | + _ = ctx; |
| 50 | + Ok(None) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/// Context passed to stats rewrite rules. |
| 55 | +pub(crate) struct StatsRewriteCtx<'a> { |
| 56 | + session: &'a VortexSession, |
| 57 | +} |
| 58 | + |
| 59 | +impl<'a> StatsRewriteCtx<'a> { |
| 60 | + /// Create a rewrite context for `session`. |
| 61 | + pub(crate) fn new(session: &'a VortexSession) -> Self { |
| 62 | + Self { session } |
| 63 | + } |
| 64 | + |
| 65 | + /// Returns the session that owns the rewrite registry. |
| 66 | + pub(crate) fn session(&self) -> &'a VortexSession { |
| 67 | + self.session |
| 68 | + } |
| 69 | + |
| 70 | + /// Rewrite `expr` into a stats-backed falsifier. |
| 71 | + pub(crate) fn falsify(&self, expr: &Expression) -> VortexResult<Option<Expression>> { |
| 72 | + rewrite(expr, self, StatsRewriteRule::falsify) |
| 73 | + } |
| 74 | + |
| 75 | + /// Rewrite `expr` into a stats-backed satisfier. |
| 76 | + pub(crate) fn satisfy(&self, expr: &Expression) -> VortexResult<Option<Expression>> { |
| 77 | + rewrite(expr, self, StatsRewriteRule::satisfy) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +fn rewrite( |
| 82 | + expr: &Expression, |
| 83 | + ctx: &StatsRewriteCtx<'_>, |
| 84 | + apply: fn( |
| 85 | + &dyn StatsRewriteRule, |
| 86 | + &Expression, |
| 87 | + &StatsRewriteCtx<'_>, |
| 88 | + ) -> VortexResult<Option<Expression>>, |
| 89 | +) -> VortexResult<Option<Expression>> { |
| 90 | + let rules = ctx |
| 91 | + .session() |
| 92 | + .stats_rewrites() |
| 93 | + .rules_for(expr.scalar_fn().id()); |
| 94 | + let Some(rules) = rules else { |
| 95 | + return Ok(None); |
| 96 | + }; |
| 97 | + |
| 98 | + let mut rewrites = Vec::new(); |
| 99 | + for rule in rules.iter() { |
| 100 | + if let Some(rewrite) = apply(rule.as_ref(), expr, ctx)? { |
| 101 | + rewrites.push(rewrite); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + Ok(or_collect(rewrites)) |
| 106 | +} |
| 107 | + |
| 108 | +#[cfg(test)] |
| 109 | +mod tests { |
| 110 | + use vortex_error::VortexResult; |
| 111 | + use vortex_session::VortexSession; |
| 112 | + |
| 113 | + use super::StatsRewriteCtx; |
| 114 | + use super::StatsRewriteRule; |
| 115 | + use crate::expr::Expression; |
| 116 | + use crate::expr::lit; |
| 117 | + use crate::expr::or; |
| 118 | + use crate::scalar_fn::ScalarFnId; |
| 119 | + use crate::scalar_fn::ScalarFnVTable; |
| 120 | + use crate::scalar_fn::fns::literal::Literal; |
| 121 | + use crate::stats::session::StatsRewriteSession; |
| 122 | + use crate::stats::session::StatsRewriteSessionExt; |
| 123 | + |
| 124 | + #[derive(Debug)] |
| 125 | + struct StaticLiteralRule { |
| 126 | + falsifier: Option<Expression>, |
| 127 | + satisfier: Option<Expression>, |
| 128 | + } |
| 129 | + |
| 130 | + impl StatsRewriteRule for StaticLiteralRule { |
| 131 | + fn scalar_fn_id(&self) -> ScalarFnId { |
| 132 | + Literal.id() |
| 133 | + } |
| 134 | + |
| 135 | + fn falsify( |
| 136 | + &self, |
| 137 | + _expr: &Expression, |
| 138 | + _ctx: &StatsRewriteCtx<'_>, |
| 139 | + ) -> VortexResult<Option<Expression>> { |
| 140 | + Ok(self.falsifier.clone()) |
| 141 | + } |
| 142 | + |
| 143 | + fn satisfy( |
| 144 | + &self, |
| 145 | + _expr: &Expression, |
| 146 | + _ctx: &StatsRewriteCtx<'_>, |
| 147 | + ) -> VortexResult<Option<Expression>> { |
| 148 | + Ok(self.satisfier.clone()) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + #[test] |
| 153 | + fn combines_multiple_falsifiers_with_or() -> VortexResult<()> { |
| 154 | + let session = VortexSession::empty().with::<StatsRewriteSession>(); |
| 155 | + session.stats_rewrites().register(StaticLiteralRule { |
| 156 | + falsifier: Some(lit(false)), |
| 157 | + satisfier: None, |
| 158 | + }); |
| 159 | + session.stats_rewrites().register(StaticLiteralRule { |
| 160 | + falsifier: Some(lit(true)), |
| 161 | + satisfier: None, |
| 162 | + }); |
| 163 | + |
| 164 | + assert_eq!(lit(7).falsify(&session)?, Some(or(lit(false), lit(true)))); |
| 165 | + Ok(()) |
| 166 | + } |
| 167 | + |
| 168 | + #[test] |
| 169 | + fn combines_multiple_satisfiers_with_or() -> VortexResult<()> { |
| 170 | + let session = VortexSession::empty().with::<StatsRewriteSession>(); |
| 171 | + session.stats_rewrites().register(StaticLiteralRule { |
| 172 | + falsifier: None, |
| 173 | + satisfier: Some(lit(false)), |
| 174 | + }); |
| 175 | + session.stats_rewrites().register(StaticLiteralRule { |
| 176 | + falsifier: None, |
| 177 | + satisfier: Some(lit(true)), |
| 178 | + }); |
| 179 | + |
| 180 | + assert_eq!(lit(7).satisfy(&session)?, Some(or(lit(false), lit(true)))); |
| 181 | + Ok(()) |
| 182 | + } |
| 183 | + |
| 184 | + #[test] |
| 185 | + fn unregistered_expression_has_no_rewrite() -> VortexResult<()> { |
| 186 | + let session = VortexSession::empty().with::<StatsRewriteSession>(); |
| 187 | + |
| 188 | + assert_eq!(lit(7).falsify(&session)?, None); |
| 189 | + assert_eq!(lit(7).satisfy(&session)?, None); |
| 190 | + Ok(()) |
| 191 | + } |
| 192 | +} |
0 commit comments