Skip to content

Commit 27867b3

Browse files
committed
more
Signed-off-by: Robert Kruszewski <github@robertk.io>
1 parent d88df95 commit 27867b3

7 files changed

Lines changed: 94 additions & 102 deletions

File tree

‎vortex-array/src/expr/bound_expression.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ use crate::expr::traversal::TraversalOrder;
2222
use crate::expr::traversal::pre_order_visit_down;
2323
use crate::scalar_fn::ScalarFnRef;
2424
use crate::scalar_fn::ScalarFnVTable;
25-
use crate::stats::rewrite::StatsRewriteCtx;
25+
use crate::stats::rewrite::falsify;
26+
use crate::stats::rewrite::satisfy;
2627

2728
/// An [`Expression`] that has been type-checked against a [`Scope`].
2829
///
@@ -242,12 +243,12 @@ impl BoundExpression {
242243

243244
/// Return an expression that proves this predicate is definitely false from statistics.
244245
pub fn falsify(&self, session: &VortexSession) -> VortexResult<Option<BoundExpression>> {
245-
StatsRewriteCtx::new(session).falsify(self)
246+
falsify(self, session)
246247
}
247248

248249
/// Return an expression that proves this predicate is definitely true from statistics.
249250
pub fn satisfy(&self, session: &VortexSession) -> VortexResult<Option<BoundExpression>> {
250-
StatsRewriteCtx::new(session).satisfy(self)
251+
satisfy(self, session)
251252
}
252253

253254
/// Display the bound expression as a formatted tree structure.

‎vortex-array/src/stats/rewrite.rs‎

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ pub type StatsRewriteRuleRef = Arc<dyn StatsRewriteRule>;
4141
/// `OR`, so every proof returned by an individual rule must be sound on its own.
4242
///
4343
/// `expr` is the full predicate expression whose root scalar function id is
44-
/// [`Self::scalar_fn_id`]. Use [`StatsRewriteCtx`] to resolve dtypes and recursively rewrite child
45-
/// predicates.
44+
/// [`Self::scalar_fn_id`].
4645
pub trait StatsRewriteRule: Debug + Send + Sync + 'static {
4746
/// Returns the scalar function id handled by this rule.
4847
fn scalar_fn_id(&self) -> ScalarFnId;
@@ -57,10 +56,10 @@ pub trait StatsRewriteRule: Debug + Send + Sync + 'static {
5756
fn falsify(
5857
&self,
5958
expr: &BoundExpression,
60-
ctx: &StatsRewriteCtx<'_>,
59+
session: &VortexSession,
6160
) -> VortexResult<Option<BoundExpression>> {
6261
_ = expr;
63-
_ = ctx;
62+
_ = session;
6463
Ok(None)
6564
}
6665

@@ -77,74 +76,62 @@ pub trait StatsRewriteRule: Debug + Send + Sync + 'static {
7776
fn satisfy(
7877
&self,
7978
expr: &BoundExpression,
80-
ctx: &StatsRewriteCtx<'_>,
79+
session: &VortexSession,
8180
) -> VortexResult<Option<BoundExpression>> {
8281
_ = expr;
83-
_ = ctx;
82+
_ = session;
8483
Ok(None)
8584
}
8685
}
8786

88-
/// Context passed to stats rewrite rules.
89-
pub struct StatsRewriteCtx<'a> {
90-
session: &'a VortexSession,
87+
fn ensure_predicate(expr: &BoundExpression) -> VortexResult<()> {
88+
let dtype = expr.dtype();
89+
vortex_ensure!(
90+
matches!(dtype, DType::Bool(_)),
91+
"Stats rewrites require a boolean predicate, got {dtype}",
92+
);
93+
Ok(())
9194
}
9295

