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/cse/mod.rs
7889 views
1
mod cache_states;
2
mod csee;
3
mod cspe;
4
5
use cache_states::set_cache_states;
6
pub(super) use csee::CommonSubExprOptimizer;
7
pub use csee::NaiveExprMerger;
8
use cspe::elim_cmn_subplans;
9
10
use super::*;
11
12
type Accepted = Option<(VisitRecursion, bool)>;
13
// Don't allow this node in a cse.
14
const REFUSE_NO_MEMBER: Accepted = Some((VisitRecursion::Continue, false));
15
// Don't allow this node, but allow as a member of a cse.
16
const REFUSE_ALLOW_MEMBER: Accepted = Some((VisitRecursion::Continue, true));
17
const REFUSE_SKIP: Accepted = Some((VisitRecursion::Skip, false));
18
// Accept this node.
19
const ACCEPT: Accepted = None;
20
21
pub(super) struct CommonSubPlanOptimizer {}
22
23
impl CommonSubPlanOptimizer {
24
pub fn new() -> Self {
25
Self {}
26
}
27
28
#[allow(clippy::too_many_arguments)]
29
pub fn optimize(
30
&mut self,
31
root: Node,
32
ir_arena: &mut Arena<IR>,
33
expr_arena: &mut Arena<AExpr>,
34
pushdown_maintain_errors: bool,
35
opt_flags: &OptFlags,
36
verbose: bool,
37
scratch: &mut Vec<Node>,
38
) -> PolarsResult<Node> {
39
let (root, inserted_cache, _) = cse::elim_cmn_subplans(root, ir_arena, expr_arena);
40
41
run_projection_predicate_pushdown(
42
root,
43
ir_arena,
44
expr_arena,
45
pushdown_maintain_errors,
46
opt_flags,
47
)?;
48
49
if inserted_cache {
50
// We only want to run this on cse inserted caches
51
cse::set_cache_states(
52
root,
53
ir_arena,
54
expr_arena,
55
scratch,
56
verbose,
57
pushdown_maintain_errors,
58
opt_flags.new_streaming(),
59
)?;
60
}
61
62
Ok(root)
63
}
64
}
65
66