22// SPDX-FileCopyrightText: Copyright the Vortex contributors
33
44use num_traits:: AsPrimitive ;
5+ use vortex_array:: ExecutionCtx ;
56use vortex_array:: IntoArray ;
6- use vortex_array:: ToCanonical ;
77use vortex_array:: arrays:: PrimitiveArray ;
88use vortex_array:: arrays:: TemporalArray ;
99use vortex_array:: compute:: cast;
@@ -15,14 +15,18 @@ use vortex_dtype::datetime::TemporalMetadata;
1515use vortex_dtype:: datetime:: TimeUnit ;
1616use vortex_dtype:: match_each_integer_ptype;
1717use vortex_error:: VortexExpect as _;
18+ use vortex_error:: VortexResult ;
1819use vortex_error:: vortex_panic;
1920
2021use crate :: DateTimePartsArray ;
2122
2223/// Decode an [Array] into a [TemporalArray].
2324///
2425/// Enforces that the passed array is actually a [DateTimePartsArray] with proper metadata.
25- pub fn decode_to_temporal ( array : & DateTimePartsArray ) -> TemporalArray {
26+ pub fn decode_to_temporal (
27+ array : & DateTimePartsArray ,
28+ ctx : & mut ExecutionCtx ,
29+ ) -> VortexResult < TemporalArray > {
2630 let DType :: Extension ( ext) = array. dtype ( ) . clone ( ) else {
2731 vortex_panic ! ( ComputeError : "expected dtype to be DType::Extension variant" )
2832 } ;
@@ -44,7 +48,7 @@ pub fn decode_to_temporal(array: &DateTimePartsArray) -> TemporalArray {
4448 & DType :: Primitive ( PType :: I64 , array. dtype ( ) . nullability ( ) ) ,
4549 )
4650 . vortex_expect ( "must be able to cast days to i64" )
47- . to_primitive ( ) ;
51+ . execute :: < PrimitiveArray > ( ctx ) ? ;
4852
4953 // We start with the days component, which is always present.
5054 // And then add the seconds and subseconds components.
@@ -64,7 +68,7 @@ pub fn decode_to_temporal(array: &DateTimePartsArray) -> TemporalArray {
6468 * v += seconds;
6569 }
6670 } else {
67- let seconds_buf = array. seconds ( ) . to_primitive ( ) ;
71+ let seconds_buf = array. seconds ( ) . clone ( ) . execute :: < PrimitiveArray > ( ctx ) ? ;
6872 match_each_integer_ptype ! ( seconds_buf. ptype( ) , |S | {
6973 for ( v, second) in values. iter_mut( ) . zip( seconds_buf. as_slice:: <S >( ) ) {
7074 let second: i64 = second. as_( ) ;
@@ -82,7 +86,7 @@ pub fn decode_to_temporal(array: &DateTimePartsArray) -> TemporalArray {
8286 * v += subseconds;
8387 }
8488 } else {
85- let subseconds_buf = array. subseconds ( ) . to_primitive ( ) ;
89+ let subseconds_buf = array. subseconds ( ) . clone ( ) . execute :: < PrimitiveArray > ( ctx ) ? ;
8690 match_each_integer_ptype ! ( subseconds_buf. ptype( ) , |S | {
8791 for ( v, subseconds) in values. iter_mut( ) . zip( subseconds_buf. as_slice:: <S >( ) ) {
8892 let subseconds: i64 = subseconds. as_( ) ;
@@ -91,17 +95,18 @@ pub fn decode_to_temporal(array: &DateTimePartsArray) -> TemporalArray {
9195 } ) ;
9296 }
9397
94- TemporalArray :: new_timestamp (
98+ Ok ( TemporalArray :: new_timestamp (
9599 PrimitiveArray :: new ( values. freeze ( ) , Validity :: copy_from_array ( array. as_ref ( ) ) )
96100 . into_array ( ) ,
97101 temporal_metadata. time_unit ( ) ,
98102 temporal_metadata. time_zone ( ) . map ( ToString :: to_string) ,
99- )
103+ ) )
100104}
101105
102106#[ cfg( test) ]
103107mod test {
104108 use rstest:: rstest;
109+ use vortex_array:: ExecutionCtx ;
105110 use vortex_array:: IntoArray ;
106111 use vortex_array:: ToCanonical ;
107112 use vortex_array:: arrays:: PrimitiveArray ;
@@ -111,6 +116,8 @@ mod test {
111116 use vortex_array:: vtable:: ValidityHelper ;
112117 use vortex_buffer:: buffer;
113118 use vortex_dtype:: datetime:: TimeUnit ;
119+ use vortex_error:: VortexResult ;
120+ use vortex_session:: VortexSession ;
114121
115122 use crate :: DateTimePartsArray ;
116123 use crate :: canonical:: decode_to_temporal;
@@ -120,7 +127,7 @@ mod test {
120127 #[ case( Validity :: AllValid ) ]
121128 #[ case( Validity :: AllInvalid ) ]
122129 #[ case( Validity :: from_iter( [ true , true , false , false , true , true ] ) ) ]
123- fn test_decode_to_temporal ( #[ case] validity : Validity ) {
130+ fn test_decode_to_temporal ( #[ case] validity : Validity ) -> VortexResult < ( ) > {
124131 let milliseconds = PrimitiveArray :: new (
125132 buffer ! [
126133 86_400i64 , // element with only day component
@@ -144,11 +151,13 @@ mod test {
144151 validity. to_mask( date_times. len( ) )
145152 ) ;
146153
147- let primitive_values = decode_to_temporal ( & date_times)
154+ let mut ctx = ExecutionCtx :: new ( VortexSession :: empty ( ) ) ;
155+ let primitive_values = decode_to_temporal ( & date_times, & mut ctx) ?
148156 . temporal_values ( )
149157 . to_primitive ( ) ;
150158
151159 assert_arrays_eq ! ( primitive_values, milliseconds) ;
152160 assert_eq ! ( primitive_values. validity( ) , & validity) ;
161+ Ok ( ( ) )
153162 }
154163}
0 commit comments