Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rust, python): impl hex and base64 for binary #5892

Merged
merged 19 commits into from
Dec 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions polars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ mode = ["polars-core/mode", "polars-lazy/mode"]
take_opt_iter = ["polars-core/take_opt_iter"]
extract_jsonpath = ["polars-core/strings", "polars-ops/extract_jsonpath", "polars-ops/strings"]
string_encoding = ["polars-core/string_encoding", "polars-core/strings"]
binary_encoding = ["polars-core/binary_encoding"]
groupby_list = ["polars-core/groupby_list"]
lazy_regex = ["polars-lazy/regex"]
cum_agg = ["polars-core/cum_agg", "polars-core/cum_agg"]
Expand Down
3 changes: 3 additions & 0 deletions polars/polars-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ string_encoding = ["base64", "hex"]
# support for ObjectChunked<T> (downcastable Series of any type)
object = ["serde_json"]

# extra utilities for BinaryChunked
binary_encoding = ["base64", "hex"]

fmt = ["comfy-table/tty"]
fmt_no_tty = ["comfy-table"]

Expand Down
35 changes: 35 additions & 0 deletions polars/polars-core/src/chunked_array/binary/encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use {base64, hex};

use crate::prelude::*;

impl BinaryChunked {
pub fn hex_decode(&self, strict: Option<bool>) -> PolarsResult<BinaryChunked> {
let ca = self.apply_on_opt(|e| e.and_then(|s| hex::decode(s).map(Into::into).ok()));

if strict.unwrap_or(false) && (ca.null_count() != self.null_count()) {
Err(PolarsError::ComputeError("Unable to decode inputs".into()))
} else {
Ok(ca)
}
}

#[must_use]
pub fn hex_encode(&self) -> BinaryChunked {
self.apply(|s| hex::encode(s).into_bytes().into())
}

pub fn base64_decode(&self, strict: Option<bool>) -> PolarsResult<BinaryChunked> {
ozgrakkurt marked this conversation as resolved.
Show resolved Hide resolved
let ca = self.apply_on_opt(|e| e.and_then(|s| base64::decode(s).map(Into::into).ok()));

if strict.unwrap_or(false) && (ca.null_count() != self.null_count()) {
Err(PolarsError::ComputeError("Unable to decode inputs".into()))
} else {
Ok(ca)
}
}

#[must_use]
pub fn base64_encode(&self) -> BinaryChunked {
self.apply(|s| base64::encode(s).into_bytes().into())
}
}
2 changes: 2 additions & 0 deletions polars/polars-core/src/chunked_array/binary/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(feature = "binary_encoding")]
mod encoding;
2 changes: 2 additions & 0 deletions polars/polars-core/src/chunked_array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub mod kernels;
#[cfg(feature = "ndarray")]
mod ndarray;

#[cfg(feature = "dtype-binary")]
pub mod binary;
mod bitwise;
#[cfg(feature = "object")]
mod drop;
Expand Down