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
// SPDX-License-Identifier: Apache-2.0

//! Module for dealing with YAML-based Substrait extensions.

use crate::output::data_type;
use crate::output::path;
use crate::output::tree;
use crate::util;
use std::collections::HashMap;
use std::sync::Arc;

/// Represents a named reference to something.
#[derive(Clone, Debug, Default)]
pub struct NamedReference {
    /// The name of the type, type variation, or function.
    name: Option<String>,

    /// The path to the node that defined the anchor for this extension, if
    /// any.
    anchor_path: Option<path::PathBuf>,
}

impl PartialEq for NamedReference {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
    }
}

impl Eq for NamedReference {}

impl std::hash::Hash for NamedReference {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.name.hash(state);
    }
}

impl std::fmt::Display for NamedReference {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(name) = &self.name {
            write!(f, "{}", util::string::as_ident_or_string(name))
        } else {
            write!(f, "?")
        }
    }
}

impl NamedReference {
    /// Create a new anchor-based reference.
    pub fn new<S: ToString>(
        name: Option<S>,
        anchor_path: Option<path::PathBuf>,
    ) -> Arc<NamedReference> {
        Arc::new(NamedReference {
            name: name.map(|x| x.to_string()),
            anchor_path,
        })
    }

    /// Create a new named reference.
    pub fn new_by_name<S: ToString>(name: S) -> Arc<NamedReference> {
        Arc::new(NamedReference {
            name: Some(name.to_string()),
            anchor_path: None,
        })
    }

    /// Create a new unknown reference.
    pub fn new_unknown() -> Arc<NamedReference> {
        Arc::default()
    }

    /// Returns the name, if known.
    pub fn name(&self) -> Option<&str> {
        self.name.as_ref().map(|s| &s[..])
    }

    /// Returns the path to the anchor, if known.
    pub fn anchor_path(&self) -> Option<&path::PathBuf> {
        self.anchor_path.as_ref()
    }
}

/// Named/namespaced reference to a particular extension definition.
#[derive(Clone, Debug, Default)]
pub struct Reference<T> {
    /// The name of the type, type variation, or function.
    pub name: Arc<NamedReference>,

    /// The URI of the YAML file that defined this extension.
    pub uri: Arc<NamedReference>,

    /// Extension definition information, specific to this type of extension,
    /// if we managed to resolve the reference.
    pub definition: Option<Arc<T>>,
}

impl<T> PartialEq for Reference<T> {
    /// References are equal if they refer to the same thing, regardless of how
    /// they refer to it. If we're not sure because either reference is
    /// (partially) unresolved, return false pessimistically.
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name && self.uri == other.uri
    }
}

impl<T> Eq for Reference<T> {}

impl<T> std::hash::Hash for Reference<T> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.name.hash(state);
        self.uri.hash(state);
    }
}

impl<T> std::fmt::Display for Reference<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}::{}", self.uri, self.name)
    }
}

/// User-defined base data type.
#[derive(Clone, Debug, PartialEq, Default)]
pub struct DataType {
    /// The underlying structure of the type.
    pub structure: Vec<(String, data_type::Simple)>,
}

/// The base type of a type variation.
#[derive(Clone, Debug, PartialEq)]
pub enum TypeVariationBase {
    /// The type variation is immediately based in a physical type.
    Physical(data_type::Class),

    /// The type variation is based in another logical type variation.
    Logical(Arc<TypeVariation>),

    /// The base type is unknown.
    Unresolved,
}

impl Default for TypeVariationBase {
    fn default() -> Self {
        TypeVariationBase::Unresolved
    }
}

/// Type variation extension.
#[derive(Clone, Debug, PartialEq, Default)]
pub struct TypeVariation {
    /// The base type for this variation.
    pub base: TypeVariationBase,

    /// Function behavior for this variation.
    pub function_behavior: FunctionBehavior,
}

impl TypeVariation {
    /// Return the base class for this type variation, if known.
    pub fn get_base_class(&self) -> data_type::Class {
        match &self.base {
            TypeVariationBase::Physical(x) => x.clone(),
            TypeVariationBase::Logical(x) => x.get_base_class(),
            TypeVariationBase::Unresolved => data_type::Class::Unresolved,
        }
    }
}

