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
//! The Arrow registry keeps track of all type definitions and maps them to Arrow datatypes.

use anyhow::Context as _;
use arrow2::datatypes::{DataType, Field, UnionMode};
use std::{
    collections::{BTreeMap, HashMap},
    sync::Arc,
};

use crate::{ElementType, Object, ObjectField, Type, ATTR_ARROW_SPARSE_UNION};

// --- Registry ---

/// Computes and maintains a registry of [`arrow2::datatypes::DataType`]s for specified flatbuffers
/// definitions.
#[derive(Debug, Default)]
pub struct ArrowRegistry {
    registry: HashMap<String, LazyDatatype>,
}

impl ArrowRegistry {
    /// Computes the Arrow datatype for the specified object and stores it in the registry, to be
    /// resolved later on.
    pub fn register(&mut self, obj: &mut Object) {
        let (fqname, datatype) = (obj.fqname.clone(), self.arrow_datatype_from_object(obj));
        self.registry.insert(fqname, datatype);
    }

    /// Retrieves the [`arrow2::datatypes::DataType`] associated with the given fully-qualified
    /// name, if any.
    ///
    /// This does type resolution just-in-time.
    pub fn try_get(&self, fqname: impl AsRef<str>) -> Option<DataType> {
        self.registry
            .get(fqname.as_ref())
            .map(|dt| dt.resolve(self))
    }

    /// Retrieves the [`arrow2::datatypes::DataType`] associated with the given fully-qualified
    /// name.
    ///
    /// Panics if missing.
    ///
    /// This does type resolution just-in-time.
    pub fn get(&self, fqname: impl AsRef<str>) -> DataType {
        let fqname = fqname.as_ref();
        self.try_get(fqname)
            .with_context(|| format!("{fqname:?} not found in Arrow registry"))
            .unwrap()
    }

    // ---

    fn arrow_datatype_from_object(&mut self, obj: &mut Object) -> LazyDatatype {
        let is_struct = obj.is_struct();
        let is_arrow_transparent = obj.is_arrow_transparent();
        let num_fields = obj.fields.len();

        if is_arrow_transparent {
            assert!(
                is_struct,
                "{}: arrow-transparent objects must be structs; {:?} is {:?}",
                obj.virtpath, obj.fqname, obj.class
            );
            assert!(
                num_fields == 1,
                "{}: arrow-transparent structs must have exactly one field, but {:?} has {num_fields}",
                obj.virtpath, obj.fqname,
            );
        }

        let datatype = if is_arrow_transparent {
            LazyDatatype::Extension(
                obj.fqname.clone(),
                Box::new(
                    self.arrow_datatype_from_type(obj.fields[0].typ.clone(), &mut obj.fields[0]),
                ),
                None,
            )
        } else if is_struct {
            LazyDatatype::Extension(
                obj.fqname.clone(),
                Box::new(LazyDatatype::Struct(
                    obj.fields
                        .iter_mut()
                        .map(|obj_field| LazyField {
                            name: obj_field.name.clone(),
                            datatype: self
                                .arrow_datatype_from_type(obj_field.typ.clone(), obj_field),
                            is_nullable: obj_field.is_nullable,
                            metadata: Default::default(),
                        })
                        .collect(),
                )),
                None,
            )
        } else {
            let is_sparse = obj.is_enum() || obj.is_attr_set(ATTR_ARROW_SPARSE_UNION);
            let union_mode = if is_sparse {
                arrow2::datatypes::UnionMode::Sparse
            } else {
                arrow2::datatypes::UnionMode::Dense
            };

            // NOTE: Inject the null markers' field first and foremost! That way it is
            // guaranteed to be stable and forward-compatible.
            let fields = std::iter::once(LazyField {
                name: "_null_markers".into(),
                datatype: LazyDatatype::Null,
                // NOTE: The spec doesn't allow a `Null` array to be non-nullable. Not that
                // we care either way.
                is_nullable: true,
                metadata: Default::default(),
            })
            .chain(obj.fields.iter_mut().map(|field| LazyField {
                name: field.name.clone(),
                datatype: self.arrow_datatype_from_type(field.typ.clone(), field),
                // NOTE: The spec doesn't allow a `Null` array to be non-nullable.
                // We map Unit -> Null in enum fields, so this must be nullable.
                is_nullable: field.typ == Type::Unit,
                metadata: Default::default(),
            }))
            .collect();

            LazyDatatype::Extension(
                obj.fqname.clone(),
                Box::new(LazyDatatype::Union(
                    fields,
                    // NOTE: +1 to account for virtual nullability arm
                    Some((0..(obj.fields.len() + 1) as i32).collect()),
                    union_mode,
                )),
                None,
            )
        };

        // NOTE: Arrow-transparent objects by definition don't have a datatype of their own.
        if !is_arrow_transparent {
            obj.datatype = datatype.clone().into();
        }

        datatype
    }

