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
use substrait::proto::{
    expression::{reference_segment::ReferenceType, ReferenceSegment},
    r#type::Struct,
    NamedStruct, Type,
};

use crate::{
    error::{Result, SubstraitExprError},
    util::HasRequiredPropertiesRef,
};

use super::{
    registry::ExtensionsRegistry,
    types::{self, nullability, TypeExt},
};

/// A schema that does not know types or names
///
/// This is also the only schema type that does not know
/// how many fields there are.
#[derive(Debug, Default, PartialEq)]
pub struct EmptySchema {
    registry: ExtensionsRegistry,
}

/// A field in a names-only schema
#[derive(PartialEq, Debug)]
pub struct NamesOnlySchemaNode {
    /// The name of this node
    ///
    /// This will be the empty string for the root node
    pub name: String,
    /// The names of this node's children
    pub children: Vec<NamesOnlySchemaNode>,
}

/// Represents a potentially nested schema where we only know the names and not
/// the types of the fields.
///
/// The root of the schema is `Vec<NamesOnlySchemaNode>` where each node represents
/// a single (possibly nested) column
#[derive(PartialEq, Debug)]
pub struct NamesOnlySchema {
    registry: ExtensionsRegistry,
    /// The root node of the schema
    pub root: NamesOnlySchemaNode,
}

impl NamesOnlySchema {
    /// Create a new names-only schema
    pub fn new(root_nodes: Vec<NamesOnlySchemaNode>) -> Self {
        Self {
            root: NamesOnlySchemaNode {
                name: String::new(),
                children: root_nodes,
            },
            registry: ExtensionsRegistry::default(),
        }
    }

    /// Create a names-only schema with the given registry
    pub fn new_with_registry(
        root_nodes: Vec<NamesOnlySchemaNode>,
        registry: ExtensionsRegistry,
    ) -> Self {
        Self {
            root: NamesOnlySchemaNode {
                name: String::new(),
                children: root_nodes,
            },
            registry,
        }
    }
}

impl NamesOnlySchemaNode {
    /// Determines the type of a scheam node
    ///
    /// Since we don't know types this will typically be unknown.  However,
    /// for nested fields, we know they must be of the Struct type.
    fn as_type(&self, unknown_type: &Type) -> Type {
        if self.children.is_empty() {
            unknown_type.clone()
        } else {
            types::struct_(
                true,
                self.children
                    .iter()
                    .map(|child| child.as_type(unknown_type))
                    .collect::<Vec<_>>(),
            )
        }
    }
}

struct NamesOnlySchemaNodeNamesDfsIter<'a> {
    stack: Vec<&'a NamesOnlySchemaNode>,
}

impl<'a> Iterator for NamesOnlySchemaNodeNamesDfsIter<'a> {
    type Item = &'a str;

    fn next(&mut self) -> Option<Self::Item> {
        let next = self.stack.pop();
        if let Some(next) = next {
            self.stack.extend(next.children.iter().rev());
            Some(&next.name)
        } else {
            None
        }
    }
}

/// A schema that knows the types (but not names) of its fields
#[derive(Debug, PartialEq)]
pub struct TypesOnlySchema {
    registry: ExtensionsRegistry,
    /// The root node of the schema
    pub root: Struct,
}

impl TypesOnlySchema {
    /// Create a new types-only schema
    pub fn new(root: Struct) -> Self {
        Self {
            root,
            registry: ExtensionsRegistry::default(),
        }
    }

    /// Create a types-only schema with a given registry
    pub fn new_with_registry(root: Struct, registry: ExtensionsRegistry) -> Self {
        Self { root, registry }
    }
}

/// A field in a schema that knows both types and names
#[derive(Debug, PartialEq)]
pub struct FullSchemaNode {
    /// The name of the field
    ///
    /// This will be the empty string for the root node
    pub name: String,
    /// The type of the field
    pub r#type: Type,
    /// The child types
    pub children: Vec<FullSchemaNode>,
}

/// A schema that knows both the types and names of its fields
#[derive(Debug, PartialEq)]
pub struct FullSchema {
    registry: ExtensionsRegistry,
    /// The root node of the schema
    pub root: FullSchemaNode,
}

impl FullSchema {
    /// Create a new full schema
    pub fn new(root: FullSchemaNode) -> Self {
        Self {
            root,
            registry: ExtensionsRegistry::default(),
        }
    }

