-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FLUP: remove TrueCount
- Loading branch information
Showing
27 changed files
with
667 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use std::ops::BitAnd; | ||
|
||
use vortex_error::VortexResult; | ||
use vortex_mask::AllOr; | ||
use vortex_scalar::Scalar; | ||
|
||
use crate::arrays::{BoolArray, BoolEncoding}; | ||
use crate::compute::SumFn; | ||
use crate::stats::Stat; | ||
use crate::Array; | ||
|
||
impl SumFn<&BoolArray> for BoolEncoding { | ||
fn sum(&self, array: &BoolArray) -> VortexResult<Scalar> { | ||
let true_count: Option<u64> = match array.validity_mask()?.boolean_buffer() { | ||
AllOr::All => { | ||
// All-valid | ||
Some(array.boolean_buffer().count_set_bits() as u64) | ||
} | ||
AllOr::None => { | ||
// All-invalid | ||
None | ||
} | ||
AllOr::Some(validity_mask) => Some( | ||
array | ||
.boolean_buffer() | ||
.bitand(validity_mask) | ||
.count_set_bits() as u64, | ||
), | ||
}; | ||
Ok(Scalar::new( | ||
Stat::Sum.dtype(array.dtype()), | ||
true_count.into(), | ||
)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use num_traits::PrimInt; | ||
use vortex_dtype::{match_each_native_ptype, NativePType, PType}; | ||
use vortex_error::{VortexExpect, VortexResult}; | ||
use vortex_scalar::{FromPrimitiveOrF16, Scalar}; | ||
|
||
use crate::arrays::{ChunkedArray, ChunkedEncoding}; | ||
use crate::compute::{sum, SumFn}; | ||
use crate::stats::Stat; | ||
use crate::{Array, ArrayRef}; | ||
|
||
impl SumFn<&ChunkedArray> for ChunkedEncoding { | ||
fn sum(&self, array: &ChunkedArray) -> VortexResult<Scalar> { | ||
let sum_dtype = Stat::Sum.dtype(array.dtype()); | ||
let sum_ptype = PType::try_from(&sum_dtype).vortex_expect("sum dtype must be primitive"); | ||
|
||
let scalar_value = match_each_native_ptype!( | ||
sum_ptype, | ||
unsigned: |$T| { sum_int::<u64>(array.chunks())?.into() } | ||
signed: |$T| { sum_int::<i64>(array.chunks())?.into() } | ||
floating: |$T| { sum_float(array.chunks())?.into() } | ||
); | ||
|
||
Ok(Scalar::new(sum_dtype, scalar_value)) | ||
} | ||
} | ||
|
||
fn sum_int<T: NativePType + PrimInt + FromPrimitiveOrF16>( | ||
chunks: &[ArrayRef], | ||
) -> VortexResult<Option<T>> { | ||
let mut result = T::zero(); | ||
for chunk in chunks { | ||
let chunk_sum = sum(chunk)?; | ||
|
||
let Some(chunk_sum) = chunk_sum.as_primitive().as_::<T>()? else { | ||
// Bail out on overflow | ||
return Ok(None); | ||
}; | ||
|
||
let Some(chunk_result) = result.checked_add(&chunk_sum) else { | ||
// Bail out on overflow | ||
return Ok(None); | ||
}; | ||
|
||
result = chunk_result; | ||
} | ||
Ok(Some(result)) | ||
} | ||
|
||
fn sum_float(chunks: &[ArrayRef]) -> VortexResult<f64> { | ||
let mut result = 0f64; | ||
for chunk in chunks { | ||
let chunk_sum = sum(chunk)?; | ||
let chunk_sum = chunk_sum | ||
.as_primitive() | ||
.as_::<f64>()? | ||
.vortex_expect("Float sum should never be null"); | ||
result += chunk_sum; | ||
} | ||
Ok(result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use arrow_buffer::BooleanBuffer; | ||
use itertools::Itertools; | ||
use num_traits::{CheckedAdd, Float, ToPrimitive}; | ||
use vortex_dtype::{match_each_native_ptype, NativePType}; | ||
use vortex_error::{VortexExpect, VortexResult}; | ||
use vortex_mask::AllOr; | ||
use vortex_scalar::Scalar; | ||
|
||
use crate::arrays::{PrimitiveArray, PrimitiveEncoding}; | ||
use crate::compute::SumFn; | ||
use crate::stats::Stat; | ||
use crate::variants::PrimitiveArrayTrait; | ||
use crate::Array; | ||
|
||
impl SumFn<&PrimitiveArray> for PrimitiveEncoding { | ||
fn sum(&self, array: &PrimitiveArray) -> VortexResult<Scalar> { | ||
let scalar_value = match array.validity_mask()?.boolean_buffer() { | ||
AllOr::All => { | ||
// All-valid | ||
match_each_native_ptype!( | ||
array.ptype(), | ||
unsigned: |$T| { sum_integer::<_, u64>(array.as_slice::<$T>()).into() } | ||
signed: |$T| { sum_integer::<_, i64>(array.as_slice::<$T>()).into() } | ||
floating: |$T| { sum_float(array.as_slice::<$T>()).into() } | ||
) | ||
} | ||
AllOr::None => { | ||
// All-invalid | ||
return Ok(Scalar::null(Stat::Sum.dtype(array.dtype()))); | ||
} | ||
AllOr::Some(validity_mask) => { | ||
// Some-valid | ||
match_each_native_ptype!( | ||
array.ptype(), | ||
unsigned: |$T| { | ||
sum_integer_with_validity::<_, u64>(array.as_slice::<$T>(), validity_mask) | ||
.into() | ||
} | ||
signed: |$T| { | ||
sum_integer_with_validity::<_, i64>(array.as_slice::<$T>(), validity_mask) | ||
.into() | ||
} | ||
floating: |$T| { | ||
sum_float_with_validity(array.as_slice::<$T>(), validity_mask).into() | ||
} | ||
) | ||
} | ||
}; | ||
|
||
let sum_dtype = Stat::Sum.dtype(array.dtype()); | ||
Ok(Scalar::new(sum_dtype, scalar_value)) | ||
} | ||
} | ||
|
||
fn sum_integer<T: NativePType + ToPrimitive, R: NativePType + CheckedAdd>( | ||
values: &[T], | ||
) -> Option<R> { | ||
let mut sum = R::zero(); | ||
for &x in values { | ||
sum = sum.checked_add(&R::from(x)?)?; | ||
} | ||
Some(sum) | ||
} | ||
|
||
fn sum_integer_with_validity<T: NativePType + ToPrimitive, R: NativePType + CheckedAdd>( | ||
values: &[T], | ||
validity: &BooleanBuffer, | ||
) -> Option<R> { | ||
let mut sum = R::zero(); | ||
for (&x, valid) in values.iter().zip_eq(validity.iter()) { | ||
if valid { | ||
sum = sum.checked_add(&R::from(x)?)?; | ||
} | ||
} | ||
Some(sum) | ||
} | ||
|
||
fn sum_float<T: NativePType + Float>(values: &[T]) -> f64 { | ||
let mut sum = 0.0; | ||
for &x in values { | ||
sum += x.to_f64().vortex_expect("Failed to cast value to f64"); | ||
} | ||
sum | ||
} | ||
|
||
fn sum_float_with_validity<T: NativePType + Float>(array: &[T], validity: &BooleanBuffer) -> f64 { | ||
let mut sum = 0.0; | ||
for (&x, valid) in array.iter().zip_eq(validity.iter()) { | ||
if valid { | ||
sum += x.to_f64().vortex_expect("Failed to cast value to f64"); | ||
} | ||
} | ||
sum | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.