gitql_ast/types/
base.rs

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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
use std::any::Any;
use std::fmt;

use dyn_clone::DynClone;

use crate::expression::Expr;

use super::any::AnyType;
use super::array::ArrayType;
use super::boolean::BoolType;
use super::composite::CompositeType;
use super::date::DateType;
use super::datetime::DateTimeType;
use super::float::FloatType;
use super::integer::IntType;
use super::interval::IntervalType;
use super::null::NullType;
use super::optional::OptionType;
use super::range::RangeType;
use super::text::TextType;
use super::time::TimeType;
use super::undefined::UndefType;
use super::variant::VariantType;

dyn_clone::clone_trait_object!(DataType);

/// The in memory representation of the Data type in the GitQL query engine
pub trait DataType: DynClone {
    /// Return the literal representation for this [`DataType`]
    fn literal(&self) -> String;

    /// Return if other [`DataType`] is equal or not to current Type
    #[allow(unused_variables)]
    #[allow(clippy::borrowed_box)]
    fn equals(&self, other: &Box<dyn DataType>) -> bool {
        false
    }

    /// Return the current value as dynamic [`Any`]
    fn as_any(&self) -> &dyn Any;

    /// Return a list of types that it's possible to perform `+` operator with
    /// between current DataType and any one of them
    fn can_perform_add_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return the expected type after perform `=` operator between current type and argument type
    ///
    /// Note that you don't need to check again that the argument type is possible to perform operator with
    #[allow(unused_variables)]
    #[allow(clippy::borrowed_box)]
    fn add_op_result_type(&self, other: &Box<dyn DataType>) -> Box<dyn DataType> {
        Box::new(NullType)
    }

    /// Return a list of types that it's possible to perform `-` operator with
    /// between current DataType and any one of them
    #[allow(unused_variables)]
    #[allow(clippy::borrowed_box)]
    fn can_perform_sub_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return the expected type after perform `-` operator between current type and argument type
    ///
    /// Note that you don't need to check again that the argument type is possible to perform operator with
    #[allow(unused_variables)]
    #[allow(clippy::borrowed_box)]
    fn sub_op_result_type(&self, other: &Box<dyn DataType>) -> Box<dyn DataType> {
        Box::new(NullType)
    }

    /// Return a list of types that it's possible to perform `*` operator with
    /// between current DataType and any one of them
    fn can_perform_mul_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return the expected type after perform `*` operator between current type and argument type
    ///
    /// Note that you don't need to check again that the argument type is possible to perform operator with
    #[allow(unused_variables)]
    #[allow(clippy::borrowed_box)]
    fn mul_op_result_type(&self, other: &Box<dyn DataType>) -> Box<dyn DataType> {
        Box::new(NullType)
    }

    /// Return a list of types that it's possible to perform `/` operator with
    /// between current DataType and any one of them
    fn can_perform_div_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return the expected type after perform `/` operator between current type and argument type
    ///
    /// Note that you don't need to check again that the argument type is possible to perform operator with
    #[allow(unused_variables)]
    #[allow(clippy::borrowed_box)]
    fn div_op_result_type(&self, other: &Box<dyn DataType>) -> Box<dyn DataType> {
        Box::new(NullType)
    }

    /// Return a list of types that it's possible to perform `%` operator with
    /// between current DataType and any one of them
    fn can_perform_rem_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return the expected type after perform `&` operator between current type and argument type
    ///
    /// Note that you don't need to check again that the argument type is possible to perform operator with
    #[allow(unused_variables)]
    #[allow(clippy::borrowed_box)]
    fn rem_op_result_type(&self, other: &Box<dyn DataType>) -> Box<dyn DataType> {
        Box::new(NullType)
    }

    /// Return a list of types that it's possible to perform `^` operator with
    /// between current DataType and any one of them
    fn can_perform_caret_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return the expected type after perform `^` operator between current type and argument type
    ///
    /// Note that you don't need to check again that the argument type is possible to perform operator with
    #[allow(unused_variables)]
    #[allow(clippy::borrowed_box)]
    fn caret_op_result_type(&self, other: &Box<dyn DataType>) -> Box<dyn DataType> {
        Box::new(NullType)
    }

