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