typed-arrow 0.7.0

Compile-time Arrow schemas for Rust.
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Dictionary-encoded column bindings and key mapping.

use std::marker::PhantomData;

use arrow_array::{
    builder::{
        BinaryDictionaryBuilder, FixedSizeBinaryDictionaryBuilder, LargeBinaryDictionaryBuilder,
        LargeStringDictionaryBuilder, PrimitiveDictionaryBuilder, StringDictionaryBuilder,
    },
    types::{
        Float32Type, Float64Type, Int8Type, Int16Type, Int32Type, Int64Type, UInt8Type, UInt16Type,
        UInt32Type, UInt64Type,
    },
};
use arrow_schema::DataType;

use super::{ArrowBinding, binary::LargeBinary, strings::LargeUtf8};

/// Wrapper denoting an Arrow Dictionary column with key type `K` and values of `V`.
///
/// The inner value is intentionally not exposed. Construct with `Dictionary::new`
/// and access the contained value via `Dictionary::value` or `Dictionary::into_value`.
///
/// This prevents accidental reliance on representation details (e.g., raw keys) and
/// keeps the API focused on appending logical values. The builder handles interning to keys.
#[derive(Debug, Clone, PartialEq)]
#[repr(transparent)]
pub struct Dictionary<K, V>(V, PhantomData<K>);

impl<K, V> Dictionary<K, V> {
    /// Create a new dictionary value wrapper.
    #[inline]
    pub fn new(value: V) -> Self {
        Self(value, PhantomData)
    }

    /// Borrow the contained logical value.
    #[inline]
    pub fn value(&self) -> &V {
        &self.0
    }

    /// Consume and return the contained logical value.
    #[inline]
    pub fn into_value(self) -> V {
        self.0
    }
}

// Serialize/Deserialize implementation is transparent: forwards to that for V.
#[cfg(feature = "serde")]
impl<'de, K, V> serde::de::Deserialize<'de> for Dictionary<K, V>
where
    V: serde::de::Deserialize<'de>,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        Ok(Self(V::deserialize(deserializer)?, PhantomData))
    }
}

#[cfg(feature = "serde")]
impl<K, V> serde::Serialize for Dictionary<K, V>
where
    V: serde::Serialize,
{
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.0.serialize(serializer)
    }
}

impl<K, V> From<V> for Dictionary<K, V> {
    #[inline]
    fn from(value: V) -> Self {
        Self::new(value)
    }
}

/// Dictionary key mapping from Rust integer to Arrow key type.
pub trait DictKey {
    /// Arrow key type corresponding to this Rust integer key.
    type ArrowKey;

    /// The Arrow `DataType` for the key.
    fn data_type() -> DataType;
}

macro_rules! impl_dict_key {
    ($rust:ty, $arrow:ty, $dt:expr) => {
        impl DictKey for $rust {
            type ArrowKey = $arrow;
            fn data_type() -> DataType {
                $dt
            }
        }
    };
}

impl_dict_key!(i8, Int8Type, DataType::Int8);
impl_dict_key!(i16, Int16Type, DataType::Int16);
impl_dict_key!(i32, Int32Type, DataType::Int32);
impl_dict_key!(i64, Int64Type, DataType::Int64);
impl_dict_key!(u8, UInt8Type, DataType::UInt8);
impl_dict_key!(u16, UInt16Type, DataType::UInt16);
impl_dict_key!(u32, UInt32Type, DataType::UInt32);
impl_dict_key!(u64, UInt64Type, DataType::UInt64);

// Utf8 values
impl<K> ArrowBinding for Dictionary<K, String>
where
    K: DictKey,
    <K as DictKey>::ArrowKey: arrow_array::types::ArrowDictionaryKeyType,
{
    type Builder = StringDictionaryBuilder<<K as DictKey>::ArrowKey>;
    type Array = arrow_array::DictionaryArray<<K as DictKey>::ArrowKey>;
    fn data_type() -> DataType {
        DataType::Dictionary(
            Box::new(<K as DictKey>::data_type()),
            Box::new(DataType::Utf8),
        )
    }
    fn new_builder(_capacity: usize) -> Self::Builder {
        StringDictionaryBuilder::new()
    }
    fn append_value(b: &mut Self::Builder, v: &Self) {
        let _ = b.append(v.value().as_str());
    }
    fn append_null(b: &mut Self::Builder) {
        b.append_null();
    }
    fn finish(mut b: Self::Builder) -> Self::Array {
        b.finish()
    }
}

// Binary values
impl<K> ArrowBinding for Dictionary<K, Vec<u8>>
where
    K: DictKey,
    <K as DictKey>::ArrowKey: arrow_array::types::ArrowDictionaryKeyType,
{
    type Builder = BinaryDictionaryBuilder<<K as DictKey>::ArrowKey>;
    type Array = arrow_array::DictionaryArray<<K as DictKey>::ArrowKey>;
    fn data_type() -> DataType {
        DataType::Dictionary(
            Box::new(<K as DictKey>::data_type()),
            Box::new(DataType::Binary),
        )
    }
    fn new_builder(_capacity: usize) -> Self::Builder {
        BinaryDictionaryBuilder::new()
    }
    fn append_value(b: &mut Self::Builder, v: &Self) {
        let _ = b.append(v.value().as_slice());
    }
    fn append_null(b: &mut Self::Builder) {
        b.append_null();
    }
    fn finish(mut b: Self::Builder) -> Self::Array {
        b.finish()
    }
}

