You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: non_negative.rs
    path of file: ./repos/materialize/src/transform/src/attribute
    
    the code of the file until where you have to start completion: // Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

//! Definition and helper structs for the [`NonNegative`] attribute.

use mz_expr::{Id, MirRelationExpr};

use crate::attribute::subtree_size::SubtreeSize;
use crate::attribute::{Attribute, DerivedAttributes, DerivedAttributesBuilder, Env};

/// Traverses a [`MirRelationExpr`] tree and figures out whether for subtree
/// the sum of all diffs up to a specific time for any record can be a
/// negative value.
///
/// The results for each subtree are accumulated in a bottom-up order
/// in [`NonNegative::results`].
///
/// This method is a conservative approximation and is known to miss not-hard
/// cases.
///
/// It assumes that all `Get`s to global ids correspond to collections without negative
/// multiplicities. Local let bindings' non-negativity results are stored in `env`.
#[derive(Default)]
#[allow(missing_debug_implementations)]
pub struct NonNegative {
    /// Environment of computed values for this attribute.
    env: Env<Self>,
    /// A vector of results for all nodes in the visited tree in
    /// post-visit order.
    pub results: Vec<bool>,
}

impl Attribute for NonNegative {
    type Value = bool;

    fn derive(&mut self, expr: &MirRelationExpr, deps: &DerivedAttributes) {
        use MirRelationExpr::*;
        let n = self.results.len();
        match expr {
            Constant { rows, .. } => {
                if let Ok(rows) = rows {
                    let has_negative_rows = rows.iter().any(|(_data, diff)| diff < &0);
                    self.results.push(!has_negative_rows);
                } else {
                    self.results.push(true); // constant errors are considered "non-negative"
                }
            }
            Get { id, .. } => match id {
                Id::Local(id) => match self.env.get(id) {
                    Some(value) => self.results.push(value.clone()),
                    None => self.results.push(false),

    
    fn derive(&mut self, expr: &MirRelationExpr, deps: &DerivedAttributes) {
        use MirRelationExpr::*;
        let n = self.results.len();
        match expr {
            Constant { rows, .. } => {
                if let Ok(rows) = rows {
                    let has_negative_rows = rows.iter().any(|(_data, diff)| diff < &0);
                    self.results.push(!has_negative_rows);
                } else {
                    self.results.push(true); // constant errors are considered "non-negative"
                }
            }
            Get { id, .. } => match id {
                Id::Local(id) => match self.env.get(id) {
                    Some(value) => self.results.push(value.clone()),
                    None => self.results.push(false),
                },
                Id::Global(_) => {
                    self.results.push(true);
                }
            },
            Let {
                value: _, body: _, ..
            } => {
                let body = self.results[n - 1];
                self.results.push(body);
            }
            LetRec { .. } => {
                let body = self.results[n - 1];
                self.results.push(body);
            }
            Project { input: _, .. } => {
                let input = self.results[n - 1];
                self.results.push(input);
            }
            Map { input: _, .. } => {
                let input = self.results[n - 1];
                self.results.push(input);
            }
            FlatMap { input: _, .. } => {
                let input = self.results[n - 1];
                self.results.push(input);
            }
            Filter { input: _, .. } => {
                let input = self.results[n - 1];
                self.results.push(input);
            }
            Join { inputs, .. } => {
                let mut result = true;
                let mut offset = 1;
                for _ in 0..inputs.len() {
                    result &= &self.results[n - offset];
                    offset += &deps.get_results::<SubtreeSize>()[n - offset];
                }
                self.results.push(result);
            }
            Reduce { input: _, .. } => {
                let input = self.results[n - 1];
                self.results.push(input);
            }
            TopK { input: _, .. } => {
                let input = self.results[n - 1];
                self.results.push(input);
            }
            Negate { input: _ } => {
                // Can be refined if we have proper LetRec handling
                self.results.push(false);
            }
            Threshold { input: _ } => {
                self.results.push(true);
            }
            Union { base: _, inputs } => {
                let mut result = true;
                let mut offset = 1;
                for _ in 0..inputs.len() {
                    result &= &self.results[n - offset];
                    offset += &deps.get_results::<SubtreeSize>()[n - offset];
                }
                result &= &self.results[n - offset]; // include the base result
                self.results.push(result);
            }
            ArrangeBy { input: _, .. } => {
                let input = self.results[n - 1];
                self.results.push(input);
            }
        }
    }