mz_transform/normalize_ops.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//! Normalize the structure of various operators.
11
12use mz_expr::MirRelationExpr;
13use mz_expr::visit::Visit;
14
15use crate::TransformCtx;
16
17/// Normalize the structure of various operators.
18#[derive(Debug)]
19pub struct NormalizeOps;
20
21impl crate::Transform for NormalizeOps {
22 fn name(&self) -> &'static str {
23 "NormalizeOps"
24 }
25
26 #[mz_ore::instrument(
27 target = "optimizer",
28 level = "debug",
29 fields(path.segment = "normalize_ops")
30 )]
31 fn actually_perform_transform(
32 &self,
33 relation: &mut MirRelationExpr,
34 _ctx: &mut TransformCtx,
35 ) -> Result<(), crate::TransformError> {
36 // Canonicalize and fuse various operators as a bottom-up transforms.
37 relation.try_visit_mut_post::<_, crate::TransformError>(
38 &mut |expr: &mut MirRelationExpr| {
39 // (a) Might enable fusion in the next step.
40 crate::canonicalization::FlatMapToMap::action(expr);
41 crate::canonicalization::TopKElision::action(expr);
42 // (b) Fuse various like-kinded operators. Might enable further canonicalization.
43 crate::fusion::Fusion::action(expr);
44 // (c) Fuse join trees (might lift in-between Filters).
45 crate::fusion::join::Join::action(expr)?;
46 // (d) Extract column references in Map as Project.
47 crate::canonicalization::ProjectionExtraction::action(expr);
48 // Done!
49 Ok(())
50 },
51 )?;
52 mz_repr::explain::trace_plan(&*relation);
53 Ok(())
54 }
55}