polars_plan/plans/visitor/
expr.rs

1use std::fmt::Debug;
2#[cfg(feature = "cse")]
3use std::fmt::Formatter;
4
5use polars_core::prelude::{Field, Schema};
6use polars_utils::unitvec;
7
8use super::*;
9use crate::prelude::*;
10
11impl TreeWalker for Expr {
12    type Arena = ();
13
14    fn apply_children<F: FnMut(&Self, &Self::Arena) -> PolarsResult<VisitRecursion>>(
15        &self,
16        op: &mut F,
17        arena: &Self::Arena,
18    ) -> PolarsResult<VisitRecursion> {
19        let mut scratch = unitvec![];
20
21        self.nodes(&mut scratch);
22
23        for &child in scratch.as_slice() {
24            match op(child, arena)? {
25                // let the recursion continue
26                VisitRecursion::Continue | VisitRecursion::Skip => {},
27                // early stop
28                VisitRecursion::Stop => return Ok(VisitRecursion::Stop),
29            }
30        }
31        Ok(VisitRecursion::Continue)
32    }
33
34    fn map_children<F: FnMut(Self, &mut Self::Arena) -> PolarsResult<Self>>(
35        self,
36        f: &mut F,
37        _arena: &mut Self::Arena,
38    ) -> PolarsResult<Self> {
39        use polars_utils::functions::try_arc_map as am;
40        let mut f = |expr| f(expr, &mut ());
41        use AggExpr::*;
42        use Expr::*;
43        #[rustfmt::skip]
44        let ret = match self {
45            Alias(l, r) => Alias(am(l, f)?, r),
46            Column(_) => self,
47            Literal(_) => self,
48            DataTypeFunction(_) => self,
49            #[cfg(feature = "dtype-struct")]
50            Field(_) => self,
51            BinaryExpr { left, op, right } => {
52                BinaryExpr { left: am(left, &mut f)? , op, right: am(right, f)?}
53            },
54            Cast { expr, dtype, options: strict } => Cast { expr: am(expr, f)?, dtype, options: strict },
55            Sort { expr, options } => Sort { expr: am(expr, f)?, options },
56            Gather { expr, idx, returns_scalar } => Gather { expr: am(expr, &mut f)?, idx: am(idx, f)?, returns_scalar },
57            SortBy { expr, by, sort_options } => SortBy { expr: am(expr, &mut f)?, by: by.into_iter().map(f).collect::<Result<_, _>>()?, sort_options },
58            Agg(agg_expr) => Agg(match agg_expr {
59                Min { input, propagate_nans } => Min { input: am(input, f)?, propagate_nans },
60                Max { input, propagate_nans } => Max { input: am(input, f)?, propagate_nans },
61                Median(x) => Median(am(x, f)?),
62                NUnique(x) => NUnique(am(x, f)?),
63                First(x) => First(am(x, f)?),
64                Last(x) => Last(am(x, f)?),
65                Mean(x) => Mean(am(x, f)?),
66                Implode(x) => Implode(am(x, f)?),
67                Count(x, nulls) => Count(am(x, f)?, nulls),
68                Quantile { expr, quantile, method: interpol } => Quantile { expr: am(expr, &mut f)?, quantile: am(quantile, f)?, method: interpol },
69                Sum(x) => Sum(am(x, f)?),
70                AggGroups(x) => AggGroups(am(x, f)?),
71                Std(x, ddf) => Std(am(x, f)?, ddf),
72                Var(x, ddf) => Var(am(x, f)?, ddf),
73            }),
74            Ternary { predicate, truthy, falsy } => Ternary { predicate: am(predicate, &mut f)?, truthy: am(truthy, &mut f)?, falsy: am(falsy, f)? },
75            Function { input, function } => Function { input: input.into_iter().map(f).collect::<Result<_, _>>()?, function },
76            Explode { input, skip_empty } => Explode { input: am(input, f)?, skip_empty },
77            Filter { input, by } => Filter { input: am(input, &mut f)?, by: am(by, f)? },
78            Window { function, partition_by, order_by, options } => {
79                let partition_by = partition_by.into_iter().map(&mut f).collect::<Result<_, _>>()?;
80                Window { function: am(function, f)?, partition_by, order_by, options }
81            },
82            Slice { input, offset, length } => Slice { input: am(input, &mut f)?, offset: am(offset, &mut f)?, length: am(length, f)? },
83            KeepName(expr) => KeepName(am(expr, f)?),
84            Len => Len,
85            RenameAlias { function, expr } => RenameAlias { function, expr: am(expr, f)? },
86            AnonymousFunction { input, function, output_type, options, fmt_str } => {
87                AnonymousFunction { input: input.into_iter().map(f).collect::<Result<_, _>>()?, function, output_type, options, fmt_str }
88            },
89            Eval { expr: input, evaluation, variant } => Eval { expr: am(input, &mut f)?, evaluation: am(evaluation, f)?, variant },
90            SubPlan(_, _) => self,
91            Selector(_) => self,
92        };
93        Ok(ret)
94    }
95}
96
97#[derive(Copy, Clone, Debug)]
98pub struct AexprNode {
99    node: Node,
100}
101
102impl AexprNode {
103    pub fn new(node: Node) -> Self {
104        Self { node }
105    }
106
107    /// Get the `Node`.
108    pub fn node(&self) -> Node {
109        self.node
110    }
111
112    pub fn to_aexpr<'a>(&self, arena: &'a Arena<AExpr>) -> &'a AExpr {
113        arena.get(self.node)
114    }
115
116    pub fn to_expr(&self, arena: &Arena<AExpr>) -> Expr {
117        node_to_expr(self.node, arena)
118    }
119
120    pub fn to_field(&self, schema: &Schema, arena: &Arena<AExpr>) -> PolarsResult<Field> {
121        let aexpr = arena.get(self.node);
122        aexpr.to_field(schema, Context::Default, arena)
123    }
124
125    pub fn assign(&mut self, ae: AExpr, arena: &mut Arena<AExpr>) {
126        let node = arena.add(ae);
127        self.node = node;
128    }
129
130    #[cfg(feature = "cse")]
131    pub(crate) fn is_leaf(&self, arena: &Arena<AExpr>) -> bool {
132        matches!(self.to_aexpr(arena), AExpr::Column(_) | AExpr::Literal(_))
133    }
134
135    #[cfg(feature = "cse")]
136    pub(crate) fn hashable_and_cmp<'a>(&self, arena: &'a Arena<AExpr>) -> AExprArena<'a> {
137        AExprArena {
138            node: self.node,
139            arena,
140        }
141    }
142}
143
144#[cfg(feature = "cse")]
145pub struct AExprArena<'a> {
146    node: Node,
147    arena: &'a Arena<AExpr>,
148}
149
150#[cfg(feature = "cse")]
151impl Debug for AExprArena<'_> {
152    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
153        write!(f, "AexprArena: {}", self.node.0)
154    }
155}
156
157impl AExpr {
158    #[cfg(feature = "cse")]
159    fn is_equal_node(&self, other: &Self) -> bool {
160        use AExpr::*;
161        match (self, other) {
162            (Column(l), Column(r)) => l == r,
163            (Literal(l), Literal(r)) => l == r,
164            (Window { options: l, .. }, Window { options: r, .. }) => l == r,
165            (
166                Cast {
167                    options: strict_l,
168                    dtype: dtl,
169                    ..
170                },
171                Cast {
172                    options: strict_r,
173                    dtype: dtr,
174                    ..
175                },
176            ) => strict_l == strict_r && dtl == dtr,
177            (Sort { options: l, .. }, Sort { options: r, .. }) => l == r,
178            (Gather { .. }, Gather { .. })
179            | (Filter { .. }, Filter { .. })
180            | (Ternary { .. }, Ternary { .. })
181            | (Len, Len)
182            | (Slice { .. }, Slice { .. }) => true,
183            (
184                Explode {
185                    expr: _,
186                    skip_empty: l_skip_empty,
187                },
188                Explode {
189                    expr: _,
190                    skip_empty: r_skip_empty,
191                },
192            ) => l_skip_empty == r_skip_empty,
193            (
194                SortBy {
195                    sort_options: l_sort_options,
196                    ..
197                },
198                SortBy {
199                    sort_options: r_sort_options,
200                    ..
201                },
202            ) => l_sort_options == r_sort_options,
203            (Agg(l), Agg(r)) => l.equal_nodes(r),
204            (
205                Function {
206                    input: il,
207                    function: fl,
208                    options: ol,
209                },
210                Function {
211                    input: ir,
212                    function: fr,
213                    options: or,
214                },
215            ) => {
216                fl == fr && ol == or && {
217                    let mut all_same_name = true;
218                    for (l, r) in il.iter().zip(ir) {
219                        all_same_name &= l.output_name() == r.output_name()
220                    }
221
222                    all_same_name
223                }
224            },
225            (AnonymousFunction { .. }, AnonymousFunction { .. }) => false,
226            (BinaryExpr { op: l, .. }, BinaryExpr { op: r, .. }) => l == r,
227            _ => false,
228        }
229    }
230}
231
232#[cfg(feature = "cse")]
233impl<'a> AExprArena<'a> {
234    pub fn new(node: Node, arena: &'a Arena<AExpr>) -> Self {
235        Self { node, arena }
236    }
237    pub fn to_aexpr(&self) -> &'a AExpr {
238        self.arena.get(self.node)
239    }
240
241    // Check single node on equality
242    pub fn is_equal_single(&self, other: &Self) -> bool {
243        let self_ae = self.to_aexpr();
244        let other_ae = other.to_aexpr();
245        self_ae.is_equal_node(other_ae)
246    }
247}
248
249#[cfg(feature = "cse")]
250impl PartialEq for AExprArena<'_> {
251    fn eq(&self, other: &Self) -> bool {
252        let mut scratch1 = unitvec![];
253        let mut scratch2 = unitvec![];
254
255        scratch1.push(self.node);
256        scratch2.push(other.node);
257
258        loop {
259            match (scratch1.pop(), scratch2.pop()) {
260                (Some(l), Some(r)) => {
261                    let l = Self::new(l, self.arena);
262                    let r = Self::new(r, self.arena);
263
264                    if !l.is_equal_single(&r) {
265                        return false;
266                    }
267
268                    l.to_aexpr().inputs_rev(&mut scratch1);
269                    r.to_aexpr().inputs_rev(&mut scratch2);
270                },
271                (None, None) => return true,
272                _ => return false,
273            }
274        }
275    }
276}
277
278impl TreeWalker for AexprNode {
279    type Arena = Arena<AExpr>;
280    fn apply_children<F: FnMut(&Self, &Self::Arena) -> PolarsResult<VisitRecursion>>(
281        &self,
282        op: &mut F,
283        arena: &Self::Arena,
284    ) -> PolarsResult<VisitRecursion> {
285        let mut scratch = unitvec![];
286
287        self.to_aexpr(arena).inputs_rev(&mut scratch);
288        for node in scratch.as_slice() {
289            let aenode = AexprNode::new(*node);
290            match op(&aenode, arena)? {
291                // let the recursion continue
292                VisitRecursion::Continue | VisitRecursion::Skip => {},
293                // early stop
294                VisitRecursion::Stop => return Ok(VisitRecursion::Stop),
295            }
296        }
297        Ok(VisitRecursion::Continue)
298    }
299
300    fn map_children<F: FnMut(Self, &mut Self::Arena) -> PolarsResult<Self>>(
301        mut self,
302        op: &mut F,
303        arena: &mut Self::Arena,
304    ) -> PolarsResult<Self> {
305        let mut scratch = unitvec![];
306
307        let ae = arena.get(self.node).clone();
308        ae.inputs_rev(&mut scratch);
309
310        // rewrite the nodes
311        for node in scratch.as_mut_slice() {
312            let aenode = AexprNode::new(*node);
313            *node = op(aenode, arena)?.node;
314        }
315
316        scratch.as_mut_slice().reverse();
317        let ae = ae.replace_inputs(&scratch);
318        self.node = arena.add(ae);
319        Ok(self)
320    }
321}