Skip to content

Commit

Permalink
Deprecate ScalarValue::and, ScalarValue::or (apache#6842)
Browse files Browse the repository at this point in the history
  • Loading branch information
tustvold committed Jul 4, 2023
1 parent bfffdba commit 5f076b5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
2 changes: 2 additions & 0 deletions datafusion/common/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2048,11 +2048,13 @@ impl ScalarValue {
impl_checked_op!(self, rhs, checked_sub, -)
}

#[deprecated(note = "Use arrow kernels or specialization (#6842)")]
pub fn and<T: Borrow<ScalarValue>>(&self, other: T) -> Result<ScalarValue> {
let rhs = other.borrow();
impl_op!(self, rhs, &&)
}

#[deprecated(note = "Use arrow kernels or specialization (#6842)")]
pub fn or<T: Borrow<ScalarValue>>(&self, other: T) -> Result<ScalarValue> {
let rhs = other.borrow();
impl_op!(self, rhs, ||)
Expand Down
45 changes: 24 additions & 21 deletions datafusion/physical-expr/src/aggregate/bool_and_or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
//! Defines physical expressions that can evaluated at runtime during query execution

use std::any::Any;
use std::convert::TryFrom;
use std::sync::Arc;

use crate::{AggregateExpr, PhysicalExpr};
Expand Down Expand Up @@ -219,23 +218,26 @@ impl PartialEq<dyn Any> for BoolAnd {

#[derive(Debug)]
struct BoolAndAccumulator {
bool_and: ScalarValue,
acc: Option<bool>,
}

impl BoolAndAccumulator {
/// new bool_and accumulator
pub fn try_new(data_type: &DataType) -> Result<Self> {
Ok(Self {
bool_and: ScalarValue::try_from(data_type)?,
})
assert_eq!(data_type, &DataType::Boolean);
Ok(Self { acc: None })
}
}

impl Accumulator for BoolAndAccumulator {
fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> {
let values = &values[0];
let delta = &bool_and_batch(values)?;
self.bool_and = self.bool_and.and(delta)?;
self.acc = match (self.acc, bool_and_batch(values)?) {
(None, ScalarValue::Boolean(v)) => v,
(Some(v), ScalarValue::Boolean(None)) => Some(v),
(Some(a), ScalarValue::Boolean(Some(b))) => Some(a && b),
_ => unreachable!(),
};
Ok(())
}

Expand All @@ -244,16 +246,15 @@ impl Accumulator for BoolAndAccumulator {
}

fn state(&self) -> Result<Vec<ScalarValue>> {
Ok(vec![self.bool_and.clone()])
Ok(vec![ScalarValue::Boolean(self.acc)])
}

fn evaluate(&self) -> Result<ScalarValue> {
Ok(self.bool_and.clone())
Ok(ScalarValue::Boolean(self.acc))
}

fn size(&self) -> usize {
std::mem::size_of_val(self) - std::mem::size_of_val(&self.bool_and)
+ self.bool_and.size()
std::mem::size_of_val(self)
}
}

Expand Down Expand Up @@ -413,27 +414,30 @@ impl PartialEq<dyn Any> for BoolOr {

#[derive(Debug)]
struct BoolOrAccumulator {
bool_or: ScalarValue,
acc: Option<bool>,
}

impl BoolOrAccumulator {
/// new bool_or accumulator
pub fn try_new(data_type: &DataType) -> Result<Self> {
Ok(Self {
bool_or: ScalarValue::try_from(data_type)?,
})
assert_eq!(data_type, &DataType::Boolean);
Ok(Self { acc: None })
}
}

impl Accumulator for BoolOrAccumulator {
fn state(&self) -> Result<Vec<ScalarValue>> {
Ok(vec![self.bool_or.clone()])
Ok(vec![ScalarValue::Boolean(self.acc)])
}

fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> {
let values = &values[0];
let delta = bool_or_batch(values)?;
self.bool_or = self.bool_or.or(&delta)?;
self.acc = match (self.acc, bool_or_batch(values)?) {
(None, ScalarValue::Boolean(v)) => v,
(Some(v), ScalarValue::Boolean(None)) => Some(v),
(Some(a), ScalarValue::Boolean(Some(b))) => Some(a || b),
_ => unreachable!(),
};
Ok(())
}

Expand All @@ -442,12 +446,11 @@ impl Accumulator for BoolOrAccumulator {
}

fn evaluate(&self) -> Result<ScalarValue> {
Ok(self.bool_or.clone())
Ok(ScalarValue::Boolean(self.acc))
}

fn size(&self) -> usize {
std::mem::size_of_val(self) - std::mem::size_of_val(&self.bool_or)
+ self.bool_or.size()
std::mem::size_of_val(self)
}
}

Expand Down

0 comments on commit 5f076b5

Please sign in to comment.