Skip to content

Commit 98f903b

Browse files
committed
Move box down, demonstrate specialized functions
Signed-off-by: Moritz Hoffmann <mh@materialize.com>
1 parent 8902fb2 commit 98f903b

File tree

4 files changed

+104
-51
lines changed

4 files changed

+104
-51
lines changed

src/compute/src/compute_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ impl IndexPeek {
13921392
let mut literals = peek.literal_constraints.iter().flatten();
13931393
let mut current_literal = None;
13941394

1395-
let static_mfp = StaticMapFilterProject::from(peek.map_filter_project.clone());
1395+
let static_mfp = StaticMapFilterProject::from(&peek.map_filter_project);
13961396

13971397
while cursor.key_valid(&storage) {
13981398
if has_literal_constraints {

src/expr/src/linear.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,22 +2121,18 @@ pub struct StaticMapFilterProject {
21212121
pub input_arity: usize,
21222122
}
21232123

2124-
impl From<SafeMfpPlan> for StaticMapFilterProject {
2125-
fn from(value: SafeMfpPlan) -> Self {
2126-
value.mfp.into()
2124+
impl From<&SafeMfpPlan> for StaticMapFilterProject {
2125+
fn from(value: &SafeMfpPlan) -> Self {
2126+
(&value.mfp).into()
21272127
}
21282128
}
21292129

2130-
impl From<MapFilterProject> for StaticMapFilterProject {
2131-
fn from(mfp: MapFilterProject) -> Self {
2130+
impl From<&MapFilterProject> for StaticMapFilterProject {
2131+
fn from(mfp: &MapFilterProject) -> Self {
21322132
Self {
2133-
expressions: mfp.expressions.into_iter().map(Into::into).collect(),
2134-
predicates: mfp
2135-
.predicates
2136-
.into_iter()
2137-
.map(|(i, e)| (i, e.into()))
2138-
.collect(),
2139-
projection: mfp.projection,
2133+
expressions: mfp.expressions.iter().map(|e| e.into()).collect(),
2134+
predicates: mfp.predicates.iter().map(|(i, e)| (*i, e.into())).collect(),
2135+
projection: mfp.projection.clone(),
21402136
input_arity: mfp.input_arity,
21412137
}
21422138
}

src/expr/src/scalar/func.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ use sha1::Sha1;
6060
use sha2::{Sha224, Sha256, Sha384, Sha512};
6161
use subtle::ConstantTimeEq;
6262

63-
use crate::static_eval::StaticMirScalarExpr;
6463
use crate::scalar::func::format::DateTimeFormat;
6564
use crate::scalar::{
6665
ProtoBinaryFunc, ProtoUnaryFunc, ProtoUnmaterializableFunc, ProtoVariadicFunc,
6766
};
67+
use crate::static_eval::StaticMirScalarExpr;
6868
use crate::{like_pattern, EvalError, MirScalarExpr};
6969

7070
#[macro_use]

src/expr/src/static_eval.rs

Lines changed: 94 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
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};
1110
use mz_repr::{Datum, Row, RowArena};
1211

12+
use crate::{BinaryFunc, EvalError, MirScalarExpr, UnaryFunc, UnmaterializableFunc, VariadicFunc};
13+
1314
type StaticMSEFn = for<'a> fn(
1415
this: &'a StaticMirScalarExpr,
1516
datums: &[Datum<'a>],
@@ -19,14 +20,16 @@ type StaticMSEFn = for<'a> fn(
1920
#[derive(Debug)]
2021
pub struct StaticMirScalarExpr {
2122
func: StaticMSEFn,
22-
params: StaticMirScalarExprParams,
23+
params: Option<Box<StaticMirScalarExprParams>>,
2324
}
2425

2526
impl 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

4782
fn 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

Comments
 (0)