arrow_convert 0.12.1

Convert between nested rust types and Arrow with arrow
Documentation
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
//! Implementation and traits for mapping rust types to Arrow types

use std::sync::Arc;

use arrow_buffer::{ArrowNativeType, Buffer, ScalarBuffer};
use arrow_schema::{DataType, Field};
use chrono::{DateTime, NaiveDate, NaiveDateTime, Utc};

/// The default field name used when a specific name is not provided.
pub const DEFAULT_FIELD_NAME: &str = "_item";

/// Overrides the element field name for list-like datatypes on the given field.
///
/// This only affects the direct child field of:
/// - `DataType::List`
/// - `DataType::LargeList`
/// - `DataType::FixedSizeList`
///
/// For other datatypes, this function is a no-op.
pub fn with_list_element_name(field: Field, list_element_name: Option<&str>) -> Field {
    let Some(list_element_name) = list_element_name else {
        return field;
    };

    match field.data_type().clone() {
        DataType::List(child) => field.with_data_type(DataType::List(Arc::new(
            child.as_ref().clone().with_name(list_element_name),
        ))),
        DataType::LargeList(child) => field.with_data_type(DataType::LargeList(Arc::new(
            child.as_ref().clone().with_name(list_element_name),
        ))),
        DataType::FixedSizeList(child, size) => field.with_data_type(DataType::FixedSizeList(
            Arc::new(child.as_ref().clone().with_name(list_element_name)),
            size,
        )),
        _ => field,
    }
}

/// Adds or overrides metadata entries on a field.
pub fn with_field_metadata(mut field: Field, metadata: Vec<(String, String)>) -> Field {
    for (key, value) in metadata {
        field.metadata_mut().insert(key, value);
    }
    field
}

/// Adds or overrides metadata entries on the list-like element child field.
pub fn with_list_element_metadata(field: Field, metadata: Vec<(String, String)>) -> Field {
    if metadata.is_empty() {
        return field;
    }

    match field.data_type().clone() {
        DataType::List(child) => {
            let child = with_field_metadata(child.as_ref().clone(), metadata);
            field.with_data_type(DataType::List(Arc::new(child)))
        }
        DataType::LargeList(child) => {
            let child = with_field_metadata(child.as_ref().clone(), metadata);
            field.with_data_type(DataType::LargeList(Arc::new(child)))
        }
        DataType::FixedSizeList(child, size) => {
            let child = with_field_metadata(child.as_ref().clone(), metadata);
            field.with_data_type(DataType::FixedSizeList(Arc::new(child), size))
        }
        _ => field,
    }
}

/// Trait implemented by all types that can be used as an Arrow field.
///
/// Implementations are provided for types already supported by the arrow crate:
/// - numeric types: [`u8`], [`u16`], [`u32`], [`u64`], [`i8`], [`i16`], [`i32`], [`i128`], [`i64`], [`f32`], [`f64`],
/// - other types: [`bool`], [`String`]
/// - temporal types: [`chrono::NaiveDate`], [`chrono::NaiveDateTime`]
///
/// Custom implementations can be provided for other types.
///
/// The trait simply requires defining the [`ArrowField::data_type`]
///
/// Serialize and Deserialize functionality requires implementing the [`crate::ArrowSerialize`]
/// and the [`crate::ArrowDeserialize`] traits respectively.
pub trait ArrowField {
    /// This should be `Self` except when implementing large offset and fixed placeholder types.
    /// For the later, it should refer to the actual type. For example when the placeholder
    /// type is LargeString, this should be String.
    type Type;

    /// The [`DataType`]
    fn data_type() -> DataType;

    #[inline]
    #[doc(hidden)]
    /// For internal use and not meant to be reimplemented.
    /// returns the [`arrow::datatypes::Field`] for this field
    fn field(name: &str) -> Field {
        Field::new(name, Self::data_type(), Self::is_nullable())
    }

    #[inline]
    #[doc(hidden)]
    /// For internal use and not meant to be reimplemented.
    /// Indicates that this field is nullable. This is reimplemented by the
    /// Option<T> blanket implementation.
    fn is_nullable() -> bool {
        false
    }
}

/// Enables the blanket implementations of [`Vec<T>`] as an Arrow field
/// if `T` is an Arrow field.
///
/// This tag is needed for [`Vec<u8>`] specialization, and can be obviated
/// once implementation specialization is available in rust.
#[macro_export]
macro_rules! arrow_enable_vec_for_type {
    ($t:ty) => {
        impl $crate::field::ArrowEnableVecForType for $t {}
    };
}
/// Marker used to allow [`Vec<T>`] to be used as a [`ArrowField`].
#[doc(hidden)]
pub trait ArrowEnableVecForType {}

