Commits


Jorge C. Leitao authored and Andy Grove committed 58b38a617de
ARROW-9618: [Rust] [DataFusion] Made it easier to write optimizers This PR adds 5 auxiliary functions to the optimizer package that allow us to more easily write optimizers and re-writes the optimizers using them. This PR assumes that the majority of the optimizations will: * recurse logical expressions and re-write them (currently we have the type coercion) * recurse logical plans and re-write them (currently we have the projection pushdown) To transverse and re-write expressions, this PR introduces two functions: ``` /// Returns all expressions composing the expression. /// E.g. if the expression is "(a + 1) + 1", it returns ["a + 1", "1"] (as Expr objects) fn expr_expressions(expr: &Expr) -> Result<Vec<&Expr>> /// returns a new expression where the expressions in expr are replaced by the ones in `expr`. /// This is used in conjunction with ``expr_expressions`` to re-write expressions. pub fn from_expression(expr: &Expr, expressions: &Vec<Expr>) -> Result<Expr> { ``` these are used on an optimizer as follows: ``` let mut expressions = expr_expressions(expr)?; # recurse on the expression let mut expressions = expressions.iter().map(|e| recursion(e)).collect() # modify `expressions` (optimizer's code) utils::from_expression(expr, &expressions) ``` Likewise, this PR introduces 3 functions for the plans: ``` /// returns all expressions in the logical plan. pub fn expressions(plan: &LogicalPlan) -> Vec<Expr> /// returns all inputs in the logical plan pub fn inputs(plan: &LogicalPlan) -> Vec<&LogicalPlan> /// Returns a new logical plan based on the original one with inputs and expressions replaced pub fn from_plan( plan: &LogicalPlan, expr: &Vec<Expr>, inputs: &Vec<LogicalPlan>, ) -> Result<LogicalPlan> ``` Overall, these make writing optimizers a much developer-friendlier experience because expressions or plans that the optimizer does not care can just be left alone. Closes #7879 from jorgecarleitao/simpler Authored-by: Jorge C. Leitao <jorgecarleitao@gmail.com> Signed-off-by: Andy Grove <andygrove73@gmail.com>