mz_expr/relation/
canonicalize.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Utility functions to transform parts of a single `MirRelationExpr`
11//! into canonical form.
12
13use std::cmp::Ordering;
14use std::collections::{BTreeMap, BTreeSet};
15use std::rc::Rc;
16
17use itertools::Itertools;
18use mz_ore::soft_assert_or_log;
19use mz_repr::{SqlColumnType, SqlScalarType};
20
21use crate::visit::Visit;
22use crate::{MirScalarExpr, UnaryFunc, VariadicFunc, func};
23
24/// Canonicalize equivalence classes of a join and expressions contained in them.
25///
26/// `input_types` can be the [SqlColumnType]s of the join or the [SqlColumnType]s of
27/// the individual inputs of the join in order.
28///
29/// This function:
30/// * simplifies expressions to involve the least number of non-literal nodes.
31///   This ensures that we only replace expressions by "even simpler"
32///   expressions and that repeated substitutions reduce the complexity of
33///   expressions and a fixed point is certain to be reached. Without this
34///   rule, we might repeatedly replace a simple expression with an equivalent
35///   complex expression containing that (or another replaceable) simple
36///   expression, and repeat indefinitely.
37/// * reduces all expressions contained in `equivalences`.
38/// * Does everything that [canonicalize_equivalence_classes] does.
39pub fn canonicalize_equivalences<'a, I>(
40    equivalences: &mut Vec<Vec<MirScalarExpr>>,
41    input_column_types: I,
42) where
43    I: Iterator<Item = &'a Vec<SqlColumnType>>,
44{
45    let column_types = input_column_types
46        .flat_map(|f| f.clone())
47        .collect::<Vec<_>>();
48    // Calculate the number of non-leaves for each expression.
49    let mut to_reduce = equivalences
50        .drain(..)
51        .filter_map(|mut cls| {
52            let mut result = cls
53                .drain(..)
54                .map(|expr| (rank_complexity(&expr), expr))
55                .collect::<Vec<_>>();
56            result.sort();
57            result.dedup();
58            if result.len() > 1 { Some(result) } else { None }
59        })
60        .collect::<Vec<_>>();
61
62    let mut expressions_rewritten = true;
63    while expressions_rewritten {
64        expressions_rewritten = false;
65        for i in 0..to_reduce.len() {
66            // `to_reduce` will be borrowed as immutable, so in order to modify
67            // elements of `to_reduce[i]`, we are going to pop them out of
68            // `to_reduce[i]` and put the modified version in `new_equivalence`,
69            // which will then replace `to_reduce[i]`.
70            let mut new_equivalence = Vec::with_capacity(to_reduce[i].len());
71            while let Some((_, mut popped_expr)) = to_reduce[i].pop() {
72                #[allow(deprecated)]
73                popped_expr.visit_mut_post_nolimit(&mut |e: &mut MirScalarExpr| {
74                    // If a simpler expression can be found that is equivalent
75                    // to e,
76                    if let Some(simpler_e) = to_reduce.iter().find_map(|cls| {
77                        if cls.iter().skip(1).position(|(_, expr)| e == expr).is_some() {
78                            Some(cls[0].1.clone())
79                        } else {
80                            None
81                        }
82                    }) {
83                        // Replace e with the simpler expression.
84                        *e = simpler_e;
85                        expressions_rewritten = true;
86                    }
87                });
88                popped_expr.reduce(&column_types);
89                new_equivalence.push((rank_complexity(&popped_expr), popped_expr));
90            }
91            new_equivalence.sort();
92            new_equivalence.dedup();
93            to_reduce[i] = new_equivalence;
94        }
95    }
96
97    // Map away the complexity rating.
98    *equivalences = to_reduce
99        .drain(..)
100        .map(|mut cls| cls.drain(..).map(|(_, expr)| expr).collect::<Vec<_>>())
101        .collect::<Vec<_>>();
102
103    canonicalize_equivalence_classes(equivalences);
104}
105
106/// Canonicalize only the equivalence classes of a join.
107///
108/// This function:
109/// * ensures the same expression appears in only one equivalence class.
110/// * ensures the equivalence classes are sorted and dedupped.
111/// ```rust
112/// use mz_expr::MirScalarExpr;
113/// use mz_expr::canonicalize::canonicalize_equivalence_classes;
114///
115/// let mut equivalences = vec![
116///     vec![MirScalarExpr::column(1), MirScalarExpr::column(4)],
117///     vec![MirScalarExpr::column(3), MirScalarExpr::column(5)],
118///     vec![MirScalarExpr::column(0), MirScalarExpr::column(3)],
119///     vec![MirScalarExpr::column(2), MirScalarExpr::column(2)],
120/// ];
121/// let expected = vec![
122///     vec![MirScalarExpr::column(0),
123///         MirScalarExpr::column(3),
124///         MirScalarExpr::column(5)],
125///     vec![MirScalarExpr::column(1), MirScalarExpr::column(4)],
126/// ];
127/// canonicalize_equivalence_classes(&mut equivalences);
128/// assert_eq!(expected, equivalences)
129/// ````
130pub fn canonicalize_equivalence_classes(equivalences: &mut Vec<Vec<MirScalarExpr>>) {
131    let mut uf = BTreeMap::new();
132    for class in equivalences.iter_mut() {
133        let mut iter = class.drain(..);
134        if let Some(first) = iter.next() {
135            let head = Rc::new(first);
136            for rest in iter {
137                uf.union(&head, &Rc::new(rest));
138            }
139        }
140    }
141
142    let mut eqs: BTreeMap<Rc<MirScalarExpr>, BTreeSet<Rc<MirScalarExpr>>> = BTreeMap::new();
143    for (k, v) in uf {
144        eqs.entry(v).or_default().insert(Rc::clone(&k));
145    }
146
147    let classes = eqs.into_values().collect::<Vec<_>>();
148    equivalences.resize(classes.len(), Vec::new());
149    equivalences
150        .iter_mut()
151        .zip_eq(classes)
152        .for_each(|(equivalence, class)| {
153            equivalence.extend(
154                class
155                    .into_iter()
156                    .map(|e| Rc::try_unwrap(e).expect("there to be only one strong ref")),
157            );
158        });
159
160    equivalences.retain(|es| es.len() > 1);
161    equivalences.sort();
162}
163
164/// Gives a relative complexity ranking for an expression. Higher numbers mean
165/// greater complexity.
166///
167/// Currently, this method weighs literals as the least complex and weighs all
168/// other expressions by the number of non-literals. In the future, we can
169/// change how complexity is ranked so that repeated substitutions would result
170/// in arriving at "better" fixed points. For example, we could try to improve
171/// performance by ranking expressions by their estimated computation time.
172///
173/// To ensure we arrive at a fixed point after repeated substitutions, valid
174/// complexity rankings must fulfill the following property:
175/// For any expression `e`, there does not exist a SQL function `f` such
176/// that `complexity(e) >= complexity(f(e))`.
177///
178/// For ease of intuiting the fixed point that we will arrive at after
179/// repeated substitutions, it is nice but not required that complexity
180/// rankings additionally fulfill the following property:
181/// If expressions `e1` and `e2` are such that
182/// `complexity(e1) < complexity(e2)` then for all SQL functions `f`,
183/// `complexity(f(e1)) < complexity(f(e2))`.
184fn rank_complexity(expr: &MirScalarExpr) -> usize {
185    if expr.is_literal() {
186        // literals are the least complex
187        return 0;
188    }
189    let mut non_literal_count = 1;
190    expr.visit_pre(|e| {
191        if !e.is_literal() {
192            non_literal_count += 1
193        }
194    });
195    non_literal_count
196}
197
198/// Applies a flat_map on a Vec, and overwrites the vec with the result.
199fn flat_map_modify<T, I, F>(v: &mut Vec<T>, f: F)
200where
201    F: FnMut(T) -> I,
202    I: IntoIterator<Item = T>,
203{
204    let mut xx = v.drain(..).flat_map(f).collect();
205    v.append(&mut xx);
206}
207
208/// Canonicalize predicates of a filter.
209///
210/// This function reduces and canonicalizes the structure of each individual
211/// predicate. Then, it transforms predicates of the form "A and B" into two: "A"
212/// and "B". Afterwards, it reduces predicates based on information from other
213/// predicates in the set. Finally, it sorts and deduplicates the predicates.
214///
215/// Additionally, it also removes IS NOT NULL predicates if there is another
216/// null rejecting predicate for the same sub-expression.
217pub fn canonicalize_predicates(
218    predicates: &mut Vec<MirScalarExpr>,
219    column_types: &[SqlColumnType],
220) {
221    soft_assert_or_log!(
222        predicates
223            .iter()
224            .all(|p| p.typ(column_types).scalar_type == SqlScalarType::Bool),
225        "cannot canonicalize predicates that are not of type bool"
226    );
227
228    // 1) Reduce each individual predicate.
229    predicates.iter_mut().for_each(|p| p.reduce(column_types));
230
231    // 2) Split "A and B" into two predicates: "A" and "B"
232    // Relies on the `reduce` above having flattened nested ANDs.
233    flat_map_modify(predicates, |p| {
234        if let MirScalarExpr::CallVariadic {
235            func: VariadicFunc::And,
236            exprs,
237        } = p
238        {
239            exprs
240        } else {
241            vec![p]
242        }
243    });
244
245    // 3) Make non-null requirements explicit as predicates in order for
246    // step 4) to be able to simplify AND/OR expressions with IS NULL
247    // sub-predicates. This redundancy is removed later by step 5).
248    let mut non_null_columns = BTreeSet::new();
249    for p in predicates.iter() {
250        p.non_null_requirements(&mut non_null_columns);
251    }
252    predicates.extend(non_null_columns.iter().map(|c| {
253        MirScalarExpr::column(*c)
254            .call_unary(UnaryFunc::IsNull(func::IsNull))
255            .call_unary(UnaryFunc::Not(func::Not))
256    }));
257
258    // 4) Reduce across `predicates`.
259    // If a predicate `p` cannot be null, and `f(p)` is a nullable bool
260    // then the predicate `p & f(p)` is equal to `p & f(true)`, and
261    // `!p & f(p)` is equal to `!p & f(false)`. For any index i, the `Vec` of
262    // predicates `[p1, ... pi, ... pn]` is equivalent to the single predicate
263    // `pi & (p1 & ... & p(i-1) & p(i+1) ... & pn)`. Thus, if `pi`
264    // (resp. `!pi`) cannot be null, it is valid to replace with `true` (resp.
265    // `false`) every subexpression in `(p1 & ... & p(i-1) & p(i+1) ... & pn)`
266    // that is equal to `pi`.
267
268    // If `p` is null and `q` is a nullable bool, then `p & q` can be either
269    // `null` or `false` depending on what `q`. Our rendering pipeline treats
270    // both as "remove this row." Thus, in the specific context of filter
271    // predicates, it is acceptable to make the aforementioned substitution
272    // even if `pi` can be null.
273
274    // Note that this does some dedupping of predicates since if `p1 = p2`
275    // then this reduction process will replace `p1` with true.
276
277    // Maintain respectively:
278    // 1) A list of predicates for which we have checked for matching
279    // subexpressions
280    // 2) A list of predicates for which we have yet to do so.
281    let mut completed = Vec::new();
282    let mut todo = Vec::new();
283    // Seed `todo` with all predicates.
284    std::mem::swap(&mut todo, predicates);
285
286    while let Some(predicate_to_apply) = todo.pop() {
287        // Helper method: for each predicate `p`, see if all other predicates
288        // (a.k.a. the union of todo & completed) contains `p` as a
289        // subexpression, and replace the subexpression accordingly.
290        // This method lives inside the loop because in order to comply with
291        // Rust rules that only one mutable reference to `todo` can be held at a
292        // time.
293        let mut replace_subexpr_other_predicates =
294            |expr: &MirScalarExpr, constant_bool: &MirScalarExpr| {
295                // Do not replace subexpressions equal to `expr` if `expr` is a
296                // literal to avoid infinite looping.
297                if !expr.is_literal() {
298                    for other_predicate in todo.iter_mut() {
299                        replace_subexpr_and_reduce(
300                            other_predicate,
301                            expr,
302                            constant_bool,
303                            column_types,
304                        );
305                    }
306                    for other_idx in (0..completed.len()).rev() {
307                        if replace_subexpr_and_reduce(
308                            &mut completed[other_idx],
309                            expr,
310                            constant_bool,
311                            column_types,
312                        ) {
313                            // If a predicate in the `completed` list has
314                            // been simplified, stick it back into the `todo` list.
315                            todo.push(completed.remove(other_idx));
316                        }
317                    }
318                }
319            };
320        // Meat of loop starts here. If a predicate p is of the form `!q`, replace
321        // every instance of `q` in every other predicate with `false.`
322        // Otherwise, replace every instance of `p` in every other predicate
323        // with `true`.
324        if let MirScalarExpr::CallUnary {
325            func: UnaryFunc::Not(func::Not),
326            expr,
327        } = &predicate_to_apply
328        {
329            replace_subexpr_other_predicates(expr, &MirScalarExpr::literal_false())
330        } else {
331            replace_subexpr_other_predicates(&predicate_to_apply, &MirScalarExpr::literal_true());
332        }
333        completed.push(predicate_to_apply);
334    }
335
336    // 5) Remove redundant !isnull/isnull predicates after performing the replacements
337    // in the loop above.
338    std::mem::swap(&mut todo, &mut completed);
339    while let Some(predicate_to_apply) = todo.pop() {
340        // Remove redundant !isnull(x) predicates if there is another predicate
341        // that evaluates to NULL when `x` is NULL.
342        if let Some(operand) = is_not_null(&predicate_to_apply) {
343            if todo
344                .iter_mut()
345                .chain(completed.iter_mut())
346                .any(|p| is_null_rejecting_predicate(p, &operand))
347            {
348                // skip this predicate
349                continue;
350            }
351        } else if let MirScalarExpr::CallUnary {
352            func: UnaryFunc::IsNull(func::IsNull),
353            expr,
354        } = &predicate_to_apply
355        {
356            if todo
357                .iter_mut()
358                .chain(completed.iter_mut())
359                .any(|p| is_null_rejecting_predicate(p, expr))
360            {
361                completed.push(MirScalarExpr::literal_false());
362                break;
363            }
364        }
365        completed.push(predicate_to_apply);
366    }
367
368    if completed.iter().any(|p| {
369        (p.is_literal_false() || p.is_literal_null()) &&
370        // This extra check is only needed if we determine that the soft-assert
371        // at the top of this function would ever fail for a good reason.
372        p.typ(column_types).scalar_type == SqlScalarType::Bool
373    }) {
374        // all rows get filtered away if any predicate is null or false.
375        *predicates = vec![MirScalarExpr::literal_false()]
376    } else {
377        // Remove any predicates that have been reduced to "true"
378        completed.retain(|p| !p.is_literal_true());
379        *predicates = completed;
380    }
381
382    // 6) Sort and dedup predicates.
383    predicates.sort_by(compare_predicates);
384    predicates.dedup();
385}
386
387/// Replace any matching subexpressions in `predicate`, and if `predicate` has
388/// changed, reduce it. Return whether `predicate` has changed.
389fn replace_subexpr_and_reduce(
390    predicate: &mut MirScalarExpr,
391    replace_if_equal_to: &MirScalarExpr,
392    replace_with: &MirScalarExpr,
393    column_types: &[SqlColumnType],
394) -> bool {
395    let mut changed = false;
396    #[allow(deprecated)]
397    predicate.visit_mut_pre_post_nolimit(
398        &mut |e| {
399            // The `cond` of an if statement is not visited to prevent `then`
400            // or `els` from being evaluated before `cond`, resulting in a
401            // correctness error.
402            if let MirScalarExpr::If { then, els, .. } = e {
403                return Some(vec![then, els]);
404            }
405            None
406        },
407        &mut |e| {
408            if e == replace_if_equal_to {
409                *e = replace_with.clone();
410                changed = true;
411            } else if let MirScalarExpr::CallBinary {
412                func: r_func,
413                expr1: r_expr1,
414                expr2: r_expr2,
415            } = replace_if_equal_to
416            {
417                if let Some(negation) = r_func.negate() {
418                    if let MirScalarExpr::CallBinary {
419                        func: l_func,
420                        expr1: l_expr1,
421                        expr2: l_expr2,
422                    } = e
423                    {
424                        if negation == *l_func && l_expr1 == r_expr1 && l_expr2 == r_expr2 {
425                            *e = MirScalarExpr::CallUnary {
426                                func: UnaryFunc::Not(func::Not),
427                                expr: Box::new(replace_with.clone()),
428                            };
429                            changed = true;
430                        }
431                    }
432                }
433            }
434        },
435    );
436    if changed {
437        predicate.reduce(column_types);
438    }
439    changed
440}
441
442/// Returns the inner operand if the given predicate is an IS NOT NULL expression.
443fn is_not_null(predicate: &MirScalarExpr) -> Option<MirScalarExpr> {
444    if let MirScalarExpr::CallUnary {
445        func: UnaryFunc::Not(func::Not),
446        expr,
447    } = &predicate
448    {
449        if let MirScalarExpr::CallUnary {
450            func: UnaryFunc::IsNull(func::IsNull),
451            expr,
452        } = &**expr
453        {
454            return Some((**expr).clone());
455        }
456    }
457    None
458}
459
460/// Whether the given predicate evaluates to NULL when the given operand expression is NULL.
461#[inline(always)]
462fn is_null_rejecting_predicate(predicate: &MirScalarExpr, operand: &MirScalarExpr) -> bool {
463    propagates_null_from_subexpression(predicate, operand)
464}
465
466fn propagates_null_from_subexpression(expr: &MirScalarExpr, operand: &MirScalarExpr) -> bool {
467    if operand == expr {
468        true
469    } else if let MirScalarExpr::CallVariadic { func, exprs } = &expr {
470        func.propagates_nulls()
471            && (exprs
472                .iter()
473                .any(|e| propagates_null_from_subexpression(e, operand)))
474    } else if let MirScalarExpr::CallBinary { func, expr1, expr2 } = &expr {
475        func.propagates_nulls()
476            && (propagates_null_from_subexpression(expr1, operand)
477                || propagates_null_from_subexpression(expr2, operand))
478    } else if let MirScalarExpr::CallUnary { func, expr } = &expr {
479        func.propagates_nulls() && propagates_null_from_subexpression(expr, operand)
480    } else {
481        false
482    }
483}
484
485/// Comparison method for sorting predicates by their complexity, measured by the total
486/// number of non-literal expression nodes within the expression.
487fn compare_predicates(x: &MirScalarExpr, y: &MirScalarExpr) -> Ordering {
488    (rank_complexity(x), x).cmp(&(rank_complexity(y), y))
489}
490
491/// For each equivalence class, it finds the simplest expression, which will be the canonical one.
492/// Returns a Map that maps from each expression in each equivalence class to the canonical
493/// expression in the same equivalence class.
494pub fn get_canonicalizer_map(
495    equivalences: &Vec<Vec<MirScalarExpr>>,
496) -> BTreeMap<MirScalarExpr, MirScalarExpr> {
497    let mut canonicalizer_map = BTreeMap::new();
498    for equivalence in equivalences {
499        // The unwrap is ok, because a join equivalence class can't be empty.
500        let canonical_expr = equivalence
501            .iter()
502            .min_by(|a, b| compare_predicates(*a, *b))
503            .unwrap();
504        for e in equivalence {
505            if e != canonical_expr {
506                canonicalizer_map.insert(e.clone(), canonical_expr.clone());
507            }
508        }
509    }
510    canonicalizer_map
511}
512
513/// A trait for a union-find data structure.
514pub trait UnionFind<T> {
515    /// Sets `self[x]` to the root from `x`, and returns a reference to the root.
516    fn find<'a>(&'a mut self, x: &T) -> Option<&'a T>;
517    /// Ensures that `x` and `y` have the same root.
518    fn union(&mut self, x: &T, y: &T);
519}
520
521impl<T: Clone + Ord> UnionFind<T> for BTreeMap<T, T> {
522    fn find<'a>(&'a mut self, x: &T) -> Option<&'a T> {
523        if !self.contains_key(x) {
524            None
525        } else {
526            if self[x] != self[&self[x]] {
527                // Path halving
528                let mut y = self[x].clone();
529                while y != self[&y] {
530                    let grandparent = self[&self[&y]].clone();
531                    *self.get_mut(&y).unwrap() = grandparent;
532                    y.clone_from(&self[&y]);
533                }
534                *self.get_mut(x).unwrap() = y;
535            }
536            Some(&self[x])
537        }
538    }
539
540    fn union(&mut self, x: &T, y: &T) {
541        match (self.find(x).is_some(), self.find(y).is_some()) {
542            (true, true) => {
543                if self[x] != self[y] {
544                    let root_x = self[x].clone();
545                    let root_y = self[y].clone();
546                    self.insert(root_x, root_y);
547                }
548            }
549            (false, true) => {
550                self.insert(x.clone(), self[y].clone());
551            }
552            (true, false) => {
553                self.insert(y.clone(), self[x].clone());
554            }
555            (false, false) => {
556                self.insert(x.clone(), x.clone());
557                self.insert(y.clone(), x.clone());
558            }
559        }
560    }
561}