Path: blob/main/crates/polars-plan/src/plans/optimizer/slice_pushdown_expr.rs
6940 views
use super::*;12fn pushdown(input: Node, offset: Node, length: Node, arena: &mut Arena<AExpr>) -> Node {3arena.add(AExpr::Slice {4input,5offset,6length,7})8}910impl OptimizationRule for SlicePushDown {11fn optimize_expr(12&mut self,13expr_arena: &mut Arena<AExpr>,14expr_node: Node,15_schema: &Schema,16_ctx: OptimizeExprContext,17) -> PolarsResult<Option<AExpr>> {18if let AExpr::Slice {19input,20offset,21length,22} = expr_arena.get(expr_node)23{24let offset = *offset;25let length = *length;2627use AExpr::*;28let out = match expr_arena.get(*input) {29ae @ Cast { .. } => {30let ae = ae.clone();31let scratch = self.empty_nodes_scratch_mut();32ae.inputs_rev(scratch);33let input = scratch[0];34let new_input = pushdown(input, offset, length, expr_arena);35Some(ae.replace_inputs(&[new_input]))36},37BinaryExpr { left, right, op } => {38let left = *left;39let right = *right;40let op = *op;4142let left = pushdown(left, offset, length, expr_arena);43let right = pushdown(right, offset, length, expr_arena);44Some(BinaryExpr { left, op, right })45},46Ternary {47truthy,48falsy,49predicate,50} => {51let truthy = *truthy;52let falsy = *falsy;53let predicate = *predicate;5455let truthy = pushdown(truthy, offset, length, expr_arena);56let falsy = pushdown(falsy, offset, length, expr_arena);57let predicate = pushdown(predicate, offset, length, expr_arena);58Some(Ternary {59truthy,60falsy,61predicate,62})63},64m @ AnonymousFunction { options, .. } if options.is_elementwise() => {65if let AnonymousFunction {66mut input,67function,68options,69fmt_str,70} = m.clone()71{72input.iter_mut().for_each(|e| {73let n = pushdown(e.node(), offset, length, expr_arena);74e.set_node(n);75});7677Some(AnonymousFunction {78input,79function,80options,81fmt_str,82})83} else {84unreachable!()85}86},87m @ Function { options, .. } if options.is_elementwise() => {88if let Function {89mut input,90function,91options,92} = m.clone()93{94input.iter_mut().for_each(|e| {95let n = pushdown(e.node(), offset, length, expr_arena);96e.set_node(n);97});9899Some(Function {100input,101function,102options,103})104} else {105unreachable!()106}107},108_ => None,109};110Ok(out)111} else {112Ok(None)113}114}115}116117118