polars_plan/plans/
format.rs

1use std::fmt;
2
3use crate::prelude::*;
4
5impl fmt::Display for Expr {
6    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7        fmt::Debug::fmt(self, f)
8    }
9}
10
11impl fmt::Debug for Expr {
12    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13        use Expr::*;
14        match self {
15            Window {
16                function,
17                partition_by,
18                order_by,
19                options,
20            } => match options {
21                #[cfg(feature = "dynamic_group_by")]
22                WindowType::Rolling(options) => {
23                    write!(
24                        f,
25                        "{:?}.rolling(by='{}', offset={}, period={})",
26                        function, options.index_column, options.offset, options.period
27                    )
28                },
29                _ => {
30                    if let Some((order_by, _)) = order_by {
31                        write!(f, "{function:?}.over(partition_by: {partition_by:?}, order_by: {order_by:?})")
32                    } else {
33                        write!(f, "{function:?}.over({partition_by:?})")
34                    }
35                },
36            },
37            Nth(i) => write!(f, "nth({i})"),
38            Len => write!(f, "len()"),
39            Explode(expr) => write!(f, "{expr:?}.explode()"),
40            Alias(expr, name) => write!(f, "{expr:?}.alias(\"{name}\")"),
41            Column(name) => write!(f, "col(\"{name}\")"),
42            Literal(v) => {
43                match v {
44                    LiteralValue::String(v) => {
45                        // dot breaks with debug fmt due to \"
46                        write!(f, "String({v})")
47                    },
48                    _ => {
49                        write!(f, "{v:?}")
50                    },
51                }
52            },
53            BinaryExpr { left, op, right } => write!(f, "[({left:?}) {op:?} ({right:?})]"),
54            Sort { expr, options } => {
55                if options.descending {
56                    write!(f, "{expr:?}.sort(desc)")
57                } else {
58                    write!(f, "{expr:?}.sort(asc)")
59                }
60            },
61            SortBy {
62                expr,
63                by,
64                sort_options,
65            } => {
66                write!(
67                    f,
68                    "{expr:?}.sort_by(by={by:?}, sort_option={sort_options:?})",
69                )
70            },
71            Filter { input, by } => {
72                write!(f, "{input:?}.filter({by:?})")
73            },
74            Gather {
75                expr,
76                idx,
77                returns_scalar,
78            } => {
79                if *returns_scalar {
80                    write!(f, "{expr:?}.get({idx:?})")
81                } else {
82                    write!(f, "{expr:?}.gather({idx:?})")
83                }
84            },
85            SubPlan(lf, _) => {
86                write!(f, ".subplan({lf:?})")
87            },
88            Agg(agg) => {
89                use AggExpr::*;
90                match agg {
91                    Min {
92                        input,
93                        propagate_nans,
94                    } => {
95                        if *propagate_nans {
96                            write!(f, "{input:?}.nan_min()")
97                        } else {
98                            write!(f, "{input:?}.min()")
99                        }
100                    },
101                    Max {
102                        input,
103                        propagate_nans,
104                    } => {
105                        if *propagate_nans {
106                            write!(f, "{input:?}.nan_max()")
107                        } else {
108                            write!(f, "{input:?}.max()")
109                        }
110                    },
111                    Median(expr) => write!(f, "{expr:?}.median()"),
112                    Mean(expr) => write!(f, "{expr:?}.mean()"),
113                    First(expr) => write!(f, "{expr:?}.first()"),
114                    Last(expr) => write!(f, "{expr:?}.last()"),
115                    Implode(expr) => write!(f, "{expr:?}.list()"),
116                    NUnique(expr) => write!(f, "{expr:?}.n_unique()"),
117                    Sum(expr) => write!(f, "{expr:?}.sum()"),
118                    AggGroups(expr) => write!(f, "{expr:?}.groups()"),
119                    Count(expr, _) => write!(f, "{expr:?}.count()"),
120                    Var(expr, _) => write!(f, "{expr:?}.var()"),
121                    Std(expr, _) => write!(f, "{expr:?}.std()"),
122                    Quantile { expr, .. } => write!(f, "{expr:?}.quantile()"),
123                }
124            },
125            Cast {
126                expr,
127                dtype,
128                options,
129            } => {
130                if options.strict() {
131                    write!(f, "{expr:?}.strict_cast({dtype:?})")
132                } else {
133                    write!(f, "{expr:?}.cast({dtype:?})")
134                }
135            },
136            Ternary {
137                predicate,
138                truthy,
139                falsy,
140            } => write!(
141                f,
142                ".when({predicate:?}).then({truthy:?}).otherwise({falsy:?})",
143            ),
144            Function {
145                input, function, ..
146            } => {
147                if input.len() >= 2 {
148                    write!(f, "{:?}.{function}({:?})", input[0], &input[1..])
149                } else {
150                    write!(f, "{:?}.{function}()", input[0])
151                }
152            },
153            AnonymousFunction { input, options, .. } => {
154                if input.len() >= 2 {
155                    write!(f, "{:?}.{}({:?})", input[0], options.fmt_str, &input[1..])
156                } else {
157                    write!(f, "{:?}.{}()", input[0], options.fmt_str)
158                }
159            },
160            Slice {
161                input,
162                offset,
163                length,
164            } => write!(f, "{input:?}.slice(offset={offset:?}, length={length:?})",),
165            Wildcard => write!(f, "*"),
166            Exclude(column, names) => write!(f, "{column:?}.exclude({names:?})"),
167            KeepName(e) => write!(f, "{e:?}.name.keep()"),
168            RenameAlias { expr, .. } => write!(f, ".rename_alias({expr:?})"),
169            Columns(names) => write!(f, "cols({names:?})"),
170            DtypeColumn(dt) => write!(f, "dtype_columns({dt:?})"),
171            IndexColumn(idxs) => write!(f, "index_columns({idxs:?})"),
172            Selector(_) => write!(f, "selector"),
173            #[cfg(feature = "dtype-struct")]
174            Field(names) => write!(f, ".field({names:?})"),
175        }
176    }
177}