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.
910//! Fuses a sequence of `Negate` operators in to one or zero `Negate` operators.
1112use mz_expr::MirRelationExpr;
1314use crate::TransformCtx;
1516/// Fuses a sequence of `Negate` operators in to one or zero `Negate` operators.
17#[derive(Debug)]
18pub struct Negate;
1920impl crate::Transform for Negate {
21fn name(&self) -> &'static str {
22"NegateFusion"
23}
2425#[mz_ore::instrument(
26 target = "optimizer",
27 level = "debug",
28 fields(path.segment = "negate_fusion")
29 )]
30fn actually_perform_transform(
31&self,
32 relation: &mut MirRelationExpr,
33_: &mut TransformCtx,
34 ) -> Result<(), crate::TransformError> {
35 relation.visit_pre_mut(Self::action);
36 mz_repr::explain::trace_plan(&*relation);
37Ok(())
38 }
39}
4041impl Negate {
42/// Fuses a sequence of `Negate` operators into one or zero `Negate` operators.
43pub fn action(relation: &mut MirRelationExpr) {
44if let MirRelationExpr::Negate { input } = relation {
45let mut require_negate = true;
46while let MirRelationExpr::Negate { input: inner_input } = &mut **input {
47**input = inner_input.take_dangerous();
48 require_negate = !require_negate;
49 }
5051if !require_negate {
52*relation = input.take_dangerous();
53 }
54 }
55 }
56}