|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use std::fmt::Debug; |
| 5 | + |
| 6 | +use vortex_array::ArrayRef; |
| 7 | +use vortex_array::ExecutionCtx; |
| 8 | +use vortex_array::aggregate_fn::AggregateFnRef; |
| 9 | +use vortex_array::aggregate_fn::fns::all_non_distinct::AllNonDistinct; |
| 10 | +use vortex_array::aggregate_fn::fns::all_non_distinct::all_non_distinct; |
| 11 | +use vortex_array::aggregate_fn::kernels::DynAggregateKernel; |
| 12 | +use vortex_array::arrays::Struct; |
| 13 | +use vortex_array::arrays::struct_::StructArrayExt; |
| 14 | +use vortex_array::dtype::Nullability; |
| 15 | +use vortex_array::scalar::Scalar; |
| 16 | +use vortex_error::VortexResult; |
| 17 | + |
| 18 | +use crate::ParquetVariant; |
| 19 | +use crate::ParquetVariantArrayExt; |
| 20 | + |
| 21 | +/// Lets `AllNonDistinct` compare two `ParquetVariant` arrays without canonicalizing them. |
| 22 | +/// |
| 23 | +/// `AllNonDistinct` accumulates over a `Struct{lhs, rhs}` batch, so this kernel is registered for |
| 24 | +/// the struct encoding and inspects the two children. When both are `ParquetVariant`, we compare |
| 25 | +/// the typed (`typed_value`) arrays if both sides are shredded, and fall back to the raw `value` |
| 26 | +/// arrays otherwise. Comparing these child arrays directly avoids re-canonicalizing the variant |
| 27 | +/// (which would recurse through the `Variant` canonical form). |
| 28 | +#[derive(Debug)] |
| 29 | +pub struct AllNonDistinctParquetVariant; |
| 30 | + |
| 31 | +impl DynAggregateKernel for AllNonDistinctParquetVariant { |
| 32 | + fn aggregate( |
| 33 | + &self, |
| 34 | + aggregate_fn: &AggregateFnRef, |
| 35 | + batch: &ArrayRef, |
| 36 | + ctx: &mut ExecutionCtx, |
| 37 | + ) -> VortexResult<Option<Scalar>> { |
| 38 | + if !aggregate_fn.is::<AllNonDistinct>() { |
| 39 | + return Ok(None); |
| 40 | + } |
| 41 | + |
| 42 | + let Some(batch) = batch.as_opt::<Struct>() else { |
| 43 | + return Ok(None); |
| 44 | + }; |
| 45 | + let lhs = batch.unmasked_field(0); |
| 46 | + let rhs = batch.unmasked_field(1); |
| 47 | + let (Some(lhs), Some(rhs)) = ( |
| 48 | + lhs.as_opt::<ParquetVariant>(), |
| 49 | + rhs.as_opt::<ParquetVariant>(), |
| 50 | + ) else { |
| 51 | + return Ok(None); |
| 52 | + }; |
| 53 | + |
| 54 | + let typed_identical = match (lhs.typed_value_array(), rhs.typed_value_array()) { |
| 55 | + (Some(lhs_typed), Some(rhs_typed)) => { |
| 56 | + if lhs_typed.dtype().eq_ignore_nullability(rhs_typed.dtype()) { |
| 57 | + all_non_distinct(lhs_typed, rhs_typed, ctx)? |
| 58 | + } else { |
| 59 | + return Ok(None); |
| 60 | + } |
| 61 | + } |
| 62 | + _ => true, |
| 63 | + }; |
| 64 | + |
| 65 | + if typed_identical { |
| 66 | + let values_identical = match (lhs.value_array(), rhs.value_array()) { |
| 67 | + (Some(lhs_value), Some(rhs_value)) => all_non_distinct(lhs_value, rhs_value, ctx)?, |
| 68 | + (None, None) => true, |
| 69 | + // Mixed shredding layouts: let the generic canonical path handle it. |
| 70 | + _ => return Ok(None), |
| 71 | + }; |
| 72 | + Ok(Some(Scalar::bool( |
| 73 | + values_identical, |
| 74 | + Nullability::NonNullable, |
| 75 | + ))) |
| 76 | + } else { |
| 77 | + Ok(Some(Scalar::bool(false, Nullability::NonNullable))) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +#[cfg(test)] |
| 83 | +mod tests { |
| 84 | + use std::sync::LazyLock; |
| 85 | + |
| 86 | + use vortex_array::ArrayRef; |
| 87 | + use vortex_array::IntoArray; |
| 88 | + use vortex_array::VortexSessionExecute; |
| 89 | + use vortex_array::aggregate_fn::fns::all_non_distinct::all_non_distinct; |
| 90 | + use vortex_array::arrays::VarBinViewArray; |
| 91 | + use vortex_array::validity::Validity; |
| 92 | + use vortex_buffer::buffer; |
| 93 | + use vortex_error::VortexResult; |
| 94 | + use vortex_session::VortexSession; |
| 95 | + |
| 96 | + use crate::ParquetVariant; |
| 97 | + |
| 98 | + static SESSION: LazyLock<VortexSession> = LazyLock::new(|| { |
| 99 | + let session = vortex_array::array_session(); |
| 100 | + crate::initialize(&session); |
| 101 | + session |
| 102 | + }); |
| 103 | + |
| 104 | + /// Non-nullable, minimally-valid metadata column of `len` rows. |
| 105 | + fn metadata(len: usize) -> ArrayRef { |
| 106 | + VarBinViewArray::from_iter_bin(vec![b"\x01\x00"; len]).into_array() |
| 107 | + } |
| 108 | + |
| 109 | + /// Non-nullable binary `value` column. |
| 110 | + fn binary<T: AsRef<[u8]>>(values: impl IntoIterator<Item = T>) -> ArrayRef { |
| 111 | + VarBinViewArray::from_iter_bin(values).into_array() |
| 112 | + } |
| 113 | + |
| 114 | + fn parquet_variant( |
| 115 | + len: usize, |
| 116 | + value: Option<ArrayRef>, |
| 117 | + typed_value: Option<ArrayRef>, |
| 118 | + ) -> VortexResult<ArrayRef> { |
| 119 | + Ok( |
| 120 | + ParquetVariant::try_new(Validity::NonNullable, metadata(len), value, typed_value)? |
| 121 | + .into_array(), |
| 122 | + ) |
| 123 | + } |
| 124 | + |
| 125 | + #[test] |
| 126 | + fn all_non_distinct_matches_equal_unshredded() -> VortexResult<()> { |
| 127 | + let lhs = parquet_variant(2, Some(binary([b"\x10", b"\x11"])), None)?; |
| 128 | + let rhs = parquet_variant(2, Some(binary([b"\x10", b"\x11"])), None)?; |
| 129 | + let mut ctx = SESSION.create_execution_ctx(); |
| 130 | + assert!(all_non_distinct(&lhs, &rhs, &mut ctx)?); |
| 131 | + Ok(()) |
| 132 | + } |
| 133 | + |
| 134 | + #[test] |
| 135 | + fn all_non_distinct_detects_distinct_unshredded() -> VortexResult<()> { |
| 136 | + let lhs = parquet_variant(2, Some(binary([b"\x10", b"\x11"])), None)?; |
| 137 | + let rhs = parquet_variant(2, Some(binary([b"\x10", b"\x12"])), None)?; |
| 138 | + let mut ctx = SESSION.create_execution_ctx(); |
| 139 | + assert!(!all_non_distinct(&lhs, &rhs, &mut ctx)?); |
| 140 | + Ok(()) |
| 141 | + } |
| 142 | + |
| 143 | + #[test] |
| 144 | + fn all_non_distinct_matches_equal_value_and_typed() -> VortexResult<()> { |
| 145 | + let typed = || buffer![1i32, 2].into_array(); |
| 146 | + let lhs = parquet_variant(2, Some(binary([b"\x10", b"\x11"])), Some(typed()))?; |
| 147 | + let rhs = parquet_variant(2, Some(binary([b"\x10", b"\x11"])), Some(typed()))?; |
| 148 | + let mut ctx = SESSION.create_execution_ctx(); |
| 149 | + assert!(all_non_distinct(&lhs, &rhs, &mut ctx)?); |
| 150 | + Ok(()) |
| 151 | + } |
| 152 | + |
| 153 | + #[test] |
| 154 | + fn all_non_distinct_empty_is_true() -> VortexResult<()> { |
| 155 | + let lhs = parquet_variant(0, Some(binary(Vec::<&[u8]>::new())), None)?; |
| 156 | + let rhs = parquet_variant(0, Some(binary(Vec::<&[u8]>::new())), None)?; |
| 157 | + let mut ctx = SESSION.create_execution_ctx(); |
| 158 | + assert!(all_non_distinct(&lhs, &rhs, &mut ctx)?); |
| 159 | + Ok(()) |
| 160 | + } |
| 161 | +} |
0 commit comments