1- use super :: errors:: { ArrowDestinationError , Result } ;
1+ use super :: {
2+ errors:: { ArrowDestinationError , Result } ,
3+ typesystem:: { DateTimeWrapperMicro , NaiveDateTimeWrapperMicro , NaiveTimeWrapperMicro } ,
4+ } ;
25use crate :: constants:: SECONDS_IN_DAY ;
36use arrow:: array:: {
4- ArrayBuilder , BooleanBuilder , Date32Builder , Date64Builder , Float32Builder , Float64Builder ,
5- Int32Builder , Int64Builder , LargeBinaryBuilder , StringBuilder , Time64NanosecondBuilder ,
6- TimestampNanosecondBuilder , UInt32Builder , UInt64Builder ,
7+ ArrayBuilder , BooleanBuilder , Date32Builder , Float32Builder , Float64Builder , Int32Builder ,
8+ Int64Builder , LargeBinaryBuilder , StringBuilder , Time64MicrosecondBuilder ,
9+ Time64NanosecondBuilder , TimestampMicrosecondBuilder , TimestampNanosecondBuilder ,
10+ UInt32Builder , UInt64Builder ,
711} ;
812use arrow:: datatypes:: Field ;
913use arrow:: datatypes:: { DataType as ArrowDataType , TimeUnit } ;
@@ -188,6 +192,48 @@ impl ArrowAssoc for Option<DateTime<Utc>> {
188192 }
189193}
190194
195+ impl ArrowAssoc for DateTimeWrapperMicro {
196+ type Builder = TimestampMicrosecondBuilder ;
197+
198+ fn builder ( nrows : usize ) -> Self :: Builder {
199+ TimestampMicrosecondBuilder :: with_capacity ( nrows) . with_timezone ( "UTC" )
200+ }
201+
202+ #[ throws( ArrowDestinationError ) ]
203+ fn append ( builder : & mut Self :: Builder , value : DateTimeWrapperMicro ) {
204+ builder. append_value ( value. 0 . timestamp_micros ( ) ) ;
205+ }
206+
207+ fn field ( header : & str ) -> Field {
208+ Field :: new (
209+ header,
210+ ArrowDataType :: Timestamp ( TimeUnit :: Microsecond , Some ( "UTC" . into ( ) ) ) ,
211+ false ,
212+ )
213+ }
214+ }
215+
216+ impl ArrowAssoc for Option < DateTimeWrapperMicro > {
217+ type Builder = TimestampMicrosecondBuilder ;
218+
219+ fn builder ( nrows : usize ) -> Self :: Builder {
220+ TimestampMicrosecondBuilder :: with_capacity ( nrows) . with_timezone ( "UTC" )
221+ }
222+
223+ #[ throws( ArrowDestinationError ) ]
224+ fn append ( builder : & mut Self :: Builder , value : Option < DateTimeWrapperMicro > ) {
225+ builder. append_option ( value. map ( |x| x. 0 . timestamp_micros ( ) ) ) ;
226+ }
227+
228+ fn field ( header : & str ) -> Field {
229+ Field :: new (
230+ header,
231+ ArrowDataType :: Timestamp ( TimeUnit :: Microsecond , Some ( "UTC" . into ( ) ) ) ,
232+ true ,
233+ )
234+ }
235+ }
236+
191237fn naive_date_to_arrow ( nd : NaiveDate ) -> i32 {
192238 match nd. and_hms_opt ( 0 , 0 , 0 ) {
193239 Some ( dt) => ( dt. and_utc ( ) . timestamp ( ) / SECONDS_IN_DAY ) as i32 ,
@@ -196,7 +242,9 @@ fn naive_date_to_arrow(nd: NaiveDate) -> i32 {
196242}
197243
198244fn naive_datetime_to_arrow ( nd : NaiveDateTime ) -> i64 {
199- nd. and_utc ( ) . timestamp_millis ( )
245+ nd. and_utc ( )
246+ . timestamp_nanos_opt ( )
247+ . unwrap_or_else ( || panic ! ( "out of range DateTime" ) )
200248}
201249
202250impl ArrowAssoc for Option < NaiveDate > {
@@ -234,10 +282,10 @@ impl ArrowAssoc for NaiveDate {
234282}
235283
236284impl ArrowAssoc for Option < NaiveDateTime > {
237- type Builder = Date64Builder ;
285+ type Builder = TimestampNanosecondBuilder ;
238286
239287 fn builder ( nrows : usize ) -> Self :: Builder {
240- Date64Builder :: with_capacity ( nrows)
288+ TimestampNanosecondBuilder :: with_capacity ( nrows)
241289 }
242290
243291 fn append ( builder : & mut Self :: Builder , value : Option < NaiveDateTime > ) -> Result < ( ) > {
@@ -246,15 +294,19 @@ impl ArrowAssoc for Option<NaiveDateTime> {
246294 }
247295
248296 fn field ( header : & str ) -> Field {
249- Field :: new ( header, ArrowDataType :: Date64 , true )
297+ Field :: new (
298+ header,
299+ ArrowDataType :: Timestamp ( TimeUnit :: Nanosecond , None ) ,
300+ true ,
301+ )
250302 }
251303}
252304
253305impl ArrowAssoc for NaiveDateTime {
254- type Builder = Date64Builder ;
306+ type Builder = TimestampNanosecondBuilder ;
255307
256308 fn builder ( nrows : usize ) -> Self :: Builder {
257- Date64Builder :: with_capacity ( nrows)
309+ TimestampNanosecondBuilder :: with_capacity ( nrows)
258310 }
259311
260312 fn append ( builder : & mut Self :: Builder , value : NaiveDateTime ) -> Result < ( ) > {
@@ -263,7 +315,56 @@ impl ArrowAssoc for NaiveDateTime {
263315 }
264316
265317 fn field ( header : & str ) -> Field {
266- Field :: new ( header, ArrowDataType :: Date64 , false )
318+ Field :: new (
319+ header,
320+ ArrowDataType :: Timestamp ( TimeUnit :: Nanosecond , None ) ,
321+ false ,
322+ )
323+ }
324+ }
325+
326+ impl ArrowAssoc for Option < NaiveDateTimeWrapperMicro > {
327+ type Builder = TimestampMicrosecondBuilder ;
328+
329+ fn builder ( nrows : usize ) -> Self :: Builder {
330+ TimestampMicrosecondBuilder :: with_capacity ( nrows)
331+ }
332+
333+ fn append ( builder : & mut Self :: Builder , value : Option < NaiveDateTimeWrapperMicro > ) -> Result < ( ) > {
334+ builder. append_option ( match value {
335+ Some ( v) => Some ( v. 0 . and_utc ( ) . timestamp_micros ( ) ) ,
336+ None => None ,
337+ } ) ;
338+ Ok ( ( ) )
339+ }
340+
341+ fn field ( header : & str ) -> Field {
342+ Field :: new (
343+ header,
344+ ArrowDataType :: Timestamp ( TimeUnit :: Microsecond , None ) ,
345+ true ,
346+ )
347+ }
348+ }
349+
350+ impl ArrowAssoc for NaiveDateTimeWrapperMicro {
351+ type Builder = TimestampMicrosecondBuilder ;
352+
353+ fn builder ( nrows : usize ) -> Self :: Builder {
354+ TimestampMicrosecondBuilder :: with_capacity ( nrows)
355+ }
356+
357+ fn append ( builder : & mut Self :: Builder , value : NaiveDateTimeWrapperMicro ) -> Result < ( ) > {
358+ builder. append_value ( value. 0 . and_utc ( ) . timestamp_micros ( ) ) ;
359+ Ok ( ( ) )
360+ }
361+
362+ fn field ( header : & str ) -> Field {
363+ Field :: new (
364+ header,
365+ ArrowDataType :: Timestamp ( TimeUnit :: Microsecond , None ) ,
366+ false ,
367+ )
267368 }
268369}
269370
@@ -307,6 +408,45 @@ impl ArrowAssoc for NaiveTime {
307408 }
308409}
309410
411+ impl ArrowAssoc for Option < NaiveTimeWrapperMicro > {
412+ type Builder = Time64MicrosecondBuilder ;
413+
414+ fn builder ( nrows : usize ) -> Self :: Builder {
415+ Time64MicrosecondBuilder :: with_capacity ( nrows)
416+ }
417+
418+ fn append ( builder : & mut Self :: Builder , value : Option < NaiveTimeWrapperMicro > ) -> Result < ( ) > {
419+ builder. append_option ( value. map ( |t| {
420+ t. 0 . num_seconds_from_midnight ( ) as i64 * 1_000_000 + ( t. 0 . nanosecond ( ) as i64 ) / 1000
421+ } ) ) ;
422+ Ok ( ( ) )
423+ }
424+
425+ fn field ( header : & str ) -> Field {
426+ Field :: new ( header, ArrowDataType :: Time64 ( TimeUnit :: Microsecond ) , true )
427+ }
428+ }
429+
430+ impl ArrowAssoc for NaiveTimeWrapperMicro {
431+ type Builder = Time64MicrosecondBuilder ;
432+
433+ fn builder ( nrows : usize ) -> Self :: Builder {
434+ Time64MicrosecondBuilder :: with_capacity ( nrows)
435+ }
436+
437+ fn append ( builder : & mut Self :: Builder , value : NaiveTimeWrapperMicro ) -> Result < ( ) > {
438+ builder. append_value (
439+ value. 0 . num_seconds_from_midnight ( ) as i64 * 1_000_000
440+ + ( value. 0 . nanosecond ( ) as i64 ) / 1000 ,
441+ ) ;
442+ Ok ( ( ) )
443+ }
444+
445+ fn field ( header : & str ) -> Field {
446+ Field :: new ( header, ArrowDataType :: Time64 ( TimeUnit :: Microsecond ) , false )
447+ }
448+ }
449+
310450impl ArrowAssoc for Option < Vec < u8 > > {
311451 type Builder = LargeBinaryBuilder ;
312452
0 commit comments