|
| 1 | +use std::any::Any; |
| 2 | +use std::sync::Arc; |
| 3 | + |
| 4 | +use vortex_array::aliases::hash_set::HashSet; |
| 5 | +use vortex_array::Array; |
| 6 | +use vortex_dtype::field::Field; |
| 7 | +use vortex_error::{vortex_err, VortexResult}; |
| 8 | + |
| 9 | +use crate::{unbox_any, VortexExpr}; |
| 10 | + |
| 11 | +#[derive(Debug)] |
| 12 | +pub struct Not { |
| 13 | + child: Arc<dyn VortexExpr>, |
| 14 | +} |
| 15 | + |
| 16 | +impl Not { |
| 17 | + pub fn new(child: Arc<dyn VortexExpr>) -> Self { |
| 18 | + Self { child } |
| 19 | + } |
| 20 | + |
| 21 | + pub fn child(&self) -> &Arc<dyn VortexExpr> { |
| 22 | + &self.child |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl VortexExpr for Not { |
| 27 | + fn as_any(&self) -> &dyn Any { |
| 28 | + self |
| 29 | + } |
| 30 | + |
| 31 | + fn evaluate(&self, batch: &Array) -> VortexResult<Array> { |
| 32 | + let child_result = self.child.evaluate(batch)?; |
| 33 | + child_result.with_dyn(|a| { |
| 34 | + a.as_bool_array() |
| 35 | + .ok_or_else(|| vortex_err!("Child was not a bool array")) |
| 36 | + .and_then(|b| b.invert()) |
| 37 | + }) |
| 38 | + } |
| 39 | + |
| 40 | + fn collect_references<'a>(&'a self, references: &mut HashSet<&'a Field>) { |
| 41 | + self.child.collect_references(references) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl PartialEq<dyn Any> for Not { |
| 46 | + fn eq(&self, other: &dyn Any) -> bool { |
| 47 | + unbox_any(other) |
| 48 | + .downcast_ref::<Self>() |
| 49 | + .map(|x| x.child.eq(&self.child)) |
| 50 | + .unwrap_or(false) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +#[cfg(test)] |
| 55 | +mod tests { |
| 56 | + use std::sync::Arc; |
| 57 | + |
| 58 | + use vortex_array::array::BoolArray; |
| 59 | + use vortex_array::IntoArrayVariant; |
| 60 | + |
| 61 | + use crate::{Identity, Not, VortexExpr}; |
| 62 | + |
| 63 | + #[test] |
| 64 | + fn invert_booleans() { |
| 65 | + let not_expr = Not::new(Arc::new(Identity)); |
| 66 | + let bools = BoolArray::from(vec![false, true, false, false, true, true]); |
| 67 | + assert_eq!( |
| 68 | + not_expr |
| 69 | + .evaluate(bools.as_ref()) |
| 70 | + .unwrap() |
| 71 | + .into_bool() |
| 72 | + .unwrap() |
| 73 | + .boolean_buffer() |
| 74 | + .iter() |
| 75 | + .collect::<Vec<_>>(), |
| 76 | + vec![true, false, true, true, false, false] |
| 77 | + ); |
| 78 | + } |
| 79 | +} |
0 commit comments