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: Bitwise operations / aggregations #18994

Merged
merged 9 commits into from
Oct 1, 2024
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
285 changes: 285 additions & 0 deletions crates/polars-compute/src/bitwise/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
use std::convert::identity;

use arrow::array::{BooleanArray, PrimitiveArray};
use arrow::datatypes::ArrowDataType;
use arrow::legacy::utils::CustomIterTools;
use bytemuck::Zeroable;

pub trait BitwiseKernel {
type Scalar;

fn count_ones(&self) -> PrimitiveArray<u8>;
fn count_zeros(&self) -> PrimitiveArray<u8>;

fn leading_ones(&self) -> PrimitiveArray<u8>;
fn leading_zeros(&self) -> PrimitiveArray<u8>;

fn trailing_ones(&self) -> PrimitiveArray<u8>;
fn trailing_zeros(&self) -> PrimitiveArray<u8>;

fn reduce_and(&self) -> Option<Self::Scalar>;
fn reduce_or(&self) -> Option<Self::Scalar>;
fn reduce_xor(&self) -> Option<Self::Scalar>;

fn bit_and(lhs: Self::Scalar, rhs: Self::Scalar) -> Self::Scalar;
fn bit_or(lhs: Self::Scalar, rhs: Self::Scalar) -> Self::Scalar;
fn bit_xor(lhs: Self::Scalar, rhs: Self::Scalar) -> Self::Scalar;
}

macro_rules! impl_bitwise_kernel {
($(($T:ty, $to_bits:expr, $from_bits:expr)),+ $(,)?) => {
$(
impl BitwiseKernel for PrimitiveArray<$T> {
type Scalar = $T;

#[inline(never)]
fn count_ones(&self) -> PrimitiveArray<u8> {
PrimitiveArray::new(
ArrowDataType::UInt8,
self.values()
.iter()
.map(|&v| ($to_bits(v).count_ones() & 0xFF) as u8)
.collect_trusted::<Vec<_>>()
.into(),
self.validity().cloned(),
)
}

#[inline(never)]
fn count_zeros(&self) -> PrimitiveArray<u8> {
PrimitiveArray::new(
ArrowDataType::UInt8,
self
.values()
.iter()
.map(|&v| ($to_bits(v).count_zeros() & 0xFF) as u8)
.collect_trusted::<Vec<_>>()
.into(),
self.validity().cloned(),
)
}

#[inline(never)]
fn leading_ones(&self) -> PrimitiveArray<u8> {
PrimitiveArray::new(
ArrowDataType::UInt8,
self.values()
.iter()
.map(|&v| ($to_bits(v).leading_ones() & 0xFF) as u8)
.collect_trusted::<Vec<_>>()
.into(),
self.validity().cloned(),
)
}

#[inline(never)]
fn leading_zeros(&self) -> PrimitiveArray<u8> {
PrimitiveArray::new(
ArrowDataType::UInt8,
self.values()
.iter()
.map(|&v| ($to_bits(v).leading_zeros() & 0xFF) as u8)
.collect_trusted::<Vec<_>>()
.into(),
self.validity().cloned(),
)
}

#[inline(never)]
fn trailing_ones(&self) -> PrimitiveArray<u8> {
PrimitiveArray::new(
ArrowDataType::UInt8,
self.values()
.iter()
.map(|&v| ($to_bits(v).trailing_ones() & 0xFF) as u8)
.collect_trusted::<Vec<_>>()
.into(),
self.validity().cloned(),
)
}

#[inline(never)]
fn trailing_zeros(&self) -> PrimitiveArray<u8> {
PrimitiveArray::new(
ArrowDataType::UInt8,
self.values().iter()
.map(|&v| ($to_bits(v).trailing_zeros() & 0xFF) as u8)
.collect_trusted::<Vec<_>>()
.into(),
self.validity().cloned(),
)
}

#[inline(never)]
fn reduce_and(&self) -> Option<Self::Scalar> {
if self.validity().map_or(false, |v| v.unset_bits() > 0) {
return None;
}

let values = self.values();

if values.is_empty() {
return None;
}

Some($from_bits(values.iter().fold(!$to_bits(<$T>::zeroed()), |a, &b| a & $to_bits(b))))
}

#[inline(never)]
fn reduce_or(&self) -> Option<Self::Scalar> {
if self.validity().map_or(false, |v| v.unset_bits() > 0) {
return None;
}

let values = self.values();

if values.is_empty() {
return None;
}

Some($from_bits(values.iter().fold($to_bits(<$T>::zeroed()), |a, &b| a | $to_bits(b))))
}

#[inline(never)]
fn reduce_xor(&self) -> Option<Self::Scalar> {
if self.validity().map_or(false, |v| v.unset_bits() > 0) {
return None;
}

let values = self.values();

if values.is_empty() {
return None;
}

Some($from_bits(values.iter().fold($to_bits(<$T>::zeroed()), |a, &b| a ^ $to_bits(b))))
}

fn bit_and(lhs: Self::Scalar, rhs: Self::Scalar) -> Self::Scalar {
$from_bits($to_bits(lhs) & $to_bits(rhs))
}
fn bit_or(lhs: Self::Scalar, rhs: Self::Scalar) -> Self::Scalar {
$from_bits($to_bits(lhs) | $to_bits(rhs))
}
fn bit_xor(lhs: Self::Scalar, rhs: Self::Scalar) -> Self::Scalar {
$from_bits($to_bits(lhs) ^ $to_bits(rhs))
}
}
)+
};
}