    fn arrow_datatype_from_type(&mut self, typ: Type, field: &mut ObjectField) -> LazyDatatype {
        let datatype = match typ {
            Type::Unit => LazyDatatype::Null,
            Type::UInt8 => LazyDatatype::UInt8,
            Type::UInt16 => LazyDatatype::UInt16,
            Type::UInt32 => LazyDatatype::UInt32,
            Type::UInt64 => LazyDatatype::UInt64,
            Type::Int8 => LazyDatatype::Int8,
            Type::Int16 => LazyDatatype::Int16,
            Type::Int32 => LazyDatatype::Int32,
            Type::Int64 => LazyDatatype::Int64,
            Type::Bool => LazyDatatype::Boolean,
            Type::Float16 => LazyDatatype::Float16,
            Type::Float32 => LazyDatatype::Float32,
            Type::Float64 => LazyDatatype::Float64,
            Type::String => LazyDatatype::Utf8,
            Type::Array { elem_type, length } => LazyDatatype::FixedSizeList(
                Box::new(LazyField {
                    name: "item".into(),
                    datatype: self.arrow_datatype_from_element_type(elem_type),
                    // NOTE: Do _not_ confuse this with the nullability of the field itself!
                    // This would be the nullability of the elements of the list itself, which our IDL
                    // literally is unable to express at the moment, so you can be certain this is
                    // always false.
                    is_nullable: false,
                    metadata: Default::default(),
                }),
                length,
            ),
            Type::Vector { elem_type } => LazyDatatype::List(Box::new(LazyField {
                name: "item".into(),
                datatype: self.arrow_datatype_from_element_type(elem_type),
                // NOTE: Do _not_ confuse this with the nullability of the field itself!
                // This would be the nullability of the elements of the list itself, which our IDL
                // literally is unable to express at the moment, so you can be certain this is
                // always false.
                is_nullable: false,
                metadata: Default::default(),
            })),
            Type::Object(fqname) => LazyDatatype::Unresolved(fqname),
        };

        field.datatype = datatype.clone().into();
        self.registry.insert(field.fqname.clone(), datatype.clone());

        datatype
    }

    fn arrow_datatype_from_element_type(&self, typ: ElementType) -> LazyDatatype {
        _ = self;
        match typ {
            ElementType::UInt8 => LazyDatatype::UInt8,
            ElementType::UInt16 => LazyDatatype::UInt16,
            ElementType::UInt32 => LazyDatatype::UInt32,
            ElementType::UInt64 => LazyDatatype::UInt64,
            ElementType::Int8 => LazyDatatype::Int8,
            ElementType::Int16 => LazyDatatype::Int16,
            ElementType::Int32 => LazyDatatype::Int32,
            ElementType::Int64 => LazyDatatype::Int64,
            ElementType::Bool => LazyDatatype::Boolean,
            ElementType::Float16 => LazyDatatype::Float16,
            ElementType::Float32 => LazyDatatype::Float32,
            ElementType::Float64 => LazyDatatype::Float64,
            ElementType::String => LazyDatatype::Utf8,
            ElementType::Object(fqname) => LazyDatatype::Unresolved(fqname),
        }
    }
}

// --- Field ---

/// A yet-to-be-resolved [`arrow2::datatypes::Field`].
///
/// Type resolution is a two-pass process as we first need to register all existing types before we
/// can denormalize their definitions into their parents.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct LazyField {
    /// Its name
    pub name: String,

    /// Its logical [`DataType`]
    pub datatype: LazyDatatype,

    /// Its nullability
    pub is_nullable: bool,

    /// Additional custom (opaque) metadata.
    pub metadata: BTreeMap<String, String>,
}

impl From<Field> for LazyField {
    fn from(field: Field) -> Self {
        let Field {
            name,
            data_type,
            is_nullable,
            metadata,
        } = field;

        Self {
            name,
            datatype: data_type.into(),
            is_nullable,
            metadata,
        }
    }
}

impl LazyField {
    /// Recursively resolves the field using the specified `registry`.
    fn resolve(&self, registry: &ArrowRegistry) -> Field {
        Field {
            name: self.name.clone(),
            data_type: self.datatype.resolve(registry),
            is_nullable: self.is_nullable,
            metadata: self.metadata.clone(),
        }
    }
}

// --- Datatype ---

/// A yet-to-be-resolved [`arrow2::datatypes::DataType`].
///
/// Type resolution is a two-pass process as we first need to register all existing types before we
/// can denormalize their definitions into their parents.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum LazyDatatype {
    Null,
    Boolean,
    Int8,
    Int16,
    Int32,
    Int64,
    UInt8,
    UInt16,
    UInt32,
    UInt64,
    Float16,
    Float32,
    Float64,
    Binary,
    FixedSizeBinary(usize),
    LargeBinary,
    Utf8,
    LargeUtf8,
    List(Box<LazyField>),
    FixedSizeList(Box<LazyField>, usize),
    LargeList(Box<LazyField>),
    Struct(Vec<LazyField>),
    Union(Vec<LazyField>, Option<Vec<i32>>, UnionMode),
    Extension(String, Box<LazyDatatype>, Option<String>),
    Unresolved(String), // fqname
}

