mz_transform/
fusion.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//! Transformations that fuse together others of their kind.
11
12pub mod filter;
13pub mod join;
14pub mod map;
15pub mod negate;
16pub mod project;
17pub mod reduce;
18pub mod top_k;
19pub mod union;
20
21use mz_expr::MirRelationExpr;
22
23use crate::TransformCtx;
24
25/// Fuses multiple like operators together when possible.
26#[derive(Debug)]
27pub struct Fusion;
28
29impl crate::Transform for Fusion {
30    fn name(&self) -> &'static str {
31        "Fusion"
32    }
33
34    #[mz_ore::instrument(
35        target = "optimizer",
36        level = "debug",
37        fields(path.segment = "fusion")
38    )]
39    fn actually_perform_transform(
40        &self,
41        relation: &mut MirRelationExpr,
42        _: &mut TransformCtx,
43    ) -> Result<(), crate::TransformError> {
44        use mz_expr::visit::Visit;
45        relation.visit_mut_post(&mut Self::action)?;
46        mz_repr::explain::trace_plan(&*relation);
47        Ok(())
48    }
49}
50
51impl Fusion {
52    /// Apply fusion action for variants we know how to fuse.
53    pub(crate) fn action(expr: &mut MirRelationExpr) {
54        match expr {
55            MirRelationExpr::Filter { .. } => filter::Filter::action(expr),
56            MirRelationExpr::Map { .. } => map::Map::action(expr),
57            MirRelationExpr::Project { .. } => project::Project::action(expr),
58            MirRelationExpr::Negate { .. } => negate::Negate::action(expr),
59            MirRelationExpr::TopK { .. } => top_k::TopK::action(expr),
60            MirRelationExpr::Union { .. } => union::Union::action(expr),
61            _ => {}
62        }
63    }
64}