|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use vortex_error::VortexResult; |
| 5 | +use vortex_scalar::NumericOperator; |
| 6 | + |
| 7 | +use super::{DictArray, DictVTable}; |
| 8 | +use crate::arrays::ConstantArray; |
| 9 | +use crate::compute::{NumericKernel, NumericKernelAdapter, numeric}; |
| 10 | +use crate::{Array, ArrayRef, IntoArray, register_kernel}; |
| 11 | + |
| 12 | +impl NumericKernel for DictVTable { |
| 13 | + fn numeric( |
| 14 | + &self, |
| 15 | + lhs: &DictArray, |
| 16 | + rhs: &dyn Array, |
| 17 | + op: NumericOperator, |
| 18 | + ) -> VortexResult<Option<ArrayRef>> { |
| 19 | + // If we have more values than codes, it is faster to canonicalise first. |
| 20 | + if lhs.values().len() > lhs.codes().len() { |
| 21 | + return Ok(None); |
| 22 | + } |
| 23 | + |
| 24 | + // Only push down if all values are referenced to avoid incorrect results |
| 25 | + // See: https://github.com/vortex-data/vortex/pull/4560 |
| 26 | + // Unchecked operation will be fine to pushdown. |
| 27 | + if !lhs.has_all_values_referenced() { |
| 28 | + return Ok(None); |
| 29 | + } |
| 30 | + |
| 31 | + // If the RHS is constant, then we just need to apply the operation to our encoded values. |
| 32 | + if let Some(rhs_scalar) = rhs.as_constant() { |
| 33 | + let values_result = numeric( |
| 34 | + lhs.values(), |
| 35 | + ConstantArray::new(rhs_scalar, lhs.values().len()).as_ref(), |
| 36 | + op, |
| 37 | + )?; |
| 38 | + |
| 39 | + // SAFETY: values len preserved, codes all still point to valid values |
| 40 | + // all_values_referenced preserved since operation doesn't change which values are referenced |
| 41 | + let result = unsafe { |
| 42 | + DictArray::new_unchecked(lhs.codes().clone(), values_result) |
| 43 | + .set_all_values_referenced(lhs.has_all_values_referenced()) |
| 44 | + .into_array() |
| 45 | + }; |
| 46 | + |
| 47 | + return Ok(Some(result)); |
| 48 | + } |
| 49 | + |
| 50 | + // It's a little more complex, but we could perform binary operations against the dictionary |
| 51 | + // values in the future. |
| 52 | + Ok(None) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +register_kernel!(NumericKernelAdapter(DictVTable).lift()); |
| 57 | + |
| 58 | +#[cfg(test)] |
| 59 | +mod tests { |
| 60 | + use vortex_buffer::buffer; |
| 61 | + use vortex_scalar::NumericOperator; |
| 62 | + |
| 63 | + use crate::arrays::dict::DictArray; |
| 64 | + use crate::arrays::{ConstantArray, PrimitiveArray}; |
| 65 | + use crate::compute::numeric; |
| 66 | + use crate::{IntoArray, assert_arrays_eq}; |
| 67 | + |
| 68 | + #[test] |
| 69 | + fn test_add_const() { |
| 70 | + // Create a dict with all_values_referenced = true |
| 71 | + let dict = unsafe { |
| 72 | + DictArray::new_unchecked( |
| 73 | + buffer![0u32, 1, 2, 0, 1].into_array(), |
| 74 | + buffer![10i32, 20, 30].into_array(), |
| 75 | + ) |
| 76 | + .set_all_values_referenced(true) |
| 77 | + }; |
| 78 | + |
| 79 | + let res = numeric( |
| 80 | + dict.as_ref(), |
| 81 | + ConstantArray::new(5i32, 5).as_ref(), |
| 82 | + NumericOperator::Add, |
| 83 | + ) |
| 84 | + .unwrap(); |
| 85 | + |
| 86 | + let expected = PrimitiveArray::from_iter([15i32, 25, 35, 15, 25]); |
| 87 | + assert_arrays_eq!(res.to_canonical().into_array(), expected.to_array()); |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn test_mul_const() { |
| 92 | + // Create a dict with all_values_referenced = true |
| 93 | + let dict = unsafe { |
| 94 | + DictArray::new_unchecked( |
| 95 | + buffer![0u32, 1, 2, 1, 0].into_array(), |
| 96 | + buffer![2i32, 3, 5].into_array(), |
| 97 | + ) |
| 98 | + .set_all_values_referenced(true) |
| 99 | + }; |
| 100 | + |
| 101 | + let res = numeric( |
| 102 | + dict.as_ref(), |
| 103 | + ConstantArray::new(10i32, 5).as_ref(), |
| 104 | + NumericOperator::Mul, |
| 105 | + ) |
| 106 | + .unwrap(); |
| 107 | + |
| 108 | + let expected = PrimitiveArray::from_iter([20i32, 30, 50, 30, 20]); |
| 109 | + assert_arrays_eq!(res.to_canonical().into_array(), expected.to_array()); |
| 110 | + } |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn test_no_pushdown_when_not_all_values_referenced() { |
| 114 | + // Create a dict with all_values_referenced = false (default) |
| 115 | + let dict = DictArray::try_new( |
| 116 | + buffer![0u32, 1, 0, 1].into_array(), |
| 117 | + buffer![10i32, 20, 30].into_array(), // value at index 2 is not referenced |
| 118 | + ) |
| 119 | + .unwrap(); |
| 120 | + |
| 121 | + // Should return None, indicating no pushdown |
| 122 | + let res = numeric( |
| 123 | + dict.as_ref(), |
| 124 | + ConstantArray::new(5i32, 4).as_ref(), |
| 125 | + NumericOperator::Add, |
| 126 | + ) |
| 127 | + .unwrap(); |
| 128 | + |
| 129 | + // Verify the result by canonicalizing |
| 130 | + let expected = PrimitiveArray::from_iter([15i32, 25, 15, 25]); |
| 131 | + assert_arrays_eq!(res.to_canonical().into_array(), expected.to_array()); |
| 132 | + } |
| 133 | + |
| 134 | + #[test] |
| 135 | + fn test_sub_const() { |
| 136 | + // Create a dict with all_values_referenced = true |
| 137 | + let dict = unsafe { |
| 138 | + DictArray::new_unchecked( |
| 139 | + buffer![0u32, 1, 2].into_array(), |
| 140 | + buffer![100i32, 50, 25].into_array(), |
| 141 | + ) |
| 142 | + .set_all_values_referenced(true) |
| 143 | + }; |
| 144 | + |
| 145 | + let res = numeric( |
| 146 | + dict.as_ref(), |
| 147 | + ConstantArray::new(10i32, 3).as_ref(), |
| 148 | + NumericOperator::Sub, |
| 149 | + ) |
| 150 | + .unwrap(); |
| 151 | + |
| 152 | + let expected = PrimitiveArray::from_iter([90i32, 40, 15]); |
| 153 | + assert_arrays_eq!(res.to_canonical().into_array(), expected.to_array()); |
| 154 | + } |
| 155 | +} |
0 commit comments