|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use std::sync::Arc; |
| 5 | + |
| 6 | +use arrow_array::types::{Decimal32Type, Decimal64Type, Decimal128Type, Decimal256Type}; |
| 7 | +use arrow_array::{ArrayRef, PrimitiveArray}; |
| 8 | +use vortex_buffer::Buffer; |
| 9 | +use vortex_dtype::i256; |
| 10 | +use vortex_error::VortexResult; |
| 11 | +use vortex_vector::{DVector, DecimalVector}; |
| 12 | + |
| 13 | +use crate::arrow::IntoArrow; |
| 14 | + |
| 15 | +impl IntoArrow<ArrayRef> for DecimalVector { |
| 16 | + fn into_arrow(self) -> VortexResult<ArrayRef> { |
| 17 | + match self { |
| 18 | + DecimalVector::D8(v) => v.into_arrow(), |
| 19 | + DecimalVector::D16(v) => v.into_arrow(), |
| 20 | + DecimalVector::D32(v) => v.into_arrow(), |
| 21 | + DecimalVector::D64(v) => v.into_arrow(), |
| 22 | + DecimalVector::D128(v) => v.into_arrow(), |
| 23 | + DecimalVector::D256(v) => v.into_arrow(), |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +macro_rules! impl_decimal_upcast_i32 { |
| 29 | + ($T:ty) => { |
| 30 | + impl IntoArrow<ArrayRef> for DVector<$T> { |
| 31 | + fn into_arrow(self) -> VortexResult<ArrayRef> { |
| 32 | + let (_, elements, validity) = self.into_parts(); |
| 33 | + // Upcast the DVector to Arrow's smallest decimal type (Decimal32) |
| 34 | + let elements = |
| 35 | + Buffer::<i32>::from_trusted_len_iter(elements.iter().map(|i| *i as i32)); |
| 36 | + Ok(Arc::new(PrimitiveArray::<Decimal32Type>::new( |
| 37 | + elements.into_arrow_scalar_buffer(), |
| 38 | + validity.into_arrow()?, |
| 39 | + ))) |
| 40 | + } |
| 41 | + } |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +impl_decimal_upcast_i32!(i8); |
| 46 | +impl_decimal_upcast_i32!(i16); |
| 47 | + |
| 48 | +/// Direct Arrow conversion for vectors that map directly to Arrow decimal types. |
| 49 | +macro_rules! impl_decimal { |
| 50 | + ($T:ty, $A:ty) => { |
| 51 | + impl IntoArrow<ArrayRef> for DVector<$T> { |
| 52 | + fn into_arrow(self) -> VortexResult<ArrayRef> { |
| 53 | + let (_, elements, validity) = self.into_parts(); |
| 54 | + Ok(Arc::new(PrimitiveArray::<$A>::new( |
| 55 | + elements.into_arrow_scalar_buffer(), |
| 56 | + validity.into_arrow()?, |
| 57 | + ))) |
| 58 | + } |
| 59 | + } |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +impl_decimal!(i32, Decimal32Type); |
| 64 | +impl_decimal!(i64, Decimal64Type); |
| 65 | +impl_decimal!(i128, Decimal128Type); |
| 66 | + |
| 67 | +impl IntoArrow<ArrayRef> for DVector<i256> { |
| 68 | + fn into_arrow(self) -> VortexResult<ArrayRef> { |
| 69 | + let (_, elements, validity) = self.into_parts(); |
| 70 | + |
| 71 | + // Transmute the elements from our i256 to Arrow's. |
| 72 | + // SAFETY: we use Arrow's type internally for our layout. |
| 73 | + let elements = |
| 74 | + unsafe { std::mem::transmute::<Buffer<i256>, Buffer<arrow_buffer::i256>>(elements) }; |
| 75 | + |
| 76 | + Ok(Arc::new(PrimitiveArray::<Decimal256Type>::new( |
| 77 | + elements.into_arrow_scalar_buffer(), |
| 78 | + validity.into_arrow()?, |
| 79 | + ))) |
| 80 | + } |
| 81 | +} |
0 commit comments