77// the Business Source License, use of this software will be governed
88// by the Apache License, Version 2.0.
99
10- use crate :: { BinaryFunc , EvalError , MirScalarExpr , UnaryFunc , UnmaterializableFunc , VariadicFunc } ;
1110use mz_repr:: { Datum , Row , RowArena } ;
1211
12+ use crate :: { BinaryFunc , EvalError , MirScalarExpr , UnaryFunc , UnmaterializableFunc , VariadicFunc } ;
13+
1314type StaticMSEFn = for <' a > fn (
1415 this : & ' a StaticMirScalarExpr ,
1516 datums : & [ Datum < ' a > ] ,
@@ -19,14 +20,16 @@ type StaticMSEFn = for<'a> fn(
1920#[ derive( Debug ) ]
2021pub struct StaticMirScalarExpr {
2122 func : StaticMSEFn ,
22- params : StaticMirScalarExprParams ,
23+ params : Option < Box < StaticMirScalarExprParams > > ,
2324}
2425
2526impl StaticMirScalarExpr {
27+ #[ inline( always) ]
2628 pub ( crate ) fn params ( & self ) -> & StaticMirScalarExprParams {
27- & self . params
29+ self . params . as_ref ( ) . expect ( "params should exist" )
2830 }
2931
32+ #[ inline( always) ]
3033 pub fn eval < ' a > (
3134 & ' a self ,
3235 datums : & [ Datum < ' a > ] ,
@@ -41,15 +44,47 @@ fn static_column<'a>(
4144 datums : & [ Datum < ' a > ] ,
4245 _temp_storage : & ' a RowArena ,
4346) -> Result < Datum < ' a > , EvalError > {
44- Ok ( datums[ this. params . unwrap_column ( ) ] )
47+ Ok ( datums[ this. params ( ) . unwrap_column ( ) ] )
48+ }
49+
50+ fn static_column_0 < ' a > (
51+ _this : & ' a StaticMirScalarExpr ,
52+ datums : & [ Datum < ' a > ] ,
53+ _temp_storage : & ' a RowArena ,
54+ ) -> Result < Datum < ' a > , EvalError > {
55+ Ok ( datums[ 0 ] )
56+ }
57+
58+ fn static_column_1 < ' a > (
59+ _this : & ' a StaticMirScalarExpr ,
60+ datums : & [ Datum < ' a > ] ,
61+ _temp_storage : & ' a RowArena ,
62+ ) -> Result < Datum < ' a > , EvalError > {
63+ Ok ( datums[ 1 ] )
64+ }
65+
66+ fn static_column_2 < ' a > (
67+ _this : & ' a StaticMirScalarExpr ,
68+ datums : & [ Datum < ' a > ] ,
69+ _temp_storage : & ' a RowArena ,
70+ ) -> Result < Datum < ' a > , EvalError > {
71+ Ok ( datums[ 2 ] )
72+ }
73+
74+ fn static_column_3 < ' a > (
75+ _this : & ' a StaticMirScalarExpr ,
76+ datums : & [ Datum < ' a > ] ,
77+ _temp_storage : & ' a RowArena ,
78+ ) -> Result < Datum < ' a > , EvalError > {
79+ Ok ( datums[ 3 ] )
4580}
4681
4782fn static_literal < ' a > (
4883 this : & ' a StaticMirScalarExpr ,
4984 _datums : & [ Datum < ' a > ] ,
5085 _temp_storage : & ' a RowArena ,
5186) -> Result < Datum < ' a > , EvalError > {
52- let res = this. params . unwrap_literal ( ) ;
87+ let res = this. params ( ) . unwrap_literal ( ) ;
5388 match res {
5489 Ok ( row) => Ok ( row. unpack_first ( ) ) ,
5590 Err ( e) => Err ( e. clone ( ) ) ,
@@ -61,7 +96,7 @@ fn static_call_unmaterializable<'a>(
6196 _datums : & [ Datum < ' a > ] ,
6297 _temp_storage : & ' a RowArena ,
6398) -> Result < Datum < ' a > , EvalError > {
64- let x = this. params . unwrap_call_unmaterializable ( ) ;
99+ let x = this. params ( ) . unwrap_call_unmaterializable ( ) ;
65100 Err ( EvalError :: Internal (
66101 format ! ( "cannot evaluate unmaterializable function: {:?}" , x) . into ( ) ,
67102 ) )
@@ -72,7 +107,7 @@ fn static_call_binary<'a>(
72107 datums : & [ Datum < ' a > ] ,
73108 temp_storage : & ' a RowArena ,
74109) -> Result < Datum < ' a > , EvalError > {
75- let ( func, expr1, expr2) = this. params . unwrap_call_binary ( ) ;
110+ let ( func, expr1, expr2) = this. params ( ) . unwrap_call_binary ( ) ;
76111 let a = expr1. eval ( datums, temp_storage) ?;
77112 let b = expr2. eval ( datums, temp_storage) ?;
78113 func. eval_input ( temp_storage, a, b)
@@ -83,7 +118,7 @@ fn static_call_variadic<'a>(
83118 datums : & [ Datum < ' a > ] ,
84119 temp_storage : & ' a RowArena ,
85120) -> Result < Datum < ' a > , EvalError > {
86- let ( func, exprs) = this. params . unwrap_call_variadic ( ) ;
121+ let ( func, exprs) = this. params ( ) . unwrap_call_variadic ( ) ;
87122 func. eval_static ( datums, temp_storage, exprs)
88123}
89124
@@ -92,7 +127,7 @@ fn static_if<'a>(
92127 datums : & [ Datum < ' a > ] ,
93128 temp_storage : & ' a RowArena ,
94129) -> Result < Datum < ' a > , EvalError > {
95- let ( cond, then, els) = this. params . unwrap_if ( ) ;
130+ let ( cond, then, els) = this. params ( ) . unwrap_if ( ) ;
96131 match cond. eval ( datums, temp_storage) ? {
97132 Datum :: True => then. eval ( datums, temp_storage) ,
98133 Datum :: False | Datum :: Null => els. eval ( datums, temp_storage) ,
@@ -117,13 +152,13 @@ pub(crate) enum StaticMirScalarExprParams {
117152 /// A function call that takes one expression as an argument.
118153 CallUnary {
119154 func : UnaryFunc ,
120- expr : Box < StaticMirScalarExpr > ,
155+ expr : StaticMirScalarExpr ,
121156 } ,
122157 /// A function call that takes two expressions as arguments.
123158 CallBinary {
124159 func : BinaryFunc ,
125- expr1 : Box < StaticMirScalarExpr > ,
126- expr2 : Box < StaticMirScalarExpr > ,
160+ expr1 : StaticMirScalarExpr ,
161+ expr2 : StaticMirScalarExpr ,
127162 } ,
128163 /// A function call that takes an arbitrary number of arguments.
129164 CallVariadic {
@@ -137,9 +172,9 @@ pub(crate) enum StaticMirScalarExprParams {
137172 /// users can guard execution (other logical operator do not
138173 /// short-circuit) and we need to preserve that.
139174 If {
140- cond : Box < StaticMirScalarExpr > ,
141- then : Box < StaticMirScalarExpr > ,
142- els : Box < StaticMirScalarExpr > ,
175+ cond : StaticMirScalarExpr ,
176+ then : StaticMirScalarExpr ,
177+ els : StaticMirScalarExpr ,
143178 } ,
144179}
145180
@@ -200,54 +235,76 @@ impl StaticMirScalarExprParams {
200235 }
201236}
202237
203- impl From < MirScalarExpr > for StaticMirScalarExpr {
204- fn from ( value : MirScalarExpr ) -> Self {
238+ impl From < & MirScalarExpr > for StaticMirScalarExpr {
239+ fn from ( value : & MirScalarExpr ) -> Self {
205240 match value {
241+ MirScalarExpr :: Column ( 0 ) => StaticMirScalarExpr {
242+ func : static_column_0,
243+ params : None ,
244+ } ,
245+ MirScalarExpr :: Column ( 1 ) => StaticMirScalarExpr {
246+ func : static_column_1,
247+ params : None ,
248+ } ,
249+ MirScalarExpr :: Column ( 2 ) => StaticMirScalarExpr {
250+ func : static_column_2,
251+ params : None ,
252+ } ,
253+ MirScalarExpr :: Column ( 3 ) => StaticMirScalarExpr {
254+ func : static_column_3,
255+ params : None ,
256+ } ,
206257 MirScalarExpr :: Column ( col) => StaticMirScalarExpr {
207258 func : static_column,
208- params : StaticMirScalarExprParams :: Column ( col) ,
259+ params : Some ( Box :: new ( StaticMirScalarExprParams :: Column ( * col) ) ) ,
209260 } ,
210261 MirScalarExpr :: Literal ( res, _) => StaticMirScalarExpr {
211262 func : static_literal,
212- params : StaticMirScalarExprParams :: Literal ( res) ,
263+ params : Some ( Box :: new ( StaticMirScalarExprParams :: Literal ( res. clone ( ) ) ) ) ,
213264 } ,
214265 MirScalarExpr :: CallUnmaterializable ( f) => StaticMirScalarExpr {
215266 func : static_call_unmaterializable,
216- params : StaticMirScalarExprParams :: CallUnmaterializable ( f) ,
267+ params : Some ( Box :: new ( StaticMirScalarExprParams :: CallUnmaterializable (
268+ f. clone ( ) ,
269+ ) ) ) ,
217270 } ,
218271 MirScalarExpr :: CallUnary { func, expr } => StaticMirScalarExpr {
219272 func : func. static_fn ( ) ,
220- params : StaticMirScalarExprParams :: CallUnary {
221- func,
222- expr : Box :: new ( ( * expr) . into ( ) ) ,
223- } ,
273+ params : Box :: new ( StaticMirScalarExprParams :: CallUnary {
274+ func : func. clone ( ) ,
275+ expr : expr. as_ref ( ) . into ( ) ,
276+ } )
277+ . into ( ) ,
224278 } ,
225279 MirScalarExpr :: CallBinary { func, expr1, expr2 } => StaticMirScalarExpr {
226280 func : static_call_binary,
227- params : StaticMirScalarExprParams :: CallBinary {
228- func,
229- expr1 : Box :: new ( ( * expr1) . into ( ) ) ,
230- expr2 : Box :: new ( ( * expr2) . into ( ) ) ,
231- } ,
281+ params : Box :: new ( StaticMirScalarExprParams :: CallBinary {
282+ func : func. clone ( ) ,
283+ expr1 : expr1. as_ref ( ) . into ( ) ,
284+ expr2 : expr2. as_ref ( ) . into ( ) ,
285+ } )
286+ . into ( ) ,
232287 } ,
233288 MirScalarExpr :: CallVariadic { func, exprs } => StaticMirScalarExpr {
234289 func : static_call_variadic,
235- params : StaticMirScalarExprParams :: CallVariadic {
236- func,
290+ params : Box :: new ( StaticMirScalarExprParams :: CallVariadic {
291+ func : func . clone ( ) ,
237292 exprs : exprs
238293 . into_iter ( )
239294 . map ( |e| e. into ( ) )
240295 . collect :: < Vec < _ > > ( )
241296 . into_boxed_slice ( ) ,
242- } ,
297+ } )
298+ . into ( ) ,
243299 } ,
244300 MirScalarExpr :: If { cond, then, els } => StaticMirScalarExpr {
245301 func : static_if,
246- params : StaticMirScalarExprParams :: If {
247- cond : Box :: new ( ( * cond) . into ( ) ) ,
248- then : Box :: new ( ( * then) . into ( ) ) ,
249- els : Box :: new ( ( * els) . into ( ) ) ,
250- } ,
302+ params : Box :: new ( StaticMirScalarExprParams :: If {
303+ cond : cond. as_ref ( ) . into ( ) ,
304+ then : then. as_ref ( ) . into ( ) ,
305+ els : els. as_ref ( ) . into ( ) ,
306+ } )
307+ . into ( ) ,
251308 } ,
252309 }
253310 }
0 commit comments