/// Type variation function behavior.
#[derive(Clone, Debug, PartialEq)]
pub enum FunctionBehavior {
    Inherits,
    Separate,
}

impl Default for FunctionBehavior {
    fn default() -> Self {
        FunctionBehavior::Inherits
    }
}

/// Function extension.
#[derive(Clone, Debug, PartialEq, Default)]
pub struct Function {
    // TODO: need much more information here to do type checking.
}

/// Information about a YAML extension, which may or may not be resolved.
#[derive(Clone, Debug, PartialEq)]
pub enum YamlInfo {
    Unresolved(Arc<NamedReference>),
    Resolved(Arc<YamlData>),
}

impl YamlInfo {
    pub fn data(&self) -> Option<&YamlData> {
        match self {
            YamlInfo::Unresolved(_) => None,
            YamlInfo::Resolved(x) => Some(x),
        }
    }

    pub fn uri(&self) -> &Arc<NamedReference> {
        match self {
            YamlInfo::Unresolved(x) => x,
            YamlInfo::Resolved(x) => &x.uri,
        }
    }
}

impl Default for YamlInfo {
    fn default() -> Self {
        YamlInfo::Unresolved(Arc::default())
    }
}

impl std::fmt::Display for YamlInfo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.uri())
    }
}

/// Data for a resolved YAML file.
#[derive(Clone, Debug, PartialEq)]
pub struct YamlData {
    /// URI for the YAML file.
    pub uri: Arc<NamedReference>,

    /// Reference to the parsed YAML data, if any.
    pub data: tree::NodeReference,

    /// Functions defined in this YAML file. Names are stored in lower case
    /// (Substrait's name resolution is case-insensitive).
    pub functions: HashMap<String, Arc<Function>>,

    /// Types defined in this YAML file. Names are stored in lower case
    /// (Substrait's name resolution is case-insensitive).
    pub types: HashMap<String, Arc<DataType>>,

    /// Type variations defined in this YAML file. Names are stored in lower
    /// case (Substrait's name resolution is case-insensitive).
    pub type_variations: HashMap<String, Arc<TypeVariation>>,
}

impl YamlData {
    /// Constructs an empty YamlData object with an invalid reference to the
    /// data node. Everything still needs to be populated for this to become
    /// valid.
    pub fn new(uri: Arc<NamedReference>) -> YamlData {
        YamlData {
            uri,
            data: tree::NodeReference {
                path: path::Path::Root("").to_path_buf(),
                node: Arc::new(tree::NodeType::YamlMap.into()),
            },
            functions: HashMap::default(),
            types: HashMap::default(),
            type_variations: HashMap::default(),
        }
    }

    /// Helper function for the various resolvers.
    fn local_reference<S: ToString, T>(
        &self,
        name: S,
        definition: Option<Arc<T>>,
    ) -> Arc<Reference<T>> {
        Arc::new(Reference {
            name: NamedReference::new_by_name(name),
            uri: self.uri.clone(),
            definition,
        })
    }

    /// Resolves a function defined in this YAML data block by name. Returns an
    /// unresolved reference if it does not exist.
    pub fn resolve_function<S: ToString>(&self, name: S) -> Arc<Reference<Function>> {
        let name = name.to_string();
        let maybe_def = self.functions.get(&name).cloned();
        self.local_reference(name, maybe_def)
    }

    /// Resolves a type defined in this YAML data block by name. Returns an
    /// unresolved reference if it does not exist.
    pub fn resolve_type<S: ToString>(&self, name: S) -> Arc<Reference<DataType>> {
        let name = name.to_string();
        let maybe_def = self.types.get(&name).cloned();
        self.local_reference(name, maybe_def)
    }

    /// Resolves a type variation defined in this YAML data block by name.
    /// Returns an unresolved reference if it does not exist.
    pub fn resolve_type_variation<S: ToString>(&self, name: S) -> Arc<Reference<TypeVariation>> {
        let name = name.to_string();
        let maybe_def = self.type_variations.get(&name).cloned();
        self.local_reference(name, maybe_def)
    }
}