1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
9pub enum CommentType {
10 Line,
12 Block,
14 Hash,
16}
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
27pub enum QuoteStyle {
28 #[default]
30 None,
31 DoubleQuote,
33 Backtick,
35 Bracket,
37}
38
39impl QuoteStyle {
40 #[must_use]
42 pub fn for_dialect(dialect: crate::dialects::Dialect) -> Self {
43 use crate::dialects::Dialect;
44 match dialect {
45 Dialect::Tsql | Dialect::Fabric => QuoteStyle::Bracket,
46 Dialect::Mysql
47 | Dialect::BigQuery
48 | Dialect::Hive
49 | Dialect::Spark
50 | Dialect::Databricks
51 | Dialect::Doris
52 | Dialect::SingleStore
53 | Dialect::StarRocks => QuoteStyle::Backtick,
54 _ => QuoteStyle::DoubleQuote,
56 }
57 }
58
59 #[must_use]
61 pub fn is_quoted(self) -> bool {
62 !matches!(self, QuoteStyle::None)
63 }
64}
65
66#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
74pub enum Statement {
75 Select(SelectStatement),
76 Insert(InsertStatement),
77 Update(UpdateStatement),
78 Delete(DeleteStatement),
79 CreateTable(CreateTableStatement),
80 DropTable(DropTableStatement),
81 SetOperation(SetOperationStatement),
83 AlterTable(AlterTableStatement),
85 CreateView(CreateViewStatement),
87 DropView(DropViewStatement),
89 Truncate(TruncateStatement),
91 Transaction(TransactionStatement),
93 Explain(ExplainStatement),
95 Use(UseStatement),
97 Merge(MergeStatement),
99 Expression(Expr),
101 Command(CommandStatement),
111}
112
113#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
122pub struct SelectStatement {
123 #[serde(default, skip_serializing_if = "Vec::is_empty")]
125 pub comments: Vec<String>,
126 pub ctes: Vec<Cte>,
128 pub distinct: bool,
129 pub top: Option<Box<Expr>>,
131 pub columns: Vec<SelectItem>,
132 pub from: Option<FromClause>,
133 pub joins: Vec<JoinClause>,
134 pub where_clause: Option<Expr>,
135 pub group_by: Vec<Expr>,
136 pub having: Option<Expr>,
137 pub order_by: Vec<OrderByItem>,
138 pub limit: Option<Expr>,
139 pub offset: Option<Expr>,
140 pub fetch_first: Option<Expr>,
142 pub qualify: Option<Expr>,
144 pub window_definitions: Vec<WindowDefinition>,
146}
147
148#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
150pub struct Cte {
151 pub name: String,
152 #[serde(default)]
153 pub name_quote_style: QuoteStyle,
154 pub columns: Vec<String>,
155 pub query: Box<Statement>,
156 pub materialized: Option<bool>,
157 pub recursive: bool,
158}
159
160#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
162pub struct WindowDefinition {
163 pub name: String,
164 pub spec: WindowSpec,
165}
166
167#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
173pub struct SetOperationStatement {
174 #[serde(default, skip_serializing_if = "Vec::is_empty")]
176 pub comments: Vec<String>,
177 pub op: SetOperationType,
178 pub all: bool,
179 pub left: Box<Statement>,
180 pub right: Box<Statement>,
181 pub order_by: Vec<OrderByItem>,
182 pub limit: Option<Expr>,
183 pub offset: Option<Expr>,
184}
185
186#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
187pub enum SetOperationType {
188 Union,
189 Intersect,
190 Except,
191}
192
193#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
199pub enum SelectItem {
200 Wildcard,
202 QualifiedWildcard { table: String },
204 Expr {
206 expr: Expr,
207 alias: Option<String>,
208 #[serde(default)]
209 alias_quote_style: QuoteStyle,
210 },
211}
212
213#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
215pub struct FromClause {
216 pub source: TableSource,
217}
218
219#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
221pub enum TableSource {
222 Table(TableRef),
223 Subquery {
224 query: Box<Statement>,
225 alias: Option<String>,
226 #[serde(default)]
227 alias_quote_style: QuoteStyle,
228 },
229 TableFunction {
230 name: String,
231 args: Vec<Expr>,
232 alias: Option<String>,
233 #[serde(default)]
234 alias_quote_style: QuoteStyle,
235 },
236 Lateral {
238 source: Box<TableSource>,
239 },
240 Unnest {
242 expr: Box<Expr>,
243 alias: Option<String>,
244 #[serde(default)]
245 alias_quote_style: QuoteStyle,
246 with_offset: bool,
247 },
248 Pivot {
250 source: Box<TableSource>,
251 aggregate: Box<Expr>,
252 for_column: String,
253 in_values: Vec<PivotValue>,
254 alias: Option<String>,
255 #[serde(default)]
256 alias_quote_style: QuoteStyle,
257 },
258 Unpivot {
260 source: Box<TableSource>,
261 value_column: String,
262 for_column: String,
263 in_columns: Vec<PivotValue>,
264 alias: Option<String>,
265 #[serde(default)]
266 alias_quote_style: QuoteStyle,
267 },
268}
269
270#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
272pub struct PivotValue {
273 pub value: Expr,
274 pub alias: Option<String>,
275 #[serde(default)]
276 pub alias_quote_style: QuoteStyle,
277}
278
279#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
281pub struct TableRef {
282 pub catalog: Option<String>,
283 pub schema: Option<String>,
284 pub name: String,
285 pub alias: Option<String>,
286 #[serde(default)]
288 pub name_quote_style: QuoteStyle,
289 #[serde(default)]
291 pub alias_quote_style: QuoteStyle,
292}
293
294#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
296pub struct JoinClause {
297 pub join_type: JoinType,
298 pub table: TableSource,
299 pub on: Option<Expr>,
300 pub using: Vec<String>,
301}
302
303#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
305pub enum JoinType {
306 Inner,
307 Left,
308 Right,
309 Full,
310 Cross,
311 Natural,
313 Lateral,
315}
316
317#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
319pub struct OrderByItem {
320 pub expr: Expr,
321 pub ascending: bool,
322 pub nulls_first: Option<bool>,
324}
325
326#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
338pub enum Expr {
339 Column {
341 table: Option<String>,
342 name: String,
343 #[serde(default)]
345 quote_style: QuoteStyle,
346 #[serde(default)]
348 table_quote_style: QuoteStyle,
349 },
350 Number(String),
352 StringLiteral(String),
354 NationalStringLiteral(String),
356 Boolean(bool),
358 Null,
360 BinaryOp {
362 left: Box<Expr>,
363 op: BinaryOperator,
364 right: Box<Expr>,
365 },
366 UnaryOp { op: UnaryOperator, expr: Box<Expr> },
368 Function {
370 name: String,
371 args: Vec<Expr>,
372 distinct: bool,
373 filter: Option<Box<Expr>>,
375 over: Option<WindowSpec>,
377 #[serde(default, skip_serializing_if = "Vec::is_empty")]
382 order_by: Vec<OrderByItem>,
383 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
387 within_group: bool,
388 },
389 Between {
391 expr: Box<Expr>,
392 low: Box<Expr>,
393 high: Box<Expr>,
394 negated: bool,
395 },
396 InList {
398 expr: Box<Expr>,
399 list: Vec<Expr>,
400 negated: bool,
401 },
402 InSubquery {
404 expr: Box<Expr>,
405 subquery: Box<Statement>,
406 negated: bool,
407 },
408 AnyOp {
410 expr: Box<Expr>,
411 op: BinaryOperator,
412 right: Box<Expr>,
413 },
414 AllOp {
416 expr: Box<Expr>,
417 op: BinaryOperator,
418 right: Box<Expr>,
419 },
420 IsNull { expr: Box<Expr>, negated: bool },
422 IsBool {
424 expr: Box<Expr>,
425 value: bool,
426 negated: bool,
427 },
428 Like {
430 expr: Box<Expr>,
431 pattern: Box<Expr>,
432 negated: bool,
433 escape: Option<Box<Expr>>,
434 },
435 ILike {
437 expr: Box<Expr>,
438 pattern: Box<Expr>,
439 negated: bool,
440 escape: Option<Box<Expr>>,
441 },
442 SimilarTo {
444 expr: Box<Expr>,
445 pattern: Box<Expr>,
446 negated: bool,
447 escape: Option<Box<Expr>>,
448 },
449 Case {
451 operand: Option<Box<Expr>>,
452 when_clauses: Vec<(Expr, Expr)>,
453 else_clause: Option<Box<Expr>>,
454 },
455 Nested(Box<Expr>),
457 Wildcard,
459 Subquery(Box<Statement>),
461 Exists {
463 subquery: Box<Statement>,
464 negated: bool,
465 },
466 Cast {
468 expr: Box<Expr>,
469 data_type: DataType,
470 },
471 TryCast {
473 expr: Box<Expr>,
474 data_type: DataType,
475 },
476 Extract {
478 field: DateTimeField,
479 expr: Box<Expr>,
480 },
481 Interval {
483 value: Box<Expr>,
484 unit: Option<DateTimeField>,
485 },
486 ArrayLiteral(Vec<Expr>),
488 Tuple(Vec<Expr>),
490 Coalesce(Vec<Expr>),
492 If {
494 condition: Box<Expr>,
495 true_val: Box<Expr>,
496 false_val: Option<Box<Expr>>,
497 },
498 NullIf { expr: Box<Expr>, r#else: Box<Expr> },
500 Collate { expr: Box<Expr>, collation: String },
502 Parameter(String),
504 TypeExpr(DataType),
506 QualifiedWildcard { table: String },
508 Star,
510 Alias { expr: Box<Expr>, name: String },
512 ArrayIndex { expr: Box<Expr>, index: Box<Expr> },
514 JsonAccess {
516 expr: Box<Expr>,
517 path: Box<Expr>,
518 as_text: bool,
520 },
521 Lambda {
523 params: Vec<String>,
524 body: Box<Expr>,
525 },
526 Default,
528 Cube { exprs: Vec<Expr> },
530 Rollup { exprs: Vec<Expr> },
532 GroupingSets { sets: Vec<Expr> },
534 TypedFunction {
537 func: TypedFunction,
538 filter: Option<Box<Expr>>,
540 over: Option<WindowSpec>,
542 },
543 Commented {
546 expr: Box<Expr>,
547 comments: Vec<String>,
548 },
549}
550
551#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
557pub struct WindowSpec {
558 pub window_ref: Option<String>,
560 pub partition_by: Vec<Expr>,
561 pub order_by: Vec<OrderByItem>,
562 pub frame: Option<WindowFrame>,
563}
564
565#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
566pub struct WindowFrame {
567 pub kind: WindowFrameKind,
568 pub start: WindowFrameBound,
569 pub end: Option<WindowFrameBound>,
570}
571
572#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
573pub enum WindowFrameKind {
574 Rows,
575 Range,
576 Groups,
577}
578
579#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
580pub enum WindowFrameBound {
581 CurrentRow,
582 Preceding(Option<Box<Expr>>), Following(Option<Box<Expr>>), }
585
586#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
591pub enum DateTimeField {
592 Year,
593 Quarter,
594 Month,
595 Week,
596 Day,
597 DayOfWeek,
598 DayOfYear,
599 Hour,
600 Minute,
601 Second,
602 Millisecond,
603 Microsecond,
604 Nanosecond,
605 Epoch,
606 Timezone,
607 TimezoneHour,
608 TimezoneMinute,
609}
610
611#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
617pub enum TrimType {
618 Leading,
619 Trailing,
620 Both,
621}
622
623#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
633pub enum TypedFunction {
634 DateAdd {
637 expr: Box<Expr>,
638 interval: Box<Expr>,
639 unit: Option<DateTimeField>,
640 },
641 DateDiff {
643 start: Box<Expr>,
644 end: Box<Expr>,
645 unit: Option<DateTimeField>,
646 },
647 DateTrunc {
649 unit: DateTimeField,
650 expr: Box<Expr>,
651 },
652 DateSub {
654 expr: Box<Expr>,
655 interval: Box<Expr>,
656 unit: Option<DateTimeField>,
657 },
658 CurrentDate,
660 CurrentTime,
662 CurrentTimestamp,
664 StrToTime { expr: Box<Expr>, format: Box<Expr> },
666 TimeToStr { expr: Box<Expr>, format: Box<Expr> },
668 TsOrDsToDate { expr: Box<Expr> },
670 Year { expr: Box<Expr> },
672 Month { expr: Box<Expr> },
674 Day { expr: Box<Expr> },
676
677 Trim {
680 expr: Box<Expr>,
681 trim_type: TrimType,
682 trim_chars: Option<Box<Expr>>,
683 },
684 Substring {
686 expr: Box<Expr>,
687 start: Box<Expr>,
688 length: Option<Box<Expr>>,
689 },
690 Upper { expr: Box<Expr> },
692 Lower { expr: Box<Expr> },
694 RegexpLike {
696 expr: Box<Expr>,
697 pattern: Box<Expr>,
698 flags: Option<Box<Expr>>,
699 },
700 RegexpExtract {
702 expr: Box<Expr>,
703 pattern: Box<Expr>,
704 group_index: Option<Box<Expr>>,
705 },
706 RegexpReplace {
708 expr: Box<Expr>,
709 pattern: Box<Expr>,
710 replacement: Box<Expr>,
711 flags: Option<Box<Expr>>,
712 },
713 ConcatWs {
715 separator: Box<Expr>,
716 exprs: Vec<Expr>,
717 },
718 Split {
720 expr: Box<Expr>,
721 delimiter: Box<Expr>,
722 },
723 Initcap { expr: Box<Expr> },
725 Length { expr: Box<Expr> },
727 Replace {
729 expr: Box<Expr>,
730 from: Box<Expr>,
731 to: Box<Expr>,
732 },
733 Reverse { expr: Box<Expr> },
735 Left { expr: Box<Expr>, n: Box<Expr> },
737 Right { expr: Box<Expr>, n: Box<Expr> },
739 Lpad {
741 expr: Box<Expr>,
742 length: Box<Expr>,
743 pad: Option<Box<Expr>>,
744 },
745 Rpad {
747 expr: Box<Expr>,
748 length: Box<Expr>,
749 pad: Option<Box<Expr>>,
750 },
751
752 Count { expr: Box<Expr>, distinct: bool },
755 Sum { expr: Box<Expr>, distinct: bool },
757 Avg { expr: Box<Expr>, distinct: bool },
759 Min { expr: Box<Expr> },
761 Max { expr: Box<Expr> },
763 ArrayAgg { expr: Box<Expr>, distinct: bool },
765 ApproxDistinct { expr: Box<Expr> },
767 Variance { expr: Box<Expr> },
769 Stddev { expr: Box<Expr> },
771 GroupConcat {
776 exprs: Vec<Expr>,
779 separator: Option<Box<Expr>>,
781 order_by: Vec<OrderByItem>,
783 distinct: bool,
785 },
786
787 ArrayConcat { arrays: Vec<Expr> },
790 ArrayContains {
792 array: Box<Expr>,
793 element: Box<Expr>,
794 },
795 ArraySize { expr: Box<Expr> },
797 Explode { expr: Box<Expr> },
799 GenerateSeries {
801 start: Box<Expr>,
802 stop: Box<Expr>,
803 step: Option<Box<Expr>>,
804 },
805 Flatten { expr: Box<Expr> },
807
808 JSONExtract { expr: Box<Expr>, path: Box<Expr> },
811 JSONExtractScalar { expr: Box<Expr>, path: Box<Expr> },
813 ParseJSON { expr: Box<Expr> },
815 JSONFormat { expr: Box<Expr> },
817
818 RowNumber,
821 Rank,
823 DenseRank,
825 NTile { n: Box<Expr> },
827 Lead {
829 expr: Box<Expr>,
830 offset: Option<Box<Expr>>,
831 default: Option<Box<Expr>>,
832 },
833 Lag {
835 expr: Box<Expr>,
836 offset: Option<Box<Expr>>,
837 default: Option<Box<Expr>>,
838 },
839 FirstValue { expr: Box<Expr> },
841 LastValue { expr: Box<Expr> },
843
844 Abs { expr: Box<Expr> },
847 Ceil { expr: Box<Expr> },
849 Floor { expr: Box<Expr> },
851 Round {
853 expr: Box<Expr>,
854 decimals: Option<Box<Expr>>,
855 },
856 Log {
858 expr: Box<Expr>,
859 base: Option<Box<Expr>>,
860 },
861 Ln { expr: Box<Expr> },
863 Pow {
865 base: Box<Expr>,
866 exponent: Box<Expr>,
867 },
868 Sqrt { expr: Box<Expr> },
870 Greatest { exprs: Vec<Expr> },
872 Least { exprs: Vec<Expr> },
874 Mod { left: Box<Expr>, right: Box<Expr> },
876
877 Hex { expr: Box<Expr> },
880 Unhex { expr: Box<Expr> },
882 Md5 { expr: Box<Expr> },
884 Sha { expr: Box<Expr> },
886 Sha2 {
888 expr: Box<Expr>,
889 bit_length: Box<Expr>,
890 },
891}
892
893impl TypedFunction {
894 pub fn walk_children<F>(&self, visitor: &mut F)
896 where
897 F: FnMut(&Expr) -> bool,
898 {
899 match self {
900 TypedFunction::DateAdd { expr, interval, .. }
902 | TypedFunction::DateSub { expr, interval, .. } => {
903 expr.walk(visitor);
904 interval.walk(visitor);
905 }
906 TypedFunction::DateDiff { start, end, .. } => {
907 start.walk(visitor);
908 end.walk(visitor);
909 }
910 TypedFunction::DateTrunc { expr, .. } => expr.walk(visitor),
911 TypedFunction::CurrentDate
912 | TypedFunction::CurrentTime
913 | TypedFunction::CurrentTimestamp => {}
914 TypedFunction::StrToTime { expr, format }
915 | TypedFunction::TimeToStr { expr, format } => {
916 expr.walk(visitor);
917 format.walk(visitor);
918 }
919 TypedFunction::TsOrDsToDate { expr }
920 | TypedFunction::Year { expr }
921 | TypedFunction::Month { expr }
922 | TypedFunction::Day { expr } => expr.walk(visitor),
923
924 TypedFunction::Trim {
926 expr, trim_chars, ..
927 } => {
928 expr.walk(visitor);
929 if let Some(c) = trim_chars {
930 c.walk(visitor);
931 }
932 }
933 TypedFunction::Substring {
934 expr,
935 start,
936 length,
937 } => {
938 expr.walk(visitor);
939 start.walk(visitor);
940 if let Some(l) = length {
941 l.walk(visitor);
942 }
943 }
944 TypedFunction::Upper { expr }
945 | TypedFunction::Lower { expr }
946 | TypedFunction::Initcap { expr }
947 | TypedFunction::Length { expr }
948 | TypedFunction::Reverse { expr } => expr.walk(visitor),
949 TypedFunction::RegexpLike {
950 expr,
951 pattern,
952 flags,
953 } => {
954 expr.walk(visitor);
955 pattern.walk(visitor);
956 if let Some(f) = flags {
957 f.walk(visitor);
958 }
959 }
960 TypedFunction::RegexpExtract {
961 expr,
962 pattern,
963 group_index,
964 } => {
965 expr.walk(visitor);
966 pattern.walk(visitor);
967 if let Some(g) = group_index {
968 g.walk(visitor);
969 }
970 }
971 TypedFunction::RegexpReplace {
972 expr,
973 pattern,
974 replacement,
975 flags,
976 } => {
977 expr.walk(visitor);
978 pattern.walk(visitor);
979 replacement.walk(visitor);
980 if let Some(f) = flags {
981 f.walk(visitor);
982 }
983 }
984 TypedFunction::ConcatWs { separator, exprs } => {
985 separator.walk(visitor);
986 for e in exprs {
987 e.walk(visitor);
988 }
989 }
990 TypedFunction::Split { expr, delimiter } => {
991 expr.walk(visitor);
992 delimiter.walk(visitor);
993 }
994 TypedFunction::Replace { expr, from, to } => {
995 expr.walk(visitor);
996 from.walk(visitor);
997 to.walk(visitor);
998 }
999 TypedFunction::Left { expr, n } | TypedFunction::Right { expr, n } => {
1000 expr.walk(visitor);
1001 n.walk(visitor);
1002 }
1003 TypedFunction::Lpad { expr, length, pad }
1004 | TypedFunction::Rpad { expr, length, pad } => {
1005 expr.walk(visitor);
1006 length.walk(visitor);
1007 if let Some(p) = pad {
1008 p.walk(visitor);
1009 }
1010 }
1011
1012 TypedFunction::Count { expr, .. }
1014 | TypedFunction::Sum { expr, .. }
1015 | TypedFunction::Avg { expr, .. }
1016 | TypedFunction::Min { expr }
1017 | TypedFunction::Max { expr }
1018 | TypedFunction::ArrayAgg { expr, .. }
1019 | TypedFunction::ApproxDistinct { expr }
1020 | TypedFunction::Variance { expr }
1021 | TypedFunction::Stddev { expr } => expr.walk(visitor),
1022 TypedFunction::GroupConcat {
1023 exprs,
1024 separator,
1025 order_by,
1026 ..
1027 } => {
1028 for e in exprs {
1029 e.walk(visitor);
1030 }
1031 if let Some(s) = separator {
1032 s.walk(visitor);
1033 }
1034 for o in order_by {
1035 o.expr.walk(visitor);
1036 }
1037 }
1038
1039 TypedFunction::ArrayConcat { arrays } => {
1041 for a in arrays {
1042 a.walk(visitor);
1043 }
1044 }
1045 TypedFunction::ArrayContains { array, element } => {
1046 array.walk(visitor);
1047 element.walk(visitor);
1048 }
1049 TypedFunction::ArraySize { expr }
1050 | TypedFunction::Explode { expr }
1051 | TypedFunction::Flatten { expr } => expr.walk(visitor),
1052 TypedFunction::GenerateSeries { start, stop, step } => {
1053 start.walk(visitor);
1054 stop.walk(visitor);
1055 if let Some(s) = step {
1056 s.walk(visitor);
1057 }
1058 }
1059
1060 TypedFunction::JSONExtract { expr, path }
1062 | TypedFunction::JSONExtractScalar { expr, path } => {
1063 expr.walk(visitor);
1064 path.walk(visitor);
1065 }
1066 TypedFunction::ParseJSON { expr } | TypedFunction::JSONFormat { expr } => {
1067 expr.walk(visitor)
1068 }
1069
1070 TypedFunction::RowNumber | TypedFunction::Rank | TypedFunction::DenseRank => {}
1072 TypedFunction::NTile { n } => n.walk(visitor),
1073 TypedFunction::Lead {
1074 expr,
1075 offset,
1076 default,
1077 }
1078 | TypedFunction::Lag {
1079 expr,
1080 offset,
1081 default,
1082 } => {
1083 expr.walk(visitor);
1084 if let Some(o) = offset {
1085 o.walk(visitor);
1086 }
1087 if let Some(d) = default {
1088 d.walk(visitor);
1089 }
1090 }
1091 TypedFunction::FirstValue { expr } | TypedFunction::LastValue { expr } => {
1092 expr.walk(visitor)
1093 }
1094
1095 TypedFunction::Abs { expr }
1097 | TypedFunction::Ceil { expr }
1098 | TypedFunction::Floor { expr }
1099 | TypedFunction::Ln { expr }
1100 | TypedFunction::Sqrt { expr } => expr.walk(visitor),
1101 TypedFunction::Round { expr, decimals } => {
1102 expr.walk(visitor);
1103 if let Some(d) = decimals {
1104 d.walk(visitor);
1105 }
1106 }
1107 TypedFunction::Log { expr, base } => {
1108 expr.walk(visitor);
1109 if let Some(b) = base {
1110 b.walk(visitor);
1111 }
1112 }
1113 TypedFunction::Pow { base, exponent } => {
1114 base.walk(visitor);
1115 exponent.walk(visitor);
1116 }
1117 TypedFunction::Greatest { exprs } | TypedFunction::Least { exprs } => {
1118 for e in exprs {
1119 e.walk(visitor);
1120 }
1121 }
1122 TypedFunction::Mod { left, right } => {
1123 left.walk(visitor);
1124 right.walk(visitor);
1125 }
1126
1127 TypedFunction::Hex { expr }
1129 | TypedFunction::Unhex { expr }
1130 | TypedFunction::Md5 { expr }
1131 | TypedFunction::Sha { expr } => expr.walk(visitor),
1132 TypedFunction::Sha2 { expr, bit_length } => {
1133 expr.walk(visitor);
1134 bit_length.walk(visitor);
1135 }
1136 }
1137 }
1138
1139 #[must_use]
1141 pub fn transform_children<F>(self, func: &F) -> TypedFunction
1142 where
1143 F: Fn(Expr) -> Expr,
1144 {
1145 match self {
1146 TypedFunction::DateAdd {
1148 expr,
1149 interval,
1150 unit,
1151 } => TypedFunction::DateAdd {
1152 expr: Box::new(expr.transform(func)),
1153 interval: Box::new(interval.transform(func)),
1154 unit,
1155 },
1156 TypedFunction::DateDiff { start, end, unit } => TypedFunction::DateDiff {
1157 start: Box::new(start.transform(func)),
1158 end: Box::new(end.transform(func)),
1159 unit,
1160 },
1161 TypedFunction::DateTrunc { unit, expr } => TypedFunction::DateTrunc {
1162 unit,
1163 expr: Box::new(expr.transform(func)),
1164 },
1165 TypedFunction::DateSub {
1166 expr,
1167 interval,
1168 unit,
1169 } => TypedFunction::DateSub {
1170 expr: Box::new(expr.transform(func)),
1171 interval: Box::new(interval.transform(func)),
1172 unit,
1173 },
1174 TypedFunction::CurrentDate => TypedFunction::CurrentDate,
1175 TypedFunction::CurrentTime => TypedFunction::CurrentTime,
1176 TypedFunction::CurrentTimestamp => TypedFunction::CurrentTimestamp,
1177 TypedFunction::StrToTime { expr, format } => TypedFunction::StrToTime {
1178 expr: Box::new(expr.transform(func)),
1179 format: Box::new(format.transform(func)),
1180 },
1181 TypedFunction::TimeToStr { expr, format } => TypedFunction::TimeToStr {
1182 expr: Box::new(expr.transform(func)),
1183 format: Box::new(format.transform(func)),
1184 },
1185 TypedFunction::TsOrDsToDate { expr } => TypedFunction::TsOrDsToDate {
1186 expr: Box::new(expr.transform(func)),
1187 },
1188 TypedFunction::Year { expr } => TypedFunction::Year {
1189 expr: Box::new(expr.transform(func)),
1190 },
1191 TypedFunction::Month { expr } => TypedFunction::Month {
1192 expr: Box::new(expr.transform(func)),
1193 },
1194 TypedFunction::Day { expr } => TypedFunction::Day {
1195 expr: Box::new(expr.transform(func)),
1196 },
1197
1198 TypedFunction::Trim {
1200 expr,
1201 trim_type,
1202 trim_chars,
1203 } => TypedFunction::Trim {
1204 expr: Box::new(expr.transform(func)),
1205 trim_type,
1206 trim_chars: trim_chars.map(|c| Box::new(c.transform(func))),
1207 },
1208 TypedFunction::Substring {
1209 expr,
1210 start,
1211 length,
1212 } => TypedFunction::Substring {
1213 expr: Box::new(expr.transform(func)),
1214 start: Box::new(start.transform(func)),
1215 length: length.map(|l| Box::new(l.transform(func))),
1216 },
1217 TypedFunction::Upper { expr } => TypedFunction::Upper {
1218 expr: Box::new(expr.transform(func)),
1219 },
1220 TypedFunction::Lower { expr } => TypedFunction::Lower {
1221 expr: Box::new(expr.transform(func)),
1222 },
1223 TypedFunction::RegexpLike {
1224 expr,
1225 pattern,
1226 flags,
1227 } => TypedFunction::RegexpLike {
1228 expr: Box::new(expr.transform(func)),
1229 pattern: Box::new(pattern.transform(func)),
1230 flags: flags.map(|f| Box::new(f.transform(func))),
1231 },
1232 TypedFunction::RegexpExtract {
1233 expr,
1234 pattern,
1235 group_index,
1236 } => TypedFunction::RegexpExtract {
1237 expr: Box::new(expr.transform(func)),
1238 pattern: Box::new(pattern.transform(func)),
1239 group_index: group_index.map(|g| Box::new(g.transform(func))),
1240 },
1241 TypedFunction::RegexpReplace {
1242 expr,
1243 pattern,
1244 replacement,
1245 flags,
1246 } => TypedFunction::RegexpReplace {
1247 expr: Box::new(expr.transform(func)),
1248 pattern: Box::new(pattern.transform(func)),
1249 replacement: Box::new(replacement.transform(func)),
1250 flags: flags.map(|f| Box::new(f.transform(func))),
1251 },
1252 TypedFunction::ConcatWs { separator, exprs } => TypedFunction::ConcatWs {
1253 separator: Box::new(separator.transform(func)),
1254 exprs: exprs.into_iter().map(|e| e.transform(func)).collect(),
1255 },
1256 TypedFunction::Split { expr, delimiter } => TypedFunction::Split {
1257 expr: Box::new(expr.transform(func)),
1258 delimiter: Box::new(delimiter.transform(func)),
1259 },
1260 TypedFunction::Initcap { expr } => TypedFunction::Initcap {
1261 expr: Box::new(expr.transform(func)),
1262 },
1263 TypedFunction::Length { expr } => TypedFunction::Length {
1264 expr: Box::new(expr.transform(func)),
1265 },
1266 TypedFunction::Replace { expr, from, to } => TypedFunction::Replace {
1267 expr: Box::new(expr.transform(func)),
1268 from: Box::new(from.transform(func)),
1269 to: Box::new(to.transform(func)),
1270 },
1271 TypedFunction::Reverse { expr } => TypedFunction::Reverse {
1272 expr: Box::new(expr.transform(func)),
1273 },
1274 TypedFunction::Left { expr, n } => TypedFunction::Left {
1275 expr: Box::new(expr.transform(func)),
1276 n: Box::new(n.transform(func)),
1277 },
1278 TypedFunction::Right { expr, n } => TypedFunction::Right {
1279 expr: Box::new(expr.transform(func)),
1280 n: Box::new(n.transform(func)),
1281 },
1282 TypedFunction::Lpad { expr, length, pad } => TypedFunction::Lpad {
1283 expr: Box::new(expr.transform(func)),
1284 length: Box::new(length.transform(func)),
1285 pad: pad.map(|p| Box::new(p.transform(func))),
1286 },
1287 TypedFunction::Rpad { expr, length, pad } => TypedFunction::Rpad {
1288 expr: Box::new(expr.transform(func)),
1289 length: Box::new(length.transform(func)),
1290 pad: pad.map(|p| Box::new(p.transform(func))),
1291 },
1292
1293 TypedFunction::Count { expr, distinct } => TypedFunction::Count {
1295 expr: Box::new(expr.transform(func)),
1296 distinct,
1297 },
1298 TypedFunction::Sum { expr, distinct } => TypedFunction::Sum {
1299 expr: Box::new(expr.transform(func)),
1300 distinct,
1301 },
1302 TypedFunction::Avg { expr, distinct } => TypedFunction::Avg {
1303 expr: Box::new(expr.transform(func)),
1304 distinct,
1305 },
1306 TypedFunction::Min { expr } => TypedFunction::Min {
1307 expr: Box::new(expr.transform(func)),
1308 },
1309 TypedFunction::Max { expr } => TypedFunction::Max {
1310 expr: Box::new(expr.transform(func)),
1311 },
1312 TypedFunction::ArrayAgg { expr, distinct } => TypedFunction::ArrayAgg {
1313 expr: Box::new(expr.transform(func)),
1314 distinct,
1315 },
1316 TypedFunction::ApproxDistinct { expr } => TypedFunction::ApproxDistinct {
1317 expr: Box::new(expr.transform(func)),
1318 },
1319 TypedFunction::Variance { expr } => TypedFunction::Variance {
1320 expr: Box::new(expr.transform(func)),
1321 },
1322 TypedFunction::Stddev { expr } => TypedFunction::Stddev {
1323 expr: Box::new(expr.transform(func)),
1324 },
1325 TypedFunction::GroupConcat {
1326 exprs,
1327 separator,
1328 order_by,
1329 distinct,
1330 } => TypedFunction::GroupConcat {
1331 exprs: exprs.into_iter().map(|e| e.transform(func)).collect(),
1332 separator: separator.map(|s| Box::new(s.transform(func))),
1333 order_by: order_by
1334 .into_iter()
1335 .map(|o| OrderByItem {
1336 expr: o.expr.transform(func),
1337 ascending: o.ascending,
1338 nulls_first: o.nulls_first,
1339 })
1340 .collect(),
1341 distinct,
1342 },
1343
1344 TypedFunction::ArrayConcat { arrays } => TypedFunction::ArrayConcat {
1346 arrays: arrays.into_iter().map(|a| a.transform(func)).collect(),
1347 },
1348 TypedFunction::ArrayContains { array, element } => TypedFunction::ArrayContains {
1349 array: Box::new(array.transform(func)),
1350 element: Box::new(element.transform(func)),
1351 },
1352 TypedFunction::ArraySize { expr } => TypedFunction::ArraySize {
1353 expr: Box::new(expr.transform(func)),
1354 },
1355 TypedFunction::Explode { expr } => TypedFunction::Explode {
1356 expr: Box::new(expr.transform(func)),
1357 },
1358 TypedFunction::GenerateSeries { start, stop, step } => TypedFunction::GenerateSeries {
1359 start: Box::new(start.transform(func)),
1360 stop: Box::new(stop.transform(func)),
1361 step: step.map(|s| Box::new(s.transform(func))),
1362 },
1363 TypedFunction::Flatten { expr } => TypedFunction::Flatten {
1364 expr: Box::new(expr.transform(func)),
1365 },
1366
1367 TypedFunction::JSONExtract { expr, path } => TypedFunction::JSONExtract {
1369 expr: Box::new(expr.transform(func)),
1370 path: Box::new(path.transform(func)),
1371 },
1372 TypedFunction::JSONExtractScalar { expr, path } => TypedFunction::JSONExtractScalar {
1373 expr: Box::new(expr.transform(func)),
1374 path: Box::new(path.transform(func)),
1375 },
1376 TypedFunction::ParseJSON { expr } => TypedFunction::ParseJSON {
1377 expr: Box::new(expr.transform(func)),
1378 },
1379 TypedFunction::JSONFormat { expr } => TypedFunction::JSONFormat {
1380 expr: Box::new(expr.transform(func)),
1381 },
1382
1383 TypedFunction::RowNumber => TypedFunction::RowNumber,
1385 TypedFunction::Rank => TypedFunction::Rank,
1386 TypedFunction::DenseRank => TypedFunction::DenseRank,
1387 TypedFunction::NTile { n } => TypedFunction::NTile {
1388 n: Box::new(n.transform(func)),
1389 },
1390 TypedFunction::Lead {
1391 expr,
1392 offset,
1393 default,
1394 } => TypedFunction::Lead {
1395 expr: Box::new(expr.transform(func)),
1396 offset: offset.map(|o| Box::new(o.transform(func))),
1397 default: default.map(|d| Box::new(d.transform(func))),
1398 },
1399 TypedFunction::Lag {
1400 expr,
1401 offset,
1402 default,
1403 } => TypedFunction::Lag {
1404 expr: Box::new(expr.transform(func)),
1405 offset: offset.map(|o| Box::new(o.transform(func))),
1406 default: default.map(|d| Box::new(d.transform(func))),
1407 },
1408 TypedFunction::FirstValue { expr } => TypedFunction::FirstValue {
1409 expr: Box::new(expr.transform(func)),
1410 },
1411 TypedFunction::LastValue { expr } => TypedFunction::LastValue {
1412 expr: Box::new(expr.transform(func)),
1413 },
1414
1415 TypedFunction::Abs { expr } => TypedFunction::Abs {
1417 expr: Box::new(expr.transform(func)),
1418 },
1419 TypedFunction::Ceil { expr } => TypedFunction::Ceil {
1420 expr: Box::new(expr.transform(func)),
1421 },
1422 TypedFunction::Floor { expr } => TypedFunction::Floor {
1423 expr: Box::new(expr.transform(func)),
1424 },
1425 TypedFunction::Round { expr, decimals } => TypedFunction::Round {
1426 expr: Box::new(expr.transform(func)),
1427 decimals: decimals.map(|d| Box::new(d.transform(func))),
1428 },
1429 TypedFunction::Log { expr, base } => TypedFunction::Log {
1430 expr: Box::new(expr.transform(func)),
1431 base: base.map(|b| Box::new(b.transform(func))),
1432 },
1433 TypedFunction::Ln { expr } => TypedFunction::Ln {
1434 expr: Box::new(expr.transform(func)),
1435 },
1436 TypedFunction::Pow { base, exponent } => TypedFunction::Pow {
1437 base: Box::new(base.transform(func)),
1438 exponent: Box::new(exponent.transform(func)),
1439 },
1440 TypedFunction::Sqrt { expr } => TypedFunction::Sqrt {
1441 expr: Box::new(expr.transform(func)),
1442 },
1443 TypedFunction::Greatest { exprs } => TypedFunction::Greatest {
1444 exprs: exprs.into_iter().map(|e| e.transform(func)).collect(),
1445 },
1446 TypedFunction::Least { exprs } => TypedFunction::Least {
1447 exprs: exprs.into_iter().map(|e| e.transform(func)).collect(),
1448 },
1449 TypedFunction::Mod { left, right } => TypedFunction::Mod {
1450 left: Box::new(left.transform(func)),
1451 right: Box::new(right.transform(func)),
1452 },
1453
1454 TypedFunction::Hex { expr } => TypedFunction::Hex {
1456 expr: Box::new(expr.transform(func)),
1457 },
1458 TypedFunction::Unhex { expr } => TypedFunction::Unhex {
1459 expr: Box::new(expr.transform(func)),
1460 },
1461 TypedFunction::Md5 { expr } => TypedFunction::Md5 {
1462 expr: Box::new(expr.transform(func)),
1463 },
1464 TypedFunction::Sha { expr } => TypedFunction::Sha {
1465 expr: Box::new(expr.transform(func)),
1466 },
1467 TypedFunction::Sha2 { expr, bit_length } => TypedFunction::Sha2 {
1468 expr: Box::new(expr.transform(func)),
1469 bit_length: Box::new(bit_length.transform(func)),
1470 },
1471 }
1472 }
1473}
1474
1475#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1481pub enum BinaryOperator {
1482 Plus,
1483 Minus,
1484 Multiply,
1485 Divide,
1486 Modulo,
1487 Eq,
1488 Neq,
1489 Lt,
1490 Gt,
1491 LtEq,
1492 GtEq,
1493 And,
1494 Or,
1495 Xor,
1496 Concat,
1497 BitwiseAnd,
1498 BitwiseOr,
1499 BitwiseXor,
1500 ShiftLeft,
1501 ShiftRight,
1502 Arrow,
1504 DoubleArrow,
1506 AtArrow,
1508 ArrowAt,
1510}
1511
1512#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1514pub enum UnaryOperator {
1515 Not,
1516 Minus,
1517 Plus,
1518 BitwiseNot,
1519}
1520
1521#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1527pub struct InsertStatement {
1528 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1530 pub comments: Vec<String>,
1531 pub table: TableRef,
1532 pub columns: Vec<String>,
1533 pub source: InsertSource,
1534 pub on_conflict: Option<OnConflict>,
1536 pub returning: Vec<SelectItem>,
1538}
1539
1540#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1541pub enum InsertSource {
1542 Values(Vec<Vec<Expr>>),
1543 Query(Box<Statement>),
1544 Default,
1545}
1546
1547#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1548pub struct OnConflict {
1549 pub columns: Vec<String>,
1550 pub action: ConflictAction,
1551}
1552
1553#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1554pub enum ConflictAction {
1555 DoNothing,
1556 DoUpdate(Vec<(String, Expr)>),
1557}
1558
1559#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1561pub struct UpdateStatement {
1562 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1564 pub comments: Vec<String>,
1565 pub table: TableRef,
1566 pub assignments: Vec<(String, Expr)>,
1567 pub from: Option<FromClause>,
1568 pub where_clause: Option<Expr>,
1569 pub returning: Vec<SelectItem>,
1570}
1571
1572#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1574pub struct DeleteStatement {
1575 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1577 pub comments: Vec<String>,
1578 pub table: TableRef,
1579 pub using: Option<FromClause>,
1580 pub where_clause: Option<Expr>,
1581 pub returning: Vec<SelectItem>,
1582}
1583
1584#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1594pub struct MergeStatement {
1595 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1597 pub comments: Vec<String>,
1598 pub target: TableRef,
1599 pub source: TableSource,
1600 pub on: Expr,
1601 pub clauses: Vec<MergeClause>,
1602 pub output: Vec<SelectItem>,
1604}
1605
1606#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1608pub struct MergeClause {
1609 pub kind: MergeClauseKind,
1610 pub condition: Option<Expr>,
1612 pub action: MergeAction,
1613}
1614
1615#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1617pub enum MergeClauseKind {
1618 Matched,
1620 NotMatched,
1622 NotMatchedBySource,
1624}
1625
1626#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1628pub enum MergeAction {
1629 Update(Vec<(String, Expr)>),
1631 Insert {
1633 columns: Vec<String>,
1634 values: Vec<Expr>,
1635 },
1636 InsertRow,
1638 Delete,
1640}
1641
1642#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1648pub struct CreateTableStatement {
1649 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1651 pub comments: Vec<String>,
1652 pub if_not_exists: bool,
1653 pub temporary: bool,
1654 pub table: TableRef,
1655 pub columns: Vec<ColumnDef>,
1656 pub constraints: Vec<TableConstraint>,
1657 pub as_select: Option<Box<Statement>>,
1659}
1660
1661#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1663pub enum TableConstraint {
1664 PrimaryKey {
1665 name: Option<String>,
1666 columns: Vec<String>,
1667 },
1668 Unique {
1669 name: Option<String>,
1670 columns: Vec<String>,
1671 },
1672 ForeignKey {
1673 name: Option<String>,
1674 columns: Vec<String>,
1675 ref_table: TableRef,
1676 ref_columns: Vec<String>,
1677 on_delete: Option<ReferentialAction>,
1678 on_update: Option<ReferentialAction>,
1679 },
1680 Check {
1681 name: Option<String>,
1682 expr: Expr,
1683 },
1684}
1685
1686#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1687pub enum ReferentialAction {
1688 Cascade,
1689 Restrict,
1690 NoAction,
1691 SetNull,
1692 SetDefault,
1693}
1694
1695#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1697pub struct ColumnDef {
1698 pub name: String,
1699 pub data_type: DataType,
1700 pub nullable: Option<bool>,
1701 pub default: Option<Expr>,
1702 pub primary_key: bool,
1703 pub unique: bool,
1704 pub auto_increment: bool,
1705 pub collation: Option<String>,
1706 pub comment: Option<String>,
1707}
1708
1709#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1711pub struct AlterTableStatement {
1712 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1714 pub comments: Vec<String>,
1715 pub table: TableRef,
1716 pub actions: Vec<AlterTableAction>,
1717}
1718
1719#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1720pub enum AlterTableAction {
1721 AddColumn(ColumnDef),
1722 DropColumn { name: String, if_exists: bool },
1723 RenameColumn { old_name: String, new_name: String },
1724 AlterColumnType { name: String, data_type: DataType },
1725 AddConstraint(TableConstraint),
1726 DropConstraint { name: String },
1727 RenameTable { new_name: String },
1728}
1729
1730#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1732pub struct CreateViewStatement {
1733 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1735 pub comments: Vec<String>,
1736 pub name: TableRef,
1737 pub columns: Vec<String>,
1738 pub query: Box<Statement>,
1739 pub or_replace: bool,
1740 pub materialized: bool,
1741 pub if_not_exists: bool,
1742}
1743
1744#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1746pub struct DropViewStatement {
1747 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1749 pub comments: Vec<String>,
1750 pub name: TableRef,
1751 pub if_exists: bool,
1752 pub materialized: bool,
1753}
1754
1755#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1757pub struct TruncateStatement {
1758 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1760 pub comments: Vec<String>,
1761 pub table: TableRef,
1762}
1763
1764#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1766pub enum TransactionStatement {
1767 Begin,
1768 Commit,
1769 Rollback,
1770 Savepoint(String),
1771 ReleaseSavepoint(String),
1772 RollbackTo(String),
1773}
1774
1775#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1777pub struct ExplainStatement {
1778 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1780 pub comments: Vec<String>,
1781 pub analyze: bool,
1782 pub statement: Box<Statement>,
1783}
1784
1785#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1787pub struct UseStatement {
1788 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1790 pub comments: Vec<String>,
1791 pub name: String,
1792}
1793
1794#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1803pub struct CommandStatement {
1804 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1806 pub comments: Vec<String>,
1807 pub kind: String,
1811 pub body: String,
1814}
1815
1816#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1818pub struct DropTableStatement {
1819 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1821 pub comments: Vec<String>,
1822 pub if_exists: bool,
1823 pub table: TableRef,
1824 pub cascade: bool,
1825}
1826
1827#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1833pub enum DataType {
1834 TinyInt,
1836 SmallInt,
1837 Int,
1838 BigInt,
1839 Float,
1840 Double,
1841 Decimal {
1842 precision: Option<u32>,
1843 scale: Option<u32>,
1844 },
1845 Numeric {
1846 precision: Option<u32>,
1847 scale: Option<u32>,
1848 },
1849 Real,
1850
1851 Varchar(Option<u32>),
1853 Char(Option<u32>),
1854 Text,
1855 String,
1856 Binary(Option<u32>),
1857 Varbinary(Option<u32>),
1858
1859 Boolean,
1861
1862 Date,
1864 Time {
1865 precision: Option<u32>,
1866 },
1867 Timestamp {
1868 precision: Option<u32>,
1869 with_tz: bool,
1870 },
1871 Interval,
1872 DateTime,
1873
1874 Blob,
1876 Bytea,
1877 Bytes,
1878
1879 Json,
1881 Jsonb,
1882
1883 Uuid,
1885
1886 Array(Option<Box<DataType>>),
1888 Map {
1889 key: Box<DataType>,
1890 value: Box<DataType>,
1891 },
1892 Struct(Vec<(String, DataType)>),
1893 Tuple(Vec<DataType>),
1894
1895 Null,
1897 Unknown(String),
1898 Variant,
1899 Object,
1900 Xml,
1901 Inet,
1902 Cidr,
1903 Macaddr,
1904 Bit(Option<u32>),
1905 Money,
1906 Serial,
1907 BigSerial,
1908 SmallSerial,
1909 Regclass,
1910 Regtype,
1911 Hstore,
1912 Geography,
1913 Geometry,
1914 Super,
1915}
1916
1917impl Expr {
1922 pub fn walk<F>(&self, visitor: &mut F)
1925 where
1926 F: FnMut(&Expr) -> bool,
1927 {
1928 if !visitor(self) {
1929 return;
1930 }
1931 match self {
1932 Expr::BinaryOp { left, right, .. } => {
1933 left.walk(visitor);
1934 right.walk(visitor);
1935 }
1936 Expr::UnaryOp { expr, .. } => expr.walk(visitor),
1937 Expr::Function { args, filter, .. } => {
1938 for arg in args {
1939 arg.walk(visitor);
1940 }
1941 if let Some(f) = filter {
1942 f.walk(visitor);
1943 }
1944 }
1945 Expr::Between {
1946 expr, low, high, ..
1947 } => {
1948 expr.walk(visitor);
1949 low.walk(visitor);
1950 high.walk(visitor);
1951 }
1952 Expr::InList { expr, list, .. } => {
1953 expr.walk(visitor);
1954 for item in list {
1955 item.walk(visitor);
1956 }
1957 }
1958 Expr::InSubquery { expr, .. } => {
1959 expr.walk(visitor);
1960 }
1961 Expr::IsNull { expr, .. } => expr.walk(visitor),
1962 Expr::IsBool { expr, .. } => expr.walk(visitor),
1963 Expr::AnyOp { expr, right, .. } | Expr::AllOp { expr, right, .. } => {
1964 expr.walk(visitor);
1965 right.walk(visitor);
1966 }
1967 Expr::Like { expr, pattern, .. }
1968 | Expr::ILike { expr, pattern, .. }
1969 | Expr::SimilarTo { expr, pattern, .. } => {
1970 expr.walk(visitor);
1971 pattern.walk(visitor);
1972 }
1973 Expr::Case {
1974 operand,
1975 when_clauses,
1976 else_clause,
1977 } => {
1978 if let Some(op) = operand {
1979 op.walk(visitor);
1980 }
1981 for (cond, result) in when_clauses {
1982 cond.walk(visitor);
1983 result.walk(visitor);
1984 }
1985 if let Some(el) = else_clause {
1986 el.walk(visitor);
1987 }
1988 }
1989 Expr::Nested(inner) => inner.walk(visitor),
1990 Expr::Cast { expr, .. } | Expr::TryCast { expr, .. } => expr.walk(visitor),
1991 Expr::Extract { expr, .. } => expr.walk(visitor),
1992 Expr::Interval { value, .. } => value.walk(visitor),
1993 Expr::ArrayLiteral(items) | Expr::Tuple(items) | Expr::Coalesce(items) => {
1994 for item in items {
1995 item.walk(visitor);
1996 }
1997 }
1998 Expr::If {
1999 condition,
2000 true_val,
2001 false_val,
2002 } => {
2003 condition.walk(visitor);
2004 true_val.walk(visitor);
2005 if let Some(fv) = false_val {
2006 fv.walk(visitor);
2007 }
2008 }
2009 Expr::NullIf { expr, r#else } => {
2010 expr.walk(visitor);
2011 r#else.walk(visitor);
2012 }
2013 Expr::Collate { expr, .. } => expr.walk(visitor),
2014 Expr::Alias { expr, .. } => expr.walk(visitor),
2015 Expr::ArrayIndex { expr, index } => {
2016 expr.walk(visitor);
2017 index.walk(visitor);
2018 }
2019 Expr::JsonAccess { expr, path, .. } => {
2020 expr.walk(visitor);
2021 path.walk(visitor);
2022 }
2023 Expr::Lambda { body, .. } => body.walk(visitor),
2024 Expr::TypedFunction { func, filter, .. } => {
2025 func.walk_children(visitor);
2026 if let Some(f) = filter {
2027 f.walk(visitor);
2028 }
2029 }
2030 Expr::Cube { exprs } | Expr::Rollup { exprs } => {
2031 for item in exprs {
2032 item.walk(visitor);
2033 }
2034 }
2035 Expr::GroupingSets { sets } => {
2036 for item in sets {
2037 item.walk(visitor);
2038 }
2039 }
2040 Expr::Commented { expr, .. } => expr.walk(visitor),
2041 Expr::Column { .. }
2043 | Expr::Number(_)
2044 | Expr::StringLiteral(_)
2045 | Expr::NationalStringLiteral(_)
2046 | Expr::Boolean(_)
2047 | Expr::Null
2048 | Expr::Wildcard
2049 | Expr::Star
2050 | Expr::Parameter(_)
2051 | Expr::TypeExpr(_)
2052 | Expr::QualifiedWildcard { .. }
2053 | Expr::Default
2054 | Expr::Subquery(_)
2055 | Expr::Exists { .. } => {}
2056 }
2057 }
2058
2059 #[must_use]
2061 pub fn find<F>(&self, predicate: &F) -> Option<&Expr>
2062 where
2063 F: Fn(&Expr) -> bool,
2064 {
2065 let mut result = None;
2066 self.walk(&mut |expr| {
2067 if result.is_some() {
2068 return false;
2069 }
2070 if predicate(expr) {
2071 result = Some(expr as *const Expr);
2072 false
2073 } else {
2074 true
2075 }
2076 });
2077 result.map(|p| unsafe { &*p })
2079 }
2080
2081 #[must_use]
2083 pub fn find_all<F>(&self, predicate: &F) -> Vec<&Expr>
2084 where
2085 F: Fn(&Expr) -> bool,
2086 {
2087 let mut results: Vec<*const Expr> = Vec::new();
2088 self.walk(&mut |expr| {
2089 if predicate(expr) {
2090 results.push(expr as *const Expr);
2091 }
2092 true
2093 });
2094 results.into_iter().map(|p| unsafe { &*p }).collect()
2095 }
2096
2097 #[must_use]
2100 pub fn transform<F>(self, func: &F) -> Expr
2101 where
2102 F: Fn(Expr) -> Expr,
2103 {
2104 let transformed = match self {
2105 Expr::BinaryOp { left, op, right } => Expr::BinaryOp {
2106 left: Box::new(left.transform(func)),
2107 op,
2108 right: Box::new(right.transform(func)),
2109 },
2110 Expr::UnaryOp { op, expr } => Expr::UnaryOp {
2111 op,
2112 expr: Box::new(expr.transform(func)),
2113 },
2114 Expr::Function {
2115 name,
2116 args,
2117 distinct,
2118 filter,
2119 over,
2120 order_by,
2121 within_group,
2122 } => Expr::Function {
2123 name,
2124 args: args.into_iter().map(|a| a.transform(func)).collect(),
2125 distinct,
2126 filter: filter.map(|f| Box::new(f.transform(func))),
2127 over,
2128 order_by,
2129 within_group,
2130 },
2131 Expr::Nested(inner) => Expr::Nested(Box::new(inner.transform(func))),
2132 Expr::Cast { expr, data_type } => Expr::Cast {
2133 expr: Box::new(expr.transform(func)),
2134 data_type,
2135 },
2136 Expr::Between {
2137 expr,
2138 low,
2139 high,
2140 negated,
2141 } => Expr::Between {
2142 expr: Box::new(expr.transform(func)),
2143 low: Box::new(low.transform(func)),
2144 high: Box::new(high.transform(func)),
2145 negated,
2146 },
2147 Expr::Case {
2148 operand,
2149 when_clauses,
2150 else_clause,
2151 } => Expr::Case {
2152 operand: operand.map(|o| Box::new(o.transform(func))),
2153 when_clauses: when_clauses
2154 .into_iter()
2155 .map(|(c, r)| (c.transform(func), r.transform(func)))
2156 .collect(),
2157 else_clause: else_clause.map(|e| Box::new(e.transform(func))),
2158 },
2159 Expr::IsBool {
2160 expr,
2161 value,
2162 negated,
2163 } => Expr::IsBool {
2164 expr: Box::new(expr.transform(func)),
2165 value,
2166 negated,
2167 },
2168 Expr::AnyOp { expr, op, right } => Expr::AnyOp {
2169 expr: Box::new(expr.transform(func)),
2170 op,
2171 right: Box::new(right.transform(func)),
2172 },
2173 Expr::AllOp { expr, op, right } => Expr::AllOp {
2174 expr: Box::new(expr.transform(func)),
2175 op,
2176 right: Box::new(right.transform(func)),
2177 },
2178 Expr::TypedFunction {
2179 func: tf,
2180 filter,
2181 over,
2182 } => Expr::TypedFunction {
2183 func: tf.transform_children(func),
2184 filter: filter.map(|f| Box::new(f.transform(func))),
2185 over,
2186 },
2187 Expr::InList {
2188 expr,
2189 list,
2190 negated,
2191 } => Expr::InList {
2192 expr: Box::new(expr.transform(func)),
2193 list: list.into_iter().map(|e| e.transform(func)).collect(),
2194 negated,
2195 },
2196 Expr::InSubquery {
2197 expr,
2198 subquery,
2199 negated,
2200 } => Expr::InSubquery {
2201 expr: Box::new(expr.transform(func)),
2202 subquery, negated,
2204 },
2205 Expr::IsNull { expr, negated } => Expr::IsNull {
2206 expr: Box::new(expr.transform(func)),
2207 negated,
2208 },
2209 Expr::Like {
2210 expr,
2211 pattern,
2212 negated,
2213 escape,
2214 } => Expr::Like {
2215 expr: Box::new(expr.transform(func)),
2216 pattern: Box::new(pattern.transform(func)),
2217 negated,
2218 escape: escape.map(|e| Box::new(e.transform(func))),
2219 },
2220 Expr::ILike {
2221 expr,
2222 pattern,
2223 negated,
2224 escape,
2225 } => Expr::ILike {
2226 expr: Box::new(expr.transform(func)),
2227 pattern: Box::new(pattern.transform(func)),
2228 negated,
2229 escape: escape.map(|e| Box::new(e.transform(func))),
2230 },
2231 Expr::TryCast { expr, data_type } => Expr::TryCast {
2232 expr: Box::new(expr.transform(func)),
2233 data_type,
2234 },
2235 Expr::Extract { field, expr } => Expr::Extract {
2236 field,
2237 expr: Box::new(expr.transform(func)),
2238 },
2239 Expr::Interval { value, unit } => Expr::Interval {
2240 value: Box::new(value.transform(func)),
2241 unit,
2242 },
2243 Expr::ArrayLiteral(elems) => {
2244 Expr::ArrayLiteral(elems.into_iter().map(|e| e.transform(func)).collect())
2245 }
2246 Expr::Tuple(elems) => {
2247 Expr::Tuple(elems.into_iter().map(|e| e.transform(func)).collect())
2248 }
2249 Expr::Coalesce(elems) => {
2250 Expr::Coalesce(elems.into_iter().map(|e| e.transform(func)).collect())
2251 }
2252 Expr::If {
2253 condition,
2254 true_val,
2255 false_val,
2256 } => Expr::If {
2257 condition: Box::new(condition.transform(func)),
2258 true_val: Box::new(true_val.transform(func)),
2259 false_val: false_val.map(|f| Box::new(f.transform(func))),
2260 },
2261 Expr::NullIf { expr, r#else } => Expr::NullIf {
2262 expr: Box::new(expr.transform(func)),
2263 r#else: Box::new(r#else.transform(func)),
2264 },
2265 Expr::Collate { expr, collation } => Expr::Collate {
2266 expr: Box::new(expr.transform(func)),
2267 collation,
2268 },
2269 Expr::Alias { expr, name } => Expr::Alias {
2270 expr: Box::new(expr.transform(func)),
2271 name,
2272 },
2273 Expr::ArrayIndex { expr, index } => Expr::ArrayIndex {
2274 expr: Box::new(expr.transform(func)),
2275 index: Box::new(index.transform(func)),
2276 },
2277 Expr::JsonAccess {
2278 expr,
2279 path,
2280 as_text,
2281 } => Expr::JsonAccess {
2282 expr: Box::new(expr.transform(func)),
2283 path: Box::new(path.transform(func)),
2284 as_text,
2285 },
2286 Expr::Lambda { params, body } => Expr::Lambda {
2287 params,
2288 body: Box::new(body.transform(func)),
2289 },
2290 Expr::Cube { exprs } => Expr::Cube {
2291 exprs: exprs.into_iter().map(|e| e.transform(func)).collect(),
2292 },
2293 Expr::Rollup { exprs } => Expr::Rollup {
2294 exprs: exprs.into_iter().map(|e| e.transform(func)).collect(),
2295 },
2296 Expr::GroupingSets { sets } => Expr::GroupingSets {
2297 sets: sets.into_iter().map(|e| e.transform(func)).collect(),
2298 },
2299 Expr::Commented { expr, comments } => Expr::Commented {
2300 expr: Box::new(expr.transform(func)),
2301 comments,
2302 },
2303 other => other,
2304 };
2305 func(transformed)
2306 }
2307
2308 #[must_use]
2310 pub fn is_column(&self) -> bool {
2311 matches!(self, Expr::Column { .. })
2312 }
2313
2314 #[must_use]
2316 pub fn is_literal(&self) -> bool {
2317 matches!(
2318 self,
2319 Expr::Number(_)
2320 | Expr::StringLiteral(_)
2321 | Expr::NationalStringLiteral(_)
2322 | Expr::Boolean(_)
2323 | Expr::Null
2324 )
2325 }
2326
2327 #[must_use]
2330 pub fn sql(&self) -> String {
2331 use crate::generator::Generator;
2332 Generator::expr_to_sql(self)
2333 }
2334}
2335
2336#[must_use]
2338pub fn find_columns(expr: &Expr) -> Vec<&Expr> {
2339 expr.find_all(&|e| matches!(e, Expr::Column { .. }))
2340}
2341
2342#[must_use]
2344pub fn find_tables(statement: &Statement) -> Vec<&TableRef> {
2345 match statement {
2346 Statement::Select(sel) => {
2347 let mut tables = Vec::new();
2348 if let Some(from) = &sel.from {
2349 collect_table_refs_from_source(&from.source, &mut tables);
2350 }
2351 for join in &sel.joins {
2352 collect_table_refs_from_source(&join.table, &mut tables);
2353 }
2354 tables
2355 }
2356 Statement::Insert(ins) => vec![&ins.table],
2357 Statement::Update(upd) => vec![&upd.table],
2358 Statement::Delete(del) => vec![&del.table],
2359 Statement::CreateTable(ct) => vec![&ct.table],
2360 Statement::DropTable(dt) => vec![&dt.table],
2361 _ => vec![],
2362 }
2363}
2364
2365fn collect_table_refs_from_source<'a>(source: &'a TableSource, tables: &mut Vec<&'a TableRef>) {
2366 match source {
2367 TableSource::Table(table_ref) => tables.push(table_ref),
2368 TableSource::Subquery { .. } => {}
2369 TableSource::TableFunction { .. } => {}
2370 TableSource::Lateral { source } => collect_table_refs_from_source(source, tables),
2371 TableSource::Pivot { source, .. } | TableSource::Unpivot { source, .. } => {
2372 collect_table_refs_from_source(source, tables);
2373 }
2374 TableSource::Unnest { .. } => {}
2375 }
2376}