Path: blob/main/crates/polars-plan/src/plans/aexpr/function_expr/binary.rs
8353 views
use super::*;12#[cfg_attr(feature = "ir_serde", derive(serde::Serialize, serde::Deserialize))]3#[derive(Clone, PartialEq, Debug, Eq, Hash)]4pub enum IRBinaryFunction {5Contains,6StartsWith,7EndsWith,8#[cfg(feature = "binary_encoding")]9HexDecode(bool),10#[cfg(feature = "binary_encoding")]11HexEncode,12#[cfg(feature = "binary_encoding")]13Base64Decode(bool),14#[cfg(feature = "binary_encoding")]15Base64Encode,16Size,17#[cfg(feature = "binary_encoding")]18Reinterpret(DataType, bool),19Slice,20Head,21Tail,22Get(bool),23}2425impl IRBinaryFunction {26pub(super) fn get_field(&self, mapper: FieldsMapper) -> PolarsResult<Field> {27use IRBinaryFunction::*;28match self {29Contains => mapper.with_dtype(DataType::Boolean),30EndsWith | StartsWith => mapper.with_dtype(DataType::Boolean),31#[cfg(feature = "binary_encoding")]32HexDecode(_) | Base64Decode(_) => mapper.with_same_dtype(),33#[cfg(feature = "binary_encoding")]34HexEncode | Base64Encode => mapper.with_dtype(DataType::String),35Size => mapper.with_dtype(DataType::UInt32),36#[cfg(feature = "binary_encoding")]37Reinterpret(dtype, _) => mapper.with_dtype(dtype.clone()),38Slice | Head | Tail => mapper.with_same_dtype(),39Get(_) => mapper.with_dtype(DataType::UInt8),40}41}4243pub fn function_options(&self) -> FunctionOptions {44use IRBinaryFunction as B;45match self {46B::Contains | B::StartsWith | B::EndsWith => {47FunctionOptions::elementwise().with_supertyping(Default::default())48},49B::Size => FunctionOptions::elementwise(),50#[cfg(feature = "binary_encoding")]51B::HexDecode(_)52| B::HexEncode53| B::Base64Decode(_)54| B::Base64Encode55| B::Reinterpret(_, _) => FunctionOptions::elementwise(),56B::Slice | B::Head | B::Tail | B::Get(_) => FunctionOptions::elementwise(),57}58}59}6061impl Display for IRBinaryFunction {62fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {63use IRBinaryFunction::*;64let s = match self {65Contains => "contains",66StartsWith => "starts_with",67EndsWith => "ends_with",68#[cfg(feature = "binary_encoding")]69HexDecode(_) => "hex_decode",70#[cfg(feature = "binary_encoding")]71HexEncode => "hex_encode",72#[cfg(feature = "binary_encoding")]73Base64Decode(_) => "base64_decode",74#[cfg(feature = "binary_encoding")]75Base64Encode => "base64_encode",76Size => "size_bytes",77#[cfg(feature = "binary_encoding")]78Reinterpret(_, _) => "reinterpret",79Slice => "slice",80Head => "head",81Tail => "tail",82Get(_) => "get",83};84write!(f, "bin.{s}")85}86}8788impl From<IRBinaryFunction> for IRFunctionExpr {89fn from(b: IRBinaryFunction) -> Self {90IRFunctionExpr::BinaryExpr(b)91}92}939495