MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / memoize_expr

Function memoize_expr

src/expr/src/linear.rs:1443–1479  ·  view source on GitHub ↗

TODO: move this elsewhere? Recursively memoize parts of `expr`, storing those parts in `memoized_parts`. A part of `expr` that is memoized is replaced by a reference to column `(input_arity + pos)`, where `pos` is the position of the memoized part in `memoized_parts`, and `input_arity` is the arity of the input that `expr` refers to.

(
    expr: &mut E,
    memoized_parts: &mut Vec<E>,
    input_arity: usize,
)

Source from the content-addressed store, hash-verified

1441/// `memoized_parts`, and `input_arity` is the arity of the input that `expr`
1442/// refers to.
1443pub fn memoize_expr<E: OptimizableExpr>(
1444 expr: &mut E,
1445 memoized_parts: &mut Vec<E>,
1446 input_arity: usize,
1447) {
1448 expr.visit_mut_pre_post(&mut |e| e.eager_children(), &mut |e| {
1449 if E::is_literal(e) {
1450 // Literals do not need to be memoized.
1451 return;
1452 }
1453 if let Some(col) = e.as_column_mut() {
1454 // Column references do not need to be memoized, but may need to be
1455 // updated if they reference a column reference themselves.
1456 if *col > input_arity {
1457 if let Some(col2) = memoized_parts[*col - input_arity].as_column() {
1458 // Update the column index in place, preserving any name information.
1459 *col = col2;
1460 }
1461 }
1462 return;
1463 }
1464 // TODO: OOO (Optimizer Optimization Opportunity):
1465 // we are quadratic in expression size because of this .iter().position
1466 if let Some(position) = memoized_parts.iter().position(|e2| e2 == e) {
1467 // Any complex expression that already exists as a prior column can
1468 // be replaced by a reference to that column.
1469 *e = E::column(input_arity + position);
1470 } else {
1471 // A complex expression that does not exist should be memoized, and
1472 // replaced by a reference to the column.
1473 memoized_parts.push(std::mem::replace(
1474 e,
1475 E::column(input_arity + memoized_parts.len()),
1476 ));
1477 }
1478 });
1479}
1480
1481pub mod util {
1482 use std::collections::BTreeMap;

Callers 1

memoize_expressionsMethod · 0.85

Calls 9

visit_mut_pre_postMethod · 0.80
eager_childrenMethod · 0.80
as_column_mutMethod · 0.80
as_columnMethod · 0.80
replaceFunction · 0.50
positionMethod · 0.45
iterMethod · 0.45
pushMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected