fn find_redundancy(
    input: usize,
    keys: &[Vec<usize>],
    input_mapper: &JoinInputMapper,
    equivalences: &[Vec<MirScalarExpr>],
    input_prov: &[Vec<ProvInfo>],
) -> Option<Vec<MirScalarExpr>> {
    // Whether the `equivalence` contains an expression that only references
    // `input` that leads to the same as `root_expr` once dereferenced.
    let contains_equivalent_expr_from_input = |equivalence: &[MirScalarExpr],
                                               root_expr: &MirScalarExpr,
                                               input: usize,
                                               provenance: &ProvInfo|
     -> bool {
        equivalence.iter().any(|expr| {
            Some(input) == input_mapper.single_input(expr)
                && provenance
                    .dereference(&input_mapper.map_expr_to_local(expr.clone()))
                    .as_ref()
                    == Some(root_expr)
        })
    };
    for provenance in input_prov[input].iter() {
        // We can only elide if the input contains all records, and binds all columns.
        if provenance.exact
            && provenance
                .dereferenced_projection
                .iter()
                .all(|e| e.is_some())
        {
            // examine all *other* inputs that have not been removed...
            for other in (0..input_mapper.total_inputs()).filter(|other| other != &input) {
                for other_prov in input_prov[other].iter().filter(|p| p.id == provenance.id) {
                    let all_columns_equated = |cols: &Vec<usize>| {
                        cols.iter().all(|input_col| {
                            // The root expression behind the key column, ie. the expression
                            // re-written in terms of elements in the projection of the Get
                            // operator.
                            let root_expr =
                                provenance.dereference(&MirScalarExpr::column(*input_col));
                            root_expr.as_ref().map_or(false, |root_expr| {
                                // Check if there is a join equivalence that joins 'input' and
                                // 'other' on expressions that lead to the same root expression
                                // as the key column.
                                equivalences.iter().any(|equivalence| {
                                    contains_equivalent_expr_from_input(
                                        equivalence,
                                        root_expr,
                                        input,
                                        provenance,
                                    ) && contains_equivalent_expr_from_input(
                                        equivalence,
                                        root_expr,
                                        other,
                                        other_prov,
                                    )
                                })
                            })
                        })
                    };

                    if keys.iter().any(|key| all_columns_equated(key)) {
                        // Find out whether we can produce input's projection strictly with
                        // elements in other's projection.
                        let expressions = provenance
                            .dereferenced_projection
                            .iter()
                            .enumerate()
                            .flat_map(|(c, _)| {
                                // Check if the expression under input's 'c' column can be built
                                // with elements in other's projection.
                                provenance.dereferenced_projection[c].as_ref().map_or(
                                    None,
                                    |root_expr| {
                                        try_build_expression_using_other(
                                            root_expr,
                                            other,
                                            other_prov,
                                            input_mapper,
                                        )
                                    },
                                )
                            })
                            .collect_vec();
                        if expressions.len() == provenance.dereferenced_projection.len() {
                            return Some(expressions);
                        }
                    }
                }
            }
        }
    }

    None
}
/// Attempts to find column bindings that make `input` redundant.
///
/// This method attempts to determine that `input` may be redundant by searching
/// the join structure for another relation `other` with provenance that contains some
/// provenance of `input`, and keys for `input` that are equated by the join to the
/// corresponding columns of `other` under their provenance. The `input` provenance
/// must also have its `exact` bit set.
///
/// In these circumstances, the claim is that because the key columns are equated and
/// determine non-key columns, any matches between `input` and
/// `other` will neither introduce new information to `other`, nor restrict the rows
/// of `other`, nor alter their multplicity.
Attempts to find column bindings that make `input` redundant.


