|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use std::ops::BitAnd; |
| 5 | + |
| 6 | +use vortex_error::VortexResult; |
| 7 | +use vortex_mask::Mask; |
| 8 | + |
| 9 | +use crate::ArrayRef; |
| 10 | +use crate::IntoArray; |
| 11 | +use crate::aggregate_fn::accumulator::Accumulator; |
| 12 | +use crate::arrays::PrimitiveArray; |
| 13 | +use crate::canonical::ToCanonical; |
| 14 | +use crate::scalar::Scalar; |
| 15 | + |
| 16 | +/// Accumulator that sums boolean values by counting `true` as 1 and `false` as 0. |
| 17 | +/// |
| 18 | +/// Output type is `u64` (nullable). Overflow is theoretically possible but extremely |
| 19 | +/// unlikely since it would require more than `u64::MAX` true values. |
| 20 | +pub(super) struct BoolSumAccumulator { |
| 21 | + count: u64, |
| 22 | + /// Whether at least one non-null value has been accumulated. |
| 23 | + has_values: bool, |
| 24 | + /// Whether accumulate() or merge() has been called at all (even with all-null data). |
| 25 | + has_input: bool, |
| 26 | + checked: bool, |
| 27 | + overflowed: bool, |
| 28 | + results: Vec<Option<u64>>, |
| 29 | +} |
| 30 | + |
| 31 | +impl BoolSumAccumulator { |
| 32 | + pub(super) fn new(checked: bool) -> Self { |
| 33 | + Self { |
| 34 | + count: 0, |
| 35 | + has_values: false, |
| 36 | + has_input: false, |
| 37 | + checked, |
| 38 | + overflowed: false, |
| 39 | + results: Vec::new(), |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl Accumulator for BoolSumAccumulator { |
| 45 | + fn accumulate(&mut self, batch: &ArrayRef) -> VortexResult<()> { |
| 46 | + self.has_input = true; |
| 47 | + if self.overflowed { |
| 48 | + return Ok(()); |
| 49 | + } |
| 50 | + |
| 51 | + let bool_array = batch.to_bool(); |
| 52 | + let validity = bool_array.validity_mask()?; |
| 53 | + |
| 54 | + let true_count = match &validity { |
| 55 | + Mask::AllTrue(_) => bool_array.to_bit_buffer().true_count() as u64, |
| 56 | + Mask::AllFalse(_) => return Ok(()), |
| 57 | + Mask::Values(v) => bool_array |
| 58 | + .to_bit_buffer() |
| 59 | + .bitand(v.bit_buffer()) |
| 60 | + .true_count() as u64, |
| 61 | + }; |
| 62 | + |
| 63 | + self.has_values = true; |
| 64 | + if self.checked { |
| 65 | + if let Some(new_count) = self.count.checked_add(true_count) { |
| 66 | + self.count = new_count; |
| 67 | + } else { |
| 68 | + self.overflowed = true; |
| 69 | + } |
| 70 | + } else { |
| 71 | + self.count = self.count.wrapping_add(true_count); |
| 72 | + } |
| 73 | + |
| 74 | + Ok(()) |
| 75 | + } |
| 76 | + |
| 77 | + fn merge(&mut self, state: &Scalar) -> VortexResult<()> { |
| 78 | + if state.is_null() { |
| 79 | + return Ok(()); |
| 80 | + } |
| 81 | + self.has_input = true; |
| 82 | + if let Some(v) = state.as_primitive().typed_value::<u64>() { |
| 83 | + self.has_values = true; |
| 84 | + if self.checked { |
| 85 | + if let Some(new_count) = self.count.checked_add(v) { |
| 86 | + self.count = new_count; |
| 87 | + } else { |
| 88 | + self.overflowed = true; |
| 89 | + } |
| 90 | + } else { |
| 91 | + self.count = self.count.wrapping_add(v); |
| 92 | + } |
| 93 | + } |
| 94 | + Ok(()) |
| 95 | + } |
| 96 | + |
| 97 | + fn is_saturated(&self) -> bool { |
| 98 | + self.checked && self.overflowed |
| 99 | + } |
| 100 | + |
| 101 | + fn flush(&mut self) -> VortexResult<()> { |
| 102 | + let result = if self.overflowed { |
| 103 | + None |
| 104 | + } else if self.has_values { |
| 105 | + Some(self.count) |
| 106 | + } else if self.has_input { |
| 107 | + // All-null group. |
| 108 | + None |
| 109 | + } else { |
| 110 | + // Empty group: identity is zero. |
| 111 | + Some(0) |
| 112 | + }; |
| 113 | + self.results.push(result); |
| 114 | + self.count = 0; |
| 115 | + self.has_values = false; |
| 116 | + self.has_input = false; |
| 117 | + self.overflowed = false; |
| 118 | + Ok(()) |
| 119 | + } |
| 120 | + |
| 121 | + fn finish(self: Box<Self>) -> VortexResult<ArrayRef> { |
| 122 | + Ok(PrimitiveArray::from_option_iter(self.results).into_array()) |
| 123 | + } |
| 124 | +} |
0 commit comments