    /// Create a full schema with the given registry
    pub fn new_with_registry(root: FullSchemaNode, registry: ExtensionsRegistry) -> Self {
        Self { root, registry }
    }
}

/// A schema represents what we know about the input to an expression
///
/// TODO: Expand, copy over content from crate docs
#[derive(PartialEq, Debug)]
pub enum SchemaInfo {
    Empty(EmptySchema),
    Names(NamesOnlySchema),
    Types(TypesOnlySchema),
    Full(FullSchema),
}

struct TypesOnlySchemaTypesDfsIter<'a> {
    stack: Vec<&'a Type>,
    include_inner: bool,
}

impl<'a> Iterator for TypesOnlySchemaTypesDfsIter<'a> {
    type Item = &'a Type;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let next = self.stack.pop();
            if let Some(next) = next {
                let children = next.children();
                self.stack.extend(children.iter().rev());
                if self.include_inner || children.is_empty() {
                    return Some(next);
                }
            } else {
                return None;
            }
        }
    }
}

struct FullSchemaFieldsDfsIter<'a> {
    stack: Vec<&'a FullSchemaNode>,
    include_inner: bool,
}

impl<'a> Iterator for FullSchemaFieldsDfsIter<'a> {
    type Item = &'a FullSchemaNode;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let next = self.stack.pop();
            if let Some(next) = next {
                let children = &next.children;
                self.stack.extend(children.iter().rev());
                if self.include_inner || children.is_empty() {
                    return Some(next);
                }
            } else {
                return None;
            }
        }
    }
}

impl SchemaInfo {
    /// Return a reference to the schema's extensions registry
    ///
    /// This registry keeps track of the user defined types
    /// that are referenced by the schema
    pub fn extensions_registry(&self) -> &ExtensionsRegistry {
        match self {
            SchemaInfo::Empty(schm) => &schm.registry,
            SchemaInfo::Names(schm) => &schm.registry,
            SchemaInfo::Types(schm) => &schm.registry,
            SchemaInfo::Full(schm) => &schm.registry,
        }
    }

    /// Return true if this schema knows the names of its fields
    pub fn names_aware(&self) -> bool {
        match self {
            SchemaInfo::Empty(_) => false,
            SchemaInfo::Names(_) => true,
            SchemaInfo::Types(_) => false,
            SchemaInfo::Full(_) => true,
        }
    }

    /// Return true if this schema knows the types of its fields
    pub fn types_aware(&self) -> bool {
        match self {
            SchemaInfo::Empty(_) => false,
            SchemaInfo::Names(_) => false,
            SchemaInfo::Types(_) => true,
            SchemaInfo::Full(_) => true,
        }
    }

    /// Return true if this schema knows the number of fields
    pub fn len_aware(&self) -> bool {
        match self {
            SchemaInfo::Empty(_) => false,
            SchemaInfo::Names(_) => true,
            SchemaInfo::Types(_) => true,
            SchemaInfo::Full(_) => true,
        }
    }

