polars_plan/dsl/
struct_.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use super::*;
use crate::plans::conversion::is_regex_projection;

/// Specialized expressions for Struct dtypes.
pub struct StructNameSpace(pub(crate) Expr);

impl StructNameSpace {
    pub fn field_by_index(self, index: i64) -> Expr {
        self.0
            .map_private(FunctionExpr::StructExpr(StructFunction::FieldByIndex(
                index,
            )))
            .with_function_options(|mut options| {
                options.flags |= FunctionFlags::ALLOW_RENAME;
                options
            })
    }

    /// Retrieve one or multiple of the fields of this [`StructChunked`] as a new Series.
    /// This expression also expands the `"*"` wildcard column.
    pub fn field_by_names<I, S>(self, names: I) -> Expr
    where
        I: IntoIterator<Item = S>,
        S: Into<PlSmallStr>,
    {
        self.field_by_names_impl(names.into_iter().map(|x| x.into()).collect())
    }

    fn field_by_names_impl(self, names: Arc<[PlSmallStr]>) -> Expr {
        self.0
            .map_private(FunctionExpr::StructExpr(StructFunction::MultipleFields(
                names,
            )))
            .with_function_options(|mut options| {
                options.flags |= FunctionFlags::ALLOW_RENAME;
                options
            })
    }

    /// Retrieve one of the fields of this [`StructChunked`] as a new Series.
    /// This expression also supports wildcard "*" and regex expansion.
    pub fn field_by_name(self, name: &str) -> Expr {
        if name == "*" || is_regex_projection(name) {
            return self.field_by_names([name]);
        }
        self.0
            .map_private(FunctionExpr::StructExpr(StructFunction::FieldByName(
                name.into(),
            )))
            .with_function_options(|mut options| {
                options.flags |= FunctionFlags::ALLOW_RENAME;
                options
            })
    }

    /// Rename the fields of the [`StructChunked`].
    pub fn rename_fields<I, S>(self, names: I) -> Expr
    where
        I: IntoIterator<Item = S>,
        S: Into<PlSmallStr>,
    {
        self._rename_fields_impl(names.into_iter().map(|x| x.into()).collect())
    }

    pub fn _rename_fields_impl(self, names: Arc<[PlSmallStr]>) -> Expr {
        self.0
            .map_private(FunctionExpr::StructExpr(StructFunction::RenameFields(
                names,
            )))
    }

    #[cfg(feature = "json")]
    pub fn json_encode(self) -> Expr {
        self.0
            .map_private(FunctionExpr::StructExpr(StructFunction::JsonEncode))
    }

    pub fn with_fields(self, fields: Vec<Expr>) -> PolarsResult<Expr> {
        fn materialize_field(this: &Expr, field: Expr) -> PolarsResult<Expr> {
            field.try_map_expr(|e| match e {
                Expr::Field(names) => {
                    let this = this.clone().struct_();
                    Ok(if names.len() == 1 {
                        this.field_by_name(names[0].as_ref())
                    } else {
                        this.field_by_names_impl(names)
                    })
                },
                Expr::Exclude(_, _) => {
                    polars_bail!(InvalidOperation: "'exclude' not allowed in 'field'")
                },
                _ => Ok(e),
            })
        }

        let mut new_fields = Vec::with_capacity(fields.len());
        new_fields.push(Default::default());

        for e in fields.into_iter().map(|e| materialize_field(&self.0, e)) {
            new_fields.push(e?)
        }
        new_fields[0] = self.0;
        Ok(Expr::Function {
            input: new_fields,
            function: FunctionExpr::StructExpr(StructFunction::WithFields),
            options: FunctionOptions {
                collect_groups: ApplyOptions::ElementWise,
                flags: FunctionFlags::default() & !FunctionFlags::ALLOW_GROUP_AWARE
                    | FunctionFlags::PASS_NAME_TO_APPLY
                    | FunctionFlags::INPUT_WILDCARD_EXPANSION,
                ..Default::default()
            },
        })
    }
}