@@ -71,7 +71,7 @@ pub fn array_with_timezone(
7171) -> Result < ArrayRef , ArrowError > {
7272 match array. data_type ( ) {
7373 DataType :: Timestamp ( TimeUnit :: Millisecond , None ) => {
74- assert ! ( ! timezone. is_empty ( ) ) ;
74+ require_timezone ( & timezone) ? ;
7575 match to_type {
7676 Some ( DataType :: Utf8 ) | Some ( DataType :: Date32 ) => Ok ( array) ,
7777 Some ( DataType :: Timestamp ( _, Some ( target_tz) ) ) => {
@@ -90,7 +90,7 @@ pub fn array_with_timezone(
9090 }
9191 }
9292 DataType :: Timestamp ( TimeUnit :: Microsecond , None ) => {
93- assert ! ( ! timezone. is_empty ( ) ) ;
93+ require_timezone ( & timezone) ? ;
9494 match to_type {
9595 Some ( DataType :: Utf8 ) | Some ( DataType :: Date32 ) => Ok ( array) ,
9696 Some ( DataType :: Timestamp ( _, Some ( target_tz) ) ) => {
@@ -107,7 +107,7 @@ pub fn array_with_timezone(
107107 }
108108 }
109109 DataType :: Timestamp ( _, None ) => {
110- assert ! ( ! timezone. is_empty ( ) ) ;
110+ require_timezone ( & timezone) ? ;
111111 match to_type {
112112 Some ( DataType :: Utf8 ) | Some ( DataType :: Date32 ) => Ok ( array) ,
113113 Some ( DataType :: Timestamp ( _, Some ( target_tz) ) ) => {
@@ -124,7 +124,7 @@ pub fn array_with_timezone(
124124 }
125125 }
126126 DataType :: Timestamp ( TimeUnit :: Microsecond , Some ( _) ) => {
127- assert ! ( ! timezone. is_empty ( ) ) ;
127+ require_timezone ( & timezone) ? ;
128128 let array = as_primitive_array :: < TimestampMicrosecondType > ( & array) ;
129129 let array_with_timezone = array. clone ( ) . with_timezone ( timezone. clone ( ) ) ;
130130 let array = Arc :: new ( array_with_timezone) as ArrayRef ;
@@ -136,7 +136,7 @@ pub fn array_with_timezone(
136136 }
137137 }
138138 DataType :: Timestamp ( TimeUnit :: Millisecond , Some ( _) ) => {
139- assert ! ( ! timezone. is_empty ( ) ) ;
139+ require_timezone ( & timezone) ? ;
140140 let array = as_primitive_array :: < TimestampMillisecondType > ( & array) ;
141141 let array_with_timezone = array. clone ( ) . with_timezone ( timezone. clone ( ) ) ;
142142 let array = Arc :: new ( array_with_timezone) as ArrayRef ;
@@ -161,6 +161,17 @@ pub fn array_with_timezone(
161161 }
162162}
163163
164+ /// Converting a timestamp needs the session timezone. An empty one means the caller built the
165+ /// conversion without it, which is reported as an error rather than a panic.
166+ fn require_timezone ( timezone : & str ) -> Result < ( ) , ArrowError > {
167+ if timezone. is_empty ( ) {
168+ return Err ( ArrowError :: InvalidArgumentError (
169+ "Converting a timestamp requires a timezone, but none was given" . to_string ( ) ,
170+ ) ) ;
171+ }
172+ Ok ( ( ) )
173+ }
174+
164175fn datetime_cast_err ( value : i64 ) -> ArrowError {
165176 ArrowError :: CastError ( format ! (
166177 "Cannot convert TimestampMicrosecondType {value} to datetime. Comet only supports dates between Jan 1, 262145 BCE and Dec 31, 262143 CE" ,
@@ -208,7 +219,6 @@ pub(crate) fn timestamp_ntz_to_timestamp(
208219 tz : & str ,
209220 to_timezone : Option < & str > ,
210221) -> Result < ArrayRef , ArrowError > {
211- assert ! ( !tz. is_empty( ) ) ;
212222 match array. data_type ( ) {
213223 DataType :: Timestamp ( TimeUnit :: Microsecond , None ) => {
214224 let array = as_primitive_array :: < TimestampMicrosecondType > ( & array) ;
@@ -261,7 +271,6 @@ pub(crate) fn cast_timestamp_to_ntz(
261271 array : ArrayRef ,
262272 timezone : & str ,
263273) -> Result < ArrayRef , ArrowError > {
264- assert ! ( !timezone. is_empty( ) ) ;
265274 let tz: Tz = timezone. parse ( ) ?;
266275 match array. data_type ( ) {
267276 DataType :: Timestamp ( TimeUnit :: Microsecond , Some ( _) ) => {
@@ -289,7 +298,6 @@ pub(crate) fn cast_timestamp_to_ntz(
289298
290299/// This takes for special pre-casting cases of Spark. E.g., Timestamp to String.
291300fn pre_timestamp_cast ( array : ArrayRef , timezone : String ) -> Result < ArrayRef , ArrowError > {
292- assert ! ( !timezone. is_empty( ) ) ;
293301 match array. data_type ( ) {
294302 DataType :: Timestamp ( _, _) => {
295303 // Spark doesn't output timezone while casting timestamp to string, but arrow's cast
@@ -426,6 +434,17 @@ mod tests {
426434 Arc :: new ( TimestampMicrosecondArray :: from ( vec ! [ ts] ) . with_timezone ( tz. to_string ( ) ) )
427435 }
428436
437+ #[ test]
438+ fn test_array_with_timezone_requires_a_timezone ( ) {
439+ let ltz = ts_with_tz ( "2024-01-15 10:30:00" , "UTC" ) ;
440+ let err = array_with_timezone ( ltz, String :: new ( ) , Some ( & DataType :: Utf8 ) ) . unwrap_err ( ) ;
441+ assert ! ( err. to_string( ) . contains( "requires a timezone" ) , "{err}" ) ;
442+
443+ let ntz = array_containing ( "2024-01-15 10:30:00" ) ;
444+ let utc = DataType :: Timestamp ( TimeUnit :: Microsecond , Some ( "UTC" . into ( ) ) ) ;
445+ assert ! ( array_with_timezone( ntz, String :: new( ) , Some ( & utc) ) . is_err( ) ) ;
446+ }
447+
429448 #[ test]
430449 fn test_cast_timestamp_to_ntz_utc ( ) {
431450 // In UTC, local time == UTC time, so NTZ value == UTC epoch value
0 commit comments