forked from apache/datafusion-comet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemporal.rs
More file actions
1312 lines (1237 loc) · 47.9 KB
/
temporal.rs
File metadata and controls
1312 lines (1237 loc) · 47.9 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.
//! temporal kernels
use chrono::{
DateTime, Datelike, Duration, LocalResult, NaiveDate, NaiveDateTime, Offset, TimeZone,
Timelike, Utc,
};
use std::sync::Arc;
use arrow::array::{
downcast_dictionary_array, downcast_temporal_array,
temporal_conversions::*,
timezone::Tz,
types::{ArrowDictionaryKeyType, ArrowTemporalType, TimestampMicrosecondType},
ArrowNumericType,
};
use arrow::{
array::*,
datatypes::{DataType, TimeUnit},
};
use crate::SparkError;
// Copied from arrow_arith/temporal.rs
macro_rules! return_compute_error_with {
($msg:expr, $param:expr) => {
return { Err(SparkError::Internal(format!("{}: {:?}", $msg, $param))) }
};
}
// The number of days between the beginning of the proleptic gregorian calendar (0001-01-01)
// and the beginning of the Unix Epoch (1970-01-01)
const DAYS_TO_UNIX_EPOCH: i32 = 719_163;
// Optimized date truncation functions that work directly with days since epoch
// These avoid the overhead of converting to/from NaiveDateTime
/// Convert days since Unix epoch to NaiveDate
#[inline]
fn days_to_date(days: i32) -> Option<NaiveDate> {
NaiveDate::from_num_days_from_ce_opt(days + DAYS_TO_UNIX_EPOCH)
}
/// Truncate date to first day of year - optimized version
/// Uses ordinal (day of year) to avoid creating a new date
#[inline]
fn trunc_days_to_year(days: i32) -> Option<i32> {
let date = days_to_date(days)?;
let day_of_year_offset = date.ordinal() as i32 - 1;
Some(days - day_of_year_offset)
}
/// Truncate date to first day of quarter - optimized version
/// Computes offset from first day of quarter without creating a new date
#[inline]
fn trunc_days_to_quarter(days: i32) -> Option<i32> {
let date = days_to_date(days)?;
let month = date.month(); // 1-12
let quarter = (month - 1) / 3; // 0-3
let first_month_of_quarter = quarter * 3 + 1; // 1, 4, 7, or 10
// Find day of year for first day of quarter
let first_day_of_quarter = NaiveDate::from_ymd_opt(date.year(), first_month_of_quarter, 1)?;
let quarter_start_ordinal = first_day_of_quarter.ordinal() as i32;
let current_ordinal = date.ordinal() as i32;
Some(days - (current_ordinal - quarter_start_ordinal))
}
/// Truncate date to first day of month - optimized version
/// Instead of creating a new date, just subtract day offset
#[inline]
fn trunc_days_to_month(days: i32) -> Option<i32> {
let date = days_to_date(days)?;
let day_offset = date.day() as i32 - 1;
Some(days - day_offset)
}
/// Truncate date to first day of week (Monday) - optimized version
#[inline]
fn trunc_days_to_week(days: i32) -> Option<i32> {
let date = days_to_date(days)?;
// weekday().num_days_from_monday() gives 0 for Monday, 1 for Tuesday, etc.
let days_since_monday = date.weekday().num_days_from_monday() as i32;
Some(days - days_since_monday)
}
// Based on arrow_arith/temporal.rs:extract_component_from_datetime_array
// Transforms an array of DateTime<Tz> to an arrayOf TimeStampMicrosecond after applying an
// operation
fn as_timestamp_tz_with_op<A: ArrayAccessor<Item = T::Native>, T: ArrowTemporalType, F>(
iter: ArrayIter<A>,
mut builder: PrimitiveBuilder<TimestampMicrosecondType>,
tz: &str,
op: F,
) -> Result<TimestampMicrosecondArray, SparkError>
where
F: Fn(DateTime<Tz>) -> i64,
i64: From<T::Native>,
{
let tz: Tz = tz.parse()?;
for value in iter {
match value {
Some(value) => match as_datetime_with_timezone::<T>(value.into(), tz) {
Some(time) => builder.append_value(op(time)),
_ => {
return Err(SparkError::Internal(
"Unable to read value as datetime".to_string(),
));
}
},
None => builder.append_null(),
}
}
Ok(builder.finish())
}
fn as_timestamp_tz_with_op_single<T: ArrowTemporalType, F>(
value: Option<T::Native>,
builder: &mut PrimitiveBuilder<TimestampMicrosecondType>,
tz: &Tz,
op: F,
) -> Result<(), SparkError>
where
F: Fn(DateTime<Tz>) -> i64,
i64: From<T::Native>,
{
match value {
Some(value) => match as_datetime_with_timezone::<T>(value.into(), *tz) {
Some(time) => builder.append_value(op(time)),
_ => {
return Err(SparkError::Internal(
"Unable to read value as datetime".to_string(),
));
}
},
None => builder.append_null(),
}
Ok(())
}
// Apply the Tz to the Naive Date Time, convert to UTC, and return as microseconds in Unix epoch.
// This function re-interprets the local datetime in the timezone to ensure the correct DST offset
// is used for the target date (not the original date's offset). This is important when truncation
// changes the date to a different DST period (e.g., from December/PST to October/PDT).
//
// Note: For far-future dates (approximately beyond year 2100), chrono-tz may not accurately
// calculate DST transitions, which can result in incorrect offsets. See the compatibility
// guide for more information.
#[inline]
fn as_micros_from_unix_epoch_utc(dt: Option<DateTime<Tz>>) -> i64 {
let dt = dt.unwrap();
let naive = dt.naive_local();
let tz = dt.timezone();
// Re-interpret the local time in the timezone to get the correct DST offset
// for the truncated date. Use noon to avoid DST gaps that occur around midnight.
let noon = naive.date().and_hms_opt(12, 0, 0).unwrap_or(naive);
let offset = match tz.offset_from_local_datetime(&noon) {
LocalResult::Single(off) | LocalResult::Ambiguous(off, _) => off.fix(),
LocalResult::None => return dt.with_timezone(&Utc).timestamp_micros(),
};
(naive - offset).and_utc().timestamp_micros()
}
#[inline]
fn trunc_date_to_year<T: Datelike + Timelike>(dt: T) -> Option<T> {
Some(dt)
.and_then(|d| d.with_nanosecond(0))
.and_then(|d| d.with_second(0))
.and_then(|d| d.with_minute(0))
.and_then(|d| d.with_hour(0))
.and_then(|d| d.with_day0(0))
.and_then(|d| d.with_month0(0))
}
/// returns the month of the beginning of the quarter
#[inline]
fn quarter_month<T: Datelike>(dt: &T) -> u32 {
1 + 3 * ((dt.month() - 1) / 3)
}
#[inline]
fn trunc_date_to_quarter<T: Datelike + Timelike>(dt: T) -> Option<T> {
Some(dt)
.and_then(|d| d.with_nanosecond(0))
.and_then(|d| d.with_second(0))
.and_then(|d| d.with_minute(0))
.and_then(|d| d.with_hour(0))
.and_then(|d| d.with_day0(0))
.and_then(|d| d.with_month(quarter_month(&d)))
}
#[inline]
fn trunc_date_to_month<T: Datelike + Timelike>(dt: T) -> Option<T> {
Some(dt)
.and_then(|d| d.with_nanosecond(0))
.and_then(|d| d.with_second(0))
.and_then(|d| d.with_minute(0))
.and_then(|d| d.with_hour(0))
.and_then(|d| d.with_day0(0))
}
#[inline]
fn trunc_date_to_week<T>(dt: T) -> Option<T>
where
T: Datelike + Timelike + std::ops::Sub<Duration, Output = T> + Copy,
{
Some(dt)
.map(|d| d - Duration::try_seconds(60 * 60 * 24 * d.weekday() as i64).unwrap())
.and_then(|d| d.with_nanosecond(0))
.and_then(|d| d.with_second(0))
.and_then(|d| d.with_minute(0))
.and_then(|d| d.with_hour(0))
}
#[inline]
fn trunc_date_to_day<T: Timelike>(dt: T) -> Option<T> {
Some(dt)
.and_then(|d| d.with_nanosecond(0))
.and_then(|d| d.with_second(0))
.and_then(|d| d.with_minute(0))
.and_then(|d| d.with_hour(0))
}
#[inline]
fn trunc_date_to_hour<T: Timelike>(dt: T) -> Option<T> {
Some(dt)
.and_then(|d| d.with_nanosecond(0))
.and_then(|d| d.with_second(0))
.and_then(|d| d.with_minute(0))
}
#[inline]
fn trunc_date_to_minute<T: Timelike>(dt: T) -> Option<T> {
Some(dt)
.and_then(|d| d.with_nanosecond(0))
.and_then(|d| d.with_second(0))
}
#[inline]
fn trunc_date_to_second<T: Timelike>(dt: T) -> Option<T> {
Some(dt).and_then(|d| d.with_nanosecond(0))
}
#[inline]
fn trunc_date_to_ms<T: Timelike>(dt: T) -> Option<T> {
Some(dt).and_then(|d| d.with_nanosecond(1_000_000 * (d.nanosecond() / 1_000_000)))
}
#[inline]
fn trunc_date_to_microsec<T: Timelike>(dt: T) -> Option<T> {
Some(dt).and_then(|d| d.with_nanosecond(1_000 * (d.nanosecond() / 1_000)))
}
///
/// Implements the spark [TRUNC](https://spark.apache.org/docs/latest/api/sql/index.html#trunc)
/// function where the specified format is a scalar value
///
/// array is an array of Date32 values. The array may be a dictionary array.
///
/// format is a scalar string specifying the format to apply to the timestamp value.
pub fn date_trunc_dyn(array: &dyn Array, format: String) -> Result<ArrayRef, SparkError> {
match array.data_type().clone() {
DataType::Dictionary(_, _) => {
downcast_dictionary_array!(
array => {
let truncated_values = date_trunc_dyn(array.values(), format)?;
Ok(Arc::new(array.with_values(truncated_values)))
}
dt => return_compute_error_with!("date_trunc does not support", dt),
)
}
_ => {
downcast_temporal_array!(
array => {
date_trunc(array, format)
.map(|a| Arc::new(a) as ArrayRef)
}
dt => return_compute_error_with!("date_trunc does not support", dt),
)
}
}
}
pub(crate) fn date_trunc<T>(
array: &PrimitiveArray<T>,
format: String,
) -> Result<Date32Array, SparkError>
where
T: ArrowTemporalType + ArrowNumericType,
i64: From<T::Native>,
{
match array.data_type() {
DataType::Date32 => {
// Use optimized path for Date32 that works directly with days
date_trunc_date32(
array
.as_any()
.downcast_ref::<Date32Array>()
.expect("Date32 type mismatch"),
format,
)
}
dt => return_compute_error_with!(
"Unsupported input type '{:?}' for function 'date_trunc'",
dt
),
}
}
/// Optimized date truncation for Date32 arrays
/// Works directly with days since epoch instead of converting to/from NaiveDateTime
fn date_trunc_date32(array: &Date32Array, format: String) -> Result<Date32Array, SparkError> {
// Select the truncation function based on format
let trunc_fn: fn(i32) -> Option<i32> = match format.to_uppercase().as_str() {
"YEAR" | "YYYY" | "YY" => trunc_days_to_year,
"QUARTER" => trunc_days_to_quarter,
"MONTH" | "MON" | "MM" => trunc_days_to_month,
"WEEK" => trunc_days_to_week,
_ => {
return Err(SparkError::Internal(format!(
"Unsupported format: {format:?} for function 'date_trunc'"
)))
}
};
// Apply truncation to each element
let result: Date32Array = array
.iter()
.map(|opt_days| opt_days.and_then(trunc_fn))
.collect();
Ok(result)
}
///
/// Implements the spark [TRUNC](https://spark.apache.org/docs/latest/api/sql/index.html#trunc)
/// function where the specified format may be an array
///
/// array is an array of Date32 values. The array may be a dictionary array.
///
/// format is an array of strings specifying the format to apply to the corresponding date value.
/// The array may be a dictionary array.
pub(crate) fn date_trunc_array_fmt_dyn(
array: &dyn Array,
formats: &dyn Array,
) -> Result<ArrayRef, SparkError> {
match (array.data_type().clone(), formats.data_type().clone()) {
(DataType::Dictionary(_, v), DataType::Dictionary(_, f)) => {
if !matches!(*v, DataType::Date32) {
return_compute_error_with!("date_trunc does not support", v)
}
if !matches!(*f, DataType::Utf8) {
return_compute_error_with!("date_trunc does not support format type ", f)
}
downcast_dictionary_array!(
formats => {
downcast_dictionary_array!(
array => {
date_trunc_array_fmt_dict_dict(
&array.downcast_dict::<Date32Array>().unwrap(),
&formats.downcast_dict::<StringArray>().unwrap())
.map(|a| Arc::new(a) as ArrayRef)
}
dt => return_compute_error_with!("date_trunc does not support", dt)
)
}
fmt => return_compute_error_with!("date_trunc does not support format type", fmt),
)
}
(DataType::Dictionary(_, v), DataType::Utf8) => {
if !matches!(*v, DataType::Date32) {
return_compute_error_with!("date_trunc does not support", v)
}
downcast_dictionary_array!(
array => {
date_trunc_array_fmt_dict_plain(
&array.downcast_dict::<Date32Array>().unwrap(),
formats.as_any().downcast_ref::<StringArray>()
.expect("Unexpected value type in formats"))
.map(|a| Arc::new(a) as ArrayRef)
}
dt => return_compute_error_with!("date_trunc does not support", dt),
)
}
(DataType::Date32, DataType::Dictionary(_, f)) => {
if !matches!(*f, DataType::Utf8) {
return_compute_error_with!("date_trunc does not support format type ", f)
}
downcast_dictionary_array!(
formats => {
downcast_temporal_array!(array => {
date_trunc_array_fmt_plain_dict(
array.as_any().downcast_ref::<Date32Array>()
.expect("Unexpected error in casting date array"),
&formats.downcast_dict::<StringArray>().unwrap())
.map(|a| Arc::new(a) as ArrayRef)
}
dt => return_compute_error_with!("date_trunc does not support", dt),
)
}
fmt => return_compute_error_with!("date_trunc does not support format type", fmt),
)
}
(DataType::Date32, DataType::Utf8) => date_trunc_array_fmt_plain_plain(
array
.as_any()
.downcast_ref::<Date32Array>()
.expect("Unexpected error in casting date array"),
formats
.as_any()
.downcast_ref::<StringArray>()
.expect("Unexpected value type in formats"),
)
.map(|a| Arc::new(a) as ArrayRef),
(dt, fmt) => Err(SparkError::Internal(format!(
"Unsupported datatype: {dt:}, format: {fmt:?} for function 'date_trunc'"
))),
}
}
macro_rules! date_trunc_array_fmt_helper {
($array: ident, $formats: ident, $datatype: ident) => {{
let mut builder = Date32Builder::with_capacity($array.len());
let iter = $array.into_iter();
match $datatype {
DataType::Date32 => {
for (index, val) in iter.enumerate() {
let trunc_fn: fn(i32) -> Option<i32> =
match $formats.value(index).to_uppercase().as_str() {
"YEAR" | "YYYY" | "YY" => trunc_days_to_year,
"QUARTER" => trunc_days_to_quarter,
"MONTH" | "MON" | "MM" => trunc_days_to_month,
"WEEK" => trunc_days_to_week,
_ => {
return Err(SparkError::Internal(format!(
"Unsupported format: {:?} for function 'date_trunc'",
$formats.value(index)
)))
}
};
match val.and_then(trunc_fn) {
Some(days) => builder.append_value(days),
None => builder.append_null(),
}
}
Ok(builder.finish())
}
dt => return_compute_error_with!(
"Unsupported input type '{:?}' for function 'date_trunc'",
dt
),
}
}};
}
fn date_trunc_array_fmt_plain_plain(
array: &Date32Array,
formats: &StringArray,
) -> Result<Date32Array, SparkError>
where
{
let data_type = array.data_type();
date_trunc_array_fmt_helper!(array, formats, data_type)
}
fn date_trunc_array_fmt_plain_dict<K>(
array: &Date32Array,
formats: &TypedDictionaryArray<K, StringArray>,
) -> Result<Date32Array, SparkError>
where
K: ArrowDictionaryKeyType,
{
let data_type = array.data_type();
date_trunc_array_fmt_helper!(array, formats, data_type)
}
fn date_trunc_array_fmt_dict_plain<K>(
array: &TypedDictionaryArray<K, Date32Array>,
formats: &StringArray,
) -> Result<Date32Array, SparkError>
where
K: ArrowDictionaryKeyType,
{
let data_type = array.values().data_type();
date_trunc_array_fmt_helper!(array, formats, data_type)
}
fn date_trunc_array_fmt_dict_dict<K, F>(
array: &TypedDictionaryArray<K, Date32Array>,
formats: &TypedDictionaryArray<F, StringArray>,
) -> Result<Date32Array, SparkError>
where
K: ArrowDictionaryKeyType,
F: ArrowDictionaryKeyType,
{
let data_type = array.values().data_type();
date_trunc_array_fmt_helper!(array, formats, data_type)
}
///
/// Implements the spark [DATE_TRUNC](https://spark.apache.org/docs/latest/api/sql/index.html#date_trunc)
/// function where the specified format is a scalar value
///
/// array is an array of Timestamp(Microsecond) values. Timestamp values must have a valid
/// timezone or no timezone. The array may be a dictionary array.
///
/// format is a scalar string specifying the format to apply to the timestamp value.
pub(crate) fn timestamp_trunc_dyn(
array: &dyn Array,
format: String,
) -> Result<ArrayRef, SparkError> {
match array.data_type().clone() {
DataType::Dictionary(_, _) => {
downcast_dictionary_array!(
array => {
let truncated_values = timestamp_trunc_dyn(array.values(), format)?;
Ok(Arc::new(array.with_values(truncated_values)))
}
dt => return_compute_error_with!("timestamp_trunc does not support", dt),
)
}
_ => {
downcast_temporal_array!(
array => {
timestamp_trunc(array, format)
.map(|a| Arc::new(a) as ArrayRef)
}
dt => return_compute_error_with!("timestamp_trunc does not support", dt),
)
}
}
}
/// Convert microseconds since epoch to NaiveDateTime
#[inline]
fn micros_to_naive(micros: i64) -> Option<NaiveDateTime> {
DateTime::from_timestamp_micros(micros).map(|dt| dt.naive_utc())
}
/// Convert NaiveDateTime back to microseconds since epoch
#[inline]
fn naive_to_micros(dt: NaiveDateTime) -> i64 {
dt.and_utc().timestamp_micros()
}
/// Truncate a TimestampNTZ array without any timezone conversion.
/// NTZ values are timezone-independent; we treat the raw microseconds as a naive datetime.
fn timestamp_trunc_ntz<T>(
array: &PrimitiveArray<T>,
format: String,
) -> Result<TimestampMicrosecondArray, SparkError>
where
T: ArrowTemporalType + ArrowNumericType,
i64: From<T::Native>,
{
let trunc_fn: fn(NaiveDateTime) -> Option<NaiveDateTime> = match format.to_uppercase().as_str()
{
"YEAR" | "YYYY" | "YY" => trunc_date_to_year,
"QUARTER" => trunc_date_to_quarter,
"MONTH" | "MON" | "MM" => trunc_date_to_month,
"WEEK" => trunc_date_to_week,
"DAY" | "DD" => trunc_date_to_day,
"HOUR" => trunc_date_to_hour,
"MINUTE" => trunc_date_to_minute,
"SECOND" => trunc_date_to_second,
"MILLISECOND" => trunc_date_to_ms,
"MICROSECOND" => trunc_date_to_microsec,
_ => {
return Err(SparkError::Internal(format!(
"Unsupported format: {format:?} for function 'timestamp_trunc'"
)))
}
};
let result: TimestampMicrosecondArray = array
.iter()
.map(|opt_val| {
opt_val.and_then(|v| {
let micros: i64 = v.into();
micros_to_naive(micros)
.and_then(trunc_fn)
.map(naive_to_micros)
})
})
.collect();
Ok(result)
}
/// Truncate a single NTZ value and append to builder
fn timestamp_trunc_ntz_single<F>(
value: Option<i64>,
builder: &mut PrimitiveBuilder<TimestampMicrosecondType>,
op: F,
) -> Result<(), SparkError>
where
F: Fn(NaiveDateTime) -> Option<NaiveDateTime>,
{
match value {
Some(micros) => match micros_to_naive(micros).and_then(op) {
Some(truncated) => builder.append_value(naive_to_micros(truncated)),
None => {
return Err(SparkError::Internal(
"Unable to truncate NTZ timestamp".to_string(),
))
}
},
None => builder.append_null(),
}
Ok(())
}
pub(crate) fn timestamp_trunc<T>(
array: &PrimitiveArray<T>,
format: String,
) -> Result<TimestampMicrosecondArray, SparkError>
where
T: ArrowTemporalType + ArrowNumericType,
i64: From<T::Native>,
{
let builder = TimestampMicrosecondBuilder::with_capacity(array.len());
let iter = ArrayIter::new(array);
match array.data_type() {
DataType::Timestamp(TimeUnit::Microsecond, None) => {
// TimestampNTZ: operate directly on naive microsecond values without timezone
timestamp_trunc_ntz(array, format)
}
DataType::Timestamp(TimeUnit::Microsecond, Some(tz)) => {
match format.to_uppercase().as_str() {
"YEAR" | "YYYY" | "YY" => {
as_timestamp_tz_with_op::<&PrimitiveArray<T>, T, _>(iter, builder, tz, |dt| {
as_micros_from_unix_epoch_utc(trunc_date_to_year(dt))
})
}
"QUARTER" => {
as_timestamp_tz_with_op::<&PrimitiveArray<T>, T, _>(iter, builder, tz, |dt| {
as_micros_from_unix_epoch_utc(trunc_date_to_quarter(dt))
})
}
"MONTH" | "MON" | "MM" => {
as_timestamp_tz_with_op::<&PrimitiveArray<T>, T, _>(iter, builder, tz, |dt| {
as_micros_from_unix_epoch_utc(trunc_date_to_month(dt))
})
}
"WEEK" => {
as_timestamp_tz_with_op::<&PrimitiveArray<T>, T, _>(iter, builder, tz, |dt| {
as_micros_from_unix_epoch_utc(trunc_date_to_week(dt))
})
}
"DAY" | "DD" => {
as_timestamp_tz_with_op::<&PrimitiveArray<T>, T, _>(iter, builder, tz, |dt| {
as_micros_from_unix_epoch_utc(trunc_date_to_day(dt))
})
}
"HOUR" => {
as_timestamp_tz_with_op::<&PrimitiveArray<T>, T, _>(iter, builder, tz, |dt| {
as_micros_from_unix_epoch_utc(trunc_date_to_hour(dt))
})
}
"MINUTE" => {
as_timestamp_tz_with_op::<&PrimitiveArray<T>, T, _>(iter, builder, tz, |dt| {
as_micros_from_unix_epoch_utc(trunc_date_to_minute(dt))
})
}
"SECOND" => {
as_timestamp_tz_with_op::<&PrimitiveArray<T>, T, _>(iter, builder, tz, |dt| {
as_micros_from_unix_epoch_utc(trunc_date_to_second(dt))
})
}
"MILLISECOND" => {
as_timestamp_tz_with_op::<&PrimitiveArray<T>, T, _>(iter, builder, tz, |dt| {
as_micros_from_unix_epoch_utc(trunc_date_to_ms(dt))
})
}
"MICROSECOND" => {
as_timestamp_tz_with_op::<&PrimitiveArray<T>, T, _>(iter, builder, tz, |dt| {
as_micros_from_unix_epoch_utc(trunc_date_to_microsec(dt))
})
}
_ => Err(SparkError::Internal(format!(
"Unsupported format: {format:?} for function 'timestamp_trunc'"
))),
}
}
dt => return_compute_error_with!(
"Unsupported input type '{:?}' for function 'timestamp_trunc'",
dt
),
}
}
///
/// Implements the spark [DATE_TRUNC](https://spark.apache.org/docs/latest/api/sql/index.html#date_trunc)
/// function where the specified format may be an array
///
/// array is an array of Timestamp(Microsecond) values. Timestamp values must have a valid
/// timezone or no timezone. The array may be a dictionary array.
///
/// format is an array of strings specifying the format to apply to the corresponding timestamp
/// value. The array may be a dictionary array.
pub(crate) fn timestamp_trunc_array_fmt_dyn(
array: &dyn Array,
formats: &dyn Array,
) -> Result<ArrayRef, SparkError> {
match (array.data_type().clone(), formats.data_type().clone()) {
(DataType::Dictionary(_, _), DataType::Dictionary(_, _)) => {
downcast_dictionary_array!(
formats => {
downcast_dictionary_array!(
array => {
timestamp_trunc_array_fmt_dict_dict(
&array.downcast_dict::<TimestampMicrosecondArray>().unwrap(),
&formats.downcast_dict::<StringArray>().unwrap())
.map(|a| Arc::new(a) as ArrayRef)
}
dt => return_compute_error_with!("timestamp_trunc does not support", dt)
)
}
fmt => return_compute_error_with!("timestamp_trunc does not support format type", fmt),
)
}
(DataType::Dictionary(_, _), DataType::Utf8) => {
downcast_dictionary_array!(
array => {
timestamp_trunc_array_fmt_dict_plain(
&array.downcast_dict::<PrimitiveArray<TimestampMicrosecondType>>().unwrap(),
formats.as_any().downcast_ref::<StringArray>()
.expect("Unexpected value type in formats"))
.map(|a| Arc::new(a) as ArrayRef)
}
dt => return_compute_error_with!("timestamp_trunc does not support", dt),
)
}
(DataType::Timestamp(TimeUnit::Microsecond, _), DataType::Dictionary(_, _)) => {
downcast_dictionary_array!(
formats => {
downcast_temporal_array!(array => {
timestamp_trunc_array_fmt_plain_dict(
array,
&formats.downcast_dict::<StringArray>().unwrap())
.map(|a| Arc::new(a) as ArrayRef)
}
dt => return_compute_error_with!("timestamp_trunc does not support", dt),
)
}
fmt => return_compute_error_with!("timestamp_trunc does not support format type", fmt),
)
}
(DataType::Timestamp(TimeUnit::Microsecond, _), DataType::Utf8) => {
downcast_temporal_array!(
array => {
timestamp_trunc_array_fmt_plain_plain(array,
formats.as_any().downcast_ref::<StringArray>().expect("Unexpected value type in formats"))
.map(|a| Arc::new(a) as ArrayRef)
},
dt => return_compute_error_with!("timestamp_trunc does not support", dt),
)
}
(dt, fmt) => Err(SparkError::Internal(format!(
"Unsupported datatype: {dt:}, format: {fmt:?} for function 'timestamp_trunc'"
))),
}
}
macro_rules! timestamp_trunc_array_fmt_helper {
($array: ident, $formats: ident, $datatype: ident) => {{
let mut builder = TimestampMicrosecondBuilder::with_capacity($array.len());
let iter = $array.into_iter();
assert_eq!(
$array.len(),
$formats.len(),
"lengths of values array and format array must be the same"
);
match $datatype {
DataType::Timestamp(TimeUnit::Microsecond, None) => {
// TimestampNTZ: operate directly on naive microsecond values
for (index, val) in iter.enumerate() {
let micros_val = val.map(|v| i64::from(v));
let op_result = match $formats.value(index).to_uppercase().as_str() {
"YEAR" | "YYYY" | "YY" => {
timestamp_trunc_ntz_single(micros_val, &mut builder, trunc_date_to_year)
}
"QUARTER" => timestamp_trunc_ntz_single(
micros_val,
&mut builder,
trunc_date_to_quarter,
),
"MONTH" | "MON" | "MM" => timestamp_trunc_ntz_single(
micros_val,
&mut builder,
trunc_date_to_month,
),
"WEEK" => {
timestamp_trunc_ntz_single(micros_val, &mut builder, trunc_date_to_week)
}
"DAY" | "DD" => {
timestamp_trunc_ntz_single(micros_val, &mut builder, trunc_date_to_day)
}
"HOUR" => {
timestamp_trunc_ntz_single(micros_val, &mut builder, trunc_date_to_hour)
}
"MINUTE" => timestamp_trunc_ntz_single(
micros_val,
&mut builder,
trunc_date_to_minute,
),
"SECOND" => timestamp_trunc_ntz_single(
micros_val,
&mut builder,
trunc_date_to_second,
),
"MILLISECOND" => {
timestamp_trunc_ntz_single(micros_val, &mut builder, trunc_date_to_ms)
}
"MICROSECOND" => timestamp_trunc_ntz_single(
micros_val,
&mut builder,
trunc_date_to_microsec,
),
_ => Err(SparkError::Internal(format!(
"Unsupported format: {:?} for function 'timestamp_trunc'",
$formats.value(index)
))),
};
op_result?
}
Ok(builder.finish())
}
DataType::Timestamp(TimeUnit::Microsecond, Some(tz)) => {
let tz: Tz = tz.parse()?;
for (index, val) in iter.enumerate() {
let op_result = match $formats.value(index).to_uppercase().as_str() {
"YEAR" | "YYYY" | "YY" => {
as_timestamp_tz_with_op_single::<T, _>(val, &mut builder, &tz, |dt| {
as_micros_from_unix_epoch_utc(trunc_date_to_year(dt))
})
}
"QUARTER" => {
as_timestamp_tz_with_op_single::<T, _>(val, &mut builder, &tz, |dt| {
as_micros_from_unix_epoch_utc(trunc_date_to_quarter(dt))
})
}
"MONTH" | "MON" | "MM" => {
as_timestamp_tz_with_op_single::<T, _>(val, &mut builder, &tz, |dt| {
as_micros_from_unix_epoch_utc(trunc_date_to_month(dt))
})
}
"WEEK" => {
as_timestamp_tz_with_op_single::<T, _>(val, &mut builder, &tz, |dt| {
as_micros_from_unix_epoch_utc(trunc_date_to_week(dt))
})
}
"DAY" | "DD" => {
as_timestamp_tz_with_op_single::<T, _>(val, &mut builder, &tz, |dt| {
as_micros_from_unix_epoch_utc(trunc_date_to_day(dt))
})
}
"HOUR" => {
as_timestamp_tz_with_op_single::<T, _>(val, &mut builder, &tz, |dt| {
as_micros_from_unix_epoch_utc(trunc_date_to_hour(dt))
})
}
"MINUTE" => {
as_timestamp_tz_with_op_single::<T, _>(val, &mut builder, &tz, |dt| {
as_micros_from_unix_epoch_utc(trunc_date_to_minute(dt))
})
}
"SECOND" => {
as_timestamp_tz_with_op_single::<T, _>(val, &mut builder, &tz, |dt| {
as_micros_from_unix_epoch_utc(trunc_date_to_second(dt))
})
}
"MILLISECOND" => {
as_timestamp_tz_with_op_single::<T, _>(val, &mut builder, &tz, |dt| {
as_micros_from_unix_epoch_utc(trunc_date_to_ms(dt))
})
}
"MICROSECOND" => {
as_timestamp_tz_with_op_single::<T, _>(val, &mut builder, &tz, |dt| {
as_micros_from_unix_epoch_utc(trunc_date_to_microsec(dt))
})
}
_ => Err(SparkError::Internal(format!(
"Unsupported format: {:?} for function 'timestamp_trunc'",
$formats.value(index)
))),
};
op_result?
}
Ok(builder.finish())
}
dt => {
return_compute_error_with!(
"Unsupported input type '{:?}' for function 'timestamp_trunc'",
dt
)
}
}
}};
}
fn timestamp_trunc_array_fmt_plain_plain<T>(
array: &PrimitiveArray<T>,
formats: &StringArray,
) -> Result<TimestampMicrosecondArray, SparkError>
where
T: ArrowTemporalType + ArrowNumericType,
i64: From<T::Native>,
{
let data_type = array.data_type();
timestamp_trunc_array_fmt_helper!(array, formats, data_type)
}
fn timestamp_trunc_array_fmt_plain_dict<T, K>(
array: &PrimitiveArray<T>,
formats: &TypedDictionaryArray<K, StringArray>,
) -> Result<TimestampMicrosecondArray, SparkError>
where
T: ArrowTemporalType + ArrowNumericType,
i64: From<T::Native>,
K: ArrowDictionaryKeyType,
{
let data_type = array.data_type();
timestamp_trunc_array_fmt_helper!(array, formats, data_type)
}
fn timestamp_trunc_array_fmt_dict_plain<T, K>(
array: &TypedDictionaryArray<K, PrimitiveArray<T>>,
formats: &StringArray,
) -> Result<TimestampMicrosecondArray, SparkError>
where
T: ArrowTemporalType + ArrowNumericType,
i64: From<T::Native>,
K: ArrowDictionaryKeyType,
{
let data_type = array.values().data_type();
timestamp_trunc_array_fmt_helper!(array, formats, data_type)
}
fn timestamp_trunc_array_fmt_dict_dict<T, K, F>(
array: &TypedDictionaryArray<K, PrimitiveArray<T>>,
formats: &TypedDictionaryArray<F, StringArray>,
) -> Result<TimestampMicrosecondArray, SparkError>
where
T: ArrowTemporalType + ArrowNumericType,
i64: From<T::Native>,
K: ArrowDictionaryKeyType,
F: ArrowDictionaryKeyType,
{
let data_type = array.values().data_type();
timestamp_trunc_array_fmt_helper!(array, formats, data_type)
}
#[cfg(test)]
mod tests {
use crate::kernels::temporal::{
date_trunc, date_trunc_array_fmt_dyn, timestamp_trunc, timestamp_trunc_array_fmt_dyn,
};
use arrow::array::{
builder::{PrimitiveDictionaryBuilder, StringDictionaryBuilder},
iterator::ArrayIter,
types::{Date32Type, Int32Type, TimestampMicrosecondType},
Array, Date32Array, PrimitiveArray, StringArray, TimestampMicrosecondArray,
};
use std::sync::Arc;
#[test]
#[cfg_attr(miri, ignore)] // test takes too long with miri
fn test_date_trunc() {
let size = 1000;
let mut vec: Vec<i32> = Vec::with_capacity(size);
for i in 0..size {
vec.push(i as i32);
}
let array = Date32Array::from(vec);
for fmt in [
"YEAR", "YYYY", "YY", "QUARTER", "MONTH", "MON", "MM", "WEEK",
] {
match date_trunc(&array, fmt.to_string()) {
Ok(a) => {