// FixedSizeBinary values: [u8; N]
impl<K, const N: usize> ArrowBinding for Dictionary<K, [u8; N]>
where
    K: DictKey,
    <K as DictKey>::ArrowKey: arrow_array::types::ArrowDictionaryKeyType,
{
    type Builder = FixedSizeBinaryDictionaryBuilder<<K as DictKey>::ArrowKey>;
    type Array = arrow_array::DictionaryArray<<K as DictKey>::ArrowKey>;
    fn data_type() -> DataType {
        DataType::Dictionary(
            Box::new(<K as DictKey>::data_type()),
            Box::new(DataType::FixedSizeBinary(
                i32::try_from(N).expect("width fits i32"),
            )),
        )
    }
    fn new_builder(_capacity: usize) -> Self::Builder {
        // Builder enforces width on appended values; pass byte width
        FixedSizeBinaryDictionaryBuilder::new(i32::try_from(N).expect("width fits i32"))
    }
    fn append_value(b: &mut Self::Builder, v: &Self) {
        let _ = b.append(*v.value());
    }
    fn append_null(b: &mut Self::Builder) {
        b.append_null();
    }
    fn finish(mut b: Self::Builder) -> Self::Array {
        b.finish()
    }
}

// LargeBinary values
impl<K> ArrowBinding for Dictionary<K, LargeBinary>
where
    K: DictKey,
    <K as DictKey>::ArrowKey: arrow_array::types::ArrowDictionaryKeyType,
{
    type Builder = LargeBinaryDictionaryBuilder<<K as DictKey>::ArrowKey>;
    type Array = arrow_array::DictionaryArray<<K as DictKey>::ArrowKey>;
    fn data_type() -> DataType {
        DataType::Dictionary(
            Box::new(<K as DictKey>::data_type()),
            Box::new(DataType::LargeBinary),
        )
    }
    fn new_builder(_capacity: usize) -> Self::Builder {
        LargeBinaryDictionaryBuilder::new()
    }
    fn append_value(b: &mut Self::Builder, v: &Self) {
        let _ = b.append(v.value().as_slice());
    }
    fn append_null(b: &mut Self::Builder) {
        b.append_null();
    }
    fn finish(mut b: Self::Builder) -> Self::Array {
        b.finish()
    }
}

// LargeUtf8 values
impl<K> ArrowBinding for Dictionary<K, LargeUtf8>
where
    K: DictKey,
    <K as DictKey>::ArrowKey: arrow_array::types::ArrowDictionaryKeyType,
{
    type Builder = LargeStringDictionaryBuilder<<K as DictKey>::ArrowKey>;
    type Array = arrow_array::DictionaryArray<<K as DictKey>::ArrowKey>;
    fn data_type() -> DataType {
        DataType::Dictionary(
            Box::new(<K as DictKey>::data_type()),
            Box::new(DataType::LargeUtf8),
        )
    }
    fn new_builder(_capacity: usize) -> Self::Builder {
        LargeStringDictionaryBuilder::new()
    }
    fn append_value(b: &mut Self::Builder, v: &Self) {
        let _ = b.append(v.value().as_str());
    }
    fn append_null(b: &mut Self::Builder) {
        b.append_null();
    }
    fn finish(mut b: Self::Builder) -> Self::Array {
        b.finish()
    }
}

// Primitive values via macro
macro_rules! impl_dict_primitive_value {
    ($rust:ty, $atype:ty, $dt:expr) => {
        impl<K> ArrowBinding for Dictionary<K, $rust>
        where
            K: DictKey,
            <K as DictKey>::ArrowKey: arrow_array::types::ArrowDictionaryKeyType,
        {
            type Builder = PrimitiveDictionaryBuilder<<K as DictKey>::ArrowKey, $atype>;
            type Array = arrow_array::DictionaryArray<<K as DictKey>::ArrowKey>;
            fn data_type() -> DataType {
                DataType::Dictionary(Box::new(<K as DictKey>::data_type()), Box::new($dt))
            }
            fn new_builder(_capacity: usize) -> Self::Builder {
                PrimitiveDictionaryBuilder::<_, $atype>::new()
            }
            fn append_value(b: &mut Self::Builder, v: &Self) {
                let _ = b.append(*v.value());
            }
            fn append_null(b: &mut Self::Builder) {
                b.append_null();
            }
            fn finish(mut b: Self::Builder) -> Self::Array {
                b.finish()
            }
        }
    };
}

