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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use super::*;
use crate::plans::conversion::rewrite_projections;

// Except for Opaque functions, this only has the DSL name of the function.
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum DslFunction {
    FunctionNode(FunctionNode),
    Explode {
        columns: Vec<Expr>,
    },
    Unpivot {
        args: UnpivotArgs,
    },
    RowIndex {
        name: Arc<str>,
        offset: Option<IdxSize>,
    },
    Rename {
        existing: Arc<[SmartString]>,
        new: Arc<[SmartString]>,
    },
    Stats(StatsFunction),
    /// FillValue
    FillNan(Expr),
    Drop(DropFunction),
}

#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct DropFunction {
    /// Columns that are going to be dropped
    pub(crate) to_drop: PlHashSet<String>,
    /// If `true`, performs a check for each item in `to_drop` against the schema. Returns an
    /// `ColumnNotFound` error if the column does not exist in the schema.
    pub(crate) strict: bool,
}

#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum StatsFunction {
    Var {
        ddof: u8,
    },
    Std {
        ddof: u8,
    },
    Quantile {
        quantile: Expr,
        interpol: QuantileInterpolOptions,
    },
    Median,
    Mean,
    Sum,
    Min,
    Max,
}

impl DslFunction {
    pub(crate) fn into_function_node(self, input_schema: &Schema) -> PolarsResult<FunctionNode> {
        let function = match self {
            DslFunction::Explode { columns } => {
                let columns = rewrite_projections(columns, input_schema, &[])?;
                // columns to string
                let columns = columns
                    .iter()
                    .map(|e| {
                        let Expr::Column(name) = e else {
                            polars_bail!(InvalidOperation: "expected column expression")
                        };

                        polars_ensure!(input_schema.contains(name), ColumnNotFound: "{name}");

                        Ok(name.clone())
                    })
                    .collect::<PolarsResult<Arc<[Arc<str>]>>>()?;
                FunctionNode::Explode {
                    columns,
                    schema: Default::default(),
                }
            },
            DslFunction::Unpivot { args } => FunctionNode::Unpivot {
                args: Arc::new(args),
                schema: Default::default(),
            },
            DslFunction::FunctionNode(func) => func,
            DslFunction::RowIndex { name, offset } => FunctionNode::RowIndex {
                name,
                offset,
                schema: Default::default(),
            },
            DslFunction::Rename { existing, new } => {
                let swapping = new.iter().any(|name| input_schema.get(name).is_some());

                // Check if the name exists.
                for name in existing.iter() {
                    let _ = input_schema.try_get(name)?;
                }

                FunctionNode::Rename {
                    existing,
                    new,
                    swapping,
                    schema: Default::default(),
                }
            },
            DslFunction::Stats(_) | DslFunction::FillNan(_) | DslFunction::Drop(_) => {
                // We should not reach this.
                panic!("impl error")
            },
        };
        Ok(function)
    }
}

impl Debug for DslFunction {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self}")
    }
}

impl Display for DslFunction {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        use DslFunction::*;
        match self {
            FunctionNode(inner) => write!(f, "{inner}"),
            Explode { .. } => write!(f, "EXPLODE"),
            Unpivot { .. } => write!(f, "UNPIVOT"),
            RowIndex { .. } => write!(f, "WITH ROW INDEX"),
            Stats(_) => write!(f, "STATS"),
            FillNan(_) => write!(f, "FILL NAN"),
            Drop(_) => write!(f, "DROP"),
            Rename { .. } => write!(f, "RENAME"),
        }
    }
}

impl From<FunctionNode> for DslFunction {
    fn from(value: FunctionNode) -> Self {
        DslFunction::FunctionNode(value)
    }
}