    /// Return a list of types that it's possible to perform `|` operator with
    /// between current DataType and any one of them
    fn can_perform_or_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return the expected type after perform `|` operator between current type and argument type
    ///
    /// Note that you don't need to check again that the argument type is possible to perform operator with
    #[allow(unused_variables)]
    #[allow(clippy::borrowed_box)]
    fn or_op_result_type(&self, other: &Box<dyn DataType>) -> Box<dyn DataType> {
        Box::new(NullType)
    }

    /// Return a list of types that it's possible to perform `&` operator with
    /// between current DataType and any one of them
    fn can_perform_and_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return the expected type after perform `&` operator between current type and argument type
    ///
    /// Note that you don't need to check again that the argument type is possible to perform operator with
    #[allow(unused_variables)]
    #[allow(clippy::borrowed_box)]
    fn and_op_result_type(&self, other: &Box<dyn DataType>) -> Box<dyn DataType> {
        Box::new(NullType)
    }

    /// Return a list of types that it's possible to perform `#` operator with
    /// between current DataType and any one of them
    fn can_perform_xor_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return the expected type after perform `#` operator between current type and argument type
    ///
    /// Note that you don't need to check again that the argument type is possible to perform operator with
    #[allow(unused_variables)]
    #[allow(clippy::borrowed_box)]
    fn xor_op_result_type(&self, other: &Box<dyn DataType>) -> Box<dyn DataType> {
        Box::new(NullType)
    }

    /// Return a list of types that it's possible to perform `<<` operator with
    /// between current DataType and any one of them
    fn can_perform_shl_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return the expected type after perform `<<` operator between current type and argument type
    ///
    /// Note that you don't need to check again that the argument type is possible to perform operator with
    #[allow(unused_variables)]
    #[allow(clippy::borrowed_box)]
    fn shl_op_result_type(&self, other: &Box<dyn DataType>) -> Box<dyn DataType> {
        Box::new(NullType)
    }

    /// Return a list of types that it's possible to perform `>>` operator with
    /// between current DataType and any one of them
    fn can_perform_shr_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return the expected type after perform `>>` operator between current type and argument type
    ///
    /// Note that you don't need to check again that the argument type is possible to perform operator with
    #[allow(unused_variables)]
    #[allow(clippy::borrowed_box)]
    fn shr_op_result_type(&self, other: &Box<dyn DataType>) -> Box<dyn DataType> {
        Box::new(NullType)
    }

    /// Return a list of types that it's possible to perform `||` or  `OR` operator with
    /// between current DataType and any one of them
    fn can_perform_logical_or_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return the expected type after perform `||` or `OR` operator between current type and argument type
    ///
    /// Note that you don't need to check again that the argument type is possible to perform operator with
    #[allow(unused_variables)]
    #[allow(clippy::borrowed_box)]
    fn logical_or_op_result_type(&self, other: &Box<dyn DataType>) -> Box<dyn DataType> {
        Box::new(NullType)
    }

    /// Return a list of types that it's possible to perform `&&` or `AND` operator with
    /// between current DataType and any one of them
    fn can_perform_logical_and_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return the expected type after perform `&&` or `AND` operator between current type and argument type
    ///
    /// Note that you don't need to check again that the argument type is possible to perform operator with
    #[allow(unused_variables)]
    #[allow(clippy::borrowed_box)]
    fn logical_and_op_result_type(&self, other: &Box<dyn DataType>) -> Box<dyn DataType> {
        Box::new(NullType)
    }

    /// Return a list of types that it's possible to perform `XOR' operator with
    /// between current DataType and any one of them
    fn can_perform_logical_xor_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return the expected type after perform 'XOR' operator between current type and argument type
    ///
    /// Note that you don't need to check again that the argument type is possible to perform operator with
    #[allow(unused_variables)]
    #[allow(clippy::borrowed_box)]
    fn logical_xor_op_result_type(&self, other: &Box<dyn DataType>) -> Box<dyn DataType> {
        Box::new(NullType)
    }