impl_dict_primitive_value!(i8, Int8Type, DataType::Int8);
impl_dict_primitive_value!(i16, Int16Type, DataType::Int16);
impl_dict_primitive_value!(i32, Int32Type, DataType::Int32);
impl_dict_primitive_value!(i64, Int64Type, DataType::Int64);
impl_dict_primitive_value!(u8, UInt8Type, DataType::UInt8);
impl_dict_primitive_value!(u16, UInt16Type, DataType::UInt16);
impl_dict_primitive_value!(u32, UInt32Type, DataType::UInt32);
impl_dict_primitive_value!(u64, UInt64Type, DataType::UInt64);
impl_dict_primitive_value!(f32, Float32Type, DataType::Float32);
impl_dict_primitive_value!(f64, Float64Type, DataType::Float64);

// ArrowBindingView implementation for Dictionary types
// Decodes the dictionary value at the given index
#[cfg(feature = "views")]
impl<K, V> super::ArrowBindingView for Dictionary<K, V>
where
    K: DictKey + 'static,
    V: ArrowBinding + super::ArrowBindingView + 'static,
    <K as DictKey>::ArrowKey: arrow_array::types::ArrowDictionaryKeyType,
{
    type Array = arrow_array::DictionaryArray<<K as DictKey>::ArrowKey>;
    type View<'a>
        = V::View<'a>
    where
        Self: 'a;

    fn get_view(
        array: &Self::Array,
        index: usize,
    ) -> Result<Self::View<'_>, crate::schema::ViewAccessError> {
        use arrow_array::Array;
        use arrow_buffer::ArrowNativeType;

        if index >= array.len() {
            return Err(crate::schema::ViewAccessError::OutOfBounds {
                index,
                len: array.len(),
                field_name: None,
            });
        }
        if array.is_null(index) {
            return Err(crate::schema::ViewAccessError::UnexpectedNull {
                index,
                field_name: None,
            });
        }

        // Get the key (dictionary index) for this row
        let keys = array.keys();
        let key_value = keys.value(index);
        let dict_index = key_value.as_usize();

        // Get the values array and downcast to the correct type
        let values_array = array.values();
        let typed_values = values_array
            .as_any()
            .downcast_ref::<<V as super::ArrowBindingView>::Array>()
            .ok_or_else(|| crate::schema::ViewAccessError::TypeMismatch {
                expected: V::data_type(),
                actual: values_array.data_type().clone(),
                field_name: None,
            })?;

        // Return a view of the decoded value
        V::get_view(typed_values, dict_index)
    }
}

// TryFrom implementations for converting views to owned Dictionary types
// Note: Dictionary<K, V> only stores V at runtime; K is a compile-time marker
// for the encoding strategy. The view is just V::View, so we convert from that.

// String (Utf8)
#[cfg(feature = "views")]
impl<K> TryFrom<&str> for Dictionary<K, String>
where
    K: DictKey,
{
    type Error = crate::schema::ViewAccessError;

    fn try_from(view: &str) -> Result<Self, Self::Error> {
        Ok(Dictionary::new(view.into()))
    }
}

// Binary
#[cfg(feature = "views")]
impl<K> TryFrom<&[u8]> for Dictionary<K, Vec<u8>>
where
    K: DictKey,
{
    type Error = crate::schema::ViewAccessError;

    fn try_from(view: &[u8]) -> Result<Self, Self::Error> {
        Ok(Dictionary::new(view.to_vec()))
    }
}

// FixedSizeBinary
#[cfg(feature = "views")]
impl<K, const N: usize> TryFrom<&[u8]> for Dictionary<K, [u8; N]>
where
    K: DictKey,
{
    type Error = crate::schema::ViewAccessError;

    fn try_from(view: &[u8]) -> Result<Self, Self::Error> {
        let arr: [u8; N] =
            view.try_into()
                .map_err(|_| crate::schema::ViewAccessError::TypeMismatch {
                    expected: arrow_schema::DataType::FixedSizeBinary(N as i32),
                    actual: arrow_schema::DataType::Binary,
                    field_name: None,
                })?;
        Ok(Dictionary::new(arr))
    }
}

// LargeBinary
#[cfg(feature = "views")]
impl<K> TryFrom<&[u8]> for Dictionary<K, super::binary::LargeBinary>
where
    K: DictKey,
{
    type Error = crate::schema::ViewAccessError;

    fn try_from(view: &[u8]) -> Result<Self, Self::Error> {
        Ok(Dictionary::new(super::binary::LargeBinary::new(
            view.to_vec(),
        )))
    }
}

// LargeUtf8
#[cfg(feature = "views")]
impl<K> TryFrom<&str> for Dictionary<K, super::strings::LargeUtf8>
where
    K: DictKey,
{
    type Error = crate::schema::ViewAccessError;

    fn try_from(view: &str) -> Result<Self, Self::Error> {
        Ok(Dictionary::new(super::strings::LargeUtf8::new(
            view.to_string(),
        )))
    }
}

// Note: Primitive types (i8, i16, i32, i64, u8, u16, u32, u64, f32, f64) are already
// covered by the generic impl From<V> for Dictionary<K, V> above (line 49).
// Rust automatically provides TryFrom<V> with Error = Infallible via the blanket impl,
// which works with our E: Into<ViewAccessError> bounds in generic code.