fn try_build_expression_using_other(
    root_expr: &MirScalarExpr,
    other: usize,
    other_prov: &ProvInfo,
    input_mapper: &JoinInputMapper,
) -> Option<MirScalarExpr> {
    if root_expr.is_literal() {
        return Some(root_expr.clone());
    }

    // Check if 'other' projects a column that lead to `root_expr`.
    for (other_col, derefed) in other_prov.dereferenced_projection.iter().enumerate() {
        if let Some(derefed) = derefed {
            if derefed == root_expr {
                return Some(MirScalarExpr::Column(
                    input_mapper.map_column_to_global(other_col, other),
                ));
            }
        }
    }

    // Otherwise, try to build root_expr's sub-expressions recursively
    // other's projection.
    match root_expr {
        MirScalarExpr::Column(_) => None,
        MirScalarExpr::CallUnary { func, expr } => {
            try_build_expression_using_other(expr, other, other_prov, input_mapper).and_then(
                |expr| {
                    Some(MirScalarExpr::CallUnary {
                        func: func.clone(),
                        expr: Box::new(expr),
                    })
                },
            )
        }
        MirScalarExpr::CallBinary { func, expr1, expr2 } => {
            try_build_expression_using_other(expr1, other, other_prov, input_mapper).and_then(
                |expr1| {
                    try_build_expression_using_other(expr2, other, other_prov, input_mapper)
                        .and_then(|expr2| {
                            Some(MirScalarExpr::CallBinary {
                                func: func.clone(),
                                expr1: Box::new(expr1),
                                expr2: Box::new(expr2),
                            })
                        })
                },
            )
        }
        MirScalarExpr::CallVariadic { func, exprs } => {
            let new_exprs = exprs
                .iter()
                .flat_map(|e| try_build_expression_using_other(e, other, other_prov, input_mapper))
                .collect_vec();
            if new_exprs.len() == exprs.len() {
                Some(MirScalarExpr::CallVariadic {
                    func: func.clone(),
                    exprs: new_exprs,
                })
            } else {
                None
            }
        }
        MirScalarExpr::Literal(..) | MirScalarExpr::CallNullary(..) => Some(root_expr.clone()),
        MirScalarExpr::If { cond, then, els } => {
            try_build_expression_using_other(cond, other, other_prov, input_mapper).and_then(
                |cond| {
                    try_build_expression_using_other(then, other, other_prov, input_mapper)
                        .and_then(|then| {
                            try_build_expression_using_other(els, other, other_prov, input_mapper)
                                .and_then(|els| {
                                    Some(MirScalarExpr::If {
                                        cond: Box::new(cond),
                                        then: Box::new(then),
                                        els: Box::new(els),
                                    })
                                })
                        })
                },
            )
        }
    }
}
/// Tries to build `root_expr` using elements from other's projection.
Tries to build `root_expr` using elements from other's projection.
Tries to build ` root_expr ` using elements from other ' s projection .
fn try_build_expression_using_other(
    root_expr: &MirScalarExpr,
    other: usize,
    other_prov: &ProvInfo,
    input_mapper: &JoinInputMapper,
) -> Option<MirScalarExpr> {
    if root_expr.is_literal() {
        return Some(root_expr.clone());
    }
    // Check if 'other' projects a column that lead to `root_expr`.
    for (other_col, derefed) in other_prov.dereferenced_projection.iter().enumerate() {
        if let Some(derefed) = derefed {
            if derefed == root_expr {
                return Some(MirScalarExpr::Column(
                    input_mapper.map_column_to_global(other_col, other),
                ));
            }
        }
    }
    // Otherwise, try to build root_expr's sub-expressions recursively
    // other's projection.
    match root_expr {
        MirScalarExpr::Column(_) => None,
        MirScalarExpr::CallUnary { func, expr } => {
            try_build_expression_using_other(expr, other, other_prov, input_mapper).and_then(
                |expr| {
                    Some(MirScalarExpr::CallUnary {
                        func: func.clone(),
                        expr: Box::new(expr),
                    })
                },
            )
        }
        MirScalarExpr::CallBinary { func, expr1, expr2 } => {
            try_build_expression_using_other(expr1, other, other_prov, input_mapper).and_then(
                |expr1| {
                    try_build_expression_using_other(expr2, other, other_prov, input_mapper)
                        .and_then(|expr2| {
                            Some(MirScalarExpr::CallBinary {
                                func: func.clone(),
                                expr1: Box::new(expr1),
                                expr2: Box::new(expr2),
                            })
                        })
                },
            )
        }
        MirScalarExpr::CallVariadic { func, exprs } => {
            let new_exprs = exprs
                .iter()
                .flat_map(|e| try_build_expression_using_other(e, other, other_prov, input_mapper))
                .collect_vec();
            if new_exprs.len() == exprs.len() {
                Some(MirScalarExpr::CallVariadic {
                    func: func.clone(),
                    exprs: new_exprs,
                })
            } else {
                None
            }
        }
        MirScalarExpr::Literal(..) | MirScalarExpr::CallNullary(..) => Some(root_expr.clone()),
        MirScalarExpr::If { cond, then, els } => {
            try_build_expression_using_other(cond, other, other_prov, input_mapper).and_then(
                |cond| {
                    try_build_expression_using_other(then, other, other_prov, input_mapper)
                        .and_then(|then| {
                            try_build_expression_using_other(els, other, other_prov, input_mapper)
                                .and_then(|els| {
                                    Some(MirScalarExpr::If {
                                        cond: Box::new(cond),
                                        then: Box::new(then),
                                        els: Box::new(els),
                                    })
                                })
                        })
                },
            )
        }
    }
}
fn try_build_expression_using_other ( root_expr : & MirScalarExpr , other : usize , other_prov : & ProvInfo , input_mapper : & JoinInputMapper , ) -> Option < MirScalarExpr > { if root_expr . is_literal ( ) { return Some ( root_expr . clone ( ) ) ; } // Check if 'other' projects a column that lead to `root_expr`. for ( other_col , derefed ) in other_prov . dereferenced_projection . iter ( ) . enumerate ( ) { if let Some ( derefed ) = derefed { if derefed == root_expr { return Some ( MirScalarExpr :: Column ( input_mapper . map_column_to_global ( other_col , other ) , ) ) ; } } } // Otherwise, try to build root_expr's sub-expressions recursively // other's projection. match root_expr { MirScalarExpr :: Column ( _ ) => None , MirScalarExpr :: CallUnary { func , expr } => { try_build_expression_using_other ( expr , other , other_prov , input_mapper ) . and_then ( | expr | { Some ( MirScalarExpr :: CallUnary { func : func . clone ( ) , expr : Box :: new ( expr ) , } ) } , ) } MirScalarExpr :: CallBinary { func , expr1 , expr2 } => { try_build_expression_using_other ( expr1 , other , other_prov , input_mapper ) . and_then ( | expr1 | { try_build_expression_using_other ( expr2 , other , other_prov , input_mapper ) . and_then ( | expr2 | { Some ( MirScalarExpr :: CallBinary { func : func . clone ( ) , expr1 : Box :: new ( expr1 ) , expr2 : Box :: new ( expr2 ) , } ) } ) } , ) } MirScalarExpr :: CallVariadic { func , exprs } => { let new_exprs = exprs . iter ( ) . flat_map ( | e | try_build_expression_using_other ( e , other , other_prov , input_mapper ) ) . collect_vec ( ) ; if new_exprs . len ( ) == exprs . len ( ) { Some ( MirScalarExpr :: CallVariadic { func : func . clone ( ) , exprs : new_exprs , } ) } else { None } } MirScalarExpr :: Literal ( .. ) | MirScalarExpr :: CallNullary ( .. ) => Some ( root_expr . clone ( ) ) , MirScalarExpr :: If { cond , then , els } => { try_build_expression_using_other ( cond , other , other_prov , input_mapper ) . and_then ( | cond | { try_build_expression_using_other ( then , other , other_prov , input_mapper ) . and_then ( | then | { try_build_expression_using_other ( els , other , other_prov , input_mapper ) . and_then ( | els | { Some ( MirScalarExpr :: If { cond : Box :: new ( cond ) , then : Box :: new ( then ) , els : Box :: new ( els ) , } ) } ) } ) } , ) } } }
Tries to build `root_expr` using elements from other's projection.
Tries to build ` root_expr ` using elements from other ' s projection .
root_expr
&MirScalarExpr
other
usize
other_prov
&ProvInfo
input_mapper
&JoinInputMapper
root_expr
&MirScalarExpr
other
usize
other_prov
&ProvInfo
input_mapper
&JoinInputMapper