|
1 | 1 | mod filter;
|
2 | 2 | mod take;
|
3 | 3 |
|
4 |
| -use itertools::Itertools as _; |
5 | 4 | use vortex_array::array::{PrimitiveArray, TemporalArray};
|
6 | 5 | use vortex_array::compute::{
|
7 |
| - scalar_at, slice, ComputeVTable, FilterFn, ScalarAtFn, SliceFn, TakeFn, |
| 6 | + scalar_at, slice, try_cast, ComputeVTable, FilterFn, ScalarAtFn, SliceFn, TakeFn, |
8 | 7 | };
|
9 | 8 | use vortex_array::validity::ArrayValidity;
|
10 | 9 | use vortex_array::{ArrayDType, ArrayData, IntoArrayData, IntoArrayVariant};
|
11 | 10 | use vortex_datetime_dtype::{TemporalMetadata, TimeUnit};
|
12 |
| -use vortex_dtype::DType; |
13 |
| -use vortex_error::{vortex_bail, VortexResult}; |
14 |
| -use vortex_scalar::Scalar; |
| 11 | +use vortex_dtype::Nullability::NonNullable; |
| 12 | +use vortex_dtype::{DType, PType}; |
| 13 | +use vortex_error::{vortex_bail, VortexExpect, VortexResult}; |
| 14 | +use vortex_scalar::{PrimitiveScalar, Scalar}; |
15 | 15 |
|
16 | 16 | use crate::{DateTimePartsArray, DateTimePartsEncoding};
|
17 | 17 |
|
@@ -106,17 +106,55 @@ pub fn decode_to_temporal(array: &DateTimePartsArray) -> VortexResult<TemporalAr
|
106 | 106 | TimeUnit::D => vortex_bail!(InvalidArgument: "cannot decode into TimeUnit::D"),
|
107 | 107 | };
|
108 | 108 |
|
109 |
| - let days_buf = array.days().into_primitive()?; |
110 |
| - let seconds_buf = array.seconds().into_primitive()?; |
111 |
| - let subsecond_buf = array.subsecond().into_primitive()?; |
112 |
| - |
113 |
| - let values = days_buf |
114 |
| - .maybe_null_slice::<i64>() |
115 |
| - .iter() |
116 |
| - .zip_eq(seconds_buf.maybe_null_slice::<i64>().iter()) |
117 |
| - .zip_eq(subsecond_buf.maybe_null_slice::<i64>().iter()) |
118 |
| - .map(|((d, s), ss)| d * 86_400 * divisor + s * divisor + ss) |
119 |
| - .collect::<Vec<_>>(); |
| 109 | + let days_buf = try_cast( |
| 110 | + array.days(), |
| 111 | + &DType::Primitive(PType::I64, array.dtype().nullability()), |
| 112 | + )? |
| 113 | + .into_primitive()?; |
| 114 | + let mut values: Vec<i64> = days_buf |
| 115 | + .into_maybe_null_slice::<i64>() |
| 116 | + .into_iter() |
| 117 | + .map(|d| d * 86_400 * divisor) |
| 118 | + .collect(); |
| 119 | + |
| 120 | + if let Some(seconds) = array.seconds().as_constant() { |
| 121 | + let seconds = |
| 122 | + PrimitiveScalar::try_from(&seconds.cast(&DType::Primitive(PType::I64, NonNullable))?)? |
| 123 | + .typed_value::<i64>() |
| 124 | + .vortex_expect("non-nullable"); |
| 125 | + for v in values.iter_mut() { |
| 126 | + *v += seconds * divisor; |
| 127 | + } |
| 128 | + } else { |
| 129 | + let seconds_buf = try_cast(array.seconds(), &DType::Primitive(PType::U32, NonNullable))? |
| 130 | + .into_primitive()?; |
| 131 | + for (v, second) in values.iter_mut().zip(seconds_buf.maybe_null_slice::<u32>()) { |
| 132 | + *v += (*second as i64) * divisor; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + if let Some(subseconds) = array.subsecond().as_constant() { |
| 137 | + let subseconds = PrimitiveScalar::try_from( |
| 138 | + &subseconds.cast(&DType::Primitive(PType::I64, NonNullable))?, |
| 139 | + )? |
| 140 | + .typed_value::<i64>() |
| 141 | + .vortex_expect("non-nullable"); |
| 142 | + for v in values.iter_mut() { |
| 143 | + *v += subseconds; |
| 144 | + } |
| 145 | + } else { |
| 146 | + let subsecond_buf = try_cast( |
| 147 | + array.subsecond(), |
| 148 | + &DType::Primitive(PType::I64, NonNullable), |
| 149 | + )? |
| 150 | + .into_primitive()?; |
| 151 | + for (v, subsecond) in values |
| 152 | + .iter_mut() |
| 153 | + .zip(subsecond_buf.maybe_null_slice::<i64>()) |
| 154 | + { |
| 155 | + *v += *subsecond; |
| 156 | + } |
| 157 | + } |
120 | 158 |
|
121 | 159 | Ok(TemporalArray::new_timestamp(
|
122 | 160 | PrimitiveArray::from_vec(values, array.validity()).into_array(),
|
|
0 commit comments