impl_bitwise_kernel! {
(i8, identity, identity),
(i16, identity, identity),
(i32, identity, identity),
(i64, identity, identity),
(u8, identity, identity),
(u16, identity, identity),
(u32, identity, identity),
(u64, identity, identity),
(f32, f32::to_bits, f32::from_bits),
(f64, f64::to_bits, f64::from_bits),
}

impl BitwiseKernel for BooleanArray {
type Scalar = bool;

#[inline(never)]
fn count_ones(&self) -> PrimitiveArray<u8> {
PrimitiveArray::new(
ArrowDataType::UInt8,
self.values()
.iter()
.map(u8::from)
.collect_trusted::<Vec<_>>()
.into(),
self.validity().cloned(),
)
}

#[inline(never)]
fn count_zeros(&self) -> PrimitiveArray<u8> {
PrimitiveArray::new(
ArrowDataType::UInt8,
self.values()
.iter()
.map(|v| u8::from(!v))
.collect_trusted::<Vec<_>>()
.into(),
self.validity().cloned(),
)
}

#[inline(always)]
fn leading_ones(&self) -> PrimitiveArray<u8> {
self.count_ones()
}

#[inline(always)]
fn leading_zeros(&self) -> PrimitiveArray<u8> {
self.count_zeros()
}

#[inline(always)]
fn trailing_ones(&self) -> PrimitiveArray<u8> {
self.count_ones()
}

#[inline(always)]
fn trailing_zeros(&self) -> PrimitiveArray<u8> {
self.count_zeros()
}

fn reduce_and(&self) -> Option<Self::Scalar> {
if self.validity().map_or(false, |v| v.unset_bits() > 0) {
return None;
}

let values = self.values();

if values.is_empty() {
return None;
}

Some(values.unset_bits() == 0)
}

fn reduce_or(&self) -> Option<Self::Scalar> {
if self.validity().map_or(false, |v| v.unset_bits() > 0) {
return None;
}

let values = self.values();

if values.is_empty() {
return None;
}

Some(values.set_bits() > 0)
}

fn reduce_xor(&self) -> Option<Self::Scalar> {
if self.validity().map_or(false, |v| v.unset_bits() > 0) {
return None;
}

let values = self.values();

if values.is_empty() {
return None;
}

Some(values.set_bits() % 2 == 1)
}

fn bit_and(lhs: Self::Scalar, rhs: Self::Scalar) -> Self::Scalar {
lhs & rhs
}
fn bit_or(lhs: Self::Scalar, rhs: Self::Scalar) -> Self::Scalar {
lhs | rhs
}
fn bit_xor(lhs: Self::Scalar, rhs: Self::Scalar) -> Self::Scalar {
lhs ^ rhs
}
}
1 change: 1 addition & 0 deletions crates/polars-compute/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use arrow::types::NativeType;