    /// Return a list of types that it's possible to perform `[I]' operator with
    /// between current DataType and any one of them
    fn can_perform_index_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return the expected type after perform '[]' operator on current type
    ///
    /// * `other` - The index type
    ///
    /// Note that you don't need to check again that the argument type is possible to perform operator with
    #[allow(unused_variables)]
    #[allow(clippy::borrowed_box)]
    fn index_op_result_type(&self, other: &Box<dyn DataType>) -> Box<dyn DataType> {
        Box::new(NullType)
    }

    /// Return true if this type support Slice operator with
    fn can_perform_slice_op(&self) -> bool {
        false
    }

    /// Return a list of types that it's possible to perform `[S : E]' operator with
    /// between current DataType and any one of them
    fn can_perform_slice_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return a list of types that it's possible to perform `=' operator with
    /// between current DataType and any one of them
    ///
    /// No need to define the result type, it always BoolType
    fn can_perform_eq_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return a list of types that it's possible to perform `!=' or `<>` operator with
    /// between current DataType and any one of them
    ///
    /// No need to define the result type, it always BoolType
    fn can_perform_bang_eq_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return a list of types that it's possible to perform `<=>' operator with
    /// between current DataType and any one of them
    ///
    /// No need to define the result type, it always BoolType
    fn can_perform_null_safe_eq_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return a list of types that it's possible to perform `>' operator with
    /// between current DataType and any one of them
    ///
    /// No need to define the result type, it always BoolType
    fn can_perform_gt_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return a list of types that it's possible to perform `>=' operator with
    /// between current DataType and any one of them
    ///
    /// No need to define the result type, it always BoolType
    fn can_perform_gte_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return a list of types that it's possible to perform `<' operator with
    /// between current DataType and any one of them
    ///
    /// No need to define the result type, it always BoolType
    fn can_perform_lt_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return a list of types that it's possible to perform `=<' operator with
    /// between current DataType and any one of them
    ///
    /// No need to define the result type, it always BoolType
    fn can_perform_lte_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return a list of types that it's possible to perform unary `NOT' operator with
    /// between current DataType and any one of them
    fn can_perform_not_op(&self) -> bool {
        false
    }

    /// Return the expected type after perform unary `NOT' operator on current type
    ///
    /// Note that you don't need to check again that the argument type is possible to perform operator with
    fn not_op_result_type(&self) -> Box<dyn DataType> {
        Box::new(NullType)
    }

    /// Return a list of types that it's possible to perform unary `-' operator with
    /// between current DataType and any one of them
    fn can_perform_neg_op(&self) -> bool {
        false
    }

    /// Return the expected type after perform unary `-' operator on current type
    ///
    /// Note that you don't need to check again that the argument type is possible to perform operator with
    fn neg_op_result_type(&self) -> Box<dyn DataType> {
        Box::new(NullType)
    }

    /// Return a list of types that it's possible to perform unary `!' operator with
    /// between current DataType and any one of them
    fn can_perform_bang_op(&self) -> bool {
        false
    }

    /// Return the expected type after perform unary `!' operator on current type
    ///
    /// Note that you don't need to check again that the argument type is possible to perform operator with
    fn bang_op_result_type(&self) -> Box<dyn DataType> {
        Box::new(NullType)
    }

    /// Return a list of types that it's possible to perform unary `@>' operator with
    /// between current DataType and any one of them
    ///
    /// No need to define the result type, it always BoolType
    fn can_perform_contains_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return true if this Expression can be casted to the current type without any evaluation
    ///
    /// For example casting Text with specific format to Date or Time
    ///
    /// The result type is equal to the current type
    #[allow(unused_variables)]
    #[allow(clippy::borrowed_box)]
    fn has_implicit_cast_from(&self, expr: &Box<dyn Expr>) -> bool {
        false
    }

    /// Return a list of types that it's possible to perform `Cast' operator to
    ///
    /// For example casting column with float value to integer or verse versa
    ///
    /// The result type is equal to the current type
    fn can_perform_explicit_cast_op_to(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return a list of types that it's possible to perform unary `LIKE' operator with
    /// between current DataType and any one of them
    ///
    /// No need to define the result type, it always BoolType
    fn can_perform_like_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return a list of types that it's possible to perform unary `GLOB' operator with
    /// between current DataType and any one of them
    ///
    /// No need to define the result type, it always BoolType
    fn can_perform_glob_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }

