Skip to content

Commit 5f076b5

Browse files
committed
Deprecate ScalarValue::and, ScalarValue::or (apache#6842)
1 parent bfffdba commit 5f076b5

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

datafusion/common/src/scalar.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,11 +2048,13 @@ impl ScalarValue {
20482048
impl_checked_op!(self, rhs, checked_sub, -)
20492049
}
20502050

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

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

datafusion/physical-expr/src/aggregate/bool_and_or.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
//! Defines physical expressions that can evaluated at runtime during query execution
1919
2020
use std::any::Any;
21-
use std::convert::TryFrom;
2221
use std::sync::Arc;
2322

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

220219
#[derive(Debug)]
221220
struct BoolAndAccumulator {
222-
bool_and: ScalarValue,
221+
acc: Option<bool>,
223222
}
224223

225224
impl BoolAndAccumulator {
226225
/// new bool_and accumulator
227226
pub fn try_new(data_type: &DataType) -> Result<Self> {
228-
Ok(Self {
229-
bool_and: ScalarValue::try_from(data_type)?,
230-
})
227+
assert_eq!(data_type, &DataType::Boolean);
228+
Ok(Self { acc: None })
231229
}
232230
}
233231

234232
impl Accumulator for BoolAndAccumulator {
235233
fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> {
236234
let values = &values[0];
237-
let delta = &bool_and_batch(values)?;
238-
self.bool_and = self.bool_and.and(delta)?;
235+
self.acc = match (self.acc, bool_and_batch(values)?) {
236+
(None, ScalarValue::Boolean(v)) => v,
237+
(Some(v), ScalarValue::Boolean(None)) => Some(v),
238+
(Some(a), ScalarValue::Boolean(Some(b))) => Some(a && b),
239+
_ => unreachable!(),
240+
};
239241
Ok(())
240242
}
241243

@@ -244,16 +246,15 @@ impl Accumulator for BoolAndAccumulator {
244246
}
245247

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

250252
fn evaluate(&self) -> Result<ScalarValue> {
251-
Ok(self.bool_and.clone())
253+
Ok(ScalarValue::Boolean(self.acc))
252254
}
253255

254256
fn size(&self) -> usize {
255-
std::mem::size_of_val(self) - std::mem::size_of_val(&self.bool_and)
256-
+ self.bool_and.size()
257+
std::mem::size_of_val(self)
257258
}
258259
}
259260

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

414415
#[derive(Debug)]
415416
struct BoolOrAccumulator {
416-
bool_or: ScalarValue,
417+
acc: Option<bool>,
417418
}
418419

419420
impl BoolOrAccumulator {
420421
/// new bool_or accumulator
421422
pub fn try_new(data_type: &DataType) -> Result<Self> {
422-
Ok(Self {
423-
bool_or: ScalarValue::try_from(data_type)?,
424-
})
423+
assert_eq!(data_type, &DataType::Boolean);
424+
Ok(Self { acc: None })
425425
}
426426
}
427427

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

433433
fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> {
434434
let values = &values[0];
435-
let delta = bool_or_batch(values)?;
436-
self.bool_or = self.bool_or.or(&delta)?;
435+
self.acc = match (self.acc, bool_or_batch(values)?) {
436+
(None, ScalarValue::Boolean(v)) => v,
437+
(Some(v), ScalarValue::Boolean(None)) => Some(v),
438+
(Some(a), ScalarValue::Boolean(Some(b))) => Some(a || b),
439+
_ => unreachable!(),
440+
};
437441
Ok(())
438442
}
439443

@@ -442,12 +446,11 @@ impl Accumulator for BoolOrAccumulator {
442446
}
443447

444448
fn evaluate(&self) -> Result<ScalarValue> {
445-
Ok(self.bool_or.clone())
449+
Ok(ScalarValue::Boolean(self.acc))
446450
}
447451

448452
fn size(&self) -> usize {
449-
std::mem::size_of_val(self) - std::mem::size_of_val(&self.bool_or)
450-
+ self.bool_or.size()
453+
std::mem::size_of_val(self)
451454
}
452455
}
453456

0 commit comments

Comments
 (0)