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/bitwise.rs
7889 views
1
use std::fmt;
2
3
use polars_core::prelude::*;
4
use strum_macros::IntoStaticStr;
5
6
use crate::plans::aexpr::function_expr::{FieldsMapper, FunctionOptions};
7
use crate::prelude::FunctionFlags;
8
9
#[cfg_attr(feature = "ir_serde", derive(serde::Serialize, serde::Deserialize))]
10
#[derive(Clone, Copy, PartialEq, Debug, Eq, Hash, IntoStaticStr)]
11
#[strum(serialize_all = "snake_case")]
12
pub enum IRBitwiseFunction {
13
CountOnes,
14
CountZeros,
15
16
LeadingOnes,
17
LeadingZeros,
18
19
TrailingOnes,
20
TrailingZeros,
21
22
// Bitwise Aggregations
23
And,
24
Or,
25
Xor,
26
}
27
28
impl IRBitwiseFunction {
29
pub(super) fn get_field(&self, mapper: FieldsMapper) -> PolarsResult<Field> {
30
mapper.try_map_dtype(|dtype| {
31
let is_valid = dtype.is_bool() || dtype.is_integer();
32
if !is_valid {
33
polars_bail!(InvalidOperation: "dtype {} not supported in '{}' operation", dtype, self);
34
}
35
36
match self {
37
Self::CountOnes |
38
Self::CountZeros |
39
Self::LeadingOnes |
40
Self::LeadingZeros |
41
Self::TrailingOnes |
42
Self::TrailingZeros => Ok(DataType::UInt32),
43
Self::And |
44
Self::Or |
45
Self::Xor => Ok(dtype.clone()),
46
}
47
})
48
}
49
50
pub fn function_options(&self) -> FunctionOptions {
51
use IRBitwiseFunction as B;
52
match self {
53
B::CountOnes
54
| B::CountZeros
55
| B::LeadingOnes
56
| B::LeadingZeros
57
| B::TrailingOnes
58
| B::TrailingZeros => FunctionOptions::elementwise(),
59
B::And | B::Or | B::Xor => FunctionOptions::aggregation()
60
.with_flags(|f| f | FunctionFlags::NON_ORDER_OBSERVING),
61
}
62
}
63
}
64
65
impl fmt::Display for IRBitwiseFunction {
66
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
67
use IRBitwiseFunction as B;
68
69
let s = match self {
70
B::CountOnes => "count_ones",
71
B::CountZeros => "count_zeros",
72
B::LeadingOnes => "leading_ones",
73
B::LeadingZeros => "leading_zeros",
74
B::TrailingOnes => "trailing_ones",
75
B::TrailingZeros => "trailing_zeros",
76
77
B::And => "and",
78
B::Or => "or",
79
B::Xor => "xor",
80
};
81
82
f.write_str(s)
83
}
84
}
85
86