Attempts to find column bindings that make ` input ` redundant .
fn find_redundancy(
    input: usize,
    keys: &[Vec<usize>],
    input_mapper: &JoinInputMapper,
    equivalences: &[Vec<MirScalarExpr>],
    input_prov: &[Vec<ProvInfo>],
) -> Option<Vec<MirScalarExpr>> {
    // Whether the `equivalence` contains an expression that only references
    // `input` that leads to the same as `root_expr` once dereferenced.
    let contains_equivalent_expr_from_input = |equivalence: &[MirScalarExpr],
                                               root_expr: &MirScalarExpr,
                                               input: usize,
                                               provenance: &ProvInfo|
     -> bool {
        equivalence.iter().any(|expr| {
            Some(input) == input_mapper.single_input(expr)
                && provenance
                    .dereference(&input_mapper.map_expr_to_local(expr.clone()))
                    .as_ref()
                    == Some(root_expr)
        })
    };
    for provenance in input_prov[input].iter() {
        // We can only elide if the input contains all records, and binds all columns.
        if provenance.exact
            && provenance
                .dereferenced_projection
                .iter()
                .all(|e| e.is_some())
        {
            // examine all *other* inputs that have not been removed...
            for other in (0..input_mapper.total_inputs()).filter(|other| other != &input) {
                for other_prov in input_prov[other].iter().filter(|p| p.id == provenance.id) {
                    let all_columns_equated = |cols: &Vec<usize>| {
                        cols.iter().all(|input_col| {
                            // The root expression behind the key column, ie. the expression
                            // re-written in terms of elements in the projection of the Get
                            // operator.
                            let root_expr =
                                provenance.dereference(&MirScalarExpr::column(*input_col));
                            root_expr.as_ref().map_or(false, |root_expr| {
                                // Check if there is a join equivalence that joins 'input' and
                                // 'other' on expressions that lead to the same root expression
                                // as the key column.
                                equivalences.iter().any(|equivalence| {
                                    contains_equivalent_expr_from_input(
                                        equivalence,
                                        root_expr,
                                        input,
                                        provenance,
                                    ) && contains_equivalent_expr_from_input(
                                        equivalence,
                                        root_expr,
                                        other,
                                        other_prov,
                                    )
                                })
                            })
                        })
                    };
                    if keys.iter().any(|key| all_columns_equated(key)) {
                        // Find out whether we can produce input's projection strictly with
                        // elements in other's projection.
                        let expressions = provenance
                            .dereferenced_projection
                            .iter()
                            .enumerate()
                            .flat_map(|(c, _)| {
                                // Check if the expression under input's 'c' column can be built
                                // with elements in other's projection.
                                provenance.dereferenced_projection[c].as_ref().map_or(
                                    None,
                                    |root_expr| {
                                        try_build_expression_using_other(
                                            root_expr,
                                            other,
                                            other_prov,
                                            input_mapper,
                                        )
                                    },
                                )
                            })
                            .collect_vec();
                        if expressions.len() == provenance.dereferenced_projection.len() {
                            return Some(expressions);
                        }
                    }
                }
            }
        }
    }
    None
}
fn find_redundancy ( input : usize , keys : & [ Vec < usize > ] , input_mapper : & JoinInputMapper , equivalences : & [ Vec < MirScalarExpr > ] , input_prov : & [ Vec < ProvInfo > ] , ) -> Option < Vec < MirScalarExpr > > { // Whether the `equivalence` contains an expression that only references // `input` that leads to the same as `root_expr` once dereferenced. let contains_equivalent_expr_from_input = | equivalence : & [ MirScalarExpr ] , root_expr : & MirScalarExpr , input : usize , provenance : & ProvInfo | -> bool { equivalence . iter ( ) . any ( | expr | { Some ( input ) == input_mapper . single_input ( expr ) && provenance . dereference ( & input_mapper . map_expr_to_local ( expr . clone ( ) ) ) . as_ref ( ) == Some ( root_expr ) } ) } ; for provenance in input_prov [ input ] . iter ( ) { // We can only elide if the input contains all records, and binds all columns. if provenance . exact && provenance . dereferenced_projection . iter ( ) . all ( | e | e . is_some ( ) ) { // examine all *other* inputs that have not been removed... for other in ( 0 .. input_mapper . total_inputs ( ) ) . filter ( | other | other != & input ) { for other_prov in input_prov [ other ] . iter ( ) . filter ( | p | p . id == provenance . id ) { let all_columns_equated = | cols : & Vec < usize > | { cols . iter ( ) . all ( | input_col | { // The root expression behind the key column, ie. the expression // re-written in terms of elements in the projection of the Get // operator. let root_expr = provenance . dereference ( & MirScalarExpr :: column ( * input_col ) ) ; root_expr . as_ref ( ) . map_or ( false , | root_expr | { // Check if there is a join equivalence that joins 'input' and // 'other' on expressions that lead to the same root expression // as the key column. equivalences . iter ( ) . any ( | equivalence | { contains_equivalent_expr_from_input ( equivalence , root_expr , input , provenance , ) && contains_equivalent_expr_from_input ( equivalence , root_expr , other , other_prov , ) } ) } ) } ) } ; if keys . iter ( ) . any ( | key | all_columns_equated ( key ) ) { // Find out whether we can produce input's projection strictly with // elements in other's projection. let expressions = provenance . dereferenced_projection . iter ( ) . enumerate ( ) . flat_map ( | ( c , _ ) | { // Check if the expression under input's 'c' column can be built // with elements in other's projection. provenance . dereferenced_projection [ c ] . as_ref ( ) . map_or ( None , | root_expr | { try_build_expression_using_other ( root_expr , other , other_prov , input_mapper , ) } , ) } ) . collect_vec ( ) ; if expressions . len ( ) == provenance . dereferenced_projection . len ( ) { return Some ( expressions ) ; } } } } } } None }
Attempts to find column bindings that make `input` redundant.
Attempts to find column bindings that make ` input ` redundant .
input
usize
keys
&[Vec<usize>]
input_mapper
&JoinInputMapper
equivalences
&[Vec<MirScalarExpr>]
input_prov
&[Vec<ProvInfo>]
input
usize
keys
&[Vec<usize>]
input_mapper
&JoinInputMapper
equivalences
&[Vec<MirScalarExpr>]
input_prov
&[Vec<ProvInfo>]