-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtypes.rs
More file actions
2040 lines (1830 loc) · 73 KB
/
Copy pathtypes.rs
File metadata and controls
2040 lines (1830 loc) · 73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! Zero-sized types used to parameterize generic array implementations
use crate::delta::{
add_days_datetime, add_months_date, add_months_datetime, sub_days_datetime, sub_months_datetime,
};
use crate::temporal_conversions::as_datetime_with_timezone;
use crate::timezone::Tz;
use crate::{ArrowNativeTypeOp, OffsetSizeTrait};
use arrow_buffer::{Buffer, OffsetBuffer, i256};
use arrow_data::decimal::{
format_decimal_str, is_validate_decimal_precision, is_validate_decimal32_precision,
is_validate_decimal64_precision, is_validate_decimal256_precision, validate_decimal_precision,
validate_decimal32_precision, validate_decimal64_precision, validate_decimal256_precision,
};
use arrow_data::{validate_binary_view, validate_string_view};
use arrow_schema::{
ArrowError, DECIMAL_DEFAULT_SCALE, DECIMAL32_DEFAULT_SCALE, DECIMAL32_MAX_PRECISION,
DECIMAL32_MAX_SCALE, DECIMAL64_DEFAULT_SCALE, DECIMAL64_MAX_PRECISION, DECIMAL64_MAX_SCALE,
DECIMAL128_MAX_PRECISION, DECIMAL128_MAX_SCALE, DECIMAL256_MAX_PRECISION, DECIMAL256_MAX_SCALE,
DataType, IntervalUnit, TimeUnit,
};
use chrono::{DateTime, Duration, NaiveDate, NaiveDateTime, TimeZone};
use half::f16;
use std::fmt::Debug;
use std::marker::PhantomData;
use std::ops::Sub;
// re-export types so that they can be used without importing arrow_buffer explicitly
pub use arrow_buffer::{IntervalDayTime, IntervalMonthDayNano};
// BooleanType is special: its bit-width is not the size of the primitive type, and its `index`
// operation assumes bit-packing.
/// A boolean datatype
#[derive(Debug)]
pub struct BooleanType {}
impl BooleanType {
/// The corresponding Arrow data type
pub const DATA_TYPE: DataType = DataType::Boolean;
}
/// Trait for [primitive values].
///
/// This trait bridges the dynamic-typed nature of Arrow
/// (via [`DataType`]) with the static-typed nature of rust types
/// ([`ArrowNativeType`]) for all types that implement [`ArrowNativeType`].
///
/// [primitive values]: https://arrow.apache.org/docs/format/Columnar.html#fixed-size-primitive-layout
/// [`ArrowNativeType`]: arrow_buffer::ArrowNativeType
pub trait ArrowPrimitiveType: primitive::PrimitiveTypeSealed + 'static {
/// Corresponding Rust native type for the primitive type.
type Native: ArrowNativeTypeOp;
/// the corresponding Arrow data type of this primitive type.
const DATA_TYPE: DataType;
/// Returns a default value of this primitive type.
///
/// This is useful for aggregate array ops like `sum()`, `mean()`.
fn default_value() -> Self::Native {
Default::default()
}
}
mod primitive {
pub trait PrimitiveTypeSealed {}
}
macro_rules! make_type {
($name:ident, $native_ty:ty, $data_ty:expr, $doc_string: literal) => {
#[derive(Debug)]
#[doc = $doc_string]
pub struct $name {}
impl ArrowPrimitiveType for $name {
type Native = $native_ty;
const DATA_TYPE: DataType = $data_ty;
}
impl primitive::PrimitiveTypeSealed for $name {}
};
}
make_type!(Int8Type, i8, DataType::Int8, "A signed 8-bit integer type.");
make_type!(
Int16Type,
i16,
DataType::Int16,
"Signed 16-bit integer type."
);
make_type!(
Int32Type,
i32,
DataType::Int32,
"Signed 32-bit integer type."
);
make_type!(
Int64Type,
i64,
DataType::Int64,
"Signed 64-bit integer type."
);
make_type!(
UInt8Type,
u8,
DataType::UInt8,
"Unsigned 8-bit integer type."
);
make_type!(
UInt16Type,
u16,
DataType::UInt16,
"Unsigned 16-bit integer type."
);
make_type!(
UInt32Type,
u32,
DataType::UInt32,
"Unsigned 32-bit integer type."
);
make_type!(
UInt64Type,
u64,
DataType::UInt64,
"Unsigned 64-bit integer type."
);
make_type!(
Float16Type,
f16,
DataType::Float16,
"16-bit floating point number type."
);
make_type!(
Float32Type,
f32,
DataType::Float32,
"32-bit floating point number type."
);
make_type!(
Float64Type,
f64,
DataType::Float64,
"64-bit floating point number type."
);
make_type!(
TimestampSecondType,
i64,
DataType::Timestamp(TimeUnit::Second, None),
"Timestamp second type with an optional timezone."
);
make_type!(
TimestampMillisecondType,
i64,
DataType::Timestamp(TimeUnit::Millisecond, None),
"Timestamp millisecond type with an optional timezone."
);
make_type!(
TimestampMicrosecondType,
i64,
DataType::Timestamp(TimeUnit::Microsecond, None),
"Timestamp microsecond type with an optional timezone."
);
make_type!(
TimestampNanosecondType,
i64,
DataType::Timestamp(TimeUnit::Nanosecond, None),
"Timestamp nanosecond type with an optional timezone."
);
make_type!(
Date32Type,
i32,
DataType::Date32,
"32-bit date type: the elapsed time since UNIX epoch in days (32 bits)."
);
make_type!(
Date64Type,
i64,
DataType::Date64,
"64-bit date type: the elapsed time since UNIX epoch in milliseconds (64 bits). \
Values must be divisible by `86_400_000`. \
See [`DataType::Date64`] for more details."
);
make_type!(
Time32SecondType,
i32,
DataType::Time32(TimeUnit::Second),
"32-bit time type: the elapsed time since midnight in seconds."
);
make_type!(
Time32MillisecondType,
i32,
DataType::Time32(TimeUnit::Millisecond),
"32-bit time type: the elapsed time since midnight in milliseconds."
);
make_type!(
Time64MicrosecondType,
i64,
DataType::Time64(TimeUnit::Microsecond),
"64-bit time type: the elapsed time since midnight in microseconds."
);
make_type!(
Time64NanosecondType,
i64,
DataType::Time64(TimeUnit::Nanosecond),
"64-bit time type: the elapsed time since midnight in nanoseconds."
);
make_type!(
IntervalYearMonthType,
i32,
DataType::Interval(IntervalUnit::YearMonth),
"32-bit “calendar” interval type: the number of whole months."
);
make_type!(
IntervalDayTimeType,
IntervalDayTime,
DataType::Interval(IntervalUnit::DayTime),
"“Calendar” interval type: days and milliseconds. See [`IntervalDayTime`] for more details."
);
make_type!(
IntervalMonthDayNanoType,
IntervalMonthDayNano,
DataType::Interval(IntervalUnit::MonthDayNano),
r"“Calendar” interval type: months, days, and nanoseconds. See [`IntervalMonthDayNano`] for more details."
);
make_type!(
DurationSecondType,
i64,
DataType::Duration(TimeUnit::Second),
"Elapsed time type: seconds."
);
make_type!(
DurationMillisecondType,
i64,
DataType::Duration(TimeUnit::Millisecond),
"Elapsed time type: milliseconds."
);
make_type!(
DurationMicrosecondType,
i64,
DataType::Duration(TimeUnit::Microsecond),
"Elapsed time type: microseconds."
);
make_type!(
DurationNanosecondType,
i64,
DataType::Duration(TimeUnit::Nanosecond),
"Elapsed time type: nanoseconds."
);
/// A subtype of primitive type that represents legal dictionary keys.
/// See <https://arrow.apache.org/docs/format/Columnar.html>
pub trait ArrowDictionaryKeyType: ArrowPrimitiveType {}
impl ArrowDictionaryKeyType for Int8Type {}
impl ArrowDictionaryKeyType for Int16Type {}
impl ArrowDictionaryKeyType for Int32Type {}
impl ArrowDictionaryKeyType for Int64Type {}
impl ArrowDictionaryKeyType for UInt8Type {}
impl ArrowDictionaryKeyType for UInt16Type {}
impl ArrowDictionaryKeyType for UInt32Type {}
impl ArrowDictionaryKeyType for UInt64Type {}
/// A subtype of primitive type that is used as run-ends index
/// in `RunArray`.
/// See <https://arrow.apache.org/docs/format/Columnar.html>
pub trait RunEndIndexType: ArrowPrimitiveType {}
impl RunEndIndexType for Int16Type {}
impl RunEndIndexType for Int32Type {}
impl RunEndIndexType for Int64Type {}
/// A subtype of primitive type that represents temporal values.
pub trait ArrowTemporalType: ArrowPrimitiveType {}
impl ArrowTemporalType for TimestampSecondType {}
impl ArrowTemporalType for TimestampMillisecondType {}
impl ArrowTemporalType for TimestampMicrosecondType {}
impl ArrowTemporalType for TimestampNanosecondType {}
impl ArrowTemporalType for Date32Type {}
impl ArrowTemporalType for Date64Type {}
impl ArrowTemporalType for Time32SecondType {}
impl ArrowTemporalType for Time32MillisecondType {}
impl ArrowTemporalType for Time64MicrosecondType {}
impl ArrowTemporalType for Time64NanosecondType {}
// impl ArrowTemporalType for IntervalYearMonthType {}
// impl ArrowTemporalType for IntervalDayTimeType {}
// impl ArrowTemporalType for IntervalMonthDayNanoType {}
impl ArrowTemporalType for DurationSecondType {}
impl ArrowTemporalType for DurationMillisecondType {}
impl ArrowTemporalType for DurationMicrosecondType {}
impl ArrowTemporalType for DurationNanosecondType {}
/// A timestamp type allows us to create array builders that take a timestamp.
pub trait ArrowTimestampType: ArrowTemporalType<Native = i64> {
/// The [`TimeUnit`] of this timestamp.
const UNIT: TimeUnit;
/// Creates a ArrowTimestampType::Native from the provided [`NaiveDateTime`]
///
/// See [`DataType::Timestamp`] for more information on timezone handling
#[deprecated(since = "58.1.0", note = "Use from_naive_datetime instead")]
fn make_value(naive: NaiveDateTime) -> Option<i64>;
/// Creates a timestamp value from a [`DateTime`] in any timezone.
///
/// Returns `None` if the timestamp value would overflow the i64 range
/// (e.g., for nanosecond precision with extreme datetime values).
///
/// # Arguments
///
/// * `datetime` - The datetime to convert
fn from_datetime<Tz: TimeZone>(datetime: DateTime<Tz>) -> Option<i64>;
/// Creates a timestamp value from a [`NaiveDateTime`] interpreted in the given timezone.
///
/// # Arguments
///
/// * `naive` - The local datetime to convert
/// * `tz` - Optional timezone. If `None`, interprets as UTC
/// (equivalent to calling [`Self::make_value`]).
fn from_naive_datetime(naive: NaiveDateTime, tz: Option<&Tz>) -> Option<i64> {
match tz {
Some(tz) => match tz.from_local_datetime(&naive) {
chrono::offset::LocalResult::Single(dt) => Self::from_datetime(dt),
chrono::offset::LocalResult::Ambiguous(dt1, _) => Self::from_datetime(dt1),
chrono::offset::LocalResult::None => None,
},
None => Self::from_datetime(naive.and_utc()),
}
}
}
impl ArrowTimestampType for TimestampSecondType {
const UNIT: TimeUnit = TimeUnit::Second;
fn make_value(naive: NaiveDateTime) -> Option<i64> {
Some(naive.and_utc().timestamp())
}
fn from_datetime<Tz: TimeZone>(datetime: DateTime<Tz>) -> Option<i64> {
Some(datetime.timestamp())
}
}
impl ArrowTimestampType for TimestampMillisecondType {
const UNIT: TimeUnit = TimeUnit::Millisecond;
fn make_value(naive: NaiveDateTime) -> Option<i64> {
let utc = naive.and_utc();
let millis = utc.timestamp().checked_mul(1_000)?;
millis.checked_add(utc.timestamp_subsec_millis() as i64)
}
fn from_datetime<Tz: TimeZone>(datetime: DateTime<Tz>) -> Option<i64> {
let millis = datetime.timestamp().checked_mul(1_000)?;
millis.checked_add(datetime.timestamp_subsec_millis() as i64)
}
}
impl ArrowTimestampType for TimestampMicrosecondType {
const UNIT: TimeUnit = TimeUnit::Microsecond;
fn make_value(naive: NaiveDateTime) -> Option<i64> {
let utc = naive.and_utc();
let micros = utc.timestamp().checked_mul(1_000_000)?;
micros.checked_add(utc.timestamp_subsec_micros() as i64)
}
fn from_datetime<Tz: TimeZone>(datetime: DateTime<Tz>) -> Option<i64> {
let micros = datetime.timestamp().checked_mul(1_000_000)?;
micros.checked_add(datetime.timestamp_subsec_micros() as i64)
}
}
impl ArrowTimestampType for TimestampNanosecondType {
const UNIT: TimeUnit = TimeUnit::Nanosecond;
fn make_value(naive: NaiveDateTime) -> Option<i64> {
let utc = naive.and_utc();
let nanos = utc.timestamp().checked_mul(1_000_000_000)?;
nanos.checked_add(utc.timestamp_subsec_nanos() as i64)
}
fn from_datetime<Tz: TimeZone>(datetime: DateTime<Tz>) -> Option<i64> {
datetime.timestamp_nanos_opt()
}
}
fn add_year_months<T: ArrowTimestampType>(
timestamp: <T as ArrowPrimitiveType>::Native,
delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<T as ArrowPrimitiveType>::Native> {
let months = IntervalYearMonthType::to_months(delta);
let res = as_datetime_with_timezone::<T>(timestamp, tz)?;
let res = add_months_datetime(res, months)?;
T::from_naive_datetime(res.naive_utc(), None)
}
fn add_day_time<T: ArrowTimestampType>(
timestamp: <T as ArrowPrimitiveType>::Native,
delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<T as ArrowPrimitiveType>::Native> {
let (days, ms) = IntervalDayTimeType::to_parts(delta);
let res = as_datetime_with_timezone::<T>(timestamp, tz)?;
let res = add_days_datetime(res, days)?;
let res = res.checked_add_signed(Duration::try_milliseconds(ms as i64)?)?;
T::from_naive_datetime(res.naive_utc(), None)
}
fn add_month_day_nano<T: ArrowTimestampType>(
timestamp: <T as ArrowPrimitiveType>::Native,
delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<T as ArrowPrimitiveType>::Native> {
let (months, days, nanos) = IntervalMonthDayNanoType::to_parts(delta);
let res = as_datetime_with_timezone::<T>(timestamp, tz)?;
let res = add_months_datetime(res, months)?;
let res = add_days_datetime(res, days)?;
let res = res.checked_add_signed(Duration::nanoseconds(nanos))?;
T::from_naive_datetime(res.naive_utc(), None)
}
fn subtract_year_months<T: ArrowTimestampType>(
timestamp: <T as ArrowPrimitiveType>::Native,
delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<T as ArrowPrimitiveType>::Native> {
let months = IntervalYearMonthType::to_months(delta);
let res = as_datetime_with_timezone::<T>(timestamp, tz)?;
let res = sub_months_datetime(res, months)?;
T::from_naive_datetime(res.naive_utc(), None)
}
fn subtract_day_time<T: ArrowTimestampType>(
timestamp: <T as ArrowPrimitiveType>::Native,
delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<T as ArrowPrimitiveType>::Native> {
let (days, ms) = IntervalDayTimeType::to_parts(delta);
let res = as_datetime_with_timezone::<T>(timestamp, tz)?;
let res = sub_days_datetime(res, days)?;
let res = res.checked_sub_signed(Duration::try_milliseconds(ms as i64)?)?;
T::from_naive_datetime(res.naive_utc(), None)
}
fn subtract_month_day_nano<T: ArrowTimestampType>(
timestamp: <T as ArrowPrimitiveType>::Native,
delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<T as ArrowPrimitiveType>::Native> {
let (months, days, nanos) = IntervalMonthDayNanoType::to_parts(delta);
let res = as_datetime_with_timezone::<T>(timestamp, tz)?;
let res = sub_months_datetime(res, months)?;
let res = sub_days_datetime(res, days)?;
let res = res.checked_sub_signed(Duration::nanoseconds(nanos))?;
T::from_naive_datetime(res.naive_utc(), None)
}
impl TimestampSecondType {
/// Adds the given IntervalYearMonthType to an arrow TimestampSecondType.
///
/// Returns `None` when it will result in overflow.
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn add_year_months(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
add_year_months::<Self>(timestamp, delta, tz)
}
/// Adds the given IntervalDayTimeType to an arrow TimestampSecondType.
///
/// Returns `None` when it will result in overflow.
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn add_day_time(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
add_day_time::<Self>(timestamp, delta, tz)
}
/// Adds the given IntervalMonthDayNanoType to an arrow TimestampSecondType
///
/// Returns `None` when it will result in overflow.
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn add_month_day_nano(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
add_month_day_nano::<Self>(timestamp, delta, tz)
}
/// Subtracts the given IntervalYearMonthType to an arrow TimestampSecondType
///
/// Returns `None` when it will result in overflow.
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn subtract_year_months(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
subtract_year_months::<Self>(timestamp, delta, tz)
}
/// Subtracts the given IntervalDayTimeType to an arrow TimestampSecondType
///
/// Returns `None` when it will result in overflow.
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn subtract_day_time(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
subtract_day_time::<Self>(timestamp, delta, tz)
}
/// Subtracts the given IntervalMonthDayNanoType to an arrow TimestampSecondType
///
/// Returns `None` when it will result in overflow.
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn subtract_month_day_nano(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
subtract_month_day_nano::<Self>(timestamp, delta, tz)
}
}
impl TimestampMicrosecondType {
/// Adds the given IntervalYearMonthType to an arrow TimestampMicrosecondType
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn add_year_months(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
add_year_months::<Self>(timestamp, delta, tz)
}
/// Adds the given IntervalDayTimeType to an arrow TimestampMicrosecondType
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn add_day_time(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
add_day_time::<Self>(timestamp, delta, tz)
}
/// Adds the given IntervalMonthDayNanoType to an arrow TimestampMicrosecondType
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn add_month_day_nano(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
add_month_day_nano::<Self>(timestamp, delta, tz)
}
/// Subtracts the given IntervalYearMonthType to an arrow TimestampMicrosecondType
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn subtract_year_months(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
subtract_year_months::<Self>(timestamp, delta, tz)
}
/// Subtracts the given IntervalDayTimeType to an arrow TimestampMicrosecondType
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn subtract_day_time(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
subtract_day_time::<Self>(timestamp, delta, tz)
}
/// Subtracts the given IntervalMonthDayNanoType to an arrow TimestampMicrosecondType
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn subtract_month_day_nano(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
subtract_month_day_nano::<Self>(timestamp, delta, tz)
}
}
impl TimestampMillisecondType {
/// Adds the given IntervalYearMonthType to an arrow TimestampMillisecondType
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn add_year_months(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
add_year_months::<Self>(timestamp, delta, tz)
}
/// Adds the given IntervalDayTimeType to an arrow TimestampMillisecondType
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn add_day_time(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
add_day_time::<Self>(timestamp, delta, tz)
}
/// Adds the given IntervalMonthDayNanoType to an arrow TimestampMillisecondType
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn add_month_day_nano(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
add_month_day_nano::<Self>(timestamp, delta, tz)
}
/// Subtracts the given IntervalYearMonthType to an arrow TimestampMillisecondType
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn subtract_year_months(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
subtract_year_months::<Self>(timestamp, delta, tz)
}
/// Subtracts the given IntervalDayTimeType to an arrow TimestampMillisecondType
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn subtract_day_time(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
subtract_day_time::<Self>(timestamp, delta, tz)
}
/// Subtracts the given IntervalMonthDayNanoType to an arrow TimestampMillisecondType
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn subtract_month_day_nano(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
subtract_month_day_nano::<Self>(timestamp, delta, tz)
}
}
impl TimestampNanosecondType {
/// Adds the given IntervalYearMonthType to an arrow TimestampNanosecondType
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn add_year_months(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
add_year_months::<Self>(timestamp, delta, tz)
}
/// Adds the given IntervalDayTimeType to an arrow TimestampNanosecondType
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn add_day_time(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
add_day_time::<Self>(timestamp, delta, tz)
}
/// Adds the given IntervalMonthDayNanoType to an arrow TimestampNanosecondType
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn add_month_day_nano(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
add_month_day_nano::<Self>(timestamp, delta, tz)
}
/// Subtracts the given IntervalYearMonthType to an arrow TimestampNanosecondType
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn subtract_year_months(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
subtract_year_months::<Self>(timestamp, delta, tz)
}
/// Subtracts the given IntervalDayTimeType to an arrow TimestampNanosecondType
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn subtract_day_time(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
subtract_day_time::<Self>(timestamp, delta, tz)
}
/// Subtracts the given IntervalMonthDayNanoType to an arrow TimestampNanosecondType
///
/// # Arguments
///
/// * `timestamp` - The date on which to perform the operation
/// * `delta` - The interval to add
/// * `tz` - The timezone in which to interpret `timestamp`
pub fn subtract_month_day_nano(
timestamp: <Self as ArrowPrimitiveType>::Native,
delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
tz: Tz,
) -> Option<<Self as ArrowPrimitiveType>::Native> {
subtract_month_day_nano::<Self>(timestamp, delta, tz)
}
}
impl IntervalYearMonthType {
/// Creates a IntervalYearMonthType::Native
///
/// # Arguments
///
/// * `years` - The number of years (+/-) represented in this interval
/// * `months` - The number of months (+/-) represented in this interval
#[inline]
pub fn make_value(
years: i32,
months: i32,
) -> <IntervalYearMonthType as ArrowPrimitiveType>::Native {
years * 12 + months
}
/// Turns a IntervalYearMonthType type into an i32 of months.
///
/// This operation is technically a no-op, it is included for comprehensiveness.
///
/// # Arguments
///
/// * `i` - The IntervalYearMonthType::Native to convert
#[inline]
pub fn to_months(i: <IntervalYearMonthType as ArrowPrimitiveType>::Native) -> i32 {
i
}
}
impl IntervalDayTimeType {
/// Creates a IntervalDayTimeType::Native
///
/// # Arguments
///
/// * `days` - The number of days (+/-) represented in this interval
/// * `millis` - The number of milliseconds (+/-) represented in this interval
#[inline]
pub fn make_value(days: i32, milliseconds: i32) -> IntervalDayTime {
IntervalDayTime { days, milliseconds }
}
/// Turns a IntervalDayTimeType into a tuple of (days, milliseconds)
///
/// # Arguments
///
/// * `i` - The IntervalDayTimeType to convert
#[inline]
pub fn to_parts(i: IntervalDayTime) -> (i32, i32) {
(i.days, i.milliseconds)
}
}
impl IntervalMonthDayNanoType {
/// Creates a IntervalMonthDayNanoType::Native
///
/// # Arguments
///
/// * `months` - The number of months (+/-) represented in this interval
/// * `days` - The number of days (+/-) represented in this interval
/// * `nanos` - The number of nanoseconds (+/-) represented in this interval
#[inline]
pub fn make_value(months: i32, days: i32, nanoseconds: i64) -> IntervalMonthDayNano {
IntervalMonthDayNano {
months,
days,
nanoseconds,
}
}
/// Turns a IntervalMonthDayNanoType into a tuple of (months, days, nanos)
///
/// # Arguments
///
/// * `i` - The IntervalMonthDayNanoType to convert
#[inline]
pub fn to_parts(i: IntervalMonthDayNano) -> (i32, i32, i64) {
(i.months, i.days, i.nanoseconds)
}
}
impl Date32Type {
/// Converts an arrow Date32Type into a chrono::NaiveDate
///
/// # Arguments
///
/// * `i` - The Date32Type to convert
#[deprecated(since = "58.0.0", note = "Use to_naive_date_opt instead.")]
pub fn to_naive_date(i: <Date32Type as ArrowPrimitiveType>::Native) -> NaiveDate {
Self::to_naive_date_opt(i)
.unwrap_or_else(|| panic!("Date32Type::to_naive_date overflowed for date: {i}",))
}
/// Converts an arrow Date32Type into a chrono::NaiveDate
///
/// # Arguments
///
/// * `i` - The Date32Type to convert
///
/// Returns `Some(NaiveDate)` if it fits, `None` otherwise.
pub fn to_naive_date_opt(i: <Date32Type as ArrowPrimitiveType>::Native) -> Option<NaiveDate> {
let epoch = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
Duration::try_days(i as i64).and_then(|d| epoch.checked_add_signed(d))
}
/// Converts a chrono::NaiveDate into an arrow Date32Type
///
/// # Arguments
///
/// * `d` - The NaiveDate to convert
pub fn from_naive_date(d: NaiveDate) -> <Date32Type as ArrowPrimitiveType>::Native {
let epoch = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
d.sub(epoch).num_days() as <Date32Type as ArrowPrimitiveType>::Native
}
/// Adds the given IntervalYearMonthType to an arrow Date32Type
///
/// # Arguments
///
/// * `date` - The date on which to perform the operation
/// * `delta` - The interval to add
#[deprecated(
since = "58.0.0",
note = "Use `add_year_months_opt` instead, which returns an Option to handle overflow."
)]
pub fn add_year_months(
date: <Date32Type as ArrowPrimitiveType>::Native,
delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
) -> <Date32Type as ArrowPrimitiveType>::Native {
Self::add_year_months_opt(date, delta).unwrap_or_else(|| {
panic!("Date32Type::add_year_months overflowed for date: {date}, delta: {delta}",)
})
}
/// Adds the given IntervalYearMonthType to an arrow Date32Type
///
/// # Arguments
///
/// * `date` - The date on which to perform the operation
/// * `delta` - The interval to add