gitql_ast/
expression.rs

1use std::any::Any;
2
3use dyn_clone::DynClone;
4
5use super::types::array::ArrayType;
6use super::types::boolean::BoolType;
7use super::types::integer::IntType;
8use super::types::null::NullType;
9use super::types::text::TextType;
10use super::types::DataType;
11
12use crate::interval::Interval;
13use crate::operator::ArithmeticOperator;
14use crate::operator::BinaryBitwiseOperator;
15use crate::operator::BinaryLogicalOperator;
16use crate::operator::ComparisonOperator;
17use crate::operator::GroupComparisonOperator;
18use crate::operator::PrefixUnaryOperator;
19use crate::types::float::FloatType;
20use crate::types::interval::IntervalType;
21use crate::types::row::RowType;
22
23#[derive(PartialEq)]
24pub enum ExprKind {
25    Assignment,
26    String,
27    Symbol,
28    Array,
29    GlobalVariable,
30    Number,
31    Boolean,
32    Interval,
33    PrefixUnary,
34    Index,
35    Slice,
36    Arithmetic,
37    Comparison,
38    GroupComparison,
39    Contains,
40    ContainedBy,
41    Like,
42    Regex,
43    Glob,
44    Logical,
45    Bitwise,
46    Call,
47    BenchmarkCall,
48    Between,
49    Case,
50    In,
51    IsNull,
52    Null,
53    Cast,
54    Column,
55    Row,
56    MemberAccess,
57}
58
59dyn_clone::clone_trait_object!(Expr);
60
61pub trait Expr: DynClone {
62    fn kind(&self) -> ExprKind;
63    fn expr_type(&self) -> Box<dyn DataType>;
64    fn as_any(&self) -> &dyn Any;
65}
66
67impl dyn Expr {
68    pub fn is_const(&self) -> bool {
69        matches!(
70            self.kind(),
71            ExprKind::Number | ExprKind::Boolean | ExprKind::String | ExprKind::Null
72        )
73    }
74}
75
76#[derive(Clone)]
77pub struct AssignmentExpr {
78    pub symbol: String,
79    pub value: Box<dyn Expr>,
80}
81
82impl Expr for AssignmentExpr {
83    fn kind(&self) -> ExprKind {
84        ExprKind::Assignment
85    }
86
87    fn expr_type(&self) -> Box<dyn DataType> {
88        self.value.expr_type()
89    }
90
91    fn as_any(&self) -> &dyn Any {
92        self
93    }
94}
95
96#[derive(Clone)]
97pub struct StringExpr {
98    pub value: String,
99}
100
101impl Expr for StringExpr {
102    fn kind(&self) -> ExprKind {
103        ExprKind::String
104    }
105
106    fn expr_type(&self) -> Box<dyn DataType> {
107        Box::new(TextType)
108    }
109
110    fn as_any(&self) -> &dyn Any {
111        self
112    }
113}
114
115#[derive(PartialEq, Clone)]
116pub enum SymbolFlag {
117    AggregationReference,
118    WindowReference,
119    None,
120}
121
122#[derive(Clone)]
123pub struct SymbolExpr {
124    pub value: String,
125    pub expr_type: Box<dyn DataType>,
126    pub flag: SymbolFlag,
127}
128
129impl Expr for SymbolExpr {
130    fn kind(&self) -> ExprKind {
131        ExprKind::Symbol
132    }
133
134    fn expr_type(&self) -> Box<dyn DataType> {
135        self.expr_type.clone()
136    }
137
138    fn as_any(&self) -> &dyn Any {
139        self
140    }
141}
142
143#[derive(Clone)]
144pub struct ArrayExpr {
145    pub values: Vec<Box<dyn Expr>>,
146    pub element_type: Box<dyn DataType>,
147}
148
149impl Expr for ArrayExpr {
150    fn kind(&self) -> ExprKind {
151        ExprKind::Array
152    }
153
154    fn expr_type(&self) -> Box<dyn DataType> {
155        Box::new(ArrayType::new(self.element_type.clone()))
156    }
157
158    fn as_any(&self) -> &dyn Any {
159        self
160    }
161}
162
163#[derive(Clone)]
164pub struct GlobalVariableExpr {
165    pub name: String,
166    pub result_type: Box<dyn DataType>,
167}
168
169impl Expr for GlobalVariableExpr {
170    fn kind(&self) -> ExprKind {
171        ExprKind::GlobalVariable
172    }
173
174    fn expr_type(&self) -> Box<dyn DataType> {
175        self.result_type.clone()
176    }
177
178    fn as_any(&self) -> &dyn Any {
179        self
180    }
181}
182
183#[derive(Clone, PartialEq)]
184pub enum Number {
185    Int(i64),
186    Float(f64),
187}
188
189#[derive(Clone)]
190pub struct NumberExpr {
191    pub value: Number,
192}
193
194impl NumberExpr {
195    pub fn int(int_value: i64) -> Self {
196        NumberExpr {
197            value: Number::Int(int_value),
198        }
199    }
200
201    pub fn float(int_value: f64) -> Self {
202        NumberExpr {
203            value: Number::Float(int_value),
204        }
205    }
206}
207
208impl Expr for NumberExpr {
209    fn kind(&self) -> ExprKind {
210        ExprKind::Number
211    }
212
213    fn expr_type(&self) -> Box<dyn DataType> {
214        match self.value {
215            Number::Int(_) => Box::new(IntType),
216            Number::Float(_) => Box::new(FloatType),
217        }
218    }
219
220    fn as_any(&self) -> &dyn Any {
221        self
222    }
223}
224
225#[derive(Clone)]
226pub struct IntervalExpr {
227    pub interval: Interval,
228}
229
230impl IntervalExpr {
231    pub fn new(interval: Interval) -> Self {
232        IntervalExpr { interval }
233    }
234}
235
236impl Expr for IntervalExpr {
237    fn kind(&self) -> ExprKind {
238        ExprKind::Interval
239    }
240
241    fn expr_type(&self) -> Box<dyn DataType> {
242        Box::new(IntervalType)
243    }
244
245    fn as_any(&self) -> &dyn Any {
246        self
247    }
248}
249
250#[derive(Clone)]
251pub struct BooleanExpr {
252    pub is_true: bool,
253}
254
255impl Expr for BooleanExpr {
256    fn kind(&self) -> ExprKind {
257        ExprKind::Boolean
258    }
259
260    fn expr_type(&self) -> Box<dyn DataType> {
261        Box::new(BoolType)
262    }
263
264    fn as_any(&self) -> &dyn Any {
265        self
266    }
267}
268
269#[derive(Clone)]
270pub struct UnaryExpr {
271    pub right: Box<dyn Expr>,
272    pub operator: PrefixUnaryOperator,
273    pub result_type: Box<dyn DataType>,
274}
275
276impl Expr for UnaryExpr {
277    fn kind(&self) -> ExprKind {
278        ExprKind::PrefixUnary
279    }
280
281    fn expr_type(&self) -> Box<dyn DataType> {
282        self.result_type.clone()
283    }
284
285    fn as_any(&self) -> &dyn Any {
286        self
287    }
288}
289
290#[derive(Clone)]
291pub struct IndexExpr {
292    pub collection: Box<dyn Expr>,
293    pub element_type: Box<dyn DataType>,
294    pub index: Box<dyn Expr>,
295    pub result_type: Box<dyn DataType>,
296}
297
298impl Expr for IndexExpr {
299    fn kind(&self) -> ExprKind {
300        ExprKind::Index
301    }
302
303    fn expr_type(&self) -> Box<dyn DataType> {
304        self.result_type.clone()
305    }
306
307    fn as_any(&self) -> &dyn Any {
308        self
309    }
310}
311
312#[derive(Clone)]
313pub struct SliceExpr {
314    pub collection: Box<dyn Expr>,
315    pub start: Option<Box<dyn Expr>>,
316    pub end: Option<Box<dyn Expr>>,
317    pub result_type: Box<dyn DataType>,
318}
319
320impl Expr for SliceExpr {
321    fn kind(&self) -> ExprKind {
322        ExprKind::Slice
323    }
324
325    fn expr_type(&self) -> Box<dyn DataType> {
326        self.result_type.clone()
327    }
328
329    fn as_any(&self) -> &dyn Any {
330        self
331    }
332}
333
334#[derive(Clone)]
335pub struct ArithmeticExpr {
336    pub left: Box<dyn Expr>,
337    pub operator: ArithmeticOperator,
338    pub right: Box<dyn Expr>,
339    pub result_type: Box<dyn DataType>,
340}
341
342impl Expr for ArithmeticExpr {
343    fn kind(&self) -> ExprKind {
344        ExprKind::Arithmetic
345    }
346
347    fn expr_type(&self) -> Box<dyn DataType> {
348        self.result_type.clone()
349    }
350
351    fn as_any(&self) -> &dyn Any {
352        self
353    }
354}
355
356#[derive(Clone)]
357pub struct ComparisonExpr {
358    pub left: Box<dyn Expr>,
359    pub operator: ComparisonOperator,
360    pub right: Box<dyn Expr>,
361}
362
363impl Expr for ComparisonExpr {
364    fn kind(&self) -> ExprKind {
365        ExprKind::Comparison
366    }
367
368    fn expr_type(&self) -> Box<dyn DataType> {
369        if self.operator == ComparisonOperator::NullSafeEqual {
370            Box::new(IntType)
371        } else {
372            Box::new(BoolType)
373        }
374    }
375
376    fn as_any(&self) -> &dyn Any {
377        self
378    }
379}
380
381#[derive(Clone)]
382pub struct GroupComparisonExpr {
383    pub left: Box<dyn Expr>,
384    pub comparison_operator: ComparisonOperator,
385    pub group_operator: GroupComparisonOperator,
386    pub right: Box<dyn Expr>,
387}
388
389impl Expr for GroupComparisonExpr {
390    fn kind(&self) -> ExprKind {
391        ExprKind::GroupComparison
392    }
393
394    fn expr_type(&self) -> Box<dyn DataType> {
395        if self.comparison_operator == ComparisonOperator::NullSafeEqual {
396            Box::new(IntType)
397        } else {
398            Box::new(BoolType)
399        }
400    }
401
402    fn as_any(&self) -> &dyn Any {
403        self
404    }
405}
406
407#[derive(Clone)]
408pub struct ContainsExpr {
409    pub left: Box<dyn Expr>,
410    pub right: Box<dyn Expr>,
411}
412
413impl Expr for ContainsExpr {
414    fn kind(&self) -> ExprKind {
415        ExprKind::Contains
416    }
417
418    fn expr_type(&self) -> Box<dyn DataType> {
419        Box::new(BoolType)
420    }
421
422    fn as_any(&self) -> &dyn Any {
423        self
424    }
425}
426
427#[derive(Clone)]
428pub struct ContainedByExpr {
429    pub left: Box<dyn Expr>,
430    pub right: Box<dyn Expr>,
431}
432
433impl Expr for ContainedByExpr {
434    fn kind(&self) -> ExprKind {
435        ExprKind::ContainedBy
436    }
437
438    fn expr_type(&self) -> Box<dyn DataType> {
439        Box::new(BoolType)
440    }
441
442    fn as_any(&self) -> &dyn Any {
443        self
444    }
445}
446
447#[derive(Clone)]
448pub struct LikeExpr {
449    pub input: Box<dyn Expr>,
450    pub pattern: Box<dyn Expr>,
451}
452
453impl Expr for LikeExpr {
454    fn kind(&self) -> ExprKind {
455        ExprKind::Like
456    }
457
458    fn expr_type(&self) -> Box<dyn DataType> {
459        Box::new(BoolType)
460    }
461
462    fn as_any(&self) -> &dyn Any {
463        self
464    }
465}
466
467#[derive(Clone)]
468pub struct RegexExpr {
469    pub input: Box<dyn Expr>,
470    pub pattern: Box<dyn Expr>,
471}
472
473impl Expr for RegexExpr {
474    fn kind(&self) -> ExprKind {
475        ExprKind::Regex
476    }
477
478    fn expr_type(&self) -> Box<dyn DataType> {
479        Box::new(BoolType)
480    }
481
482    fn as_any(&self) -> &dyn Any {
483        self
484    }
485}
486
487#[derive(Clone)]
488pub struct GlobExpr {
489    pub input: Box<dyn Expr>,
490    pub pattern: Box<dyn Expr>,
491}
492
493impl Expr for GlobExpr {
494    fn kind(&self) -> ExprKind {
495        ExprKind::Glob
496    }
497
498    fn expr_type(&self) -> Box<dyn DataType> {
499        Box::new(BoolType)
500    }
501
502    fn as_any(&self) -> &dyn Any {
503        self
504    }
505}
506
507#[derive(Clone)]
508pub struct LogicalExpr {
509    pub left: Box<dyn Expr>,
510    pub operator: BinaryLogicalOperator,
511    pub right: Box<dyn Expr>,
512}
513
514impl Expr for LogicalExpr {
515    fn kind(&self) -> ExprKind {
516        ExprKind::Logical
517    }
518
519    fn expr_type(&self) -> Box<dyn DataType> {
520        let lhs_type = self.left.expr_type();
521        let rhs_type = &self.right.expr_type();
522        match self.operator {
523            BinaryLogicalOperator::Or => lhs_type.logical_or_op_result_type(rhs_type),
524            BinaryLogicalOperator::And => lhs_type.logical_and_op_result_type(rhs_type),
525            BinaryLogicalOperator::Xor => lhs_type.logical_xor_op_result_type(rhs_type),
526        }
527    }
528
529    fn as_any(&self) -> &dyn Any {
530        self
531    }
532}
533
534#[derive(Clone)]
535pub struct BitwiseExpr {
536    pub left: Box<dyn Expr>,
537    pub operator: BinaryBitwiseOperator,
538    pub right: Box<dyn Expr>,
539    pub result_type: Box<dyn DataType>,
540}
541
542impl Expr for BitwiseExpr {
543    fn kind(&self) -> ExprKind {
544        ExprKind::Bitwise
545    }
546
547    fn expr_type(&self) -> Box<dyn DataType> {
548        self.result_type.clone()
549    }
550
551    fn as_any(&self) -> &dyn Any {
552        self
553    }
554}
555
556#[derive(Clone)]
557pub struct CallExpr {
558    pub function_name: String,
559    pub arguments: Vec<Box<dyn Expr>>,
560    pub return_type: Box<dyn DataType>,
561}
562
563impl Expr for CallExpr {
564    fn kind(&self) -> ExprKind {
565        ExprKind::Call
566    }
567
568    fn expr_type(&self) -> Box<dyn DataType> {
569        self.return_type.clone()
570    }
571
572    fn as_any(&self) -> &dyn Any {
573        self
574    }
575}
576
577#[derive(Clone)]
578pub struct BenchmarkCallExpr {
579    pub expression: Box<dyn Expr>,
580    pub count: Box<dyn Expr>,
581}
582
583impl Expr for BenchmarkCallExpr {
584    fn kind(&self) -> ExprKind {
585        ExprKind::BenchmarkCall
586    }
587
588    fn expr_type(&self) -> Box<dyn DataType> {
589        Box::new(IntType)
590    }
591
592    fn as_any(&self) -> &dyn Any {
593        self
594    }
595}
596
597/// ASYMMETRIC is default. ASYMMETRIC is noise, it's current behavior.
598/// If b <= c, then SYMMETRIC and ASYMMETRIC work the same way.
599/// If b > c, then SYMMETRIC in effect reverses the operands.
600#[derive(PartialEq, Clone)]
601pub enum BetweenKind {
602    Symmetric,
603    Asymmetric,
604}
605
606#[derive(Clone)]
607pub struct BetweenExpr {
608    pub value: Box<dyn Expr>,
609    pub range_start: Box<dyn Expr>,
610    pub range_end: Box<dyn Expr>,
611    pub kind: BetweenKind,
612}
613
614impl Expr for BetweenExpr {
615    fn kind(&self) -> ExprKind {
616        ExprKind::Between
617    }
618
619    fn expr_type(&self) -> Box<dyn DataType> {
620        Box::new(BoolType)
621    }
622
623    fn as_any(&self) -> &dyn Any {
624        self
625    }
626}
627
628#[derive(Clone)]
629pub struct CaseExpr {
630    pub conditions: Vec<Box<dyn Expr>>,
631    pub values: Vec<Box<dyn Expr>>,
632    pub default_value: Option<Box<dyn Expr>>,
633    pub values_type: Box<dyn DataType>,
634}
635
636impl Expr for CaseExpr {
637    fn kind(&self) -> ExprKind {
638        ExprKind::Case
639    }
640
641    fn expr_type(&self) -> Box<dyn DataType> {
642        self.values_type.clone()
643    }
644
645    fn as_any(&self) -> &dyn Any {
646        self
647    }
648}
649
650#[derive(Clone)]
651pub struct InExpr {
652    pub argument: Box<dyn Expr>,
653    pub values: Vec<Box<dyn Expr>>,
654    pub values_type: Box<dyn DataType>,
655    pub has_not_keyword: bool,
656}
657
658impl Expr for InExpr {
659    fn kind(&self) -> ExprKind {
660        ExprKind::In
661    }
662
663    fn expr_type(&self) -> Box<dyn DataType> {
664        self.values_type.clone()
665    }
666
667    fn as_any(&self) -> &dyn Any {
668        self
669    }
670}
671
672#[derive(Clone)]
673pub struct IsNullExpr {
674    pub argument: Box<dyn Expr>,
675    pub has_not: bool,
676}
677
678impl Expr for IsNullExpr {
679    fn kind(&self) -> ExprKind {
680        ExprKind::IsNull
681    }
682
683    fn expr_type(&self) -> Box<dyn DataType> {
684        Box::new(BoolType)
685    }
686
687    fn as_any(&self) -> &dyn Any {
688        self
689    }
690}
691
692#[derive(Clone)]
693pub struct NullExpr;
694
695impl Expr for NullExpr {
696    fn kind(&self) -> ExprKind {
697        ExprKind::Null
698    }
699
700    fn expr_type(&self) -> Box<dyn DataType> {
701        Box::new(NullType)
702    }
703
704    fn as_any(&self) -> &dyn Any {
705        self
706    }
707}
708
709#[derive(Clone)]
710pub struct CastExpr {
711    pub value: Box<dyn Expr>,
712    pub result_type: Box<dyn DataType>,
713}
714
715impl Expr for CastExpr {
716    fn kind(&self) -> ExprKind {
717        ExprKind::Cast
718    }
719
720    fn expr_type(&self) -> Box<dyn DataType> {
721        self.result_type.clone()
722    }
723
724    fn as_any(&self) -> &dyn Any {
725        self
726    }
727}
728
729#[derive(Clone)]
730pub struct ColumnExpr {
731    pub expr: Box<dyn Expr>,
732}
733
734impl Expr for ColumnExpr {
735    fn kind(&self) -> ExprKind {
736        ExprKind::Column
737    }
738
739    fn expr_type(&self) -> Box<dyn DataType> {
740        self.expr.expr_type().clone()
741    }
742
743    fn as_any(&self) -> &dyn Any {
744        self
745    }
746}
747
748#[derive(Clone)]
749pub struct RowExpr {
750    pub exprs: Vec<Box<dyn Expr>>,
751    pub row_type: RowType,
752}
753
754impl Expr for RowExpr {
755    fn kind(&self) -> ExprKind {
756        ExprKind::Row
757    }
758
759    fn expr_type(&self) -> Box<dyn DataType> {
760        Box::new(self.row_type.clone())
761    }
762
763    fn as_any(&self) -> &dyn Any {
764        self
765    }
766}
767
768#[derive(Clone)]
769pub struct MemberAccessExpr {
770    pub composite: Box<dyn Expr>,
771    pub member_name: String,
772    pub member_type: Box<dyn DataType>,
773}
774
775impl Expr for MemberAccessExpr {
776    fn kind(&self) -> ExprKind {
777        ExprKind::MemberAccess
778    }
779
780    fn expr_type(&self) -> Box<dyn DataType> {
781        self.member_type.clone()
782    }
783
784    fn as_any(&self) -> &dyn Any {
785        self
786    }
787}