-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rust, python): impl hex and base64 for binary (#5892)
- Loading branch information
1 parent
ab3b2c2
commit 3dccb4a
Showing
26 changed files
with
579 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use {base64, hex}; | ||
|
||
use crate::prelude::*; | ||
|
||
impl BinaryChunked { | ||
pub fn hex_decode(&self) -> PolarsResult<BinaryChunked> { | ||
self.try_apply(|s| { | ||
let bytes = | ||
hex::decode(s).map_err(|e| PolarsError::ComputeError(e.to_string().into()))?; | ||
Ok(bytes.into()) | ||
}) | ||
} | ||
|
||
pub fn hex_encode(&self) -> Series { | ||
self.apply(|s| hex::encode(s).into_bytes().into()) | ||
.cast_unchecked(&DataType::Utf8) | ||
.unwrap() | ||
} | ||
|
||
pub fn base64_decode(&self) -> PolarsResult<BinaryChunked> { | ||
self.try_apply(|s| { | ||
let bytes = | ||
base64::decode(s).map_err(|e| PolarsError::ComputeError(e.to_string().into()))?; | ||
Ok(bytes.into()) | ||
}) | ||
} | ||
|
||
pub fn base64_encode(&self) -> Series { | ||
self.apply(|s| base64::encode(s).into_bytes().into()) | ||
.cast_unchecked(&DataType::Utf8) | ||
.unwrap() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#[cfg(feature = "binary_encoding")] | ||
mod encoding; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use super::function_expr::BinaryFunction; | ||
use super::*; | ||
/// Specialized expressions for [`Series`] of [`DataType::Utf8`]. | ||
pub struct BinaryNameSpace(pub(crate) Expr); | ||
|
||
impl BinaryNameSpace { | ||
/// Check if a binary value contains a literal binary. | ||
pub fn contains_literal<S: AsRef<[u8]>>(self, pat: S) -> Expr { | ||
let pat = pat.as_ref().into(); | ||
self.0 | ||
.map_private(BinaryFunction::Contains { pat, literal: true }.into()) | ||
} | ||
|
||
/// Check if a binary value ends with the given sequence. | ||
pub fn ends_with<S: AsRef<[u8]>>(self, sub: S) -> Expr { | ||
let sub = sub.as_ref().into(); | ||
self.0.map_private(BinaryFunction::EndsWith(sub).into()) | ||
} | ||
|
||
/// Check if a binary value starts with the given sequence. | ||
pub fn starts_with<S: AsRef<[u8]>>(self, sub: S) -> Expr { | ||
let sub = sub.as_ref().into(); | ||
self.0.map_private(BinaryFunction::StartsWith(sub).into()) | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
polars/polars-lazy/polars-plan/src/dsl/function_expr/binary.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#[cfg(feature = "serde")] | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::*; | ||
|
||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[derive(Clone, PartialEq, Debug, Eq, Hash)] | ||
pub enum BinaryFunction { | ||
Contains { pat: Vec<u8>, literal: bool }, | ||
StartsWith(Vec<u8>), | ||
EndsWith(Vec<u8>), | ||
} | ||
|
||
impl Display for BinaryFunction { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
use self::*; | ||
let s = match self { | ||
BinaryFunction::Contains { .. } => "contains", | ||
BinaryFunction::StartsWith(_) => "starts_with", | ||
BinaryFunction::EndsWith(_) => "ends_with", | ||
}; | ||
|
||
write!(f, "str.{s}") | ||
} | ||
} | ||
|
||
pub(super) fn contains(s: &Series, pat: &[u8], literal: bool) -> PolarsResult<Series> { | ||
let ca = s.binary()?; | ||
if literal { | ||
ca.contains_literal(pat).map(|ca| ca.into_series()) | ||
} else { | ||
ca.contains(pat).map(|ca| ca.into_series()) | ||
} | ||
} | ||
|
||
pub(super) fn ends_with(s: &Series, sub: &[u8]) -> PolarsResult<Series> { | ||
let ca = s.binary()?; | ||
Ok(ca.ends_with(sub).into_series()) | ||
} | ||
pub(super) fn starts_with(s: &Series, sub: &[u8]) -> PolarsResult<Series> { | ||
let ca = s.binary()?; | ||
Ok(ca.starts_with(sub).into_series()) | ||
} | ||
|
||
impl From<BinaryFunction> for FunctionExpr { | ||
fn from(b: BinaryFunction) -> Self { | ||
FunctionExpr::BinaryExpr(b) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#[cfg_attr(docsrs, doc(cfg(feature = "dtype-binary")))] | ||
mod namespace; | ||
|
||
pub use namespace::*; | ||
use polars_core::prelude::*; | ||
|
||
pub trait AsBinary { | ||
fn as_binary(&self) -> &BinaryChunked; | ||
} | ||
|
||
impl AsBinary for BinaryChunked { | ||
fn as_binary(&self) -> &BinaryChunked { | ||
self | ||
} | ||
} |
Oops, something went wrong.