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
use substrait::proto::{
    r#type::{
        Binary, Boolean, Fp32, Fp64, Kind, Nullability, String as SubstraitString, Struct, I16,
        I32, I64, I8,
    },
    Type,
};

use crate::error::Result;
use crate::util::HasRequiredPropertiesRef;

use super::registry::ExtensionsRegistry;

/// Helper methods for substrait Type objects
pub trait TypeExt {
    /// Return true if two types are the same "kind", ignoring nullability and type parameters
    fn same_kind(&self, other: &Type) -> Result<bool>;
    /// Returns true if this is the unknown type
    fn is_unknown(&self, registry: &ExtensionsRegistry) -> bool;
    /// Returns the total number of types (including this one) represented by this type
    ///
    /// Will return 1 if this is not a struct type
    fn num_types(&self) -> u32;
    /// Returns the child types
    fn children(&self) -> Vec<&Type>;
}

impl TypeExt for Type {
    fn same_kind(&self, other: &Type) -> Result<bool> {
        let self_kind = self.kind.required("kind")?;
        let other_kind = other.kind.required("kind")?;
        Ok(std::mem::discriminant(self_kind) == std::mem::discriminant(other_kind))
    }

    fn is_unknown(&self, registry: &ExtensionsRegistry) -> bool {
        match &self.kind {
            Some(Kind::UserDefined(user_defined)) => {
                let type_name = registry.lookup_type(user_defined.type_reference);
                match type_name {
                    Some(type_name) => {
                        type_name.uri == UNKNOWN_TYPE_URI && type_name.name == UNKNOWN_TYPE_NAME
                    }
                    None => false,
                }
            }
            _ => false,
        }
    }

    fn num_types(&self) -> u32 {
        match &self.kind {
            Some(Kind::Struct(strct)) => {
                strct.types.iter().map(|typ| typ.num_types()).sum::<u32>() + 1
            }
            _ => 1,
        }
    }

    fn children(&self) -> Vec<&Type> {
        match &self.kind {
            Some(Kind::Struct(strct)) => strct.types.iter().collect(),
            _ => vec![],
        }
    }
}

pub(crate) const fn nullability(nullable: bool) -> i32 {
    if nullable {
        Nullability::Nullable as i32
    } else {
        Nullability::Required as i32
    }
}

/// This trait helps convert from rust types to substrait types
///
/// It's implemented for all the standard types
///
/// Implement this to use methods like
/// [`from_rust`](crate::helpers::types::from_rust) on your own user defined types
pub trait TypeInfer {
    /// Return an instance of the substrait type for this type
    fn as_substrait(nullable: bool) -> Type;
}

impl TypeInfer for i8 {
    fn as_substrait(nullable: bool) -> Type {
        Type {
            kind: Some(substrait::proto::r#type::Kind::I8(I8 {
                nullability: nullability(nullable),
                type_variation_reference: 0,
            })),
        }
    }
}

impl TypeInfer for i16 {
    fn as_substrait(nullable: bool) -> Type {
        Type {
            kind: Some(substrait::proto::r#type::Kind::I16(I16 {
                nullability: nullability(nullable),
                type_variation_reference: 0,
            })),
        }
    }
}

impl TypeInfer for i32 {
    fn as_substrait(nullable: bool) -> Type {
        Type {
            kind: Some(substrait::proto::r#type::Kind::I32(I32 {
                nullability: nullability(nullable),
                type_variation_reference: 0,
            })),
        }
    }
}

impl TypeInfer for i64 {
    fn as_substrait(nullable: bool) -> Type {
        Type {
            kind: Some(substrait::proto::r#type::Kind::I64(I64 {
                nullability: nullability(nullable),
                type_variation_reference: 0,
            })),
        }
    }
}

impl TypeInfer for bool {
    fn as_substrait(nullable: bool) -> Type {
        Type {
            kind: Some(substrait::proto::r#type::Kind::Bool(Boolean {
                nullability: nullability(nullable),
                type_variation_reference: 0,
            })),
        }
    }
}

impl TypeInfer for f32 {
    fn as_substrait(nullable: bool) -> Type {
        Type {
            kind: Some(substrait::proto::r#type::Kind::Fp32(Fp32 {
                nullability: nullability(nullable),
                type_variation_reference: 0,
            })),
        }
    }
}

impl TypeInfer for f64 {
    fn as_substrait(nullable: bool) -> Type {
        Type {
            kind: Some(substrait::proto::r#type::Kind::Fp64(Fp64 {
                nullability: nullability(nullable),
                type_variation_reference: 0,
            })),
        }
    }
}

impl TypeInfer for String {
    fn as_substrait(nullable: bool) -> Type {
        Type {
            kind: Some(substrait::proto::r#type::Kind::String(SubstraitString {
                nullability: nullability(nullable),
                type_variation_reference: 0,
            })),
        }
    }
}

impl TypeInfer for &str {
    fn as_substrait(nullable: bool) -> Type {
        Type {
            kind: Some(substrait::proto::r#type::Kind::String(SubstraitString {
                nullability: nullability(nullable),
                type_variation_reference: 0,
            })),
        }
    }
}

impl TypeInfer for &[u8] {
    fn as_substrait(nullable: bool) -> Type {
        Type {
            kind: Some(substrait::proto::r#type::Kind::Binary(Binary {
                nullability: nullability(nullable),
                type_variation_reference: 0,
            })),
        }
    }
}

impl TypeInfer for Vec<u8> {
    fn as_substrait(nullable: bool) -> Type {
        Type {
            kind: Some(substrait::proto::r#type::Kind::Binary(Binary {
                nullability: nullability(nullable),
                type_variation_reference: 0,
            })),
        }
    }
}

/// Create a substrait type from a rust type
pub fn from_rust<T: TypeInfer>(nullable: bool) -> Type {
    <T as TypeInfer>::as_substrait(nullable)
}
/// Create an instance of the bool type
pub fn bool(nullable: bool) -> Type {
    from_rust::<bool>(nullable)
}
/// Create an instance of the i8 type
pub fn i8(nullable: bool) -> Type {
    from_rust::<i8>(nullable)
}
/// Create an instance of the i16 type
pub fn i16(nullable: bool) -> Type {
    from_rust::<i16>(nullable)
}
/// Create an instance of the i32 type
pub fn i32(nullable: bool) -> Type {
    from_rust::<i32>(nullable)
}
/// Create an instance of the i64 type
pub fn i64(nullable: bool) -> Type {
    from_rust::<i64>(nullable)
}
/// Create an instance of the fp32 type
pub fn fp32(nullable: bool) -> Type {
    from_rust::<f32>(nullable)
}
/// Create an instance of the fp64 type
pub fn fp64(nullable: bool) -> Type {
    from_rust::<f64>(nullable)
}
/// Create an instance of the string type
pub fn string(nullable: bool) -> Type {
    from_rust::<&str>(nullable)
}
/// Create an instance of the binary type
pub fn binary(nullable: bool) -> Type {
    from_rust::<&[u8]>(nullable)
}
/// Create an instance of the struct type
pub fn struct_(nullable: bool, children: Vec<Type>) -> Type {
    Type {
        kind: Some(Kind::Struct(Struct {
            types: children,
            nullability: nullability(nullable),
            ..Default::default()
        })),
    }
}
/// The URI of the unknown type
pub const UNKNOWN_TYPE_URI: &'static str = "https://substrait.io/types";
/// The name of the unknown type
pub const UNKNOWN_TYPE_NAME: &'static str = "unknown";
/// A friendly name that indicates there is no type variation being used
pub const NO_VARIATION: u32 = 0;