mz_transform/
threshold_elision.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//! Remove Threshold operators when we are certain no records have negative multiplicity.
11//!
12//! If we have Threshold(A - Subset(A)) and we believe that A has no negative multiplicities,
13//! then we can replace this with A - Subset(A).
14//!
15//! The Subset(X) notation means that the collection is a multiset subset of X:
16//! multiplicities of each record in Subset(X) are at most that of X.
17
18use itertools::Itertools;
19use mz_expr::MirRelationExpr;
20
21use crate::TransformCtx;
22use crate::analysis::{DerivedBuilder, NonNegative, SubtreeSize};
23
24/// Remove Threshold operators that have no effect.
25#[derive(Debug)]
26pub struct ThresholdElision;
27
28impl crate::Transform for ThresholdElision {
29    fn name(&self) -> &'static str {
30        "ThresholdElision"
31    }
32
33    #[mz_ore::instrument(
34        target = "optimizer",
35        level = "debug",
36        fields(path.segment = "threshold_elision")
37    )]
38    fn actually_perform_transform(
39        &self,
40        relation: &mut MirRelationExpr,
41        ctx: &mut TransformCtx,
42    ) -> Result<(), crate::TransformError> {
43        let mut builder = DerivedBuilder::new(ctx.features);
44        builder.require(NonNegative);
45        builder.require(SubtreeSize);
46        let derived = builder.visit(&*relation);
47
48        // Descend the AST, removing `Threshold` operators whose inputs are non-negative.
49        let mut todo = vec![(&mut *relation, derived.as_view())];
50        while let Some((expr, mut view)) = todo.pop() {
51            if let MirRelationExpr::Threshold { input } = expr {
52                if *view
53                    .last_child()
54                    .value::<NonNegative>()
55                    .expect("NonNegative required")
56                {
57                    *expr = input.take_dangerous();
58                    view = view.last_child();
59                }
60            }
61            todo.extend(expr.children_mut().rev().zip_eq(view.children_rev()))
62        }
63
64        mz_repr::explain::trace_plan(&*relation);
65        Ok(())
66    }
67}