93-
impl<'a> StatsRewriteCtx<'a> {
94-
/// Create a rewrite context for `session`.
95-
pub fn new(session: &'a VortexSession) -> Self {
96-
Self { session }
97-
}
98-
99-
/// Returns the session that owns the rewrite registry.
100-
pub fn session(&self) -> &'a VortexSession {
101-
self.session
102-
}
103-
104-
/// Rewrite `expr` into a stats-backed falsifier.
105-
pub fn falsify(&self, expr: &BoundExpression) -> VortexResult<Option<BoundExpression>> {
106-
self.ensure_predicate(expr)?;
107-
rewrite(expr, self, StatsRewriteRule::falsify)
108-
}
109-
110-
/// Rewrite `expr` into a stats-backed satisfier.
111-
pub fn satisfy(&self, expr: &BoundExpression) -> VortexResult<Option<BoundExpression>> {
112-
self.ensure_predicate(expr)?;
113-
rewrite(expr, self, StatsRewriteRule::satisfy)
114-
}
96+
/// Rewrite `expr` into a stats-backed falsifier.
97+
pub fn falsify(
98+
expr: &BoundExpression,
99+
session: &VortexSession,
100+
) -> VortexResult<Option<BoundExpression>> {
101+
ensure_predicate(expr)?;
102+
rewrite(expr, session, StatsRewriteRule::falsify)
103+
}
115104

116-
#[inline]
117-
fn ensure_predicate(&self, expr: &BoundExpression) -> VortexResult<()> {
118-
let dtype = expr.dtype();
119-
vortex_ensure!(
120-
matches!(dtype, DType::Bool(_)),
121-
"Stats rewrites require a boolean predicate, got {dtype}",
122-
);
123-
Ok(())
124-
}
105+
/// Rewrite `expr` into a stats-backed satisfier.
106+
pub fn satisfy(
107+
expr: &BoundExpression,
108+
session: &VortexSession,
109+
) -> VortexResult<Option<BoundExpression>> {
110+
ensure_predicate(expr)?;
111+
rewrite(expr, session, StatsRewriteRule::satisfy)
125112
}
126113

127114
fn rewrite(
128115
expr: &BoundExpression,
129-
ctx: &StatsRewriteCtx<'_>,
116+
session: &VortexSession,
130117
apply: fn(
131118
&dyn StatsRewriteRule,
132119
&BoundExpression,
133-
&StatsRewriteCtx<'_>,
120+
&VortexSession,
134121
) -> VortexResult<Option<BoundExpression>>,
135122
) -> VortexResult<Option<BoundExpression>> {
136123
// The scope alone proves nothing about the rows it contains.
137124
let Some(scalar_fn) = expr.as_scalar() else {
138125
return Ok(None);
139126
};
140-
let rules = ctx.session().stats().rewrite_rules_for(scalar_fn.id());
127+
let rules = session.stats().rewrite_rules_for(scalar_fn.id());
141128
let Some(rules) = rules else {
142129
return Ok(None);
143130
};
144131

145132
let mut rewrites = Vec::new();
146133
for rule in rules.iter() {
147-
if let Some(rewrite) = apply(rule.as_ref(), expr, ctx)? {
134+
if let Some(rewrite) = apply(rule.as_ref(), expr, session)? {
148135
rewrites.push(rewrite);
149136
}
150137
}
@@ -157,8 +144,8 @@ fn rewrite(
157144
#[cfg(test)]
158145
mod tests {
159146
use vortex_error::VortexResult;
147+
use vortex_session::VortexSession;
160148

161-
use super::StatsRewriteCtx;
162149
use super::StatsRewriteRule;
163150
use crate::dtype::DType;
164151
use crate::dtype::Nullability;
@@ -185,15 +172,15 @@ mod tests {
185172
fn falsify(
186173
&self,
187174
_expr: &BoundExpression,
188-
_ctx: &StatsRewriteCtx<'_>,
175+
_session: &VortexSession,
189176
) -> VortexResult<Option<BoundExpression>> {
190177
Ok(self.falsifier.clone())
191178
}
192179

193180
fn satisfy(
194181
&self,
195182
_expr: &BoundExpression,
196-
_ctx: &StatsRewriteCtx<'_>,
183+
_session: &VortexSession,
197184
) -> VortexResult<Option<BoundExpression>> {
198185
Ok(self.satisfier.clone())
199186
}

0 commit comments

Comments
 (0)