@@ -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`].
4645pub 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
127114fn 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) ]
158145mod 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