1use crate::delta::{
21 add_days_datetime, add_months_date, add_months_datetime, sub_days_datetime, sub_months_datetime,
22};
23use crate::temporal_conversions::as_datetime_with_timezone;
24use crate::timezone::Tz;
25use crate::{ArrowNativeTypeOp, OffsetSizeTrait};
26use arrow_buffer::{Buffer, OffsetBuffer, i256};
27use arrow_data::decimal::{
28 format_decimal_str, is_validate_decimal_precision, is_validate_decimal32_precision,
29 is_validate_decimal64_precision, is_validate_decimal256_precision, validate_decimal_precision,
30 validate_decimal32_precision, validate_decimal64_precision, validate_decimal256_precision,
31};
32use arrow_data::{validate_binary_view, validate_string_view};
33use arrow_schema::{
34 ArrowError, DECIMAL_DEFAULT_SCALE, DECIMAL32_DEFAULT_SCALE, DECIMAL32_MAX_PRECISION,
35 DECIMAL32_MAX_SCALE, DECIMAL64_DEFAULT_SCALE, DECIMAL64_MAX_PRECISION, DECIMAL64_MAX_SCALE,
36 DECIMAL128_MAX_PRECISION, DECIMAL128_MAX_SCALE, DECIMAL256_MAX_PRECISION, DECIMAL256_MAX_SCALE,
37 DataType, IntervalUnit, TimeUnit,
38};
39use chrono::{DateTime, Duration, NaiveDate, NaiveDateTime, TimeZone};
40use half::f16;
41use std::fmt::Debug;
42use std::marker::PhantomData;
43use std::ops::Sub;
44
45pub use arrow_buffer::{IntervalDayTime, IntervalMonthDayNano};
47
48#[derive(Debug)]
52pub struct BooleanType {}
53
54impl BooleanType {
55 pub const DATA_TYPE: DataType = DataType::Boolean;
57}
58
59pub trait ArrowPrimitiveType: primitive::PrimitiveTypeSealed + 'static {
68 type Native: ArrowNativeTypeOp;
70
71 const DATA_TYPE: DataType;
73
74 fn default_value() -> Self::Native {
78 Default::default()
79 }
80}
81
82mod primitive {
83 pub trait PrimitiveTypeSealed {}
84}
85
86macro_rules! make_type {
87 ($name:ident, $native_ty:ty, $data_ty:expr, $doc_string: literal) => {
88 #[derive(Debug)]
89 #[doc = $doc_string]
90 pub struct $name {}
91
92 impl ArrowPrimitiveType for $name {
93 type Native = $native_ty;
94 const DATA_TYPE: DataType = $data_ty;
95 }
96
97 impl primitive::PrimitiveTypeSealed for $name {}
98 };
99}
100
101make_type!(Int8Type, i8, DataType::Int8, "A signed 8-bit integer type.");
102make_type!(
103 Int16Type,
104 i16,
105 DataType::Int16,
106 "Signed 16-bit integer type."
107);
108make_type!(
109 Int32Type,
110 i32,
111 DataType::Int32,
112 "Signed 32-bit integer type."
113);
114make_type!(
115 Int64Type,
116 i64,
117 DataType::Int64,
118 "Signed 64-bit integer type."
119);
120make_type!(
121 UInt8Type,
122 u8,
123 DataType::UInt8,
124 "Unsigned 8-bit integer type."
125);
126make_type!(
127 UInt16Type,
128 u16,
129 DataType::UInt16,
130 "Unsigned 16-bit integer type."
131);
132make_type!(
133 UInt32Type,
134 u32,
135 DataType::UInt32,
136 "Unsigned 32-bit integer type."
137);
138make_type!(
139 UInt64Type,
140 u64,
141 DataType::UInt64,
142 "Unsigned 64-bit integer type."
143);
144make_type!(
145 Float16Type,
146 f16,
147 DataType::Float16,
148 "16-bit floating point number type."
149);
150make_type!(
151 Float32Type,
152 f32,
153 DataType::Float32,
154 "32-bit floating point number type."
155);
156make_type!(
157 Float64Type,
158 f64,
159 DataType::Float64,
160 "64-bit floating point number type."
161);
162make_type!(
163 TimestampSecondType,
164 i64,
165 DataType::Timestamp(TimeUnit::Second, None),
166 "Timestamp second type with an optional timezone."
167);
168make_type!(
169 TimestampMillisecondType,
170 i64,
171 DataType::Timestamp(TimeUnit::Millisecond, None),
172 "Timestamp millisecond type with an optional timezone."
173);
174make_type!(
175 TimestampMicrosecondType,
176 i64,
177 DataType::Timestamp(TimeUnit::Microsecond, None),
178 "Timestamp microsecond type with an optional timezone."
179);
180make_type!(
181 TimestampNanosecondType,
182 i64,
183 DataType::Timestamp(TimeUnit::Nanosecond, None),
184 "Timestamp nanosecond type with an optional timezone."
185);
186make_type!(
187 Date32Type,
188 i32,
189 DataType::Date32,
190 "32-bit date type: the elapsed time since UNIX epoch in days (32 bits)."
191);
192make_type!(
193 Date64Type,
194 i64,
195 DataType::Date64,
196 "64-bit date type: the elapsed time since UNIX epoch in milliseconds (64 bits). \
197 Values must be divisible by `86_400_000`. \
198 See [`DataType::Date64`] for more details."
199);
200make_type!(
201 Time32SecondType,
202 i32,
203 DataType::Time32(TimeUnit::Second),
204 "32-bit time type: the elapsed time since midnight in seconds."
205);
206make_type!(
207 Time32MillisecondType,
208 i32,
209 DataType::Time32(TimeUnit::Millisecond),
210 "32-bit time type: the elapsed time since midnight in milliseconds."
211);
212make_type!(
213 Time64MicrosecondType,
214 i64,
215 DataType::Time64(TimeUnit::Microsecond),
216 "64-bit time type: the elapsed time since midnight in microseconds."
217);
218make_type!(
219 Time64NanosecondType,
220 i64,
221 DataType::Time64(TimeUnit::Nanosecond),
222 "64-bit time type: the elapsed time since midnight in nanoseconds."
223);
224make_type!(
225 IntervalYearMonthType,
226 i32,
227 DataType::Interval(IntervalUnit::YearMonth),
228 "32-bit “calendar” interval type: the number of whole months."
229);
230make_type!(
231 IntervalDayTimeType,
232 IntervalDayTime,
233 DataType::Interval(IntervalUnit::DayTime),
234 "“Calendar” interval type: days and milliseconds. See [`IntervalDayTime`] for more details."
235);
236make_type!(
237 IntervalMonthDayNanoType,
238 IntervalMonthDayNano,
239 DataType::Interval(IntervalUnit::MonthDayNano),
240 r"“Calendar” interval type: months, days, and nanoseconds. See [`IntervalMonthDayNano`] for more details."
241);
242make_type!(
243 DurationSecondType,
244 i64,
245 DataType::Duration(TimeUnit::Second),
246 "Elapsed time type: seconds."
247);
248make_type!(
249 DurationMillisecondType,
250 i64,
251 DataType::Duration(TimeUnit::Millisecond),
252 "Elapsed time type: milliseconds."
253);
254make_type!(
255 DurationMicrosecondType,
256 i64,
257 DataType::Duration(TimeUnit::Microsecond),
258 "Elapsed time type: microseconds."
259);
260make_type!(
261 DurationNanosecondType,
262 i64,
263 DataType::Duration(TimeUnit::Nanosecond),
264 "Elapsed time type: nanoseconds."
265);
266
267pub trait ArrowDictionaryKeyType: ArrowPrimitiveType {}
270
271impl ArrowDictionaryKeyType for Int8Type {}
272
273impl ArrowDictionaryKeyType for Int16Type {}
274
275impl ArrowDictionaryKeyType for Int32Type {}
276
277impl ArrowDictionaryKeyType for Int64Type {}
278
279impl ArrowDictionaryKeyType for UInt8Type {}
280
281impl ArrowDictionaryKeyType for UInt16Type {}
282
283impl ArrowDictionaryKeyType for UInt32Type {}
284
285impl ArrowDictionaryKeyType for UInt64Type {}
286
287pub trait RunEndIndexType: ArrowPrimitiveType {}
291
292impl RunEndIndexType for Int16Type {}
293
294impl RunEndIndexType for Int32Type {}
295
296impl RunEndIndexType for Int64Type {}
297
298pub trait ArrowTemporalType: ArrowPrimitiveType {}
300
301impl ArrowTemporalType for TimestampSecondType {}
302impl ArrowTemporalType for TimestampMillisecondType {}
303impl ArrowTemporalType for TimestampMicrosecondType {}
304impl ArrowTemporalType for TimestampNanosecondType {}
305impl ArrowTemporalType for Date32Type {}
306impl ArrowTemporalType for Date64Type {}
307impl ArrowTemporalType for Time32SecondType {}
308impl ArrowTemporalType for Time32MillisecondType {}
309impl ArrowTemporalType for Time64MicrosecondType {}
310impl ArrowTemporalType for Time64NanosecondType {}
311impl ArrowTemporalType for DurationSecondType {}
315impl ArrowTemporalType for DurationMillisecondType {}
316impl ArrowTemporalType for DurationMicrosecondType {}
317impl ArrowTemporalType for DurationNanosecondType {}
318
319pub trait ArrowTimestampType: ArrowTemporalType<Native = i64> {
321 const UNIT: TimeUnit;
323
324 #[deprecated(since = "58.1.0", note = "Use from_naive_datetime instead")]
328 fn make_value(naive: NaiveDateTime) -> Option<i64>;
329
330 fn from_datetime<Tz: TimeZone>(datetime: DateTime<Tz>) -> Option<i64>;
339
340 fn from_naive_datetime(naive: NaiveDateTime, tz: Option<&Tz>) -> Option<i64> {
348 match tz {
349 Some(tz) => match tz.from_local_datetime(&naive) {
350 chrono::offset::LocalResult::Single(dt) => Self::from_datetime(dt),
351 chrono::offset::LocalResult::Ambiguous(dt1, _) => Self::from_datetime(dt1),
352 chrono::offset::LocalResult::None => None,
353 },
354 None => Self::from_datetime(naive.and_utc()),
355 }
356 }
357}
358
359impl ArrowTimestampType for TimestampSecondType {
360 const UNIT: TimeUnit = TimeUnit::Second;
361
362 fn make_value(naive: NaiveDateTime) -> Option<i64> {
363 Some(naive.and_utc().timestamp())
364 }
365
366 fn from_datetime<Tz: TimeZone>(datetime: DateTime<Tz>) -> Option<i64> {
367 Some(datetime.timestamp())
368 }
369}
370impl ArrowTimestampType for TimestampMillisecondType {
371 const UNIT: TimeUnit = TimeUnit::Millisecond;
372
373 fn make_value(naive: NaiveDateTime) -> Option<i64> {
374 let utc = naive.and_utc();
375 let millis = utc.timestamp().checked_mul(1_000)?;
376 millis.checked_add(utc.timestamp_subsec_millis() as i64)
377 }
378
379 fn from_datetime<Tz: TimeZone>(datetime: DateTime<Tz>) -> Option<i64> {
380 let millis = datetime.timestamp().checked_mul(1_000)?;
381 millis.checked_add(datetime.timestamp_subsec_millis() as i64)
382 }
383}
384impl ArrowTimestampType for TimestampMicrosecondType {
385 const UNIT: TimeUnit = TimeUnit::Microsecond;
386
387 fn make_value(naive: NaiveDateTime) -> Option<i64> {
388 let utc = naive.and_utc();
389 let micros = utc.timestamp().checked_mul(1_000_000)?;
390 micros.checked_add(utc.timestamp_subsec_micros() as i64)
391 }
392
393 fn from_datetime<Tz: TimeZone>(datetime: DateTime<Tz>) -> Option<i64> {
394 let micros = datetime.timestamp().checked_mul(1_000_000)?;
395 micros.checked_add(datetime.timestamp_subsec_micros() as i64)
396 }
397}
398impl ArrowTimestampType for TimestampNanosecondType {
399 const UNIT: TimeUnit = TimeUnit::Nanosecond;
400
401 fn make_value(naive: NaiveDateTime) -> Option<i64> {
402 let utc = naive.and_utc();
403 let nanos = utc.timestamp().checked_mul(1_000_000_000)?;
404 nanos.checked_add(utc.timestamp_subsec_nanos() as i64)
405 }
406
407 fn from_datetime<Tz: TimeZone>(datetime: DateTime<Tz>) -> Option<i64> {
408 datetime.timestamp_nanos_opt()
409 }
410}
411
412fn add_year_months<T: ArrowTimestampType>(
413 timestamp: <T as ArrowPrimitiveType>::Native,
414 delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
415 tz: Tz,
416) -> Option<<T as ArrowPrimitiveType>::Native> {
417 let months = IntervalYearMonthType::to_months(delta);
418 let res = as_datetime_with_timezone::<T>(timestamp, tz)?;
419 let res = add_months_datetime(res, months)?;
420 T::from_naive_datetime(res.naive_utc(), None)
421}
422
423fn add_day_time<T: ArrowTimestampType>(
424 timestamp: <T as ArrowPrimitiveType>::Native,
425 delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
426 tz: Tz,
427) -> Option<<T as ArrowPrimitiveType>::Native> {
428 let (days, ms) = IntervalDayTimeType::to_parts(delta);
429 let res = as_datetime_with_timezone::<T>(timestamp, tz)?;
430 let res = add_days_datetime(res, days)?;
431 let res = res.checked_add_signed(Duration::try_milliseconds(ms as i64)?)?;
432 T::from_naive_datetime(res.naive_utc(), None)
433}
434
435fn add_month_day_nano<T: ArrowTimestampType>(
436 timestamp: <T as ArrowPrimitiveType>::Native,
437 delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
438 tz: Tz,
439) -> Option<<T as ArrowPrimitiveType>::Native> {
440 let (months, days, nanos) = IntervalMonthDayNanoType::to_parts(delta);
441 let res = as_datetime_with_timezone::<T>(timestamp, tz)?;
442 let res = add_months_datetime(res, months)?;
443 let res = add_days_datetime(res, days)?;
444 let res = res.checked_add_signed(Duration::nanoseconds(nanos))?;
445 T::from_naive_datetime(res.naive_utc(), None)
446}
447
448fn subtract_year_months<T: ArrowTimestampType>(
449 timestamp: <T as ArrowPrimitiveType>::Native,
450 delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
451 tz: Tz,
452) -> Option<<T as ArrowPrimitiveType>::Native> {
453 let months = IntervalYearMonthType::to_months(delta);
454 let res = as_datetime_with_timezone::<T>(timestamp, tz)?;
455 let res = sub_months_datetime(res, months)?;
456 T::from_naive_datetime(res.naive_utc(), None)
457}
458
459fn subtract_day_time<T: ArrowTimestampType>(
460 timestamp: <T as ArrowPrimitiveType>::Native,
461 delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
462 tz: Tz,
463) -> Option<<T as ArrowPrimitiveType>::Native> {
464 let (days, ms) = IntervalDayTimeType::to_parts(delta);
465 let res = as_datetime_with_timezone::<T>(timestamp, tz)?;
466 let res = sub_days_datetime(res, days)?;
467 let res = res.checked_sub_signed(Duration::try_milliseconds(ms as i64)?)?;
468 T::from_naive_datetime(res.naive_utc(), None)
469}
470
471fn subtract_month_day_nano<T: ArrowTimestampType>(
472 timestamp: <T as ArrowPrimitiveType>::Native,
473 delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
474 tz: Tz,
475) -> Option<<T as ArrowPrimitiveType>::Native> {
476 let (months, days, nanos) = IntervalMonthDayNanoType::to_parts(delta);
477 let res = as_datetime_with_timezone::<T>(timestamp, tz)?;
478 let res = sub_months_datetime(res, months)?;
479 let res = sub_days_datetime(res, days)?;
480 let res = res.checked_sub_signed(Duration::nanoseconds(nanos))?;
481 T::from_naive_datetime(res.naive_utc(), None)
482}
483
484impl TimestampSecondType {
485 pub fn add_year_months(
495 timestamp: <Self as ArrowPrimitiveType>::Native,
496 delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
497 tz: Tz,
498 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
499 add_year_months::<Self>(timestamp, delta, tz)
500 }
501
502 pub fn add_day_time(
512 timestamp: <Self as ArrowPrimitiveType>::Native,
513 delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
514 tz: Tz,
515 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
516 add_day_time::<Self>(timestamp, delta, tz)
517 }
518
519 pub fn add_month_day_nano(
528 timestamp: <Self as ArrowPrimitiveType>::Native,
529 delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
530 tz: Tz,
531 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
532 add_month_day_nano::<Self>(timestamp, delta, tz)
533 }
534
535 pub fn subtract_year_months(
545 timestamp: <Self as ArrowPrimitiveType>::Native,
546 delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
547 tz: Tz,
548 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
549 subtract_year_months::<Self>(timestamp, delta, tz)
550 }
551
552 pub fn subtract_day_time(
562 timestamp: <Self as ArrowPrimitiveType>::Native,
563 delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
564 tz: Tz,
565 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
566 subtract_day_time::<Self>(timestamp, delta, tz)
567 }
568
569 pub fn subtract_month_day_nano(
579 timestamp: <Self as ArrowPrimitiveType>::Native,
580 delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
581 tz: Tz,
582 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
583 subtract_month_day_nano::<Self>(timestamp, delta, tz)
584 }
585}
586
587impl TimestampMicrosecondType {
588 pub fn add_year_months(
596 timestamp: <Self as ArrowPrimitiveType>::Native,
597 delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
598 tz: Tz,
599 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
600 add_year_months::<Self>(timestamp, delta, tz)
601 }
602
603 pub fn add_day_time(
611 timestamp: <Self as ArrowPrimitiveType>::Native,
612 delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
613 tz: Tz,
614 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
615 add_day_time::<Self>(timestamp, delta, tz)
616 }
617
618 pub fn add_month_day_nano(
626 timestamp: <Self as ArrowPrimitiveType>::Native,
627 delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
628 tz: Tz,
629 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
630 add_month_day_nano::<Self>(timestamp, delta, tz)
631 }
632
633 pub fn subtract_year_months(
641 timestamp: <Self as ArrowPrimitiveType>::Native,
642 delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
643 tz: Tz,
644 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
645 subtract_year_months::<Self>(timestamp, delta, tz)
646 }
647
648 pub fn subtract_day_time(
656 timestamp: <Self as ArrowPrimitiveType>::Native,
657 delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
658 tz: Tz,
659 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
660 subtract_day_time::<Self>(timestamp, delta, tz)
661 }
662
663 pub fn subtract_month_day_nano(
671 timestamp: <Self as ArrowPrimitiveType>::Native,
672 delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
673 tz: Tz,
674 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
675 subtract_month_day_nano::<Self>(timestamp, delta, tz)
676 }
677}
678
679impl TimestampMillisecondType {
680 pub fn add_year_months(
688 timestamp: <Self as ArrowPrimitiveType>::Native,
689 delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
690 tz: Tz,
691 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
692 add_year_months::<Self>(timestamp, delta, tz)
693 }
694
695 pub fn add_day_time(
703 timestamp: <Self as ArrowPrimitiveType>::Native,
704 delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
705 tz: Tz,
706 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
707 add_day_time::<Self>(timestamp, delta, tz)
708 }
709
710 pub fn add_month_day_nano(
718 timestamp: <Self as ArrowPrimitiveType>::Native,
719 delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
720 tz: Tz,
721 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
722 add_month_day_nano::<Self>(timestamp, delta, tz)
723 }
724
725 pub fn subtract_year_months(
733 timestamp: <Self as ArrowPrimitiveType>::Native,
734 delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
735 tz: Tz,
736 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
737 subtract_year_months::<Self>(timestamp, delta, tz)
738 }
739
740 pub fn subtract_day_time(
748 timestamp: <Self as ArrowPrimitiveType>::Native,
749 delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
750 tz: Tz,
751 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
752 subtract_day_time::<Self>(timestamp, delta, tz)
753 }
754
755 pub fn subtract_month_day_nano(
763 timestamp: <Self as ArrowPrimitiveType>::Native,
764 delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
765 tz: Tz,
766 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
767 subtract_month_day_nano::<Self>(timestamp, delta, tz)
768 }
769}
770
771impl TimestampNanosecondType {
772 pub fn add_year_months(
780 timestamp: <Self as ArrowPrimitiveType>::Native,
781 delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
782 tz: Tz,
783 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
784 add_year_months::<Self>(timestamp, delta, tz)
785 }
786
787 pub fn add_day_time(
795 timestamp: <Self as ArrowPrimitiveType>::Native,
796 delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
797 tz: Tz,
798 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
799 add_day_time::<Self>(timestamp, delta, tz)
800 }
801
802 pub fn add_month_day_nano(
810 timestamp: <Self as ArrowPrimitiveType>::Native,
811 delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
812 tz: Tz,
813 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
814 add_month_day_nano::<Self>(timestamp, delta, tz)
815 }
816
817 pub fn subtract_year_months(
825 timestamp: <Self as ArrowPrimitiveType>::Native,
826 delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
827 tz: Tz,
828 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
829 subtract_year_months::<Self>(timestamp, delta, tz)
830 }
831
832 pub fn subtract_day_time(
840 timestamp: <Self as ArrowPrimitiveType>::Native,
841 delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
842 tz: Tz,
843 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
844 subtract_day_time::<Self>(timestamp, delta, tz)
845 }
846
847 pub fn subtract_month_day_nano(
855 timestamp: <Self as ArrowPrimitiveType>::Native,
856 delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
857 tz: Tz,
858 ) -> Option<<Self as ArrowPrimitiveType>::Native> {
859 subtract_month_day_nano::<Self>(timestamp, delta, tz)
860 }
861}
862
863impl IntervalYearMonthType {
864 #[inline]
871 pub fn make_value(
872 years: i32,
873 months: i32,
874 ) -> <IntervalYearMonthType as ArrowPrimitiveType>::Native {
875 years * 12 + months
876 }
877
878 #[inline]
886 pub fn to_months(i: <IntervalYearMonthType as ArrowPrimitiveType>::Native) -> i32 {
887 i
888 }
889}
890
891impl IntervalDayTimeType {
892 #[inline]
899 pub fn make_value(days: i32, milliseconds: i32) -> IntervalDayTime {
900 IntervalDayTime { days, milliseconds }
901 }
902
903 #[inline]
909 pub fn to_parts(i: IntervalDayTime) -> (i32, i32) {
910 (i.days, i.milliseconds)
911 }
912}
913
914impl IntervalMonthDayNanoType {
915 #[inline]
923 pub fn make_value(months: i32, days: i32, nanoseconds: i64) -> IntervalMonthDayNano {
924 IntervalMonthDayNano {
925 months,
926 days,
927 nanoseconds,
928 }
929 }
930
931 #[inline]
937 pub fn to_parts(i: IntervalMonthDayNano) -> (i32, i32, i64) {
938 (i.months, i.days, i.nanoseconds)
939 }
940}
941
942impl Date32Type {
943 #[deprecated(since = "58.0.0", note = "Use to_naive_date_opt instead.")]
949 pub fn to_naive_date(i: <Date32Type as ArrowPrimitiveType>::Native) -> NaiveDate {
950 Self::to_naive_date_opt(i)
951 .unwrap_or_else(|| panic!("Date32Type::to_naive_date overflowed for date: {i}",))
952 }
953
954 pub fn to_naive_date_opt(i: <Date32Type as ArrowPrimitiveType>::Native) -> Option<NaiveDate> {
962 let epoch = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
963 Duration::try_days(i as i64).and_then(|d| epoch.checked_add_signed(d))
964 }
965
966 pub fn from_naive_date(d: NaiveDate) -> <Date32Type as ArrowPrimitiveType>::Native {
972 let epoch = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
973 d.sub(epoch).num_days() as <Date32Type as ArrowPrimitiveType>::Native
974 }
975
976 #[deprecated(
983 since = "58.0.0",
984 note = "Use `add_year_months_opt` instead, which returns an Option to handle overflow."
985 )]
986 pub fn add_year_months(
987 date: <Date32Type as ArrowPrimitiveType>::Native,
988 delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
989 ) -> <Date32Type as ArrowPrimitiveType>::Native {
990 Self::add_year_months_opt(date, delta).unwrap_or_else(|| {
991 panic!("Date32Type::add_year_months overflowed for date: {date}, delta: {delta}",)
992 })
993 }
994
995 pub fn add_year_months_opt(
1004 date: <Date32Type as ArrowPrimitiveType>::Native,
1005 delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
1006 ) -> Option<<Date32Type as ArrowPrimitiveType>::Native> {
1007 let prior = Date32Type::to_naive_date_opt(date)?;
1008 let months = IntervalYearMonthType::to_months(delta);
1009 let posterior = add_months_date(prior, months)?;
1010 Some(Date32Type::from_naive_date(posterior))
1011 }
1012
1013 #[deprecated(
1020 since = "58.0.0",
1021 note = "Use `add_day_time_opt` instead, which returns an Option to handle overflow."
1022 )]
1023 pub fn add_day_time(
1024 date: <Date32Type as ArrowPrimitiveType>::Native,
1025 delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
1026 ) -> <Date32Type as ArrowPrimitiveType>::Native {
1027 Self::add_day_time_opt(date, delta).unwrap_or_else(|| {
1028 panic!("Date32Type::add_day_time overflowed for date: {date}, delta: {delta:?}",)
1029 })
1030 }
1031
1032 pub fn add_day_time_opt(
1041 date: <Date32Type as ArrowPrimitiveType>::Native,
1042 delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
1043 ) -> Option<<Date32Type as ArrowPrimitiveType>::Native> {
1044 let (days, ms) = IntervalDayTimeType::to_parts(delta);
1045 let res = Date32Type::to_naive_date_opt(date)?;
1046 let res = res.checked_add_signed(Duration::try_days(days as i64)?)?;
1047 let res = res.checked_add_signed(Duration::try_milliseconds(ms as i64)?)?;
1048 Some(Date32Type::from_naive_date(res))
1049 }
1050
1051 #[deprecated(
1058 since = "58.0.0",
1059 note = "Use `add_month_day_nano_opt` instead, which returns an Option to handle overflow."
1060 )]
1061 pub fn add_month_day_nano(
1062 date: <Date32Type as ArrowPrimitiveType>::Native,
1063 delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
1064 ) -> <Date32Type as ArrowPrimitiveType>::Native {
1065 Self::add_month_day_nano_opt(date, delta).unwrap_or_else(|| {
1066 panic!("Date32Type::add_month_day_nano overflowed for date: {date}, delta: {delta:?}",)
1067 })
1068 }
1069
1070 pub fn add_month_day_nano_opt(
1079 date: <Date32Type as ArrowPrimitiveType>::Native,
1080 delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
1081 ) -> Option<<Date32Type as ArrowPrimitiveType>::Native> {
1082 let (months, days, nanos) = IntervalMonthDayNanoType::to_parts(delta);
1083 let res = Date32Type::to_naive_date_opt(date)?;
1084 let res = add_months_date(res, months)?;
1085 let res = res.checked_add_signed(Duration::try_days(days as i64)?)?;
1086 let res = res.checked_add_signed(Duration::nanoseconds(nanos))?;
1087 Some(Date32Type::from_naive_date(res))
1088 }
1089
1090 #[deprecated(
1097 since = "58.0.0",
1098 note = "Use `subtract_year_months_opt` instead, which returns an Option to handle overflow."
1099 )]
1100 pub fn subtract_year_months(
1101 date: <Date32Type as ArrowPrimitiveType>::Native,
1102 delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
1103 ) -> <Date32Type as ArrowPrimitiveType>::Native {
1104 Self::subtract_year_months_opt(date, delta).unwrap_or_else(|| {
1105 panic!("Date32Type::subtract_year_months overflowed for date: {date}, delta: {delta}",)
1106 })
1107 }
1108
1109 pub fn subtract_year_months_opt(
1118 date: <Date32Type as ArrowPrimitiveType>::Native,
1119 delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
1120 ) -> Option<<Date32Type as ArrowPrimitiveType>::Native> {
1121 let prior = Date32Type::to_naive_date_opt(date)?;
1122 let months = IntervalYearMonthType::to_months(-delta);
1123 let posterior = add_months_date(prior, months)?;
1124 Some(Date32Type::from_naive_date(posterior))
1125 }
1126
1127 #[deprecated(
1134 since = "58.0.0",
1135 note = "Use `subtract_day_time_opt` instead, which returns an Option to handle overflow."
1136 )]
1137 pub fn subtract_day_time(
1138 date: <Date32Type as ArrowPrimitiveType>::Native,
1139 delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
1140 ) -> <Date32Type as ArrowPrimitiveType>::Native {
1141 Self::subtract_day_time_opt(date, delta).unwrap_or_else(|| {
1142 panic!("Date32Type::subtract_day_time overflowed for date: {date}, delta: {delta:?}",)
1143 })
1144 }
1145
1146 pub fn subtract_day_time_opt(
1155 date: <Date32Type as ArrowPrimitiveType>::Native,
1156 delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
1157 ) -> Option<<Date32Type as ArrowPrimitiveType>::Native> {
1158 let (days, ms) = IntervalDayTimeType::to_parts(delta);
1159 let res = Date32Type::to_naive_date_opt(date)?;
1160 let res = res.checked_sub_signed(Duration::try_days(days as i64)?)?;
1161 let res = res.checked_sub_signed(Duration::try_milliseconds(ms as i64)?)?;
1162 Some(Date32Type::from_naive_date(res))
1163 }
1164
1165 #[deprecated(
1172 since = "58.0.0",
1173 note = "Use `subtract_month_day_nano_opt` instead, which returns an Option to handle overflow."
1174 )]
1175 pub fn subtract_month_day_nano(
1176 date: <Date32Type as ArrowPrimitiveType>::Native,
1177 delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
1178 ) -> <Date32Type as ArrowPrimitiveType>::Native {
1179 Self::subtract_month_day_nano_opt(date, delta).unwrap_or_else(|| {
1180 panic!(
1181 "Date32Type::subtract_month_day_nano overflowed for date: {date}, delta: {delta:?}",
1182 )
1183 })
1184 }
1185
1186 pub fn subtract_month_day_nano_opt(
1195 date: <Date32Type as ArrowPrimitiveType>::Native,
1196 delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
1197 ) -> Option<<Date32Type as ArrowPrimitiveType>::Native> {
1198 let (months, days, nanos) = IntervalMonthDayNanoType::to_parts(delta);
1199 let res = Date32Type::to_naive_date_opt(date)?;
1200 let res = add_months_date(res, -months)?;
1201 let res = res.checked_sub_signed(Duration::try_days(days as i64)?)?;
1202 let res = res.checked_sub_signed(Duration::nanoseconds(nanos))?;
1203 Some(Date32Type::from_naive_date(res))
1204 }
1205}
1206
1207impl Date64Type {
1208 pub fn to_naive_date_opt(i: <Date64Type as ArrowPrimitiveType>::Native) -> Option<NaiveDate> {
1219 let epoch = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
1220 Duration::try_milliseconds(i).and_then(|d| epoch.checked_add_signed(d))
1221 }
1222
1223 pub fn from_naive_date(d: NaiveDate) -> <Date64Type as ArrowPrimitiveType>::Native {
1229 let epoch = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
1230 d.sub(epoch).num_milliseconds() as <Date64Type as ArrowPrimitiveType>::Native
1231 }
1232
1233 pub fn add_year_months_opt(
1242 date: <Date64Type as ArrowPrimitiveType>::Native,
1243 delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
1244 ) -> Option<<Date64Type as ArrowPrimitiveType>::Native> {
1245 let prior = Date64Type::to_naive_date_opt(date)?;
1246 let months = IntervalYearMonthType::to_months(delta);
1247 let posterior = add_months_date(prior, months)?;
1248 Some(Date64Type::from_naive_date(posterior))
1249 }
1250
1251 pub fn add_day_time_opt(
1260 date: <Date64Type as ArrowPrimitiveType>::Native,
1261 delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
1262 ) -> Option<<Date64Type as ArrowPrimitiveType>::Native> {
1263 let (days, ms) = IntervalDayTimeType::to_parts(delta);
1264 let res = Date64Type::to_naive_date_opt(date)?;
1265 let res = res.checked_add_signed(Duration::try_days(days as i64)?)?;
1266 let res = res.checked_add_signed(Duration::try_milliseconds(ms as i64)?)?;
1267 Some(Date64Type::from_naive_date(res))
1268 }
1269
1270 pub fn add_month_day_nano_opt(
1279 date: <Date64Type as ArrowPrimitiveType>::Native,
1280 delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
1281 ) -> Option<<Date64Type as ArrowPrimitiveType>::Native> {
1282 let (months, days, nanos) = IntervalMonthDayNanoType::to_parts(delta);
1283 let res = Date64Type::to_naive_date_opt(date)?;
1284 let res = add_months_date(res, months)?;
1285 let res = res.checked_add_signed(Duration::try_days(days as i64)?)?;
1286 let res = res.checked_add_signed(Duration::nanoseconds(nanos))?;
1287 Some(Date64Type::from_naive_date(res))
1288 }
1289
1290 pub fn subtract_year_months_opt(
1299 date: <Date64Type as ArrowPrimitiveType>::Native,
1300 delta: <IntervalYearMonthType as ArrowPrimitiveType>::Native,
1301 ) -> Option<<Date64Type as ArrowPrimitiveType>::Native> {
1302 let prior = Date64Type::to_naive_date_opt(date)?;
1303 let months = IntervalYearMonthType::to_months(-delta);
1304 let posterior = add_months_date(prior, months)?;
1305 Some(Date64Type::from_naive_date(posterior))
1306 }
1307
1308 pub fn subtract_day_time_opt(
1317 date: <Date64Type as ArrowPrimitiveType>::Native,
1318 delta: <IntervalDayTimeType as ArrowPrimitiveType>::Native,
1319 ) -> Option<<Date64Type as ArrowPrimitiveType>::Native> {
1320 let (days, ms) = IntervalDayTimeType::to_parts(delta);
1321 let res = Date64Type::to_naive_date_opt(date)?;
1322 let res = res.checked_sub_signed(Duration::try_days(days as i64)?)?;
1323 let res = res.checked_sub_signed(Duration::try_milliseconds(ms as i64)?)?;
1324 Some(Date64Type::from_naive_date(res))
1325 }
1326
1327 pub fn subtract_month_day_nano_opt(
1336 date: <Date64Type as ArrowPrimitiveType>::Native,
1337 delta: <IntervalMonthDayNanoType as ArrowPrimitiveType>::Native,
1338 ) -> Option<<Date64Type as ArrowPrimitiveType>::Native> {
1339 let (months, days, nanos) = IntervalMonthDayNanoType::to_parts(delta);
1340 let res = Date64Type::to_naive_date_opt(date)?;
1341 let res = add_months_date(res, -months)?;
1342 let res = res.checked_sub_signed(Duration::try_days(days as i64)?)?;
1343 let res = res.checked_sub_signed(Duration::nanoseconds(nanos))?;
1344 Some(Date64Type::from_naive_date(res))
1345 }
1346}
1347
1348mod decimal {
1352 use super::*;
1353
1354 pub trait DecimalTypeSealed {}
1355 impl DecimalTypeSealed for Decimal32Type {}
1356 impl DecimalTypeSealed for Decimal64Type {}
1357 impl DecimalTypeSealed for Decimal128Type {}
1358 impl DecimalTypeSealed for Decimal256Type {}
1359}
1360
1361pub trait DecimalType:
1373 'static + Send + Sync + ArrowPrimitiveType + decimal::DecimalTypeSealed
1374{
1375 const BYTE_LENGTH: usize;
1377 const MAX_PRECISION: u8;
1379 const MAX_SCALE: i8;
1381 const MAX_FOR_EACH_PRECISION: &'static [Self::Native];
1383 const TYPE_CONSTRUCTOR: fn(u8, i8) -> DataType;
1385 const DEFAULT_TYPE: DataType;
1387
1388 const PREFIX: &'static str;
1390
1391 fn format_decimal(value: Self::Native, precision: u8, scale: i8) -> String;
1393
1394 fn validate_decimal_precision(
1396 value: Self::Native,
1397 precision: u8,
1398 scale: i8,
1399 ) -> Result<(), ArrowError>;
1400
1401 fn is_valid_decimal_precision(value: Self::Native, precision: u8) -> bool;
1403}
1404
1405pub fn validate_decimal_precision_and_scale<T: DecimalType>(
1413 precision: u8,
1414 scale: i8,
1415) -> Result<(), ArrowError> {
1416 if precision == 0 {
1417 return Err(ArrowError::InvalidArgumentError(format!(
1418 "precision cannot be 0, has to be between [1, {}]",
1419 T::MAX_PRECISION
1420 )));
1421 }
1422 if precision > T::MAX_PRECISION {
1423 return Err(ArrowError::InvalidArgumentError(format!(
1424 "precision {} is greater than max {}",
1425 precision,
1426 T::MAX_PRECISION
1427 )));
1428 }
1429 if scale > T::MAX_SCALE {
1430 return Err(ArrowError::InvalidArgumentError(format!(
1431 "scale {} is greater than max {}",
1432 scale,
1433 T::MAX_SCALE
1434 )));
1435 }
1436 if scale > 0 && scale as u8 > precision {
1437 return Err(ArrowError::InvalidArgumentError(format!(
1438 "scale {scale} is greater than precision {precision}"
1439 )));
1440 }
1441
1442 Ok(())
1443}
1444
1445#[derive(Debug)]
1447pub struct Decimal32Type {}
1448
1449impl DecimalType for Decimal32Type {
1450 const BYTE_LENGTH: usize = 4;
1451 const MAX_PRECISION: u8 = DECIMAL32_MAX_PRECISION;
1452 const MAX_SCALE: i8 = DECIMAL32_MAX_SCALE;
1453 const MAX_FOR_EACH_PRECISION: &'static [i32] =
1454 &arrow_data::decimal::MAX_DECIMAL32_FOR_EACH_PRECISION;
1455 const TYPE_CONSTRUCTOR: fn(u8, i8) -> DataType = DataType::Decimal32;
1456 const DEFAULT_TYPE: DataType =
1457 DataType::Decimal32(DECIMAL32_MAX_PRECISION, DECIMAL32_DEFAULT_SCALE);
1458 const PREFIX: &'static str = "Decimal32";
1459
1460 fn format_decimal(value: Self::Native, precision: u8, scale: i8) -> String {
1461 format_decimal_str(&value.to_string(), precision as usize, scale)
1462 }
1463
1464 fn validate_decimal_precision(num: i32, precision: u8, scale: i8) -> Result<(), ArrowError> {
1465 validate_decimal32_precision(num, precision, scale)
1466 }
1467
1468 fn is_valid_decimal_precision(value: Self::Native, precision: u8) -> bool {
1469 is_validate_decimal32_precision(value, precision)
1470 }
1471}
1472
1473impl ArrowPrimitiveType for Decimal32Type {
1474 type Native = i32;
1475
1476 const DATA_TYPE: DataType = <Self as DecimalType>::DEFAULT_TYPE;
1477}
1478
1479impl primitive::PrimitiveTypeSealed for Decimal32Type {}
1480
1481#[derive(Debug)]
1483pub struct Decimal64Type {}
1484
1485impl DecimalType for Decimal64Type {
1486 const BYTE_LENGTH: usize = 8;
1487 const MAX_PRECISION: u8 = DECIMAL64_MAX_PRECISION;
1488 const MAX_SCALE: i8 = DECIMAL64_MAX_SCALE;
1489 const MAX_FOR_EACH_PRECISION: &'static [i64] =
1490 &arrow_data::decimal::MAX_DECIMAL64_FOR_EACH_PRECISION;
1491 const TYPE_CONSTRUCTOR: fn(u8, i8) -> DataType = DataType::Decimal64;
1492 const DEFAULT_TYPE: DataType =
1493 DataType::Decimal64(DECIMAL64_MAX_PRECISION, DECIMAL64_DEFAULT_SCALE);
1494 const PREFIX: &'static str = "Decimal64";
1495
1496 fn format_decimal(value: Self::Native, precision: u8, scale: i8) -> String {
1497 format_decimal_str(&value.to_string(), precision as usize, scale)
1498 }
1499
1500 fn validate_decimal_precision(num: i64, precision: u8, scale: i8) -> Result<(), ArrowError> {
1501 validate_decimal64_precision(num, precision, scale)
1502 }
1503
1504 fn is_valid_decimal_precision(value: Self::Native, precision: u8) -> bool {
1505 is_validate_decimal64_precision(value, precision)
1506 }
1507}
1508
1509impl ArrowPrimitiveType for Decimal64Type {
1510 type Native = i64;
1511
1512 const DATA_TYPE: DataType = <Self as DecimalType>::DEFAULT_TYPE;
1513}
1514
1515impl primitive::PrimitiveTypeSealed for Decimal64Type {}
1516
1517#[derive(Debug)]
1519pub struct Decimal128Type {}
1520
1521impl DecimalType for Decimal128Type {
1522 const BYTE_LENGTH: usize = 16;
1523 const MAX_PRECISION: u8 = DECIMAL128_MAX_PRECISION;
1524 const MAX_SCALE: i8 = DECIMAL128_MAX_SCALE;
1525 const MAX_FOR_EACH_PRECISION: &'static [i128] =
1526 &arrow_data::decimal::MAX_DECIMAL128_FOR_EACH_PRECISION;
1527 const TYPE_CONSTRUCTOR: fn(u8, i8) -> DataType = DataType::Decimal128;
1528 const DEFAULT_TYPE: DataType =
1529 DataType::Decimal128(DECIMAL128_MAX_PRECISION, DECIMAL_DEFAULT_SCALE);
1530 const PREFIX: &'static str = "Decimal128";
1531
1532 fn format_decimal(value: Self::Native, precision: u8, scale: i8) -> String {
1533 format_decimal_str(&value.to_string(), precision as usize, scale)
1534 }
1535
1536 fn validate_decimal_precision(num: i128, precision: u8, scale: i8) -> Result<(), ArrowError> {
1537 validate_decimal_precision(num, precision, scale)
1538 }
1539
1540 fn is_valid_decimal_precision(value: Self::Native, precision: u8) -> bool {
1541 is_validate_decimal_precision(value, precision)
1542 }
1543}
1544
1545impl ArrowPrimitiveType for Decimal128Type {
1546 type Native = i128;
1547
1548 const DATA_TYPE: DataType = <Self as DecimalType>::DEFAULT_TYPE;
1549}
1550
1551impl primitive::PrimitiveTypeSealed for Decimal128Type {}
1552
1553#[derive(Debug)]
1555pub struct Decimal256Type {}
1556
1557impl DecimalType for Decimal256Type {
1558 const BYTE_LENGTH: usize = 32;
1559 const MAX_PRECISION: u8 = DECIMAL256_MAX_PRECISION;
1560 const MAX_SCALE: i8 = DECIMAL256_MAX_SCALE;
1561 const MAX_FOR_EACH_PRECISION: &'static [i256] =
1562 &arrow_data::decimal::MAX_DECIMAL256_FOR_EACH_PRECISION;
1563 const TYPE_CONSTRUCTOR: fn(u8, i8) -> DataType = DataType::Decimal256;
1564 const DEFAULT_TYPE: DataType =
1565 DataType::Decimal256(DECIMAL256_MAX_PRECISION, DECIMAL_DEFAULT_SCALE);
1566 const PREFIX: &'static str = "Decimal256";
1567
1568 fn format_decimal(value: Self::Native, precision: u8, scale: i8) -> String {
1569 format_decimal_str(&value.to_string(), precision as usize, scale)
1570 }
1571
1572 fn validate_decimal_precision(num: i256, precision: u8, scale: i8) -> Result<(), ArrowError> {
1573 validate_decimal256_precision(num, precision, scale)
1574 }
1575
1576 fn is_valid_decimal_precision(value: Self::Native, precision: u8) -> bool {
1577 is_validate_decimal256_precision(value, precision)
1578 }
1579}
1580
1581impl ArrowPrimitiveType for Decimal256Type {
1582 type Native = i256;
1583
1584 const DATA_TYPE: DataType = <Self as DecimalType>::DEFAULT_TYPE;
1585}
1586
1587impl primitive::PrimitiveTypeSealed for Decimal256Type {}
1588
1589pub(crate) mod bytes {
1593 use super::*;
1594
1595 pub trait ByteArrayTypeSealed {}
1596 impl<O: OffsetSizeTrait> ByteArrayTypeSealed for GenericStringType<O> {}
1597 impl<O: OffsetSizeTrait> ByteArrayTypeSealed for GenericBinaryType<O> {}
1598
1599 pub trait ByteArrayNativeType: std::fmt::Debug + Send + Sync {
1600 fn from_bytes_checked(b: &[u8]) -> Option<&Self>;
1601
1602 unsafe fn from_bytes_unchecked(b: &[u8]) -> &Self;
1606 }
1607
1608 impl ByteArrayNativeType for [u8] {
1609 #[inline]
1610 fn from_bytes_checked(b: &[u8]) -> Option<&Self> {
1611 Some(b)
1612 }
1613
1614 #[inline]
1615 unsafe fn from_bytes_unchecked(b: &[u8]) -> &Self {
1616 b
1617 }
1618 }
1619
1620 impl ByteArrayNativeType for str {
1621 #[inline]
1622 fn from_bytes_checked(b: &[u8]) -> Option<&Self> {
1623 std::str::from_utf8(b).ok()
1624 }
1625
1626 #[inline]
1627 unsafe fn from_bytes_unchecked(b: &[u8]) -> &Self {
1628 unsafe { std::str::from_utf8_unchecked(b) }
1629 }
1630 }
1631}
1632
1633pub trait ByteArrayType: 'static + Send + Sync + bytes::ByteArrayTypeSealed {
1637 type Offset: OffsetSizeTrait;
1639 type Native: bytes::ByteArrayNativeType + AsRef<Self::Native> + AsRef<[u8]> + ?Sized;
1643
1644 const PREFIX: &'static str;
1646
1647 const DATA_TYPE: DataType;
1649
1650 fn validate(offsets: &OffsetBuffer<Self::Offset>, values: &Buffer) -> Result<(), ArrowError>;
1652}
1653
1654pub struct GenericStringType<O: OffsetSizeTrait> {
1656 phantom: PhantomData<O>,
1657}
1658
1659impl<O: OffsetSizeTrait> ByteArrayType for GenericStringType<O> {
1660 type Offset = O;
1661 type Native = str;
1662 const PREFIX: &'static str = "String";
1663
1664 const DATA_TYPE: DataType = if O::IS_LARGE {
1665 DataType::LargeUtf8
1666 } else {
1667 DataType::Utf8
1668 };
1669
1670 fn validate(offsets: &OffsetBuffer<Self::Offset>, values: &Buffer) -> Result<(), ArrowError> {
1671 let validated = std::str::from_utf8(values).map_err(|e| {
1673 ArrowError::InvalidArgumentError(format!("Encountered non UTF-8 data: {e}"))
1674 })?;
1675
1676 for offset in offsets.iter() {
1678 let o = offset.as_usize();
1679 if !validated.is_char_boundary(o) {
1680 if o < validated.len() {
1681 return Err(ArrowError::InvalidArgumentError(format!(
1682 "Split UTF-8 codepoint at offset {o}"
1683 )));
1684 }
1685 return Err(ArrowError::InvalidArgumentError(format!(
1686 "Offset of {o} exceeds length of values {}",
1687 validated.len()
1688 )));
1689 }
1690 }
1691 Ok(())
1692 }
1693}
1694
1695pub type Utf8Type = GenericStringType<i32>;
1697pub type LargeUtf8Type = GenericStringType<i64>;
1699
1700pub struct GenericBinaryType<O: OffsetSizeTrait> {
1702 phantom: PhantomData<O>,
1703}
1704
1705impl<O: OffsetSizeTrait> ByteArrayType for GenericBinaryType<O> {
1706 type Offset = O;
1707 type Native = [u8];
1708 const PREFIX: &'static str = "Binary";
1709
1710 const DATA_TYPE: DataType = if O::IS_LARGE {
1711 DataType::LargeBinary
1712 } else {
1713 DataType::Binary
1714 };
1715
1716 fn validate(offsets: &OffsetBuffer<Self::Offset>, values: &Buffer) -> Result<(), ArrowError> {
1717 let max_offset = offsets.last().unwrap().as_usize();
1719 if values.len() < max_offset {
1720 return Err(ArrowError::InvalidArgumentError(format!(
1721 "Maximum offset of {max_offset} is larger than values of length {}",
1722 values.len()
1723 )));
1724 }
1725 Ok(())
1726 }
1727}
1728
1729pub type BinaryType = GenericBinaryType<i32>;
1731pub type LargeBinaryType = GenericBinaryType<i64>;
1733
1734mod byte_view {
1735 use crate::types::{BinaryViewType, StringViewType};
1736
1737 pub trait Sealed: Send + Sync {}
1738 impl Sealed for StringViewType {}
1739 impl Sealed for BinaryViewType {}
1740}
1741
1742pub trait ByteViewType: byte_view::Sealed + 'static + PartialEq + Send + Sync {
1744 const IS_UTF8: bool;
1746
1747 const DATA_TYPE: DataType = if Self::IS_UTF8 {
1749 DataType::Utf8View
1750 } else {
1751 DataType::BinaryView
1752 };
1753
1754 const PREFIX: &'static str;
1756
1757 type Native: bytes::ByteArrayNativeType + AsRef<Self::Native> + AsRef<[u8]> + ?Sized;
1761
1762 type Owned: Debug + Clone + Sync + Send + AsRef<Self::Native>;
1764
1765 fn validate(views: &[u128], buffers: &[Buffer]) -> Result<(), ArrowError>;
1767}
1768
1769#[derive(PartialEq)]
1771pub struct StringViewType {}
1772
1773impl ByteViewType for StringViewType {
1774 const IS_UTF8: bool = true;
1775 const PREFIX: &'static str = "String";
1776
1777 type Native = str;
1778 type Owned = String;
1779
1780 fn validate(views: &[u128], buffers: &[Buffer]) -> Result<(), ArrowError> {
1781 validate_string_view(views, buffers)
1782 }
1783}
1784
1785#[derive(PartialEq)]
1787pub struct BinaryViewType {}
1788
1789impl ByteViewType for BinaryViewType {
1790 const IS_UTF8: bool = false;
1791 const PREFIX: &'static str = "Binary";
1792 type Native = [u8];
1793 type Owned = Vec<u8>;
1794
1795 fn validate(views: &[u128], buffers: &[Buffer]) -> Result<(), ArrowError> {
1796 validate_binary_view(views, buffers)
1797 }
1798}
1799
1800#[cfg(test)]
1801mod tests {
1802 use super::*;
1803 use arrow_data::{BufferSpec, layout};
1804 use chrono::DateTime;
1805
1806 #[test]
1807 fn month_day_nano_should_roundtrip() {
1808 let value = IntervalMonthDayNanoType::make_value(1, 2, 3);
1809 assert_eq!(IntervalMonthDayNanoType::to_parts(value), (1, 2, 3));
1810 }
1811
1812 #[test]
1813 fn month_day_nano_should_roundtrip_neg() {
1814 let value = IntervalMonthDayNanoType::make_value(-1, -2, -3);
1815 assert_eq!(IntervalMonthDayNanoType::to_parts(value), (-1, -2, -3));
1816 }
1817
1818 #[test]
1819 fn day_time_should_roundtrip() {
1820 let value = IntervalDayTimeType::make_value(1, 2);
1821 assert_eq!(IntervalDayTimeType::to_parts(value), (1, 2));
1822 }
1823
1824 #[test]
1825 fn day_time_should_roundtrip_neg() {
1826 let value = IntervalDayTimeType::make_value(-1, -2);
1827 assert_eq!(IntervalDayTimeType::to_parts(value), (-1, -2));
1828 }
1829
1830 #[test]
1831 fn year_month_should_roundtrip() {
1832 let value = IntervalYearMonthType::make_value(1, 2);
1833 assert_eq!(IntervalYearMonthType::to_months(value), 14);
1834 }
1835
1836 #[test]
1837 fn year_month_should_roundtrip_neg() {
1838 let value = IntervalYearMonthType::make_value(-1, -2);
1839 assert_eq!(IntervalYearMonthType::to_months(value), -14);
1840 }
1841
1842 fn test_layout<T: ArrowPrimitiveType>() {
1843 let layout = layout(&T::DATA_TYPE);
1844
1845 assert_eq!(layout.buffers.len(), 1);
1846
1847 let spec = &layout.buffers[0];
1848 assert_eq!(
1849 spec,
1850 &BufferSpec::FixedWidth {
1851 byte_width: std::mem::size_of::<T::Native>(),
1852 alignment: std::mem::align_of::<T::Native>(),
1853 }
1854 );
1855 }
1856
1857 #[test]
1858 fn test_layouts() {
1859 test_layout::<Int8Type>();
1860 test_layout::<Int16Type>();
1861 test_layout::<Int32Type>();
1862 test_layout::<Int64Type>();
1863 test_layout::<UInt8Type>();
1864 test_layout::<UInt16Type>();
1865 test_layout::<UInt32Type>();
1866 test_layout::<UInt64Type>();
1867 test_layout::<Float16Type>();
1868 test_layout::<Float32Type>();
1869 test_layout::<Float64Type>();
1870 test_layout::<Decimal32Type>();
1871 test_layout::<Decimal64Type>();
1872 test_layout::<Decimal128Type>();
1873 test_layout::<Decimal256Type>();
1874 test_layout::<TimestampNanosecondType>();
1875 test_layout::<TimestampMillisecondType>();
1876 test_layout::<TimestampMicrosecondType>();
1877 test_layout::<TimestampNanosecondType>();
1878 test_layout::<TimestampSecondType>();
1879 test_layout::<Date32Type>();
1880 test_layout::<Date64Type>();
1881 test_layout::<Time32SecondType>();
1882 test_layout::<Time32MillisecondType>();
1883 test_layout::<Time64MicrosecondType>();
1884 test_layout::<Time64NanosecondType>();
1885 test_layout::<IntervalMonthDayNanoType>();
1886 test_layout::<IntervalDayTimeType>();
1887 test_layout::<IntervalYearMonthType>();
1888 test_layout::<DurationNanosecondType>();
1889 test_layout::<DurationMicrosecondType>();
1890 test_layout::<DurationMillisecondType>();
1891 test_layout::<DurationSecondType>();
1892 }
1893
1894 #[test]
1895 fn timestamp_from_datetime() {
1896 use chrono::{FixedOffset, NaiveDate, NaiveTime, Utc};
1897
1898 let date = NaiveDate::from_ymd_opt(2021, 1, 1).unwrap();
1900 let time = NaiveTime::from_hms_opt(12, 0, 0).unwrap();
1901 let naive = NaiveDateTime::new(date, time);
1902 let datetime_utc = Utc.from_utc_datetime(&naive);
1903
1904 assert_eq!(
1905 TimestampSecondType::from_datetime(datetime_utc).unwrap(),
1906 1609502400
1907 );
1908 assert_eq!(
1909 TimestampMillisecondType::from_datetime(datetime_utc).unwrap(),
1910 1609502400000
1911 );
1912 assert_eq!(
1913 TimestampMicrosecondType::from_datetime(datetime_utc).unwrap(),
1914 1609502400000000
1915 );
1916 assert_eq!(
1917 TimestampNanosecondType::from_datetime(datetime_utc).unwrap(),
1918 1609502400000000000
1919 );
1920
1921 let tz_plus_8 = FixedOffset::east_opt(8 * 3600).unwrap();
1923 let datetime_plus_8 = tz_plus_8.from_local_datetime(&naive).unwrap();
1924 assert_eq!(
1925 TimestampSecondType::from_datetime(datetime_plus_8).unwrap(),
1926 1609502400 - 28800
1927 );
1928
1929 let datetime = DateTime::from_timestamp(1000000000, 123456789).unwrap();
1931 assert_eq!(
1932 TimestampSecondType::from_datetime(datetime).unwrap(),
1933 1000000000
1934 );
1935 assert_eq!(
1936 TimestampMillisecondType::from_datetime(datetime).unwrap(),
1937 1000000000123
1938 );
1939 assert_eq!(
1940 TimestampMicrosecondType::from_datetime(datetime).unwrap(),
1941 1000000000123456
1942 );
1943 assert_eq!(
1944 TimestampNanosecondType::from_datetime(datetime).unwrap(),
1945 1000000000123456789
1946 );
1947 }
1948
1949 #[test]
1950 fn timestamp_from_datetime_overflow() {
1951 let far_past = DateTime::from_timestamp(-10_000_000_000, 0).unwrap();
1953 assert!(TimestampNanosecondType::from_datetime(far_past).is_none());
1954 assert!(TimestampSecondType::from_datetime(far_past).is_some());
1956 assert!(TimestampMillisecondType::from_datetime(far_past).is_some());
1957 assert!(TimestampMicrosecondType::from_datetime(far_past).is_some());
1958
1959 let far_future = DateTime::from_timestamp(10_000_000_000, 0).unwrap();
1960 assert!(TimestampNanosecondType::from_datetime(far_future).is_none());
1961 assert!(TimestampSecondType::from_datetime(far_future).is_some());
1962 assert!(TimestampMillisecondType::from_datetime(far_future).is_some());
1963 assert!(TimestampMicrosecondType::from_datetime(far_future).is_some());
1964 }
1965
1966 #[test]
1967 fn timestamp_from_naive_datetime() {
1968 use crate::temporal_conversions::as_datetime_with_timezone;
1969 use chrono::{NaiveDate, NaiveTime};
1970
1971 let date = NaiveDate::from_ymd_opt(2021, 1, 1).unwrap();
1972 let time = NaiveTime::from_hms_opt(12, 0, 0).unwrap();
1973 let naive = NaiveDateTime::new(date, time);
1974
1975 assert_eq!(
1977 TimestampSecondType::from_naive_datetime(naive, None).unwrap(),
1978 1609502400
1979 );
1980 assert_eq!(
1981 TimestampMillisecondType::from_naive_datetime(naive, None).unwrap(),
1982 1609502400000
1983 );
1984
1985 let tz: Tz = "-05:00".parse().unwrap();
1987 let ts_sec = TimestampSecondType::from_naive_datetime(naive, Some(&tz)).unwrap();
1988 assert_eq!(ts_sec, 1609502400 + 5 * 3600);
1989
1990 let date = NaiveDate::from_ymd_opt(2024, 6, 15).unwrap();
1992 let time = NaiveTime::from_hms_opt(14, 30, 45).unwrap();
1993 let naive = NaiveDateTime::new(date, time);
1994 let tz: Tz = "+01:00".parse().unwrap();
1995
1996 let ts_usec = TimestampMicrosecondType::from_naive_datetime(naive, Some(&tz)).unwrap();
1997 let recovered = as_datetime_with_timezone::<TimestampMicrosecondType>(ts_usec, tz).unwrap();
1998 assert_eq!(recovered.naive_local(), naive);
1999 }
2000
2001 #[test]
2002 #[cfg(feature = "chrono-tz")]
2003 fn timestamp_from_naive_datetime_ambiguous() {
2004 use chrono::{NaiveDate, NaiveTime};
2005
2006 let date = NaiveDate::from_ymd_opt(2024, 11, 3).unwrap();
2009 let time = NaiveTime::from_hms_opt(1, 30, 0).unwrap();
2010 let naive = NaiveDateTime::new(date, time);
2011 let tz: Tz = "America/New_York".parse().unwrap();
2012
2013 let result = TimestampSecondType::from_naive_datetime(naive, Some(&tz));
2015 assert!(result.is_some());
2016 assert_eq!(result.unwrap(), 1730611800);
2017 }
2018
2019 #[test]
2020 #[cfg(feature = "chrono-tz")]
2021 fn timestamp_from_naive_datetime_none() {
2022 use chrono::{NaiveDate, NaiveTime};
2023
2024 let date = NaiveDate::from_ymd_opt(2024, 3, 10).unwrap();
2027 let time = NaiveTime::from_hms_opt(2, 30, 0).unwrap();
2028 let naive = NaiveDateTime::new(date, time);
2029 let tz: Tz = "America/New_York".parse().unwrap();
2030
2031 let result = TimestampSecondType::from_naive_datetime(naive, Some(&tz));
2033 assert!(result.is_none());
2034
2035 assert!(TimestampMillisecondType::from_naive_datetime(naive, Some(&tz)).is_none());
2037 assert!(TimestampMicrosecondType::from_naive_datetime(naive, Some(&tz)).is_none());
2038 assert!(TimestampNanosecondType::from_naive_datetime(naive, Some(&tz)).is_none());
2039 }
2040}