@@ -7,6 +7,9 @@ mod operations;
77mod validity;
88mod visitor;
99
10+ use std:: marker:: PhantomData ;
11+ use std:: ops:: Deref ;
12+
1013use itertools:: Itertools ;
1114use vortex_buffer:: BufferHandle ;
1215use vortex_dtype:: DType ;
@@ -24,6 +27,8 @@ use crate::arrays::scalar_fn::metadata::ScalarFnMetadata;
2427use crate :: execution:: ExecutionCtx ;
2528use crate :: expr:: functions;
2629use crate :: expr:: functions:: scalar:: ScalarFn ;
30+ use crate :: optimizer:: rules:: MatchKey ;
31+ use crate :: optimizer:: rules:: Matcher ;
2732use crate :: serde:: ArrayChildren ;
2833use crate :: vtable;
2934use crate :: vtable:: ArrayId ;
@@ -168,3 +173,75 @@ pub trait ScalarFnArrayExt: functions::VTable {
168173 }
169174}
170175impl < V : functions:: VTable > ScalarFnArrayExt for V { }
176+
177+ /// A matcher that matches any scalar function expression.
178+ #[ derive( Debug ) ]
179+ pub struct AnyScalarFn ;
180+ impl Matcher for AnyScalarFn {
181+ type View < ' a > = & ' a ScalarFnArray ;
182+
183+ fn key ( & self ) -> MatchKey {
184+ MatchKey :: Any
185+ }
186+
187+ fn try_match < ' a > ( & self , array : & ' a ArrayRef ) -> Option < Self :: View < ' a > > {
188+ array. as_opt :: < ScalarFnVTable > ( )
189+ }
190+ }
191+
192+ /// A matcher that matches a specific scalar function expression.
193+ #[ derive( Debug ) ]
194+ pub struct ExactScalarFn < F : functions:: VTable > {
195+ id : ArrayId ,
196+ _phantom : PhantomData < F > ,
197+ }
198+
199+ impl < F : functions:: VTable > From < & ' static F > for ExactScalarFn < F > {
200+ fn from ( value : & ' static F ) -> Self {
201+ Self {
202+ id : value. id ( ) ,
203+ _phantom : PhantomData ,
204+ }
205+ }
206+ }
207+
208+ impl < F : functions:: VTable > Matcher for ExactScalarFn < F > {
209+ type View < ' a > = ScalarFnArrayView < ' a , F > ;
210+
211+ fn key ( & self ) -> MatchKey {
212+ MatchKey :: Array ( self . id . clone ( ) )
213+ }
214+
215+ fn try_match < ' a > ( & self , array : & ' a ArrayRef ) -> Option < Self :: View < ' a > > {
216+ let scalar_fn_array = array. as_opt :: < ScalarFnVTable > ( ) ?;
217+ let scalar_fn_vtable = scalar_fn_array
218+ . scalar_fn
219+ . vtable ( )
220+ . as_any ( )
221+ . downcast_ref :: < F > ( ) ?;
222+ let scalar_fn_options = scalar_fn_array
223+ . scalar_fn
224+ . options ( )
225+ . as_any ( )
226+ . downcast_ref :: < F :: Options > ( ) ?;
227+ Some ( ScalarFnArrayView {
228+ array,
229+ vtable : scalar_fn_vtable,
230+ options : scalar_fn_options,
231+ } )
232+ }
233+ }
234+
235+ pub struct ScalarFnArrayView < ' a , F : functions:: VTable > {
236+ array : & ' a ArrayRef ,
237+ pub vtable : & ' a F ,
238+ pub options : & ' a F :: Options ,
239+ }
240+
241+ impl < F : functions:: VTable > Deref for ScalarFnArrayView < ' _ , F > {
242+ type Target = ArrayRef ;
243+
244+ fn deref ( & self ) -> & Self :: Target {
245+ self . array
246+ }
247+ }
0 commit comments