Path: blob/main/crates/polars-plan/src/plans/aexpr/function_expr/bitwise.rs
7889 views
use std::fmt;12use polars_core::prelude::*;3use strum_macros::IntoStaticStr;45use crate::plans::aexpr::function_expr::{FieldsMapper, FunctionOptions};6use crate::prelude::FunctionFlags;78#[cfg_attr(feature = "ir_serde", derive(serde::Serialize, serde::Deserialize))]9#[derive(Clone, Copy, PartialEq, Debug, Eq, Hash, IntoStaticStr)]10#[strum(serialize_all = "snake_case")]11pub enum IRBitwiseFunction {12CountOnes,13CountZeros,1415LeadingOnes,16LeadingZeros,1718TrailingOnes,19TrailingZeros,2021// Bitwise Aggregations22And,23Or,24Xor,25}2627impl IRBitwiseFunction {28pub(super) fn get_field(&self, mapper: FieldsMapper) -> PolarsResult<Field> {29mapper.try_map_dtype(|dtype| {30let is_valid = dtype.is_bool() || dtype.is_integer();31if !is_valid {32polars_bail!(InvalidOperation: "dtype {} not supported in '{}' operation", dtype, self);33}3435match self {36Self::CountOnes |37Self::CountZeros |38Self::LeadingOnes |39Self::LeadingZeros |40Self::TrailingOnes |41Self::TrailingZeros => Ok(DataType::UInt32),42Self::And |43Self::Or |44Self::Xor => Ok(dtype.clone()),45}46})47}4849pub fn function_options(&self) -> FunctionOptions {50use IRBitwiseFunction as B;51match self {52B::CountOnes53| B::CountZeros54| B::LeadingOnes55| B::LeadingZeros56| B::TrailingOnes57| B::TrailingZeros => FunctionOptions::elementwise(),58B::And | B::Or | B::Xor => FunctionOptions::aggregation()59.with_flags(|f| f | FunctionFlags::NON_ORDER_OBSERVING),60}61}62}6364impl fmt::Display for IRBitwiseFunction {65fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {66use IRBitwiseFunction as B;6768let s = match self {69B::CountOnes => "count_ones",70B::CountZeros => "count_zeros",71B::LeadingOnes => "leading_ones",72B::LeadingZeros => "leading_zeros",73B::TrailingOnes => "trailing_ones",74B::TrailingZeros => "trailing_zeros",7576B::And => "and",77B::Or => "or",78B::Xor => "xor",79};8081f.write_str(s)82}83}848586