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
use substrait::proto::{
    expression::{literal::LiteralType, Literal},
    Expression, Type,
};

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

use super::types;

/// Extends the protobuf Literal object with useful helper methods
pub trait LiteralExt {
    /// Get the substrait type of a literal
    fn data_type(&self) -> Result<Type>;
}

impl LiteralExt for Literal {
    fn data_type(&self) -> Result<Type> {
        match &self.literal_type {
            Some(LiteralType::Binary(_)) => Ok(types::binary(self.nullable)),
            Some(LiteralType::Boolean(_)) => Ok(types::bool(self.nullable)),
            Some(LiteralType::Fp32(_)) => Ok(types::fp32(self.nullable)),
            Some(LiteralType::Fp64(_)) => Ok(types::fp64(self.nullable)),
            Some(LiteralType::I8(_)) => Ok(types::i8(self.nullable)),
            Some(LiteralType::I16(_)) => Ok(types::i16(self.nullable)),
            Some(LiteralType::I32(_)) => Ok(types::i32(self.nullable)),
            Some(LiteralType::I64(_)) => Ok(types::i64(self.nullable)),
            Some(LiteralType::Null(data_type)) => Ok(data_type.clone()),
            Some(LiteralType::String(_)) => Ok(types::string(self.nullable)),
            None => Err(SubstraitExprError::invalid_substrait(
                "Literal was missing required literal_type property",
            )),
            _ => todo!(),
        }
    }
}

/// A trait that helps convert from rust types to substrait types
///
/// This trait is implemented for all the standard rust types
///
/// Implementing this for your UDF will allow you to use methods like
/// [`try_as_rust_literal`](crate::helpers::expr::ExpressionExt::try_as_rust_literal)
/// on your own types.
pub trait LiteralInference {
    /// Convert self to a substrait literal
    fn to_substrait(self) -> LiteralType;
    /// Try to convert from a substrait literal to an instance of Self
    fn try_from_substrait(lit: &LiteralType) -> Result<Self>
    where
        Self: Sized;
}

impl LiteralInference for bool {
    fn to_substrait(self) -> LiteralType {
        LiteralType::Boolean(self)
    }
    fn try_from_substrait(lit: &LiteralType) -> Result<Self> {
        match lit {
            LiteralType::Boolean(value) => Ok(*value),
            _ => Err(crate::error::SubstraitExprError::invalid_substrait(
                format!("Expected a boolean literal but found {:?}", lit),
            )),
        }
    }
}

impl LiteralInference for i8 {
    fn to_substrait(self) -> LiteralType {
        LiteralType::I8(self as i32)
    }
    fn try_from_substrait(lit: &LiteralType) -> Result<Self> {
        match lit {
            LiteralType::I8(value) => i8::try_from(*value).map_err(|_| {
                crate::error::SubstraitExprError::invalid_substrait(
                    "The substrait message had an i8 literal but the value did not fit in an i8",
                )
            }),
            _ => Err(crate::error::SubstraitExprError::invalid_substrait(
                format!("Expected an int8 literal but found {:?}", lit),
            )),
        }
    }
}

impl LiteralInference for i16 {
    fn to_substrait(self) -> LiteralType {
        LiteralType::I16(self as i32)
    }
    fn try_from_substrait(lit: &LiteralType) -> Result<Self> {
        match lit {
            LiteralType::I16(value) => i16::try_from(*value).map_err(|_| {
                crate::error::SubstraitExprError::invalid_substrait(
                    "The substrait message had an i16 literal but the value did not fit in an i16",
                )
            }),
            _ => Err(crate::error::SubstraitExprError::invalid_substrait(
                format!("Expected an int16 literal but found {:?}", lit),
            )),
        }
    }
}

impl LiteralInference for i32 {
    fn to_substrait(self) -> LiteralType {
        LiteralType::I32(self)
    }
    fn try_from_substrait(lit: &LiteralType) -> Result<Self> {
        match lit {
            LiteralType::I32(value) => Ok(*value),
            _ => Err(crate::error::SubstraitExprError::invalid_substrait(
                format!("Expected an int32 literal but found {:?}", lit),
            )),
        }
    }
}

impl LiteralInference for i64 {
    fn to_substrait(self) -> LiteralType {
        LiteralType::I64(self as i64)
    }
    fn try_from_substrait(lit: &LiteralType) -> Result<Self> {
        match lit {
            LiteralType::I64(value) => Ok(*value),
            _ => Err(crate::error::SubstraitExprError::invalid_substrait(
                format!("Expected an int64 literal but found {:?}", lit),
            )),
        }
    }
}

impl LiteralInference for f32 {
    fn to_substrait(self) -> LiteralType {
        LiteralType::Fp32(self)
    }
    fn try_from_substrait(lit: &LiteralType) -> Result<Self> {
        match lit {
            LiteralType::Fp32(value) => Ok(*value),
            _ => Err(crate::error::SubstraitExprError::invalid_substrait(
                format!("Expected an fp32 literal but found {:?}", lit),
            )),
        }
    }
}

