|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use std::hash::Hasher; |
| 5 | + |
| 6 | +use vortex_dtype::DType; |
| 7 | +use vortex_dtype::Nullability::NonNullable; |
| 8 | +use vortex_error::VortexResult; |
| 9 | +use vortex_mask::Mask; |
| 10 | +use vortex_vector::{BoolVector, VectorOps}; |
| 11 | + |
| 12 | +use crate::execution::{BatchKernelRef, BindCtx, kernel}; |
| 13 | +use crate::stats::{ArrayStats, StatsSetRef}; |
| 14 | +use crate::vtable::{ArrayVTable, NotSupported, OperatorVTable, VTable, VisitorVTable}; |
| 15 | +use crate::{ |
| 16 | + ArrayBufferVisitor, ArrayChildVisitor, ArrayEq, ArrayHash, ArrayRef, EncodingId, EncodingRef, |
| 17 | + Precision, vtable, |
| 18 | +}; |
| 19 | + |
| 20 | +vtable!(IsNotNull); |
| 21 | + |
| 22 | +#[derive(Debug, Clone)] |
| 23 | +pub struct IsNotNullArray { |
| 24 | + child: ArrayRef, |
| 25 | + stats: ArrayStats, |
| 26 | +} |
| 27 | + |
| 28 | +impl IsNotNullArray { |
| 29 | + /// Create a new is_not_null array. |
| 30 | + pub fn new(child: ArrayRef) -> Self { |
| 31 | + Self { |
| 32 | + child, |
| 33 | + stats: ArrayStats::default(), |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(Debug, Clone)] |
| 39 | +pub struct IsNotNullEncoding; |
| 40 | + |
| 41 | +impl VTable for IsNotNullVTable { |
| 42 | + type Array = IsNotNullArray; |
| 43 | + type Encoding = IsNotNullEncoding; |
| 44 | + type ArrayVTable = Self; |
| 45 | + type CanonicalVTable = NotSupported; |
| 46 | + type OperationsVTable = NotSupported; |
| 47 | + type ValidityVTable = NotSupported; |
| 48 | + type VisitorVTable = Self; |
| 49 | + type ComputeVTable = NotSupported; |
| 50 | + type EncodeVTable = NotSupported; |
| 51 | + type SerdeVTable = NotSupported; |
| 52 | + type OperatorVTable = Self; |
| 53 | + |
| 54 | + fn id(_encoding: &Self::Encoding) -> EncodingId { |
| 55 | + EncodingId::from("vortex.is_null") |
| 56 | + } |
| 57 | + |
| 58 | + fn encoding(_array: &Self::Array) -> EncodingRef { |
| 59 | + EncodingRef::from(IsNotNullEncoding.as_ref()) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl ArrayVTable<IsNotNullVTable> for IsNotNullVTable { |
| 64 | + fn len(array: &IsNotNullArray) -> usize { |
| 65 | + array.len() |
| 66 | + } |
| 67 | + |
| 68 | + fn dtype(_array: &IsNotNullArray) -> &DType { |
| 69 | + &DType::Bool(NonNullable) |
| 70 | + } |
| 71 | + |
| 72 | + fn stats(array: &IsNotNullArray) -> StatsSetRef<'_> { |
| 73 | + array.stats.to_ref(array.as_ref()) |
| 74 | + } |
| 75 | + |
| 76 | + fn array_hash<H: Hasher>(array: &IsNotNullArray, state: &mut H, precision: Precision) { |
| 77 | + array.child.array_hash(state, precision); |
| 78 | + } |
| 79 | + |
| 80 | + fn array_eq(array: &IsNotNullArray, other: &IsNotNullArray, precision: Precision) -> bool { |
| 81 | + array.child.array_eq(&other.child, precision) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl VisitorVTable<IsNotNullVTable> for IsNotNullVTable { |
| 86 | + fn visit_buffers(_array: &IsNotNullArray, _visitor: &mut dyn ArrayBufferVisitor) { |
| 87 | + // No buffers |
| 88 | + } |
| 89 | + |
| 90 | + fn visit_children(array: &IsNotNullArray, visitor: &mut dyn ArrayChildVisitor) { |
| 91 | + visitor.visit_child("child", array.child.as_ref()); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl OperatorVTable<IsNotNullVTable> for IsNotNullVTable { |
| 96 | + fn bind( |
| 97 | + array: &IsNotNullArray, |
| 98 | + selection: Option<&ArrayRef>, |
| 99 | + ctx: &mut dyn BindCtx, |
| 100 | + ) -> VortexResult<BatchKernelRef> { |
| 101 | + let child = ctx.bind(&array.child, selection)?; |
| 102 | + Ok(kernel(move || { |
| 103 | + let child = child.execute()?; |
| 104 | + let is_null = child.validity().to_bit_buffer(); |
| 105 | + Ok(BoolVector::new(is_null, Mask::AllTrue(child.len())).into()) |
| 106 | + })) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +#[cfg(test)] |
| 111 | +mod tests { |
| 112 | + use vortex_buffer::{bitbuffer, buffer}; |
| 113 | + use vortex_error::VortexResult; |
| 114 | + use vortex_vector::VectorOps; |
| 115 | + |
| 116 | + use super::IsNotNullArray; |
| 117 | + use crate::IntoArray; |
| 118 | + use crate::arrays::PrimitiveArray; |
| 119 | + use crate::validity::Validity; |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn test_is_null() -> VortexResult<()> { |
| 123 | + let validity = bitbuffer![1 0 1]; |
| 124 | + let array = PrimitiveArray::new( |
| 125 | + buffer![0, 1, 2], |
| 126 | + Validity::Array(validity.clone().into_array()), |
| 127 | + ) |
| 128 | + .into_array(); |
| 129 | + |
| 130 | + let result = IsNotNullArray::new(array).execute()?.into_bool(); |
| 131 | + assert!(result.validity().all_true()); |
| 132 | + assert_eq!(result.bits(), &validity); |
| 133 | + |
| 134 | + Ok(()) |
| 135 | + } |
| 136 | +} |
0 commit comments