|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use std::hash::Hasher; |
| 5 | + |
| 6 | +use vortex_buffer::BufferHandle; |
| 7 | +use vortex_dtype::DType; |
| 8 | +use vortex_error::VortexResult; |
| 9 | +use vortex_error::vortex_bail; |
| 10 | +use vortex_mask::Mask; |
| 11 | +use vortex_vector::Vector; |
| 12 | + |
| 13 | +use crate::Array; |
| 14 | +use crate::ArrayBufferVisitor; |
| 15 | +use crate::ArrayChildVisitor; |
| 16 | +use crate::ArrayEq; |
| 17 | +use crate::ArrayHash; |
| 18 | +use crate::Precision; |
| 19 | +use crate::arrays::filter::array::FilterArray; |
| 20 | +use crate::execution::ExecutionCtx; |
| 21 | +use crate::serde::ArrayChildren; |
| 22 | +use crate::stats::StatsSetRef; |
| 23 | +use crate::vtable; |
| 24 | +use crate::vtable::ArrayId; |
| 25 | +use crate::vtable::ArrayVTable; |
| 26 | +use crate::vtable::ArrayVTableExt; |
| 27 | +use crate::vtable::BaseArrayVTable; |
| 28 | +use crate::vtable::NotSupported; |
| 29 | +use crate::vtable::VTable; |
| 30 | +use crate::vtable::VisitorVTable; |
| 31 | + |
| 32 | +vtable!(Filter); |
| 33 | + |
| 34 | +#[derive(Debug)] |
| 35 | +pub struct FilterVTable; |
| 36 | + |
| 37 | +impl VTable for FilterVTable { |
| 38 | + type Array = FilterArray; |
| 39 | + type Metadata = Mask; |
| 40 | + type ArrayVTable = Self; |
| 41 | + type CanonicalVTable = NotSupported; |
| 42 | + type OperationsVTable = NotSupported; |
| 43 | + type ValidityVTable = NotSupported; |
| 44 | + type VisitorVTable = Self; |
| 45 | + type ComputeVTable = NotSupported; |
| 46 | + type EncodeVTable = NotSupported; |
| 47 | + |
| 48 | + fn id(&self) -> ArrayId { |
| 49 | + ArrayId::from("vortex.filter") |
| 50 | + } |
| 51 | + |
| 52 | + fn encoding(_array: &Self::Array) -> ArrayVTable { |
| 53 | + FilterVTable.as_vtable() |
| 54 | + } |
| 55 | + |
| 56 | + fn metadata(array: &Self::Array) -> VortexResult<Self::Metadata> { |
| 57 | + Ok(array.mask.clone()) |
| 58 | + } |
| 59 | + |
| 60 | + fn serialize(_metadata: Self::Metadata) -> VortexResult<Option<Vec<u8>>> { |
| 61 | + Ok(None) |
| 62 | + } |
| 63 | + |
| 64 | + fn deserialize(_bytes: &[u8]) -> VortexResult<Self::Metadata> { |
| 65 | + vortex_bail!("Filter array is not serializable") |
| 66 | + } |
| 67 | + |
| 68 | + fn build( |
| 69 | + &self, |
| 70 | + dtype: &DType, |
| 71 | + len: usize, |
| 72 | + metadata: &Self::Metadata, |
| 73 | + _buffers: &[BufferHandle], |
| 74 | + children: &dyn ArrayChildren, |
| 75 | + ) -> VortexResult<Self::Array> { |
| 76 | + let child = children.get(0, dtype, len)?; |
| 77 | + Ok(FilterArray { |
| 78 | + child, |
| 79 | + mask: metadata.clone(), |
| 80 | + stats: Default::default(), |
| 81 | + }) |
| 82 | + } |
| 83 | + |
| 84 | + fn batch_execute(array: &Self::Array, ctx: &mut ExecutionCtx) -> VortexResult<Vector> { |
| 85 | + let child = array.child.batch_execute(ctx)?; |
| 86 | + Ok(vortex_compute::filter::Filter::filter(&child, &array.mask)) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl BaseArrayVTable<FilterVTable> for FilterVTable { |
| 91 | + fn len(array: &FilterArray) -> usize { |
| 92 | + array.mask.true_count() |
| 93 | + } |
| 94 | + |
| 95 | + fn dtype(array: &FilterArray) -> &DType { |
| 96 | + array.child.dtype() |
| 97 | + } |
| 98 | + |
| 99 | + fn stats(array: &FilterArray) -> StatsSetRef<'_> { |
| 100 | + array.stats.to_ref(array.as_ref()) |
| 101 | + } |
| 102 | + |
| 103 | + fn array_hash<H: Hasher>(array: &FilterArray, state: &mut H, precision: Precision) { |
| 104 | + array.child.array_hash(state, precision); |
| 105 | + array.mask.array_hash(state, precision); |
| 106 | + } |
| 107 | + |
| 108 | + fn array_eq(array: &FilterArray, other: &FilterArray, precision: Precision) -> bool { |
| 109 | + array.child.array_eq(&other.child, precision) && array.mask.array_eq(&other.mask, precision) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +impl VisitorVTable<FilterVTable> for FilterVTable { |
| 114 | + fn visit_buffers(_array: &FilterArray, _visitor: &mut dyn ArrayBufferVisitor) {} |
| 115 | + |
| 116 | + fn visit_children(array: &FilterArray, visitor: &mut dyn ArrayChildVisitor) { |
| 117 | + visitor.visit_child("child", &array.child); |
| 118 | + } |
| 119 | +} |
0 commit comments