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 Expr for NumberExpr {
195 fn kind(&self) -> ExprKind {
196 ExprKind::Number
197 }
198
199 fn expr_type(&self) -> Box<dyn DataType> {
200 match self.value {
201 Number::Int(_) => Box::new(IntType),
202 Number::Float(_) => Box::new(FloatType),
203 }
204 }
205
206 fn as_any(&self) -> &dyn Any {
207 self
208 }
209}
210
211#[derive(Clone)]
212pub struct IntervalExpr {
213 pub interval: Interval,
214}
215
216impl IntervalExpr {
217 pub fn new(interval: Interval) -> Self {
218 IntervalExpr { interval }
219 }
220}
221
222impl Expr for IntervalExpr {
223 fn kind(&self) -> ExprKind {
224 ExprKind::Interval
225 }
226
227 fn expr_type(&self) -> Box<dyn DataType> {
228 Box::new(IntervalType)
229 }
230
231 fn as_any(&self) -> &dyn Any {
232 self
233 }
234}
235
236#[derive(Clone)]
237pub struct BooleanExpr {
238 pub is_true: bool,
239}
240
241impl Expr for BooleanExpr {
242 fn kind(&self) -> ExprKind {
243 ExprKind::Boolean
244 }
245
246 fn expr_type(&self) -> Box<dyn DataType> {
247 Box::new(BoolType)
248 }
249
250 fn as_any(&self) -> &dyn Any {
251 self
252 }
253}
254
255#[derive(Clone)]
256pub struct UnaryExpr {
257 pub right: Box<dyn Expr>,
258 pub operator: PrefixUnaryOperator,
259 pub result_type: Box<dyn DataType>,
260}
261
262impl Expr for UnaryExpr {
263 fn kind(&self) -> ExprKind {
264 ExprKind::PrefixUnary
265 }
266
267 fn expr_type(&self) -> Box<dyn DataType> {
268 self.result_type.clone()
269 }
270
271 fn as_any(&self) -> &dyn Any {
272 self
273 }
274}
275
276#[derive(Clone)]
277pub struct IndexExpr {
278 pub collection: Box<dyn Expr>,
279 pub element_type: Box<dyn DataType>,
280 pub index: Box<dyn Expr>,
281 pub result_type: Box<dyn DataType>,
282}
283
284impl Expr for IndexExpr {
285 fn kind(&self) -> ExprKind {
286 ExprKind::Index
287 }
288
289 fn expr_type(&self) -> Box<dyn DataType> {
290 self.result_type.clone()
291 }
292
293 fn as_any(&self) -> &dyn Any {
294 self
295 }
296}
297
298#[derive(Clone)]
299pub struct SliceExpr {
300 pub collection: Box<dyn Expr>,
301 pub start: Option<Box<dyn Expr>>,
302 pub end: Option<Box<dyn Expr>>,
303 pub result_type: Box<dyn DataType>,
304}
305
306impl Expr for SliceExpr {
307 fn kind(&self) -> ExprKind {
308 ExprKind::Slice
309 }
310
311 fn expr_type(&self) -> Box<dyn DataType> {
312 self.result_type.clone()
313 }
314
315 fn as_any(&self) -> &dyn Any {
316 self
317 }
318}
319
320#[derive(Clone)]
321pub struct ArithmeticExpr {
322 pub left: Box<dyn Expr>,
323 pub operator: ArithmeticOperator,
324 pub right: Box<dyn Expr>,
325 pub result_type: Box<dyn DataType>,
326}
327
328impl Expr for ArithmeticExpr {
329 fn kind(&self) -> ExprKind {
330 ExprKind::Arithmetic
331 }
332
333 fn expr_type(&self) -> Box<dyn DataType> {
334 self.result_type.clone()
335 }
336
337 fn as_any(&self) -> &dyn Any {
338 self
339 }
340}
341
342#[derive(Clone)]
343pub struct ComparisonExpr {
344 pub left: Box<dyn Expr>,
345 pub operator: ComparisonOperator,
346 pub right: Box<dyn Expr>,
347}
348
349impl Expr for ComparisonExpr {
350 fn kind(&self) -> ExprKind {
351 ExprKind::Comparison
352 }
353
354 fn expr_type(&self) -> Box<dyn DataType> {
355 if self.operator == ComparisonOperator::NullSafeEqual {
356 Box::new(IntType)
357 } else {
358 Box::new(BoolType)
359 }
360 }
361
362 fn as_any(&self) -> &dyn Any {
363 self
364 }
365}
366
367#[derive(Clone)]
368pub struct GroupComparisonExpr {
369 pub left: Box<dyn Expr>,
370 pub comparison_operator: ComparisonOperator,
371 pub group_operator: GroupComparisonOperator,
372 pub right: Box<dyn Expr>,
373}
374
375impl Expr for GroupComparisonExpr {
376 fn kind(&self) -> ExprKind {
377 ExprKind::GroupComparison
378 }
379
380 fn expr_type(&self) -> Box<dyn DataType> {
381 if self.comparison_operator == ComparisonOperator::NullSafeEqual {
382 Box::new(IntType)
383 } else {
384 Box::new(BoolType)
385 }
386 }
387
388 fn as_any(&self) -> &dyn Any {
389 self
390 }
391}
392
393#[derive(Clone)]
394pub struct ContainsExpr {
395 pub left: Box<dyn Expr>,
396 pub right: Box<dyn Expr>,
397}
398
399impl Expr for ContainsExpr {
400 fn kind(&self) -> ExprKind {
401 ExprKind::Contains
402 }
403
404 fn expr_type(&self) -> Box<dyn DataType> {
405 Box::new(BoolType)
406 }
407
408 fn as_any(&self) -> &dyn Any {
409 self
410 }
411}
412
413#[derive(Clone)]
414pub struct ContainedByExpr {
415 pub left: Box<dyn Expr>,
416 pub right: Box<dyn Expr>,
417}
418
419impl Expr for ContainedByExpr {
420 fn kind(&self) -> ExprKind {
421 ExprKind::ContainedBy
422 }
423
424 fn expr_type(&self) -> Box<dyn DataType> {
425 Box::new(BoolType)
426 }
427
428 fn as_any(&self) -> &dyn Any {
429 self
430 }
431}
432
433#[derive(Clone)]
434pub struct LikeExpr {
435 pub input: Box<dyn Expr>,
436 pub pattern: Box<dyn Expr>,
437}
438
439impl Expr for LikeExpr {
440 fn kind(&self) -> ExprKind {
441 ExprKind::Like
442 }
443
444 fn expr_type(&self) -> Box<dyn DataType> {
445 Box::new(BoolType)
446 }
447
448 fn as_any(&self) -> &dyn Any {
449 self
450 }
451}
452
453#[derive(Clone)]
454pub struct RegexExpr {
455 pub input: Box<dyn Expr>,
456 pub pattern: Box<dyn Expr>,
457}
458
459impl Expr for RegexExpr {
460 fn kind(&self) -> ExprKind {
461 ExprKind::Regex
462 }
463
464 fn expr_type(&self) -> Box<dyn DataType> {
465 Box::new(BoolType)
466 }
467
468 fn as_any(&self) -> &dyn Any {
469 self
470 }
471}
472
473#[derive(Clone)]
474pub struct GlobExpr {
475 pub input: Box<dyn Expr>,
476 pub pattern: Box<dyn Expr>,
477}
478
479impl Expr for GlobExpr {
480 fn kind(&self) -> ExprKind {
481 ExprKind::Glob
482 }
483
484 fn expr_type(&self) -> Box<dyn DataType> {
485 Box::new(BoolType)
486 }
487
488 fn as_any(&self) -> &dyn Any {
489 self
490 }
491}
492
493#[derive(Clone)]
494pub struct LogicalExpr {
495 pub left: Box<dyn Expr>,
496 pub operator: BinaryLogicalOperator,
497 pub right: Box<dyn Expr>,
498}
499
500impl Expr for LogicalExpr {
501 fn kind(&self) -> ExprKind {
502 ExprKind::Logical
503 }
504
505 fn expr_type(&self) -> Box<dyn DataType> {
506 let lhs_type = self.left.expr_type();
507 let rhs_type = &self.right.expr_type();
508 match self.operator {
509 BinaryLogicalOperator::Or => lhs_type.logical_or_op_result_type(rhs_type),
510 BinaryLogicalOperator::And => lhs_type.logical_and_op_result_type(rhs_type),
511 BinaryLogicalOperator::Xor => lhs_type.logical_xor_op_result_type(rhs_type),
512 }
513 }
514
515 fn as_any(&self) -> &dyn Any {
516 self
517 }
518}
519
520#[derive(Clone)]
521pub struct BitwiseExpr {
522 pub left: Box<dyn Expr>,
523 pub operator: BinaryBitwiseOperator,
524 pub right: Box<dyn Expr>,
525 pub result_type: Box<dyn DataType>,
526}
527
528impl Expr for BitwiseExpr {
529 fn kind(&self) -> ExprKind {
530 ExprKind::Bitwise
531 }
532
533 fn expr_type(&self) -> Box<dyn DataType> {
534 self.result_type.clone()
535 }
536
537 fn as_any(&self) -> &dyn Any {
538 self
539 }
540}
541
542#[derive(Clone)]
543pub struct CallExpr {
544 pub function_name: String,
545 pub arguments: Vec<Box<dyn Expr>>,
546 pub return_type: Box<dyn DataType>,
547}
548
549impl Expr for CallExpr {
550 fn kind(&self) -> ExprKind {
551 ExprKind::Call
552 }
553
554 fn expr_type(&self) -> Box<dyn DataType> {
555 self.return_type.clone()
556 }
557
558 fn as_any(&self) -> &dyn Any {
559 self
560 }
561}
562
563#[derive(Clone)]
564pub struct BenchmarkCallExpr {
565 pub expression: Box<dyn Expr>,
566 pub count: Box<dyn Expr>,
567}
568
569impl Expr for BenchmarkCallExpr {
570 fn kind(&self) -> ExprKind {
571 ExprKind::BenchmarkCall
572 }
573
574 fn expr_type(&self) -> Box<dyn DataType> {
575 Box::new(IntType)
576 }
577
578 fn as_any(&self) -> &dyn Any {
579 self
580 }
581}
582
583#[derive(PartialEq, Clone)]
587pub enum BetweenKind {
588 Symmetric,
589 Asymmetric,
590}
591
592#[derive(Clone)]
593pub struct BetweenExpr {
594 pub value: Box<dyn Expr>,
595 pub range_start: Box<dyn Expr>,
596 pub range_end: Box<dyn Expr>,
597 pub kind: BetweenKind,
598}
599
600impl Expr for BetweenExpr {
601 fn kind(&self) -> ExprKind {
602 ExprKind::Between
603 }
604
605 fn expr_type(&self) -> Box<dyn DataType> {
606 Box::new(BoolType)
607 }
608
609 fn as_any(&self) -> &dyn Any {
610 self
611 }
612}
613
614#[derive(Clone)]
615pub struct CaseExpr {
616 pub conditions: Vec<Box<dyn Expr>>,
617 pub values: Vec<Box<dyn Expr>>,
618 pub default_value: Option<Box<dyn Expr>>,
619 pub values_type: Box<dyn DataType>,
620}
621
622impl Expr for CaseExpr {
623 fn kind(&self) -> ExprKind {
624 ExprKind::Case
625 }
626
627 fn expr_type(&self) -> Box<dyn DataType> {
628 self.values_type.clone()
629 }
630
631 fn as_any(&self) -> &dyn Any {
632 self
633 }
634}
635
636#[derive(Clone)]
637pub struct InExpr {
638 pub argument: Box<dyn Expr>,
639 pub values: Vec<Box<dyn Expr>>,
640 pub values_type: Box<dyn DataType>,
641 pub has_not_keyword: bool,
642}
643
644impl Expr for InExpr {
645 fn kind(&self) -> ExprKind {
646 ExprKind::In
647 }
648
649 fn expr_type(&self) -> Box<dyn DataType> {
650 self.values_type.clone()
651 }
652
653 fn as_any(&self) -> &dyn Any {
654 self
655 }
656}
657
658#[derive(Clone)]
659pub struct IsNullExpr {
660 pub argument: Box<dyn Expr>,
661 pub has_not: bool,
662}
663
664impl Expr for IsNullExpr {
665 fn kind(&self) -> ExprKind {
666 ExprKind::IsNull
667 }
668
669 fn expr_type(&self) -> Box<dyn DataType> {
670 Box::new(BoolType)
671 }
672
673 fn as_any(&self) -> &dyn Any {
674 self
675 }
676}
677
678#[derive(Clone)]
679pub struct NullExpr;
680
681impl Expr for NullExpr {
682 fn kind(&self) -> ExprKind {
683 ExprKind::Null
684 }
685
686 fn expr_type(&self) -> Box<dyn DataType> {
687 Box::new(NullType)
688 }
689
690 fn as_any(&self) -> &dyn Any {
691 self
692 }
693}
694
695#[derive(Clone)]
696pub struct CastExpr {
697 pub value: Box<dyn Expr>,
698 pub result_type: Box<dyn DataType>,
699}
700
701impl Expr for CastExpr {
702 fn kind(&self) -> ExprKind {
703 ExprKind::Cast
704 }
705
706 fn expr_type(&self) -> Box<dyn DataType> {
707 self.result_type.clone()
708 }
709
710 fn as_any(&self) -> &dyn Any {
711 self
712 }
713}
714
715#[derive(Clone)]
716pub struct ColumnExpr {
717 pub expr: Box<dyn Expr>,
718}
719
720impl Expr for ColumnExpr {
721 fn kind(&self) -> ExprKind {
722 ExprKind::Column
723 }
724
725 fn expr_type(&self) -> Box<dyn DataType> {
726 self.expr.expr_type().clone()
727 }
728
729 fn as_any(&self) -> &dyn Any {
730 self
731 }
732}
733
734#[derive(Clone)]
735pub struct RowExpr {
736 pub exprs: Vec<Box<dyn Expr>>,
737 pub row_type: RowType,
738}
739
740impl Expr for RowExpr {
741 fn kind(&self) -> ExprKind {
742 ExprKind::Row
743 }
744
745 fn expr_type(&self) -> Box<dyn DataType> {
746 Box::new(self.row_type.clone())
747 }
748
749 fn as_any(&self) -> &dyn Any {
750 self
751 }
752}
753
754#[derive(Clone)]
755pub struct MemberAccessExpr {
756 pub composite: Box<dyn Expr>,
757 pub member_name: String,
758 pub member_type: Box<dyn DataType>,
759}
760
761impl Expr for MemberAccessExpr {
762 fn kind(&self) -> ExprKind {
763 ExprKind::MemberAccess
764 }
765
766 fn expr_type(&self) -> Box<dyn DataType> {
767 self.member_type.clone()
768 }
769
770 fn as_any(&self) -> &dyn Any {
771 self
772 }
773}