|
1 | 1 | // SPDX-License-Identifier: Apache-2.0 |
2 | 2 | // SPDX-FileCopyrightText: Copyright the Vortex contributors |
3 | 3 |
|
| 4 | +use vortex::array::IntoArray; |
| 5 | +use vortex::array::arrays::ConstantArray; |
| 6 | +use vortex::array::arrays::FixedSizeList; |
| 7 | +use vortex::array::builtins::ArrayBuiltins; |
4 | 8 | use vortex::array::vtable::OperationsVTable; |
| 9 | +use vortex::dtype::Nullability; |
5 | 10 | use vortex::error::VortexResult; |
| 11 | +use vortex::error::vortex_err; |
6 | 12 | use vortex::scalar::Scalar; |
| 13 | +use vortex::scalar_fn::fns::operators::Operator; |
7 | 14 |
|
8 | 15 | use crate::encodings::norm::array::NormVectorArray; |
9 | 16 | use crate::encodings::norm::vtable::NormVector; |
| 17 | +use crate::utils::extension_list_size; |
| 18 | +use crate::utils::extension_storage; |
10 | 19 |
|
11 | 20 | impl OperationsVTable<NormVector> for NormVector { |
12 | 21 | fn scalar_at(array: &NormVectorArray, index: usize) -> VortexResult<Scalar> { |
13 | | - array.vector_array().scalar_at(index) |
| 22 | + let ext = array |
| 23 | + .vector_array() |
| 24 | + .dtype() |
| 25 | + .as_extension_opt() |
| 26 | + .ok_or_else(|| { |
| 27 | + vortex_err!( |
| 28 | + "expected Vector extension dtype, got {}", |
| 29 | + array.vector_array().dtype() |
| 30 | + ) |
| 31 | + })?; |
| 32 | + let list_size = extension_list_size(ext)?; |
| 33 | + |
| 34 | + // Get the storage (FixedSizeList) and slice out the elements for this row. |
| 35 | + let storage = extension_storage(array.vector_array())?; |
| 36 | + let fsl = storage |
| 37 | + .as_opt::<FixedSizeList>() |
| 38 | + .ok_or_else(|| vortex_err!("expected FixedSizeList storage"))?; |
| 39 | + let row_elements = fsl.fixed_size_list_elements_at(index)?; |
| 40 | + |
| 41 | + // Multiply all elements by the norm using a ConstantArray broadcast. |
| 42 | + let norm_scalar = array.norms().scalar_at(index)?; |
| 43 | + let norm_broadcast = ConstantArray::new(norm_scalar, list_size).into_array(); |
| 44 | + let scaled = row_elements.binary(norm_broadcast, Operator::Mul)?; |
| 45 | + |
| 46 | + // Rebuild the FSL scalar, then wrap in the extension type. |
| 47 | + let element_dtype = ext |
| 48 | + .storage_dtype() |
| 49 | + .as_fixed_size_list_element_opt() |
| 50 | + .ok_or_else(|| { |
| 51 | + vortex_err!( |
| 52 | + "expected FixedSizeList storage dtype, got {}", |
| 53 | + ext.storage_dtype() |
| 54 | + ) |
| 55 | + })?; |
| 56 | + |
| 57 | + let children: Vec<Scalar> = (0..list_size) |
| 58 | + .map(|i| scaled.scalar_at(i)) |
| 59 | + .collect::<VortexResult<_>>()?; |
| 60 | + |
| 61 | + let fsl_scalar = |
| 62 | + Scalar::fixed_size_list(element_dtype.clone(), children, Nullability::NonNullable); |
| 63 | + |
| 64 | + Ok(Scalar::extension_ref(ext.clone(), fsl_scalar)) |
14 | 65 | } |
15 | 66 | } |
0 commit comments