    /// Return a list of types that it's possible to perform unary `REGEXP' operator with
    /// between current DataType and any one of them
    ///
    /// No need to define the result type, it always BoolType
    fn can_perform_regexp_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![]
    }
}

impl dyn DataType {
    /// Return true if this type is [`AnyType`]
    pub fn is_any(&self) -> bool {
        self.as_any().downcast_ref::<AnyType>().is_some()
    }

    /// Return true if this type is [`TextType`]
    pub fn is_text(&self) -> bool {
        self.as_any().downcast_ref::<TextType>().is_some()
    }

    /// Return true if this type is [`IntType`]
    pub fn is_int(&self) -> bool {
        self.as_any().downcast_ref::<IntType>().is_some()
    }

    /// Return true if this type is [`FloatType`]
    pub fn is_float(&self) -> bool {
        self.as_any().downcast_ref::<FloatType>().is_some()
    }

    /// Return true if this type is [`IntType`] or [`FloatType`]
    pub fn is_number(&self) -> bool {
        self.is_int() || self.is_float()
    }

    /// Return true if this type is [`BoolType`]
    pub fn is_bool(&self) -> bool {
        self.as_any().downcast_ref::<BoolType>().is_some()
    }

    /// Return true if this type is [`DateType`]
    pub fn is_date(&self) -> bool {
        self.as_any().downcast_ref::<DateType>().is_some()
    }

    /// Return true if this type is [`TimeType`]
    pub fn is_time(&self) -> bool {
        self.as_any().downcast_ref::<TimeType>().is_some()
    }

    /// Return true if this type is [`DateTimeType`]
    pub fn is_date_time(&self) -> bool {
        self.as_any().downcast_ref::<DateTimeType>().is_some()
    }

    /// Return true if this type is [`IntervalType`]
    pub fn is_interval(&self) -> bool {
        self.as_any().downcast_ref::<IntervalType>().is_some()
    }

    /// Return true if this type is [`ArrayType`]
    pub fn is_array(&self) -> bool {
        self.as_any().downcast_ref::<ArrayType>().is_some()
    }

    /// Return true if this type is [`RangeType`]
    pub fn is_range(&self) -> bool {
        self.as_any().downcast_ref::<RangeType>().is_some()
    }

    /// Return true if this type is [`VariantType`]
    pub fn is_variant(&self) -> bool {
        self.as_any().downcast_ref::<VariantType>().is_some()
    }

    /// Return true if this type is [`VariantType`]
    /// and applying the matcher function is return true on one of the variants
    pub fn is_variant_with(&self, matcher: fn(&Box<dyn DataType>) -> bool) -> bool {
        if let Some(variant_type) = self.as_any().downcast_ref::<VariantType>() {
            for variant in variant_type.variants.iter() {
                if matcher(variant) {
                    return true;
                }
            }
        }
        false
    }

    /// Return true if this type is [`VariantType`] and contain specific type as one of it variants
    #[allow(clippy::borrowed_box)]
    pub fn is_variant_contains(&self, other: &Box<dyn DataType>) -> bool {
        if let Some(variant_type) = self.as_any().downcast_ref::<VariantType>() {
            return variant_type.variants.contains(other);
        }
        false
    }

    /// Return true if this type is [`OptionType`]
    pub fn is_optional(&self) -> bool {
        self.as_any().downcast_ref::<OptionType>().is_some()
    }

    /// Return true if this type is [`VariantType`]
    pub fn is_varargs(&self) -> bool {
        self.as_any().downcast_ref::<VariantType>().is_some()
    }

    /// Return true if this type is [`CompositeType`]
    pub fn is_composite(&self) -> bool {
        self.as_any().downcast_ref::<CompositeType>().is_some()
    }

    /// Return true if this type is [`UndefType`]
    pub fn is_undefined(&self) -> bool {
        self.as_any().downcast_ref::<UndefType>().is_some()
    }

    /// Return true if this type is [`NullType`]
    pub fn is_null(&self) -> bool {
        self.as_any().downcast_ref::<NullType>().is_some()
    }
}

impl fmt::Display for Box<dyn DataType> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.literal())
    }
}

impl PartialEq for Box<dyn DataType> {
    fn eq(&self, other: &Self) -> bool {
        self.equals(other)
    }
}