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/binary.rs
8353 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 IRBinaryFunction {
6
Contains,
7
StartsWith,
8
EndsWith,
9
#[cfg(feature = "binary_encoding")]
10
HexDecode(bool),
11
#[cfg(feature = "binary_encoding")]
12
HexEncode,
13
#[cfg(feature = "binary_encoding")]
14
Base64Decode(bool),
15
#[cfg(feature = "binary_encoding")]
16
Base64Encode,
17
Size,
18
#[cfg(feature = "binary_encoding")]
19
Reinterpret(DataType, bool),
20
Slice,
21
Head,
22
Tail,
23
Get(bool),
24
}
25
26
impl IRBinaryFunction {
27
pub(super) fn get_field(&self, mapper: FieldsMapper) -> PolarsResult<Field> {
28
use IRBinaryFunction::*;
29
match self {
30
Contains => mapper.with_dtype(DataType::Boolean),
31
EndsWith | StartsWith => mapper.with_dtype(DataType::Boolean),
32
#[cfg(feature = "binary_encoding")]
33
HexDecode(_) | Base64Decode(_) => mapper.with_same_dtype(),
34
#[cfg(feature = "binary_encoding")]
35
HexEncode | Base64Encode => mapper.with_dtype(DataType::String),
36
Size => mapper.with_dtype(DataType::UInt32),
37
#[cfg(feature = "binary_encoding")]
38
Reinterpret(dtype, _) => mapper.with_dtype(dtype.clone()),
39
Slice | Head | Tail => mapper.with_same_dtype(),
40
Get(_) => mapper.with_dtype(DataType::UInt8),
41
}
42
}
43
44
pub fn function_options(&self) -> FunctionOptions {
45
use IRBinaryFunction as B;
46
match self {
47
B::Contains | B::StartsWith | B::EndsWith => {
48
FunctionOptions::elementwise().with_supertyping(Default::default())
49
},
50
B::Size => FunctionOptions::elementwise(),
51
#[cfg(feature = "binary_encoding")]
52
B::HexDecode(_)
53
| B::HexEncode
54
| B::Base64Decode(_)
55
| B::Base64Encode
56
| B::Reinterpret(_, _) => FunctionOptions::elementwise(),
57
B::Slice | B::Head | B::Tail | B::Get(_) => FunctionOptions::elementwise(),
58
}
59
}
60
}
61
62
impl Display for IRBinaryFunction {
63
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
64
use IRBinaryFunction::*;
65
let s = match self {
66
Contains => "contains",
67
StartsWith => "starts_with",
68
EndsWith => "ends_with",
69
#[cfg(feature = "binary_encoding")]
70
HexDecode(_) => "hex_decode",
71
#[cfg(feature = "binary_encoding")]
72
HexEncode => "hex_encode",
73
#[cfg(feature = "binary_encoding")]
74
Base64Decode(_) => "base64_decode",
75
#[cfg(feature = "binary_encoding")]
76
Base64Encode => "base64_encode",
77
Size => "size_bytes",
78
#[cfg(feature = "binary_encoding")]
79
Reinterpret(_, _) => "reinterpret",
80
Slice => "slice",
81
Head => "head",
82
Tail => "tail",
83
Get(_) => "get",
84
};
85
write!(f, "bin.{s}")
86
}
87
}
88
89
impl From<IRBinaryFunction> for IRFunctionExpr {
90
fn from(b: IRBinaryFunction) -> Self {
91
IRFunctionExpr::BinaryExpr(b)
92
}
93
}
94
95