impl From<DataType> for LazyDatatype {
    fn from(datatype: DataType) -> Self {
        match datatype {
            DataType::Null => LazyDatatype::Null,
            DataType::Boolean => LazyDatatype::Boolean,
            DataType::Int8 => LazyDatatype::Int8,
            DataType::Int16 => LazyDatatype::Int16,
            DataType::Int32 => LazyDatatype::Int32,
            DataType::Int64 => LazyDatatype::Int64,
            DataType::UInt8 => LazyDatatype::UInt8,
            DataType::UInt16 => LazyDatatype::UInt16,
            DataType::UInt32 => LazyDatatype::UInt32,
            DataType::UInt64 => LazyDatatype::UInt64,
            DataType::Float16 => LazyDatatype::Float16,
            DataType::Float32 => LazyDatatype::Float32,
            DataType::Float64 => LazyDatatype::Float64,
            DataType::Binary => LazyDatatype::Binary,
            DataType::FixedSizeBinary(length) => LazyDatatype::FixedSizeBinary(length),
            DataType::LargeBinary => LazyDatatype::LargeBinary,
            DataType::Utf8 => LazyDatatype::Utf8,
            DataType::LargeUtf8 => LazyDatatype::LargeUtf8,
            DataType::List(field) => LazyDatatype::List(Box::new((*field).clone().into())),
            DataType::FixedSizeList(field, length) => {
                LazyDatatype::FixedSizeList(Box::new((*field).clone().into()), length)
            }
            DataType::LargeList(field) => {
                LazyDatatype::LargeList(Box::new((*field).clone().into()))
            }
            DataType::Struct(fields) => {
                LazyDatatype::Struct(fields.iter().cloned().map(Into::into).collect())
            }
            DataType::Union(fields, x, mode) => LazyDatatype::Union(
                fields.iter().cloned().map(Into::into).collect(),
                x.map(|arc| arc.to_vec()),
                mode,
            ),
            DataType::Extension(name, datatype, metadata) => LazyDatatype::Extension(
                name,
                Box::new((*datatype).clone().into()),
                metadata.map(|arc| arc.to_string()),
            ),
            _ => unimplemented!("{datatype:#?}"),
        }
    }
}

impl LazyDatatype {
    /// Recursively resolves the datatype using the specified `registry`.
    fn resolve(&self, registry: &ArrowRegistry) -> DataType {
        match self {
            LazyDatatype::Null => DataType::Null,
            LazyDatatype::Boolean => DataType::Boolean,
            LazyDatatype::Int8 => DataType::Int8,
            LazyDatatype::Int16 => DataType::Int16,
            LazyDatatype::Int32 => DataType::Int32,
            LazyDatatype::Int64 => DataType::Int64,
            LazyDatatype::UInt8 => DataType::UInt8,
            LazyDatatype::UInt16 => DataType::UInt16,
            LazyDatatype::UInt32 => DataType::UInt32,
            LazyDatatype::UInt64 => DataType::UInt64,
            LazyDatatype::Float16 => DataType::Float16,
            LazyDatatype::Float32 => DataType::Float32,
            LazyDatatype::Float64 => DataType::Float64,
            LazyDatatype::Binary => DataType::Binary,
            LazyDatatype::FixedSizeBinary(length) => DataType::FixedSizeBinary(*length),
            LazyDatatype::LargeBinary => DataType::LargeBinary,
            LazyDatatype::Utf8 => DataType::Utf8,
            LazyDatatype::LargeUtf8 => DataType::LargeUtf8,
            LazyDatatype::List(field) => DataType::List(Arc::new(field.resolve(registry))),
            LazyDatatype::FixedSizeList(field, length) => {
                DataType::FixedSizeList(Arc::new(field.resolve(registry)), *length)
            }
            LazyDatatype::LargeList(field) => {
                DataType::LargeList(Arc::new(field.resolve(registry)))
            }
            LazyDatatype::Struct(fields) => DataType::Struct(Arc::new(
                fields.iter().map(|field| field.resolve(registry)).collect(),
            )),
            LazyDatatype::Union(fields, x, mode) => DataType::Union(
                Arc::new(fields.iter().map(|field| field.resolve(registry)).collect()),
                x.as_ref().map(|x| Arc::new(x.clone())),
                *mode,
            ),
            LazyDatatype::Extension(name, datatype, metadata) => DataType::Extension(
                name.clone(),
                Arc::new(datatype.resolve(registry)),
                metadata.as_ref().map(|s| Arc::new(s.clone())),
            ),
            LazyDatatype::Unresolved(fqname) => registry.get(fqname),
        }
    }
}