1616// under the License.
1717
1818use super :: TransformFunction ;
19- use crate :: Result ;
19+ use crate :: { Error , ErrorKind , Result } ;
2020use arrow:: array:: { Array , TimestampMicrosecondArray } ;
2121use arrow:: compute:: binary;
2222use arrow:: datatypes;
@@ -28,24 +28,28 @@ use arrow::{
2828use chrono:: Datelike ;
2929use std:: sync:: Arc ;
3030
31- /// 719163 is the number of days from 0000-01-01 to 1970-01-01
32- const EPOCH_DAY_FROM_CE : i32 = 719163 ;
33- const DAY_PER_SECOND : f64 = 0.0000115741 ;
34- const HOUR_PER_SECOND : f64 = 1_f64 / 3600.0 ;
35- const BASE_YEAR : i32 = 1970 ;
31+ /// The number of days since unix epoch.
32+ const DAY_SINCE_UNIX_EPOCH : i32 = 719163 ;
33+ /// Hour in one second.
34+ const HOUR_PER_SECOND : f64 = 1.0_f64 / 3600.0_f64 ;
35+ /// Day in one second.
36+ const DAY_PER_SECOND : f64 = 1.0_f64 / 24.0_f64 / 3600.0_f64 ;
37+ /// Year of unix epoch.
38+ const UNIX_EPOCH_YEAR : i32 = 1970 ;
3639
3740/// Extract a date or timestamp year, as years from 1970
3841pub struct Year ;
3942
4043impl TransformFunction for Year {
4144 fn transform ( & self , input : ArrayRef ) -> Result < ArrayRef > {
42- let array = year_dyn ( & input) . expect ( "Should not call transform in Year with non-date type" ) ;
45+ let array =
46+ year_dyn ( & input) . map_err ( |err| Error :: new ( ErrorKind :: Unexpected , format ! ( "{err}" ) ) ) ?;
4347 Ok ( Arc :: < Int32Array > :: new (
4448 array
4549 . as_any ( )
4650 . downcast_ref :: < Int32Array > ( )
4751 . unwrap ( )
48- . unary ( |v| v - BASE_YEAR ) ,
52+ . unary ( |v| v - UNIX_EPOCH_YEAR ) ,
4953 ) )
5054 }
5155}
@@ -56,14 +60,14 @@ pub struct Month;
5660impl TransformFunction for Month {
5761 fn transform ( & self , input : ArrayRef ) -> Result < ArrayRef > {
5862 let year_array =
59- year_dyn ( & input) . expect ( "Should not call transform in Month with non-date type" ) ;
63+ year_dyn ( & input) . map_err ( |err| Error :: new ( ErrorKind :: Unexpected , format ! ( "{err}" ) ) ) ? ;
6064 let year_array: Int32Array = year_array
6165 . as_any ( )
6266 . downcast_ref :: < Int32Array > ( )
6367 . unwrap ( )
64- . unary ( |v| 12 * ( v - BASE_YEAR ) ) ;
68+ . unary ( |v| 12 * ( v - UNIX_EPOCH_YEAR ) ) ;
6569 let month_array =
66- month_dyn ( & input) . expect ( "Should not call transform in Month with non-date type" ) ;
70+ month_dyn ( & input) . map_err ( |err| Error :: new ( ErrorKind :: Unexpected , format ! ( "{err}" ) ) ) ? ;
6771 Ok ( Arc :: < Int32Array > :: new (
6872 binary (
6973 month_array. as_any ( ) . downcast_ref :: < Int32Array > ( ) . unwrap ( ) ,
@@ -94,13 +98,18 @@ impl TransformFunction for Day {
9498 . unwrap ( )
9599 . unary ( |v| -> i32 {
96100 datatypes:: Date32Type :: to_naive_date ( v) . num_days_from_ce ( )
97- - EPOCH_DAY_FROM_CE
101+ - DAY_SINCE_UNIX_EPOCH
98102 } )
99103 }
100- _ => unreachable ! (
101- "Should not call transform in Day with type {:?}" ,
102- input. data_type( )
103- ) ,
104+ _ => {
105+ return Err ( Error :: new (
106+ ErrorKind :: Unexpected ,
107+ format ! (
108+ "Should not call internally for unsupport data type {:?}" ,
109+ input. data_type( )
110+ ) ,
111+ ) )
112+ }
104113 } ;
105114 Ok ( Arc :: new ( res) )
106115 }
@@ -117,10 +126,15 @@ impl TransformFunction for Hour {
117126 . downcast_ref :: < TimestampMicrosecondArray > ( )
118127 . unwrap ( )
119128 . unary ( |v| -> i32 { ( v as f64 * HOUR_PER_SECOND / 1000.0 / 1000.0 ) as i32 } ) ,
120- _ => unreachable ! (
121- "Should not call transform in Day with type {:?}" ,
122- input. data_type( )
123- ) ,
129+ _ => {
130+ return Err ( Error :: new (
131+ ErrorKind :: Unexpected ,
132+ format ! (
133+ "Should not call internally for unsupport data type {:?}" ,
134+ input. data_type( )
135+ ) ,
136+ ) )
137+ }
124138 } ;
125139 Ok ( Arc :: new ( res) )
126140 }
0 commit comments