|
1 | 1 | // SPDX-License-Identifier: Apache-2.0 |
2 | 2 | // SPDX-FileCopyrightText: Copyright the Vortex contributors |
3 | 3 |
|
| 4 | +use vortex_error::VortexExpect; |
| 5 | +use vortex_error::VortexResult; |
| 6 | + |
| 7 | +use crate::ArrayRef; |
| 8 | +use crate::ExecutionCtx; |
| 9 | +use crate::IntoArray; |
| 10 | +use crate::array::ArrayView; |
4 | 11 | use crate::arrays::Chunked; |
| 12 | +use crate::arrays::ChunkedArray; |
| 13 | +use crate::arrays::ScalarFn; |
| 14 | +use crate::arrays::ScalarFnArray; |
| 15 | +use crate::arrays::chunked::ChunkedArrayExt; |
5 | 16 | use crate::arrays::dict::TakeExecuteAdaptor; |
6 | 17 | use crate::arrays::filter::FilterExecuteAdaptor; |
| 18 | +use crate::arrays::scalar_fn::ExactScalarFn; |
| 19 | +use crate::arrays::scalar_fn::ScalarFnArrayExt; |
| 20 | +use crate::arrays::scalar_fn::ScalarFnArrayView; |
7 | 21 | use crate::arrays::slice::SliceExecuteAdaptor; |
| 22 | +use crate::kernel::ExecuteParentKernel; |
8 | 23 | use crate::kernel::ParentKernelSet; |
9 | 24 | use crate::scalar_fn::fns::mask::MaskExecuteAdaptor; |
| 25 | +use crate::scalar_fn::fns::stat::StatFn; |
10 | 26 | use crate::scalar_fn::fns::zip::ZipExecuteAdaptor; |
11 | 27 |
|
12 | 28 | pub(crate) static PARENT_KERNELS: ParentKernelSet<Chunked> = ParentKernelSet::new(&[ |
| 29 | + ParentKernelSet::lift(&ChunkedStatExecuteParentKernel), |
13 | 30 | ParentKernelSet::lift(&FilterExecuteAdaptor(Chunked)), |
14 | 31 | ParentKernelSet::lift(&MaskExecuteAdaptor(Chunked)), |
15 | 32 | ParentKernelSet::lift(&SliceExecuteAdaptor(Chunked)), |
16 | 33 | ParentKernelSet::lift(&TakeExecuteAdaptor(Chunked)), |
17 | 34 | ParentKernelSet::lift(&ZipExecuteAdaptor(Chunked)), |
18 | 35 | ]); |
| 36 | + |
| 37 | +#[derive(Debug)] |
| 38 | +struct ChunkedStatExecuteParentKernel; |
| 39 | + |
| 40 | +impl ExecuteParentKernel<Chunked> for ChunkedStatExecuteParentKernel { |
| 41 | + type Parent = ExactScalarFn<StatFn>; |
| 42 | + |
| 43 | + fn execute_parent( |
| 44 | + &self, |
| 45 | + array: ArrayView<'_, Chunked>, |
| 46 | + parent: ScalarFnArrayView<'_, StatFn>, |
| 47 | + child_idx: usize, |
| 48 | + _ctx: &mut ExecutionCtx, |
| 49 | + ) -> VortexResult<Option<ArrayRef>> { |
| 50 | + if child_idx != 0 { |
| 51 | + return Ok(None); |
| 52 | + } |
| 53 | + |
| 54 | + tracing::trace!( |
| 55 | + "stat({}) descending into ChunkedArray with {} chunks", |
| 56 | + parent.options.aggregate_fn(), |
| 57 | + array.nchunks() |
| 58 | + ); |
| 59 | + |
| 60 | + let scalar_fn = parent |
| 61 | + .as_opt::<ScalarFn>() |
| 62 | + .vortex_expect("ExactScalarFn matcher confirmed ScalarFnArray") |
| 63 | + .scalar_fn() |
| 64 | + .clone(); |
| 65 | + let chunks = array |
| 66 | + .iter_chunks() |
| 67 | + .map(|chunk| { |
| 68 | + ScalarFnArray::try_new(scalar_fn.clone(), vec![chunk.clone()], chunk.len()) |
| 69 | + .map(IntoArray::into_array) |
| 70 | + }) |
| 71 | + .collect::<VortexResult<Vec<_>>>()?; |
| 72 | + |
| 73 | + Ok(Some( |
| 74 | + ChunkedArray::try_new(chunks, parent.dtype().clone())?.into_array(), |
| 75 | + )) |
| 76 | + } |
| 77 | +} |
0 commit comments