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
use crate::chunked_array::{
    builder::{
        aligned_vec_to_primitive_array, build_with_existing_null_bitmap_and_slice, get_bitmap,
    },
    temporal::conversions_utils::*,
};
use crate::prelude::*;
use arrow::array::{ArrayRef, PrimitiveArray};
use chrono::{Datelike, Timelike};
use std::sync::Arc;

pub fn date32_as_duration(arr: &PrimitiveArray<Date32Type>) -> ArrayRef {
    let vals = arr.values();
    let (null_count, null_bit_buffer) = get_bitmap(arr);

    let av = vals
        .iter()
        .map(|days| (days * 3600 * 24 * 1000) as i64)
        .collect();

    Arc::new(aligned_vec_to_primitive_array::<DurationMillisecondType>(
        av,
        null_bit_buffer,
        Some(null_count),
    ))
}

pub fn date64_as_duration(arr: &PrimitiveArray<Date64Type>) -> ArrayRef {
    let vals = arr.values();
    let (null_count, null_bit_buffer) = get_bitmap(arr);
    Arc::new(build_with_existing_null_bitmap_and_slice::<
        DurationMillisecondType,
    >(null_bit_buffer, null_count, vals))
}

macro_rules! to_temporal_unit {
    ($name: ident, $chrono_method:ident, $to_datetime_fn: expr, $dtype_in: ty, $dtype_out:ty) => {
        pub fn $name(arr: &PrimitiveArray<$dtype_in>) -> ArrayRef {
            let vals = arr.values();
            let (null_count, null_bit_buffer) = get_bitmap(arr);
            let av = vals
                .iter()
                .map(|&v| {
                    let dt = $to_datetime_fn(v);
                    dt.$chrono_method()
                })
                .collect();
            Arc::new(aligned_vec_to_primitive_array::<$dtype_out>(
                av,
                null_bit_buffer,
                Some(null_count),
            ))
        }
    };
}

to_temporal_unit!(
    date32_to_year,
    year,
    date32_as_datetime,
    Date32Type,
    Int32Type
);
to_temporal_unit!(
    date32_to_month,
    month,
    date32_as_datetime,
    Date32Type,
    UInt32Type
);
to_temporal_unit!(
    date32_to_day,
    day,
    date32_as_datetime,
    Date32Type,
    UInt32Type
);
to_temporal_unit!(
    date32_to_ordinal,
    ordinal,
    date32_as_datetime,
    Date32Type,
    UInt32Type
);

to_temporal_unit!(
    date64_to_year,
    year,
    date64_as_datetime,
    Date64Type,
    Int32Type
);
to_temporal_unit!(
    date64_to_month,
    month,
    date64_as_datetime,
    Date64Type,
    UInt32Type
);
to_temporal_unit!(
    date64_to_day,
    day,
    date64_as_datetime,
    Date64Type,
    UInt32Type
);
to_temporal_unit!(
    date64_to_hour,
    hour,
    date64_as_datetime,
    Date64Type,
    UInt32Type
);
to_temporal_unit!(
    date64_to_minute,
    minute,
    date64_as_datetime,
    Date64Type,
    UInt32Type
);
to_temporal_unit!(
    date64_to_second,
    second,
    date64_as_datetime,
    Date64Type,
    UInt32Type
);
to_temporal_unit!(
    date64_to_nanosecond,
    nanosecond,
    date64_as_datetime,
    Date64Type,
    UInt32Type
);
to_temporal_unit!(
    date64_to_ordinal,
    ordinal,
    date64_as_datetime,
    Date64Type,
    UInt32Type
);