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