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

Method derive

src/transform/src/analysis/equivalences.rs:48–276  ·  view source on GitHub ↗
(
        &self,
        expr: &MirRelationExpr,
        index: usize,
        results: &[Self::Value],
        depends: &Derived,
    )

Source from the content-addressed store, hash-verified

46 }
47
48 fn derive(
49 &self,
50 expr: &MirRelationExpr,
51 index: usize,
52 results: &[Self::Value],
53 depends: &Derived,
54 ) -> Self::Value {
55 let mut equivalences = match expr {
56 MirRelationExpr::Constant { rows, typ } => {
57 // Trawl `rows` for any constant information worth recording.
58 // Literal columns may be valuable; non-nullability could be too.
59 let mut equivalences = EquivalenceClasses::default();
60 if let Ok([(row, _cnt), rows @ ..]) = rows.as_deref() {
61 // Vector of `Option<Datum>` which becomes `None` once a column has a second datum.
62 let len = row.iter().count();
63 let mut common = Vec::with_capacity(len);
64 common.extend(row.iter().map(Some));
65 // Prep initial nullability information.
66 let mut nullable_cols = common
67 .iter()
68 .map(|datum| datum == &Some(Datum::Null))
69 .collect::<Vec<_>>();
70
71 for (row, _cnt) in rows.iter() {
72 for ((datum, common), nullable) in row
73 .iter()
74 .zip_eq(common.iter_mut())
75 .zip_eq(nullable_cols.iter_mut())
76 {
77 if Some(datum) != *common {
78 *common = None;
79 }
80 if datum == Datum::Null {
81 *nullable = true;
82 }
83 }
84 }
85 for (index, common) in common.into_iter().enumerate() {
86 if let Some(datum) = common {
87 equivalences.classes.push(vec![
88 MirScalarExpr::column(index),
89 MirScalarExpr::literal_ok(
90 datum,
91 typ.column_types[index].scalar_type.clone(),
92 ),
93 ]);
94 }
95 }
96 // If any columns are non-null, introduce this fact.
97 if nullable_cols.iter().any(|x| !*x) {
98 let mut class = vec![MirScalarExpr::literal_false()];
99 for (index, nullable) in nullable_cols.iter().enumerate() {
100 if !*nullable {
101 class.push(MirScalarExpr::column(index).call_is_null());
102 }
103 }
104 equivalences.classes.push(class);
105 }

Callers

nothing calls this directly

Calls 15

aggregate_is_inputFunction · 0.85
enumerateMethod · 0.80
anyMethod · 0.80
bindingsMethod · 0.80
unwrapMethod · 0.80
children_of_revMethod · 0.80
childrenMethod · 0.65
countMethod · 0.45
iterMethod · 0.45
extendMethod · 0.45
mapMethod · 0.45
into_iterMethod · 0.45

Tested by

no test coverage detected