// Macro to facilitate implementation for numeric types.
macro_rules! impl_numeric_type {
    ($physical_type:ty, $logical_type:ident) => {
        impl ArrowField for $physical_type {
            type Type = $physical_type;

            #[inline]
            fn data_type() -> arrow_schema::DataType {
                arrow_schema::DataType::$logical_type
            }
        }
    };
}

macro_rules! impl_numeric_type_full {
    ($physical_type:ty, $logical_type:ident) => {
        impl_numeric_type!($physical_type, $logical_type);
        arrow_enable_vec_for_type!($physical_type);
    };
}

// blanket implementation for optional fields
impl<T> ArrowField for Option<T>
where
    T: ArrowField,
{
    type Type = Option<<T as ArrowField>::Type>;

    #[inline]
    fn data_type() -> arrow_schema::DataType {
        <T as ArrowField>::data_type()
    }

    #[inline]
    fn is_nullable() -> bool {
        true
    }
}

// u8 does not get the full implementation since Vec<u8> and [u8] are considered binary.
impl_numeric_type!(u8, UInt8);
impl_numeric_type_full!(u16, UInt16);
impl_numeric_type_full!(u32, UInt32);
impl_numeric_type_full!(u64, UInt64);
impl_numeric_type_full!(i8, Int8);
impl_numeric_type_full!(i16, Int16);
impl_numeric_type_full!(i32, Int32);
impl_numeric_type_full!(i64, Int64);
impl_numeric_type_full!(half::f16, Float16);
impl_numeric_type_full!(f32, Float32);
impl_numeric_type_full!(f64, Float64);

/// Maps a rust i128 to an Arrow Decimal where precision and scale are required.
pub struct I128<const PRECISION: u8, const SCALE: i8> {}

impl<const PRECISION: u8, const SCALE: i8> ArrowField for I128<PRECISION, SCALE> {
    type Type = i128;

    #[inline]
    fn data_type() -> DataType {
        DataType::Decimal128(PRECISION, SCALE)
    }
}

impl<'a> ArrowField for &'a str {
    type Type = &'a str;

    #[inline]
    fn data_type() -> DataType {
        DataType::Utf8
    }
}

impl ArrowField for String {
    type Type = Self;

    #[inline]
    fn data_type() -> DataType {
        DataType::Utf8
    }
}

/// Represents the `LargeUtf8` Arrow type
pub struct LargeString {}

impl ArrowField for LargeString {
    type Type = String;

    #[inline]
    fn data_type() -> DataType {
        DataType::LargeUtf8
    }
}

impl ArrowField for bool {
    type Type = Self;

    #[inline]
    fn data_type() -> DataType {
        DataType::Boolean
    }
}

impl ArrowField for NaiveDateTime {
    type Type = Self;

    #[inline]
    fn data_type() -> DataType {
        DataType::Timestamp(arrow_schema::TimeUnit::Nanosecond, None)
    }
}

impl ArrowField for DateTime<Utc> {
    type Type = Self;

    #[inline]
    fn data_type() -> DataType {
        DataType::Timestamp(arrow_schema::TimeUnit::Nanosecond, Some("UTC".into()))
    }
}

impl ArrowField for NaiveDate {
    type Type = Self;

    #[inline]
    fn data_type() -> DataType {
        DataType::Date32
    }
}

// Treat both Buffer and ScalarBuffer<u8> the same
impl ArrowField for Buffer {
    type Type = Self;

    #[inline]
    fn data_type() -> DataType {
        DataType::Binary
    }
}
impl ArrowField for ScalarBuffer<u8> {
    type Type = Self;

    #[inline]
    fn data_type() -> DataType {
        DataType::Binary
    }
}

impl ArrowField for Vec<u8> {
    type Type = Self;

    #[inline]
    fn data_type() -> DataType {
        DataType::Binary
    }
}

/// Represents the `LargeString` Arrow type.
pub struct LargeBinary {}

impl ArrowField for LargeBinary {
    type Type = Vec<u8>;

    #[inline]
    fn data_type() -> DataType {
        DataType::LargeBinary
    }
}

/// Represents the `FixedSizeBinary` Arrow type.
pub struct FixedSizeBinary<const SIZE: i32> {}

