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/projection_pushdown/hstack.rs
7889 views
1
use super::*;
2
3
#[allow(clippy::too_many_arguments)]
4
pub(super) fn process_hstack(
5
proj_pd: &mut ProjectionPushDown,
6
input: Node,
7
mut exprs: Vec<ExprIR>,
8
options: ProjectionOptions,
9
mut ctx: ProjectionContext,
10
lp_arena: &mut Arena<IR>,
11
expr_arena: &mut Arena<AExpr>,
12
) -> PolarsResult<IR> {
13
if ctx.has_pushed_down() {
14
let mut pruned_with_cols = Vec::with_capacity(exprs.len());
15
16
// Check if output names are used upstream
17
// if not, we can prune the `with_column` expression
18
// as it is not used in the output.
19
for e in exprs {
20
let is_used_upstream = ctx.projected_names.contains(e.output_name());
21
if is_used_upstream {
22
pruned_with_cols.push(e);
23
}
24
}
25
26
if pruned_with_cols.is_empty() {
27
proj_pd.pushdown_and_assign(input, ctx, lp_arena, expr_arena)?;
28
return Ok(lp_arena.take(input));
29
}
30
31
// Make sure that columns selected with_columns are available
32
// only if not empty. If empty we already select everything.
33
for e in &pruned_with_cols {
34
add_expr_to_accumulated(
35
e.node(),
36
&mut ctx.acc_projections,
37
&mut ctx.projected_names,
38
expr_arena,
39
);
40
}
41
42
exprs = pruned_with_cols
43
}
44
// projections that select columns added by
45
// this `with_column` operation can be dropped
46
// For instance in:
47
//
48
// q
49
// .with_column(col("a").alias("b")
50
// .select(["a", "b"])
51
//
52
// we can drop the "b" projection at this level
53
let (acc_projections, _, names) = split_acc_projections(
54
ctx.acc_projections,
55
&lp_arena.get(input).schema(lp_arena),
56
expr_arena,
57
true, // expands_schema
58
);
59
60
let ctx = ProjectionContext::new(acc_projections, names, ctx.inner);
61
proj_pd.pushdown_and_assign(input, ctx, lp_arena, expr_arena)?;
62
63
let lp = IRBuilder::new(input, expr_arena, lp_arena)
64
.with_columns(exprs, options)
65
.build();
66
Ok(lp)
67
}
68
69