impl LiteralInference for f64 {
    fn to_substrait(self) -> LiteralType {
        LiteralType::Fp64(self)
    }
    fn try_from_substrait(lit: &LiteralType) -> Result<Self> {
        match lit {
            LiteralType::Fp64(value) => Ok(*value),
            _ => Err(crate::error::SubstraitExprError::invalid_substrait(
                format!("Expected an fp64 literal but found {:?}", lit),
            )),
        }
    }
}

impl LiteralInference for &str {
    fn to_substrait(self) -> LiteralType {
        LiteralType::String(self.to_owned())
    }
    fn try_from_substrait(_: &LiteralType) -> Result<Self> {
        todo!()
    }
}

impl LiteralInference for String {
    fn to_substrait(self) -> LiteralType {
        LiteralType::String(self)
    }
    fn try_from_substrait(lit: &LiteralType) -> Result<Self> {
        match lit {
            LiteralType::String(value) => Ok(value.to_string()),
            _ => Err(crate::error::SubstraitExprError::invalid_substrait(
                format!("Expected a string literal but found {:?}", lit),
            )),
        }
    }
}

impl LiteralInference for &[u8] {
    fn to_substrait(self) -> LiteralType {
        LiteralType::Binary(Vec::from(self))
    }
    fn try_from_substrait(_: &LiteralType) -> Result<Self> {
        todo!()
    }
}

impl LiteralInference for Vec<u8> {
    fn to_substrait(self) -> LiteralType {
        LiteralType::Binary(self)
    }
    fn try_from_substrait(lit: &LiteralType) -> Result<Self> {
        match lit {
            LiteralType::Binary(value) => Ok(value.clone()),
            _ => Err(crate::error::SubstraitExprError::invalid_substrait(
                format!("Expected a binary literal but found {:?}", lit),
            )),
        }
    }
}

const NO_TYPE_VARIATION: u32 = 0;

fn make_literal(lit_type: LiteralType, nullable: bool) -> Expression {
    Expression {
        rex_type: Some(substrait::proto::expression::RexType::Literal(Literal {
            nullable,
            type_variation_reference: NO_TYPE_VARIATION,
            literal_type: Some(lit_type),
        })),
    }
}

/// Methods for creating literals from rust
pub mod literals {
    use substrait::proto::expression::literal::{Struct, VarChar};

    use crate::{error::SubstraitExprError, helpers::expr::ExpressionExt};

    use super::*;

    /// Create a fixed-char literal
    ///
    /// There are three primary string types in Substrait, string, fixed-char, and var-char
    ///
    /// By default, rust string types coerce into Substrait string literals.
    /// This method will give you a fixed-char literal instead.
    pub fn fixed_char(value: impl Into<String>) -> Expression {
        make_literal(LiteralType::FixedChar(value.into()), false)
    }

    /// Create a fixed-binary literal
    ///
    /// There are two binary types in Substrait, binary, and fixed-binary
    ///
    /// By default, rust binary types coerce into Substrait binary literals.
    /// This method will give you a fixed-length binary instead.
    pub fn fixed_binary(value: Vec<u8>) -> Expression {
        make_literal(LiteralType::FixedBinary(value), false)
    }

    /// Create a var-char literal
    ///
    /// There are three primary string types in Substrait, string, fixed-char, and var-char
    ///
    /// By default, rust string types coerce into Substrait string literals.
    /// This method will give you a var-char literal instead.
    ///
    /// This method will return an error if the provided string is longer than length
    pub fn try_varchar(value: impl Into<String>, length: u32) -> Result<Expression> {
        let value = value.into();
        if (length as usize) < value.len() {
            Err(SubstraitExprError::invalid_input(format!(
                "String of length {} does not fit in a varchar literal field of length {}",
                value.len(),
                length
            )))
        } else {
            Ok(make_literal(
                LiteralType::VarChar(VarChar {
                    value: value.into(),
                    length,
                }),
                false,
            ))
        }
    }

    /// Create a struct literal
    ///
    /// `children` must all be literal expressions and will be the children of the struct
    pub fn try_struct(children: &[Expression]) -> Result<Expression> {
        let fields = children
            .iter()
            .map(|expr| expr.try_as_literal().cloned())
            .collect::<Result<Vec<_>>>()?;
        Ok(make_literal(LiteralType::Struct(Struct { fields }), false))
    }
}

/// Create a null literal of the given type
pub fn null_literal(data_type: Type) -> Expression {
    make_literal(LiteralType::Null(data_type), true)
}

/// Create a literal from a rust value
pub fn literal<T: LiteralInference>(value: T) -> Expression {
    make_literal(value.to_substrait(), false)
}

/// Createa a nullable literal from a rust value (unusual)
pub fn nullable_literal<T: LiteralInference>(value: T) -> Expression {
    make_literal(value.to_substrait(), true)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_literals() {
        let x = literal(1_i8);
        let y = literal(1_i16);
        let z = literal(1_i32);
        literals::try_struct(&[x, y, z]).unwrap();

        literal("hello");
        literals::fixed_char("hello");
        literals::fixed_binary(vec![0, 1, 2]);
        literals::try_varchar("hello", 30).unwrap();
        literal(vec![0, 1, 2]);

        assert!(literals::try_varchar("hello", 3).is_err());
    }
}