impl<const SIZE: i32> ArrowField for FixedSizeBinary<SIZE> {
    type Type = Vec<u8>;

    #[inline]
    fn data_type() -> DataType {
        DataType::FixedSizeBinary(SIZE)
    }
}

impl<const SIZE: usize> ArrowField for [u8; SIZE] {
    type Type = Self;

    #[inline]
    fn data_type() -> DataType {
        DataType::FixedSizeBinary(SIZE as i32)
    }
}

// Blanket implementation for Buffer
impl<T> ArrowField for ScalarBuffer<T>
where
    T: ArrowField + ArrowNativeType + ArrowEnableVecForType,
{
    type Type = Self;

    #[inline]
    fn data_type() -> DataType {
        DataType::List(Arc::new(<T as ArrowField>::field(DEFAULT_FIELD_NAME)))
    }
}

// Blanket implementation for Vec.
impl<T> ArrowField for Vec<T>
where
    T: ArrowField + ArrowEnableVecForType,
{
    type Type = Vec<<T as ArrowField>::Type>;

    #[inline]
    fn data_type() -> DataType {
        DataType::List(Arc::new(<T as ArrowField>::field(DEFAULT_FIELD_NAME)))
    }
}

/// Represents the `LargeList` Arrow type.
pub struct LargeVec<T> {
    d: std::marker::PhantomData<T>,
}

impl<T> ArrowField for LargeVec<T>
where
    T: ArrowField + ArrowEnableVecForType,
{
    type Type = Vec<<T as ArrowField>::Type>;

    #[inline]
    fn data_type() -> DataType {
        DataType::LargeList(Arc::new(<T as ArrowField>::field(DEFAULT_FIELD_NAME)))
    }
}

/// Represents the `FixedSizeList` Arrow type.
pub struct FixedSizeVec<T, const SIZE: i32> {
    d: std::marker::PhantomData<T>,
}

impl<T, const SIZE: i32> ArrowField for FixedSizeVec<T, SIZE>
where
    T: ArrowField + ArrowEnableVecForType,
{
    type Type = Vec<<T as ArrowField>::Type>;

    #[inline]
    fn data_type() -> DataType {
        let field = <T as ArrowField>::field(DEFAULT_FIELD_NAME);
        DataType::FixedSizeList(Arc::new(field), SIZE)
    }
}

impl<T, const SIZE: usize> ArrowField for [T; SIZE]
where
    T: ArrowField + ArrowEnableVecForType,
{
    type Type = [<T as ArrowField>::Type; SIZE];

    #[inline]
    fn data_type() -> DataType {
        let field = <T as ArrowField>::field(DEFAULT_FIELD_NAME);
        DataType::FixedSizeList(Arc::new(field), SIZE as i32)
    }
}

arrow_enable_vec_for_type!(String);
arrow_enable_vec_for_type!(LargeString);
arrow_enable_vec_for_type!(bool);
arrow_enable_vec_for_type!(NaiveDateTime);
arrow_enable_vec_for_type!(DateTime<Utc>);
arrow_enable_vec_for_type!(NaiveDate);
arrow_enable_vec_for_type!(Vec<u8>);
arrow_enable_vec_for_type!(Buffer);
arrow_enable_vec_for_type!(ScalarBuffer<u8>);
arrow_enable_vec_for_type!(LargeBinary);
impl<const SIZE: i32> ArrowEnableVecForType for FixedSizeBinary<SIZE> {}
impl<const PRECISION: u8, const SCALE: i8> ArrowEnableVecForType for I128<PRECISION, SCALE> {}

// Blanket implementation for Vec<Option<T>> if vectors are enabled for T
impl<T> ArrowEnableVecForType for Option<T> where T: ArrowField + ArrowEnableVecForType {}

// Blanket implementation for Vec<Vec<T>> and Vec<Buffer<T>> if vectors or buffers are enabled for T
impl<T> ArrowEnableVecForType for Vec<T> where T: ArrowField + ArrowEnableVecForType {}
impl<T> ArrowEnableVecForType for ScalarBuffer<T> where T: ArrowField + ArrowEnableVecForType + ArrowNativeType {}
impl<T> ArrowEnableVecForType for LargeVec<T> where T: ArrowField + ArrowEnableVecForType {}
impl<T, const SIZE: i32> ArrowEnableVecForType for FixedSizeVec<T, SIZE> where T: ArrowField + ArrowEnableVecForType {}