pub mod arithmetic;
pub mod arity;
pub mod bitwise;
pub mod comparisons;
pub mod filter;
pub mod float_sum;
Expand Down
80 changes: 80 additions & 0 deletions crates/polars-core/src/chunked_array/ops/bitwise_reduce.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use arrow::array::{Array, PrimitiveArray};
use arrow::types::NativeType;
use polars_compute::bitwise::BitwiseKernel;

use super::{BooleanType, ChunkBitwiseReduce, ChunkedArray, PolarsNumericType};

impl<T> ChunkBitwiseReduce for ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: NativeType,
PrimitiveArray<T::Native>: BitwiseKernel<Scalar = T::Native>,
{
type Physical = T::Native;

fn and_reduce(&self) -> Option<Self::Physical> {
if self.null_count() > 0 {
return None;
}

self.downcast_iter()
.filter(|arr| !arr.is_empty())
.map(|arr| BitwiseKernel::reduce_and(arr).unwrap())
.reduce(<PrimitiveArray<T::Native> as BitwiseKernel>::bit_and)
}
fn or_reduce(&self) -> Option<Self::Physical> {
if self.null_count() > 0 {
return None;
}

self.downcast_iter()
.filter(|arr| !arr.is_empty())
.map(|arr| BitwiseKernel::reduce_or(arr).unwrap())
.reduce(<PrimitiveArray<T::Native> as BitwiseKernel>::bit_or)
}
fn xor_reduce(&self) -> Option<Self::Physical> {
if self.null_count() > 0 {
return None;
}

self.downcast_iter()
.filter(|arr| !arr.is_empty())
.map(|arr| BitwiseKernel::reduce_xor(arr).unwrap())
.reduce(<PrimitiveArray<T::Native> as BitwiseKernel>::bit_xor)
}
}

impl ChunkBitwiseReduce for ChunkedArray<BooleanType> {
type Physical = bool;

fn and_reduce(&self) -> Option<Self::Physical> {
if self.null_count() > 0 {
return None;
}

self.downcast_iter()
.filter(|arr| !arr.is_empty())
.map(|arr| BitwiseKernel::reduce_and(arr).unwrap())
.reduce(|a, b| a & b)
}
fn or_reduce(&self) -> Option<Self::Physical> {
if self.null_count() > 0 {
return None;
}

self.downcast_iter()
.filter(|arr| !arr.is_empty())
.map(|arr| BitwiseKernel::reduce_or(arr).unwrap())
.reduce(|a, b| a | b)
}
fn xor_reduce(&self) -> Option<Self::Physical> {
if self.null_count() > 0 {
return None;
}

self.downcast_iter()
.filter(|arr| !arr.is_empty())
.map(|arr| BitwiseKernel::reduce_xor(arr).unwrap())
.reduce(|a, b| a ^ b)
}
}
10 changes: 10 additions & 0 deletions crates/polars-core/src/chunked_array/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub(crate) mod append;
mod apply;
pub mod arity;
mod bit_repr;
mod bitwise_reduce;
pub(crate) mod chunkops;
pub(crate) mod compare_inner;
#[cfg(feature = "dtype-decimal")]
Expand Down Expand Up @@ -295,6 +296,15 @@ pub trait ChunkVar {
}
}

/// Bitwise Reduction Operations.
pub trait ChunkBitwiseReduce {
type Physical;

fn and_reduce(&self) -> Option<Self::Physical>;
fn or_reduce(&self) -> Option<Self::Physical>;
fn xor_reduce(&self) -> Option<Self::Physical>;
}

/// Compare [`Series`] and [`ChunkedArray`]'s and get a `boolean` mask that
/// can be used to filter rows.
///
Expand Down
Loading
Loading