    /// Returns an iterator through the names of the fields, in DFS order
    ///
    /// Returns an error if the schema does not know the names of its fields
    pub fn names_dfs<'a>(&'a self) -> Result<Box<dyn Iterator<Item = &str> + 'a>> {
        match self {
            SchemaInfo::Empty(_) => Err(SubstraitExprError::invalid_input(
                "Attempt to access field names when the schema is not name-aware",
            )),
            SchemaInfo::Names(names) => Ok(Box::new(NamesOnlySchemaNodeNamesDfsIter {
                stack: Vec::from_iter(names.root.children.iter()),
            })),
            SchemaInfo::Types(_) => Err(SubstraitExprError::invalid_input(
                "Attempt to access field names when the schema is not name-aware",
            )),
            SchemaInfo::Full(full) => Ok(Box::new(
                FullSchemaFieldsDfsIter {
                    stack: Vec::from_iter(full.root.children.iter().rev()),
                    include_inner: true,
                }
                .map(|node| node.name.as_str()),
            )),
        }
    }

    /// Returns an iterator through the types of the fields, in DFS order
    ///
    /// If the schema is empty this will return an empty iterator
    /// If this schema is names-only the types will all be the unknown type
    ///
    /// TODO: Explain include_inner, provide examples
    pub fn types_dfs<'a>(&'a self, include_inner: bool) -> Box<dyn Iterator<Item = Type> + 'a> {
        match self {
            SchemaInfo::Empty(_) => Box::new(std::iter::empty()),
            // TODO: SchemaInfo::Names is flat, SchemaInfo::Types is not.  Resolve this difference
            SchemaInfo::Names(names) => {
                let unknown_type = crate::builder::types::unknown(&names.registry);
                Box::new(
                    names
                        .root
                        .children
                        .iter()
                        .map(move |child| child.as_type(&unknown_type)),
                )
            }
            SchemaInfo::Types(type_info) => Box::new(
                TypesOnlySchemaTypesDfsIter {
                    stack: Vec::from_iter(type_info.root.types.iter().rev()),
                    include_inner,
                }
                .cloned(),
            ),
            SchemaInfo::Full(full) => Box::new(
                FullSchemaFieldsDfsIter {
                    stack: Vec::from_iter(full.root.children.iter().rev()),
                    include_inner,
                }
                .map(|node| node.r#type.clone()),
            ),
        }
    }

    /// Converts to a NamedStruct which is the closest equivalent SubstraitMessage
    pub fn to_substrait(self) -> NamedStruct {
        // TODO: Should include_inner be true here?
        let types = self.types_dfs(false).collect::<Vec<_>>();
        let names = if self.names_aware() {
            self.names_dfs()
                .unwrap()
                .map(|name| name.to_string())
                .collect::<Vec<_>>()
        } else {
            types
                .iter()
                .enumerate()
                .map(|(idx, _)| format!("field_{}", idx))
                .collect::<Vec<_>>()
        };
        NamedStruct {
            names,
            r#struct: Some(Struct {
                nullability: nullability(false),
                types,
                ..Default::default()
            }),
        }
    }

    /// Return the type of the field referenced by `ref_seg`
    ///
    /// Returns an error if the reference does not refer to a field in the schema
    ///
    /// If types are not known then the returned type will be the unknown type
    pub fn resolve_type(&self, ref_seg: &ReferenceSegment) -> Result<Type> {
        match self {
            SchemaInfo::Empty(empty) => Ok(crate::builder::types::unknown(&empty.registry)),
            // TODO: Make sure a field exists before returning unknown
            SchemaInfo::Names(names) => Ok(crate::builder::types::unknown(&names.registry)),
            SchemaInfo::Types(type_info) => {
                let mut cur = &type_info.root.types;
                let mut _owned_cur = Vec::new();
                loop {
                    match ref_seg.reference_type.required("reference_type")? {
                        ReferenceType::StructField(struct_field) => {
                            let field = &cur[struct_field.field as usize];
                            if let Some(_child) = &struct_field.child {
                                let children = field.children();
                                if children.is_empty() {
                                    // TODO: fix error message to explain what happened
                                    return Err(SubstraitExprError::invalid_input(
                                        "Invalid reference",
                                    ));
                                }
                                _owned_cur = children.into_iter().cloned().collect::<Vec<_>>();
                                cur = &_owned_cur;
                            } else {
                                return Ok(field.clone());
                            }
                        }
                        ReferenceType::ListElement(_list_element) => todo!(),
                        ReferenceType::MapKey(_map_key) => todo!(),
                    }
                }
            }
            SchemaInfo::Full(full) => {
                let mut cur_seg = ref_seg;
                let mut cur_children = &full.root.children;
                loop {
                    match cur_seg.reference_type.required("reference_type")? {
                        ReferenceType::StructField(struct_field) => {
                            // TODO: Bounds checking?
                            let field = &cur_children[struct_field.field as usize];
                            if let Some(child) = &struct_field.child {
                                let children = &field.children;
                                if children.is_empty() {
                                    // TODO: fix error message to explain what happened
                                    return Err(SubstraitExprError::invalid_input(
                                        "Invalid reference",
                                    ));
                                }
                                cur_children = children;
                                cur_seg = child.as_ref();
                            } else {
                                return Ok(field.r#type.clone());
                            }
                        }
                        ReferenceType::ListElement(_list_element) => todo!(),
                        ReferenceType::MapKey(_map_key) => todo!(),
                    }
                }
            }
        }
    }
}