Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-plan/src/plans/aexpr/function_expr/cat.rs
7889 views
1
use super::*;
2
3
#[cfg_attr(feature = "ir_serde", derive(serde::Serialize, serde::Deserialize))]
4
#[derive(Clone, PartialEq, Debug, Eq, Hash)]
5
pub enum IRCategoricalFunction {
6
GetCategories,
7
#[cfg(feature = "strings")]
8
LenBytes,
9
#[cfg(feature = "strings")]
10
LenChars,
11
#[cfg(feature = "strings")]
12
StartsWith(String),
13
#[cfg(feature = "strings")]
14
EndsWith(String),
15
#[cfg(feature = "strings")]
16
Slice(i64, Option<usize>),
17
}
18
19
impl IRCategoricalFunction {
20
pub(super) fn get_field(&self, mapper: FieldsMapper) -> PolarsResult<Field> {
21
use IRCategoricalFunction::*;
22
match self {
23
GetCategories => mapper.with_dtype(DataType::String),
24
#[cfg(feature = "strings")]
25
LenBytes => mapper.with_dtype(DataType::UInt32),
26
#[cfg(feature = "strings")]
27
LenChars => mapper.with_dtype(DataType::UInt32),
28
#[cfg(feature = "strings")]
29
StartsWith(_) => mapper.with_dtype(DataType::Boolean),
30
#[cfg(feature = "strings")]
31
EndsWith(_) => mapper.with_dtype(DataType::Boolean),
32
#[cfg(feature = "strings")]
33
Slice(_, _) => mapper.with_dtype(DataType::String),
34
}
35
}
36
37
pub fn function_options(&self) -> FunctionOptions {
38
use IRCategoricalFunction as C;
39
match self {
40
C::GetCategories => FunctionOptions::groupwise(),
41
#[cfg(feature = "strings")]
42
C::LenBytes | C::LenChars | C::StartsWith(_) | C::EndsWith(_) | C::Slice(_, _) => {
43
FunctionOptions::elementwise()
44
},
45
}
46
}
47
}
48
49
impl Display for IRCategoricalFunction {
50
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
51
use IRCategoricalFunction::*;
52
let s = match self {
53
GetCategories => "get_categories",
54
#[cfg(feature = "strings")]
55
LenBytes => "len_bytes",
56
#[cfg(feature = "strings")]
57
LenChars => "len_chars",
58
#[cfg(feature = "strings")]
59
StartsWith(_) => "starts_with",
60
#[cfg(feature = "strings")]
61
EndsWith(_) => "ends_with",
62
#[cfg(feature = "strings")]
63
Slice(_, _) => "slice",
64
};
65
write!(f, "cat.{s}")
66
}
67
}
68
69
impl From<IRCategoricalFunction> for IRFunctionExpr {
70
fn from(func: IRCategoricalFunction) -> Self {
71
IRFunctionExpr::Categorical(func)
72
}
73
}
74
75