1#[cfg(not(feature = "std"))]
19use alloc::{boxed::Box, vec::Vec};
20
21use helpers::attached_token::AttachedToken;
22#[cfg(feature = "serde")]
23use serde::{Deserialize, Serialize};
24
25#[cfg(feature = "visitor")]
26use sqlparser_derive::{Visit, VisitMut};
27
28use crate::{
29 ast::*,
30 display_utils::{indented_list, SpaceOrNewline},
31 tokenizer::{Token, TokenWithSpan},
32};
33
34#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
37#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
38#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
39#[cfg_attr(feature = "visitor", visit(with = "visit_query"))]
40pub struct Query {
41 pub with: Option<With>,
43 pub body: Box<SetExpr>,
45 pub order_by: Option<OrderBy>,
47 pub limit_clause: Option<LimitClause>,
49 pub fetch: Option<Fetch>,
51 pub locks: Vec<LockClause>,
53 pub for_clause: Option<ForClause>,
57 pub settings: Option<Vec<Setting>>,
61 pub format_clause: Option<FormatClause>,
66
67 pub pipe_operators: Vec<PipeOperator>,
69}
70
71impl fmt::Display for Query {
72 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
73 if let Some(ref with) = self.with {
74 with.fmt(f)?;
75 SpaceOrNewline.fmt(f)?;
76 }
77 self.body.fmt(f)?;
78 if let Some(ref order_by) = self.order_by {
79 f.write_str(" ")?;
80 order_by.fmt(f)?;
81 }
82
83 if let Some(ref limit_clause) = self.limit_clause {
84 limit_clause.fmt(f)?;
85 }
86 if let Some(ref settings) = self.settings {
87 f.write_str(" SETTINGS ")?;
88 display_comma_separated(settings).fmt(f)?;
89 }
90 if let Some(ref fetch) = self.fetch {
91 f.write_str(" ")?;
92 fetch.fmt(f)?;
93 }
94 if !self.locks.is_empty() {
95 f.write_str(" ")?;
96 display_separated(&self.locks, " ").fmt(f)?;
97 }
98 if let Some(ref for_clause) = self.for_clause {
99 f.write_str(" ")?;
100 for_clause.fmt(f)?;
101 }
102 if let Some(ref format) = self.format_clause {
103 f.write_str(" ")?;
104 format.fmt(f)?;
105 }
106 for pipe_operator in &self.pipe_operators {
107 f.write_str(" |> ")?;
108 pipe_operator.fmt(f)?;
109 }
110 Ok(())
111 }
112}
113
114#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
120#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
121#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
122pub struct ProjectionSelect {
123 pub projection: Vec<SelectItem>,
125 pub order_by: Option<OrderBy>,
127 pub group_by: Option<GroupByExpr>,
129}
130
131impl fmt::Display for ProjectionSelect {
132 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
133 write!(f, "SELECT {}", display_comma_separated(&self.projection))?;
134 if let Some(ref group_by) = self.group_by {
135 write!(f, " {group_by}")?;
136 }
137 if let Some(ref order_by) = self.order_by {
138 write!(f, " {order_by}")?;
139 }
140 Ok(())
141 }
142}
143
144#[allow(clippy::large_enum_variant)]
147#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
148#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
149#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
150pub enum SetExpr {
151 Select(Box<Select>),
153 Query(Box<Query>),
156 SetOperation {
159 left: Box<SetExpr>,
161 op: SetOperator,
163 set_quantifier: SetQuantifier,
165 right: Box<SetExpr>,
167 },
168 Values(Values),
170 Insert(Statement),
172 Update(Statement),
174 Delete(Statement),
176 Merge(Statement),
178 Table(Box<Table>),
180}
181
182impl SetExpr {
183 pub fn as_select(&self) -> Option<&Select> {
185 if let Self::Select(select) = self {
186 Some(&**select)
187 } else {
188 None
189 }
190 }
191}
192
193impl fmt::Display for SetExpr {
194 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
195 match self {
196 SetExpr::Select(s) => s.fmt(f),
197 SetExpr::Query(q) => {
198 f.write_str("(")?;
199 q.fmt(f)?;
200 f.write_str(")")
201 }
202 SetExpr::Values(v) => v.fmt(f),
203 SetExpr::Insert(v) => v.fmt(f),
204 SetExpr::Update(v) => v.fmt(f),
205 SetExpr::Delete(v) => v.fmt(f),
206 SetExpr::Merge(v) => v.fmt(f),
207 SetExpr::Table(t) => t.fmt(f),
208 SetExpr::SetOperation {
209 left,
210 right,
211 op,
212 set_quantifier,
213 } => {
214 left.fmt(f)?;
215 SpaceOrNewline.fmt(f)?;
216 op.fmt(f)?;
217 match set_quantifier {
218 SetQuantifier::All
219 | SetQuantifier::Distinct
220 | SetQuantifier::ByName
221 | SetQuantifier::AllByName
222 | SetQuantifier::DistinctByName => {
223 f.write_str(" ")?;
224 set_quantifier.fmt(f)?;
225 }
226 SetQuantifier::None => {}
227 }
228 SpaceOrNewline.fmt(f)?;
229 right.fmt(f)?;
230 Ok(())
231 }
232 }
233 }
234}
235
236#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
237#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
238#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
239pub enum SetOperator {
241 Union,
243 Except,
245 Intersect,
247 Minus,
249}
250
251impl fmt::Display for SetOperator {
252 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
253 f.write_str(match self {
254 SetOperator::Union => "UNION",
255 SetOperator::Except => "EXCEPT",
256 SetOperator::Intersect => "INTERSECT",
257 SetOperator::Minus => "MINUS",
258 })
259 }
260}
261
262#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
266#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
267#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
268pub enum SetQuantifier {
269 All,
271 Distinct,
273 ByName,
275 AllByName,
277 DistinctByName,
279 None,
281}
282
283impl fmt::Display for SetQuantifier {
284 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
285 match self {
286 SetQuantifier::All => write!(f, "ALL"),
287 SetQuantifier::Distinct => write!(f, "DISTINCT"),
288 SetQuantifier::ByName => write!(f, "BY NAME"),
289 SetQuantifier::AllByName => write!(f, "ALL BY NAME"),
290 SetQuantifier::DistinctByName => write!(f, "DISTINCT BY NAME"),
291 SetQuantifier::None => Ok(()),
292 }
293 }
294}
295
296#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
297#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
298#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
300pub struct Table {
302 pub table_name: Option<String>,
304 pub schema_name: Option<String>,
306}
307
308impl fmt::Display for Table {
309 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
310 if let Some(ref schema_name) = self.schema_name {
311 write!(
312 f,
313 "TABLE {}.{}",
314 schema_name,
315 self.table_name.as_ref().unwrap(),
316 )?;
317 } else {
318 write!(f, "TABLE {}", self.table_name.as_ref().unwrap(),)?;
319 }
320 Ok(())
321 }
322}
323
324#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
326#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
327#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
328pub enum SelectFlavor {
329 Standard,
331 FromFirst,
333 FromFirstNoSelect,
335}
336
337#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Default)]
355#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
356#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
357pub struct SelectModifiers {
358 pub high_priority: bool,
362 pub straight_join: bool,
366 pub sql_small_result: bool,
370 pub sql_big_result: bool,
374 pub sql_buffer_result: bool,
378 pub sql_no_cache: bool,
382 pub sql_calc_found_rows: bool,
387}
388
389impl fmt::Display for SelectModifiers {
390 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
391 if self.high_priority {
392 f.write_str(" HIGH_PRIORITY")?;
393 }
394 if self.straight_join {
395 f.write_str(" STRAIGHT_JOIN")?;
396 }
397 if self.sql_small_result {
398 f.write_str(" SQL_SMALL_RESULT")?;
399 }
400 if self.sql_big_result {
401 f.write_str(" SQL_BIG_RESULT")?;
402 }
403 if self.sql_buffer_result {
404 f.write_str(" SQL_BUFFER_RESULT")?;
405 }
406 if self.sql_no_cache {
407 f.write_str(" SQL_NO_CACHE")?;
408 }
409 if self.sql_calc_found_rows {
410 f.write_str(" SQL_CALC_FOUND_ROWS")?;
411 }
412 Ok(())
413 }
414}
415
416impl SelectModifiers {
417 pub fn is_any_set(&self) -> bool {
419 let Self {
421 high_priority,
422 straight_join,
423 sql_small_result,
424 sql_big_result,
425 sql_buffer_result,
426 sql_no_cache,
427 sql_calc_found_rows,
428 } = self;
429 *high_priority
430 || *straight_join
431 || *sql_small_result
432 || *sql_big_result
433 || *sql_buffer_result
434 || *sql_no_cache
435 || *sql_calc_found_rows
436 }
437}
438
439#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
443#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
444#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
445#[cfg_attr(feature = "visitor", visit(with = "visit_select"))]
446pub struct Select {
447 pub select_token: AttachedToken,
449 pub optimizer_hints: Vec<OptimizerHint>,
454 pub distinct: Option<Distinct>,
456 pub select_modifiers: Option<SelectModifiers>,
460 pub top: Option<Top>,
462 pub top_before_distinct: bool,
464 pub projection: Vec<SelectItem>,
466 pub exclude: Option<ExcludeSelectItem>,
471 pub into: Option<SelectInto>,
473 pub from: Vec<TableWithJoins>,
475 pub lateral_views: Vec<LateralView>,
477 pub prewhere: Option<Expr>,
482 pub selection: Option<Expr>,
484 pub connect_by: Vec<ConnectByKind>,
486 pub group_by: GroupByExpr,
488 pub cluster_by: Vec<Expr>,
490 pub distribute_by: Vec<Expr>,
492 pub sort_by: Vec<OrderByExpr>,
494 pub having: Option<Expr>,
496 pub named_window: Vec<NamedWindowDefinition>,
498 pub qualify: Option<Expr>,
500 pub window_before_qualify: bool,
505 pub value_table_mode: Option<ValueTableMode>,
507 pub flavor: SelectFlavor,
509}
510
511impl fmt::Display for Select {
512 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
513 match self.flavor {
514 SelectFlavor::Standard => {
515 write!(f, "SELECT")?;
516 }
517 SelectFlavor::FromFirst => {
518 write!(f, "FROM {} SELECT", display_comma_separated(&self.from))?;
519 }
520 SelectFlavor::FromFirstNoSelect => {
521 write!(f, "FROM {}", display_comma_separated(&self.from))?;
522 }
523 }
524
525 for hint in &self.optimizer_hints {
526 f.write_str(" ")?;
527 hint.fmt(f)?;
528 }
529
530 if let Some(value_table_mode) = self.value_table_mode {
531 f.write_str(" ")?;
532 value_table_mode.fmt(f)?;
533 }
534
535 if let Some(ref top) = self.top {
536 if self.top_before_distinct {
537 f.write_str(" ")?;
538 top.fmt(f)?;
539 }
540 }
541 if let Some(ref distinct) = self.distinct {
542 f.write_str(" ")?;
543 distinct.fmt(f)?;
544 }
545 if let Some(ref top) = self.top {
546 if !self.top_before_distinct {
547 f.write_str(" ")?;
548 top.fmt(f)?;
549 }
550 }
551
552 if let Some(ref select_modifiers) = self.select_modifiers {
553 select_modifiers.fmt(f)?;
554 }
555
556 if !self.projection.is_empty() {
557 indented_list(f, &self.projection)?;
558 }
559
560 if let Some(exclude) = &self.exclude {
561 write!(f, " {exclude}")?;
562 }
563
564 if let Some(ref into) = self.into {
565 f.write_str(" ")?;
566 into.fmt(f)?;
567 }
568
569 if self.flavor == SelectFlavor::Standard && !self.from.is_empty() {
570 SpaceOrNewline.fmt(f)?;
571 f.write_str("FROM")?;
572 indented_list(f, &self.from)?;
573 }
574 if !self.lateral_views.is_empty() {
575 for lv in &self.lateral_views {
576 lv.fmt(f)?;
577 }
578 }
579 if let Some(ref prewhere) = self.prewhere {
580 f.write_str(" PREWHERE ")?;
581 prewhere.fmt(f)?;
582 }
583 if let Some(ref selection) = self.selection {
584 SpaceOrNewline.fmt(f)?;
585 f.write_str("WHERE")?;
586 SpaceOrNewline.fmt(f)?;
587 Indent(selection).fmt(f)?;
588 }
589 for clause in &self.connect_by {
590 SpaceOrNewline.fmt(f)?;
591 clause.fmt(f)?;
592 }
593 match &self.group_by {
594 GroupByExpr::All(_) => {
595 SpaceOrNewline.fmt(f)?;
596 self.group_by.fmt(f)?;
597 }
598 GroupByExpr::Expressions(exprs, _) => {
599 if !exprs.is_empty() {
600 SpaceOrNewline.fmt(f)?;
601 self.group_by.fmt(f)?;
602 }
603 }
604 }
605 if !self.cluster_by.is_empty() {
606 SpaceOrNewline.fmt(f)?;
607 f.write_str("CLUSTER BY")?;
608 SpaceOrNewline.fmt(f)?;
609 Indent(display_comma_separated(&self.cluster_by)).fmt(f)?;
610 }
611 if !self.distribute_by.is_empty() {
612 SpaceOrNewline.fmt(f)?;
613 f.write_str("DISTRIBUTE BY")?;
614 SpaceOrNewline.fmt(f)?;
615 display_comma_separated(&self.distribute_by).fmt(f)?;
616 }
617 if !self.sort_by.is_empty() {
618 SpaceOrNewline.fmt(f)?;
619 f.write_str("SORT BY")?;
620 SpaceOrNewline.fmt(f)?;
621 Indent(display_comma_separated(&self.sort_by)).fmt(f)?;
622 }
623 if let Some(ref having) = self.having {
624 SpaceOrNewline.fmt(f)?;
625 f.write_str("HAVING")?;
626 SpaceOrNewline.fmt(f)?;
627 Indent(having).fmt(f)?;
628 }
629 if self.window_before_qualify {
630 if !self.named_window.is_empty() {
631 SpaceOrNewline.fmt(f)?;
632 f.write_str("WINDOW")?;
633 SpaceOrNewline.fmt(f)?;
634 display_comma_separated(&self.named_window).fmt(f)?;
635 }
636 if let Some(ref qualify) = self.qualify {
637 SpaceOrNewline.fmt(f)?;
638 f.write_str("QUALIFY")?;
639 SpaceOrNewline.fmt(f)?;
640 qualify.fmt(f)?;
641 }
642 } else {
643 if let Some(ref qualify) = self.qualify {
644 SpaceOrNewline.fmt(f)?;
645 f.write_str("QUALIFY")?;
646 SpaceOrNewline.fmt(f)?;
647 qualify.fmt(f)?;
648 }
649 if !self.named_window.is_empty() {
650 SpaceOrNewline.fmt(f)?;
651 f.write_str("WINDOW")?;
652 SpaceOrNewline.fmt(f)?;
653 display_comma_separated(&self.named_window).fmt(f)?;
654 }
655 }
656 Ok(())
657 }
658}
659
660#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
662#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
663#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
664pub struct LateralView {
665 pub lateral_view: Expr,
667 pub lateral_view_name: ObjectName,
669 pub lateral_col_alias: Vec<Ident>,
671 pub outer: bool,
673}
674
675impl fmt::Display for LateralView {
676 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
677 write!(
678 f,
679 " LATERAL VIEW{outer} {} {}",
680 self.lateral_view,
681 self.lateral_view_name,
682 outer = if self.outer { " OUTER" } else { "" }
683 )?;
684 if !self.lateral_col_alias.is_empty() {
685 write!(
686 f,
687 " AS {}",
688 display_comma_separated(&self.lateral_col_alias)
689 )?;
690 }
691 Ok(())
692 }
693}
694
695#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
701#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
702#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
703pub enum NamedWindowExpr {
704 NamedWindow(Ident),
714 WindowSpec(WindowSpec),
721}
722
723impl fmt::Display for NamedWindowExpr {
724 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
725 match self {
726 NamedWindowExpr::NamedWindow(named_window) => {
727 write!(f, "{named_window}")?;
728 }
729 NamedWindowExpr::WindowSpec(window_spec) => {
730 write!(f, "({window_spec})")?;
731 }
732 };
733 Ok(())
734 }
735}
736
737#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
738#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
739#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
740pub struct NamedWindowDefinition(pub Ident, pub NamedWindowExpr);
742
743impl fmt::Display for NamedWindowDefinition {
744 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
745 write!(f, "{} AS {}", self.0, self.1)
746 }
747}
748
749#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
750#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
751#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
752pub struct With {
754 pub with_token: AttachedToken,
756 pub recursive: bool,
758 pub cte_tables: Vec<Cte>,
760}
761
762impl fmt::Display for With {
763 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
764 f.write_str("WITH ")?;
765 if self.recursive {
766 f.write_str("RECURSIVE ")?;
767 }
768 display_comma_separated(&self.cte_tables).fmt(f)?;
769 Ok(())
770 }
771}
772
773#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
774#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
775#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
776pub enum CteAsMaterialized {
778 Materialized,
780 NotMaterialized,
782}
783
784impl fmt::Display for CteAsMaterialized {
785 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
786 match *self {
787 CteAsMaterialized::Materialized => {
788 write!(f, "MATERIALIZED")?;
789 }
790 CteAsMaterialized::NotMaterialized => {
791 write!(f, "NOT MATERIALIZED")?;
792 }
793 };
794 Ok(())
795 }
796}
797
798#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
803#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
804#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
805pub struct Cte {
806 pub alias: TableAlias,
808 pub query: Box<Query>,
810 pub from: Option<Ident>,
812 pub materialized: Option<CteAsMaterialized>,
814 pub closing_paren_token: AttachedToken,
816}
817
818impl fmt::Display for Cte {
819 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
820 match self.materialized.as_ref() {
821 None => {
822 self.alias.fmt(f)?;
823 f.write_str(" AS (")?;
824 NewLine.fmt(f)?;
825 Indent(&self.query).fmt(f)?;
826 NewLine.fmt(f)?;
827 f.write_str(")")?;
828 }
829 Some(materialized) => {
830 self.alias.fmt(f)?;
831 f.write_str(" AS ")?;
832 materialized.fmt(f)?;
833 f.write_str(" (")?;
834 NewLine.fmt(f)?;
835 Indent(&self.query).fmt(f)?;
836 NewLine.fmt(f)?;
837 f.write_str(")")?;
838 }
839 };
840 if let Some(ref fr) = self.from {
841 write!(f, " FROM {fr}")?;
842 }
843 Ok(())
844 }
845}
846
847#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
850#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
851#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
852pub enum SelectItemQualifiedWildcardKind {
853 ObjectName(ObjectName),
856 Expr(Expr),
859}
860
861#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
863#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
864#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
865pub enum SelectItem {
866 UnnamedExpr(Expr),
868 ExprWithAlias {
870 expr: Expr,
872 alias: Ident,
874 },
875 ExprWithAliases {
879 expr: Expr,
881 aliases: Vec<Ident>,
883 },
884 QualifiedWildcard(SelectItemQualifiedWildcardKind, WildcardAdditionalOptions),
887 Wildcard(WildcardAdditionalOptions),
889}
890
891impl fmt::Display for SelectItemQualifiedWildcardKind {
892 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
893 match &self {
894 SelectItemQualifiedWildcardKind::ObjectName(object_name) => {
895 write!(f, "{object_name}.*")
896 }
897 SelectItemQualifiedWildcardKind::Expr(expr) => write!(f, "{expr}.*"),
898 }
899 }
900}
901
902#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
909#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
910#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
911pub struct IdentWithAlias {
912 pub ident: Ident,
914 pub alias: Ident,
916}
917
918impl fmt::Display for IdentWithAlias {
919 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
920 write!(f, "{} AS {}", self.ident, self.alias)
921 }
922}
923
924#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
926#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
927#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
928pub struct WildcardAdditionalOptions {
929 pub wildcard_token: AttachedToken,
931 pub opt_ilike: Option<IlikeSelectItem>,
934 pub opt_exclude: Option<ExcludeSelectItem>,
936 pub opt_except: Option<ExceptSelectItem>,
939 pub opt_replace: Option<ReplaceSelectItem>,
944 pub opt_rename: Option<RenameSelectItem>,
946 pub opt_alias: Option<Ident>,
949}
950
951impl Default for WildcardAdditionalOptions {
952 fn default() -> Self {
953 Self {
954 wildcard_token: TokenWithSpan::wrap(Token::Mul).into(),
955 opt_ilike: None,
956 opt_exclude: None,
957 opt_except: None,
958 opt_replace: None,
959 opt_rename: None,
960 opt_alias: None,
961 }
962 }
963}
964
965impl fmt::Display for WildcardAdditionalOptions {
966 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
967 if let Some(ilike) = &self.opt_ilike {
968 write!(f, " {ilike}")?;
969 }
970 if let Some(exclude) = &self.opt_exclude {
971 write!(f, " {exclude}")?;
972 }
973 if let Some(except) = &self.opt_except {
974 write!(f, " {except}")?;
975 }
976 if let Some(replace) = &self.opt_replace {
977 write!(f, " {replace}")?;
978 }
979 if let Some(rename) = &self.opt_rename {
980 write!(f, " {rename}")?;
981 }
982 if let Some(alias) = &self.opt_alias {
983 write!(f, " AS {alias}")?;
984 }
985 Ok(())
986 }
987}
988
989#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
996#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
997#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
998pub struct IlikeSelectItem {
999 pub pattern: String,
1001}
1002
1003impl fmt::Display for IlikeSelectItem {
1004 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1005 write!(
1006 f,
1007 "ILIKE '{}'",
1008 value::escape_single_quote_string(&self.pattern)
1009 )?;
1010 Ok(())
1011 }
1012}
1013#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1021#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1022#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1023pub enum ExcludeSelectItem {
1024 Single(ObjectName),
1031 Multiple(Vec<ObjectName>),
1037}
1038
1039impl fmt::Display for ExcludeSelectItem {
1040 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1041 write!(f, "EXCLUDE")?;
1042 match self {
1043 Self::Single(column) => {
1044 write!(f, " {column}")?;
1045 }
1046 Self::Multiple(columns) => {
1047 write!(f, " ({})", display_comma_separated(columns))?;
1048 }
1049 }
1050 Ok(())
1051 }
1052}
1053
1054#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1062#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1063#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1064pub enum RenameSelectItem {
1065 Single(IdentWithAlias),
1072 Multiple(Vec<IdentWithAlias>),
1078}
1079
1080impl fmt::Display for RenameSelectItem {
1081 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1082 write!(f, "RENAME")?;
1083 match self {
1084 Self::Single(column) => {
1085 write!(f, " {column}")?;
1086 }
1087 Self::Multiple(columns) => {
1088 write!(f, " ({})", display_comma_separated(columns))?;
1089 }
1090 }
1091 Ok(())
1092 }
1093}
1094
1095#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1102#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1103#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1104pub struct ExceptSelectItem {
1105 pub first_element: Ident,
1107 pub additional_elements: Vec<Ident>,
1109}
1110
1111impl fmt::Display for ExceptSelectItem {
1112 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1113 write!(f, "EXCEPT ")?;
1114 if self.additional_elements.is_empty() {
1115 write!(f, "({})", self.first_element)?;
1116 } else {
1117 write!(
1118 f,
1119 "({}, {})",
1120 self.first_element,
1121 display_comma_separated(&self.additional_elements)
1122 )?;
1123 }
1124 Ok(())
1125 }
1126}
1127
1128#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1136#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1137#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1138pub struct ReplaceSelectItem {
1139 pub items: Vec<Box<ReplaceSelectElement>>,
1141}
1142
1143impl fmt::Display for ReplaceSelectItem {
1144 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1145 write!(f, "REPLACE")?;
1146 write!(f, " ({})", display_comma_separated(&self.items))?;
1147 Ok(())
1148 }
1149}
1150
1151#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1156#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1157#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1158pub struct ReplaceSelectElement {
1159 pub expr: Expr,
1161 pub column_name: Ident,
1163 pub as_keyword: bool,
1165}
1166
1167impl fmt::Display for ReplaceSelectElement {
1168 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1169 if self.as_keyword {
1170 write!(f, "{} AS {}", self.expr, self.column_name)
1171 } else {
1172 write!(f, "{} {}", self.expr, self.column_name)
1173 }
1174 }
1175}
1176
1177impl fmt::Display for SelectItem {
1178 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1179 use core::fmt::Write;
1180 match &self {
1181 SelectItem::UnnamedExpr(expr) => expr.fmt(f),
1182 SelectItem::ExprWithAlias { expr, alias } => {
1183 expr.fmt(f)?;
1184 f.write_str(" AS ")?;
1185 alias.fmt(f)
1186 }
1187 SelectItem::ExprWithAliases { expr, aliases } => {
1188 expr.fmt(f)?;
1189 f.write_str(" AS (")?;
1190 display_comma_separated(aliases).fmt(f)?;
1191 f.write_str(")")
1192 }
1193 SelectItem::QualifiedWildcard(kind, additional_options) => {
1194 kind.fmt(f)?;
1195 additional_options.fmt(f)
1196 }
1197 SelectItem::Wildcard(additional_options) => {
1198 f.write_char('*')?;
1199 additional_options.fmt(f)
1200 }
1201 }
1202 }
1203}
1204
1205#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1206#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1207#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1208pub struct TableWithJoins {
1210 pub relation: TableFactor,
1212 pub joins: Vec<Join>,
1214}
1215
1216impl fmt::Display for TableWithJoins {
1217 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1218 self.relation.fmt(f)?;
1219 for join in &self.joins {
1220 SpaceOrNewline.fmt(f)?;
1221 join.fmt(f)?;
1222 }
1223 Ok(())
1224 }
1225}
1226
1227#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1232#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1233#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1234pub enum ConnectByKind {
1235 ConnectBy {
1237 connect_token: AttachedToken,
1239
1240 nocycle: bool,
1244
1245 relationships: Vec<Expr>,
1247 },
1248
1249 StartWith {
1254 start_token: AttachedToken,
1256
1257 condition: Box<Expr>,
1259 },
1260}
1261
1262impl fmt::Display for ConnectByKind {
1263 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1264 match self {
1265 ConnectByKind::ConnectBy {
1266 connect_token: _,
1267 nocycle,
1268 relationships,
1269 } => {
1270 write!(
1271 f,
1272 "CONNECT BY {nocycle}{relationships}",
1273 nocycle = if *nocycle { "NOCYCLE " } else { "" },
1274 relationships = display_comma_separated(relationships)
1275 )
1276 }
1277 ConnectByKind::StartWith {
1278 start_token: _,
1279 condition,
1280 } => {
1281 write!(f, "START WITH {condition}")
1282 }
1283 }
1284 }
1285}
1286
1287#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1288#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1289#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1290pub struct Setting {
1292 pub key: Ident,
1294 pub value: Expr,
1296}
1297
1298impl fmt::Display for Setting {
1299 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1300 write!(f, "{} = {}", self.key, self.value)
1301 }
1302}
1303
1304#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1311#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1312#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1313pub struct ExprWithAlias {
1314 pub expr: Expr,
1316 pub alias: Option<Ident>,
1318}
1319
1320impl fmt::Display for ExprWithAlias {
1321 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1322 let ExprWithAlias { expr, alias } = self;
1323 write!(f, "{expr}")?;
1324 if let Some(alias) = alias {
1325 write!(f, " AS {alias}")?;
1326 }
1327 Ok(())
1328 }
1329}
1330
1331#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1338#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1339#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1340pub struct ExprWithAliasAndOrderBy {
1341 pub expr: ExprWithAlias,
1343 pub order_by: OrderByOptions,
1345}
1346
1347impl fmt::Display for ExprWithAliasAndOrderBy {
1348 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1349 write!(f, "{}{}", self.expr, self.order_by)
1350 }
1351}
1352
1353#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1355#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1356#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1357pub struct TableFunctionArgs {
1358 pub args: Vec<FunctionArg>,
1360 pub settings: Option<Vec<Setting>>,
1365}
1366
1367#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1368#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1369#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1370pub enum TableIndexHintType {
1372 Use,
1374 Ignore,
1376 Force,
1378}
1379
1380impl fmt::Display for TableIndexHintType {
1381 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1382 f.write_str(match self {
1383 TableIndexHintType::Use => "USE",
1384 TableIndexHintType::Ignore => "IGNORE",
1385 TableIndexHintType::Force => "FORCE",
1386 })
1387 }
1388}
1389
1390#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1391#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1392#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1393pub enum TableIndexType {
1395 Index,
1397 Key,
1399}
1400
1401impl fmt::Display for TableIndexType {
1402 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1403 f.write_str(match self {
1404 TableIndexType::Index => "INDEX",
1405 TableIndexType::Key => "KEY",
1406 })
1407 }
1408}
1409
1410#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1411#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1412#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1413pub enum TableIndexHintForClause {
1415 Join,
1417 OrderBy,
1419 GroupBy,
1421}
1422
1423impl fmt::Display for TableIndexHintForClause {
1424 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1425 f.write_str(match self {
1426 TableIndexHintForClause::Join => "JOIN",
1427 TableIndexHintForClause::OrderBy => "ORDER BY",
1428 TableIndexHintForClause::GroupBy => "GROUP BY",
1429 })
1430 }
1431}
1432
1433#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1434#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1435#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1436pub struct TableIndexHints {
1438 pub hint_type: TableIndexHintType,
1440 pub index_type: TableIndexType,
1442 pub for_clause: Option<TableIndexHintForClause>,
1444 pub index_names: Vec<Ident>,
1446}
1447
1448impl fmt::Display for TableIndexHints {
1449 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1450 write!(f, "{} {} ", self.hint_type, self.index_type)?;
1451 if let Some(for_clause) = &self.for_clause {
1452 write!(f, "FOR {for_clause} ")?;
1453 }
1454 write!(f, "({})", display_comma_separated(&self.index_names))
1455 }
1456}
1457
1458#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1460#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1461#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1462#[cfg_attr(feature = "visitor", visit(with = "visit_table_factor"))]
1463pub enum TableFactor {
1464 Table {
1466 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
1467 name: ObjectName,
1469 alias: Option<TableAlias>,
1471 args: Option<TableFunctionArgs>,
1479 with_hints: Vec<Expr>,
1481 version: Option<TableVersion>,
1484 with_ordinality: bool,
1488 partitions: Vec<Ident>,
1490 json_path: Option<JsonPath>,
1492 sample: Option<TableSampleKind>,
1495 index_hints: Vec<TableIndexHints>,
1498 },
1499 Derived {
1501 lateral: bool,
1503 subquery: Box<Query>,
1505 alias: Option<TableAlias>,
1507 sample: Option<TableSampleKind>,
1509 },
1510 TableFunction {
1512 expr: Expr,
1514 alias: Option<TableAlias>,
1516 },
1517 Function {
1519 lateral: bool,
1521 name: ObjectName,
1523 args: Vec<FunctionArg>,
1525 alias: Option<TableAlias>,
1527 },
1528 UNNEST {
1539 alias: Option<TableAlias>,
1541 array_exprs: Vec<Expr>,
1543 with_offset: bool,
1545 with_offset_alias: Option<Ident>,
1547 with_ordinality: bool,
1549 },
1550 JsonTable {
1566 json_expr: Expr,
1568 json_path: ValueWithSpan,
1571 columns: Vec<JsonTableColumn>,
1574 alias: Option<TableAlias>,
1576 },
1577 OpenJsonTable {
1587 json_expr: Expr,
1589 json_path: Option<ValueWithSpan>,
1592 columns: Vec<OpenJsonTableColumn>,
1595 alias: Option<TableAlias>,
1597 },
1598 NestedJoin {
1605 table_with_joins: Box<TableWithJoins>,
1607 alias: Option<TableAlias>,
1609 },
1610 Pivot {
1617 table: Box<TableFactor>,
1619 aggregate_functions: Vec<ExprWithAlias>, value_column: Vec<Expr>,
1623 value_source: PivotValueSource,
1625 default_on_null: Option<Expr>,
1627 alias: Option<TableAlias>,
1629 },
1630 Unpivot {
1642 table: Box<TableFactor>,
1644 value: Expr,
1646 name: Ident,
1648 columns: Vec<ExprWithAlias>,
1650 null_inclusion: Option<NullInclusion>,
1652 alias: Option<TableAlias>,
1654 },
1655 MatchRecognize {
1659 table: Box<TableFactor>,
1661 partition_by: Vec<Expr>,
1663 order_by: Vec<OrderByExpr>,
1665 measures: Vec<Measure>,
1667 rows_per_match: Option<RowsPerMatch>,
1669 after_match_skip: Option<AfterMatchSkip>,
1671 pattern: MatchRecognizePattern,
1673 symbols: Vec<SymbolDefinition>,
1675 alias: Option<TableAlias>,
1677 },
1678 XmlTable {
1698 namespaces: Vec<XmlNamespaceDefinition>,
1700 row_expression: Expr,
1702 passing: XmlPassingClause,
1704 columns: Vec<XmlTableColumn>,
1706 alias: Option<TableAlias>,
1708 },
1709 SemanticView {
1721 name: ObjectName,
1723 dimensions: Vec<Expr>,
1725 metrics: Vec<Expr>,
1727 facts: Vec<Expr>,
1729 where_clause: Option<Expr>,
1731 alias: Option<TableAlias>,
1733 },
1734}
1735
1736#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1738#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1739#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1740pub enum TableSampleKind {
1741 BeforeTableAlias(Box<TableSample>),
1743 AfterTableAlias(Box<TableSample>),
1745}
1746
1747#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1748#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1749#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1750pub struct TableSample {
1752 pub modifier: TableSampleModifier,
1754 pub name: Option<TableSampleMethod>,
1756 pub quantity: Option<TableSampleQuantity>,
1758 pub seed: Option<TableSampleSeed>,
1760 pub bucket: Option<TableSampleBucket>,
1762 pub offset: Option<Expr>,
1764}
1765
1766#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1767#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1768#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1769pub enum TableSampleModifier {
1771 Sample,
1773 TableSample,
1775}
1776
1777impl fmt::Display for TableSampleModifier {
1778 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1779 match self {
1780 TableSampleModifier::Sample => write!(f, "SAMPLE")?,
1781 TableSampleModifier::TableSample => write!(f, "TABLESAMPLE")?,
1782 }
1783 Ok(())
1784 }
1785}
1786
1787#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1788#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1789#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1790pub struct TableSampleQuantity {
1792 pub parenthesized: bool,
1794 pub value: Expr,
1796 pub unit: Option<TableSampleUnit>,
1798}
1799
1800impl fmt::Display for TableSampleQuantity {
1801 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1802 if self.parenthesized {
1803 write!(f, "(")?;
1804 }
1805 write!(f, "{}", self.value)?;
1806 if let Some(unit) = &self.unit {
1807 write!(f, " {unit}")?;
1808 }
1809 if self.parenthesized {
1810 write!(f, ")")?;
1811 }
1812 Ok(())
1813 }
1814}
1815
1816#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1818#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1819#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1820pub enum TableSampleMethod {
1822 Row,
1824 Bernoulli,
1826 System,
1828 Block,
1830}
1831
1832impl fmt::Display for TableSampleMethod {
1833 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1834 match self {
1835 TableSampleMethod::Bernoulli => write!(f, "BERNOULLI"),
1836 TableSampleMethod::Row => write!(f, "ROW"),
1837 TableSampleMethod::System => write!(f, "SYSTEM"),
1838 TableSampleMethod::Block => write!(f, "BLOCK"),
1839 }
1840 }
1841}
1842
1843#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1844#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1845#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1846pub struct TableSampleSeed {
1848 pub modifier: TableSampleSeedModifier,
1850 pub value: ValueWithSpan,
1852}
1853
1854impl fmt::Display for TableSampleSeed {
1855 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1856 write!(f, "{} ({})", self.modifier, self.value)?;
1857 Ok(())
1858 }
1859}
1860
1861#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1862#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1863#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1864pub enum TableSampleSeedModifier {
1866 Repeatable,
1868 Seed,
1870}
1871
1872impl fmt::Display for TableSampleSeedModifier {
1873 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1874 match self {
1875 TableSampleSeedModifier::Repeatable => write!(f, "REPEATABLE"),
1876 TableSampleSeedModifier::Seed => write!(f, "SEED"),
1877 }
1878 }
1879}
1880
1881#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1882#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1883#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1884pub enum TableSampleUnit {
1886 Rows,
1888 Percent,
1890}
1891
1892impl fmt::Display for TableSampleUnit {
1893 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1894 match self {
1895 TableSampleUnit::Percent => write!(f, "PERCENT"),
1896 TableSampleUnit::Rows => write!(f, "ROWS"),
1897 }
1898 }
1899}
1900
1901#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1902#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1903#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1904pub struct TableSampleBucket {
1906 pub bucket: ValueWithSpan,
1908 pub total: ValueWithSpan,
1910 pub on: Option<Expr>,
1912}
1913
1914impl fmt::Display for TableSampleBucket {
1915 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1916 write!(f, "BUCKET {} OUT OF {}", self.bucket, self.total)?;
1917 if let Some(on) = &self.on {
1918 write!(f, " ON {on}")?;
1919 }
1920 Ok(())
1921 }
1922}
1923impl fmt::Display for TableSample {
1924 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1925 write!(f, "{}", self.modifier)?;
1926 if let Some(name) = &self.name {
1927 write!(f, " {name}")?;
1928 }
1929 if let Some(quantity) = &self.quantity {
1930 write!(f, " {quantity}")?;
1931 }
1932 if let Some(seed) = &self.seed {
1933 write!(f, " {seed}")?;
1934 }
1935 if let Some(bucket) = &self.bucket {
1936 write!(f, " ({bucket})")?;
1937 }
1938 if let Some(offset) = &self.offset {
1939 write!(f, " OFFSET {offset}")?;
1940 }
1941 Ok(())
1942 }
1943}
1944
1945#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1947#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1948#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1949pub enum PivotValueSource {
1950 List(Vec<ExprWithAlias>),
1954 Any(Vec<OrderByExpr>),
1958 Subquery(Box<Query>),
1962}
1963
1964impl fmt::Display for PivotValueSource {
1965 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1966 match self {
1967 PivotValueSource::List(values) => write!(f, "{}", display_comma_separated(values)),
1968 PivotValueSource::Any(order_by) => {
1969 write!(f, "ANY")?;
1970 if !order_by.is_empty() {
1971 write!(f, " ORDER BY {}", display_comma_separated(order_by))?;
1972 }
1973 Ok(())
1974 }
1975 PivotValueSource::Subquery(query) => write!(f, "{query}"),
1976 }
1977 }
1978}
1979
1980#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1984#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1985#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1986pub struct Measure {
1988 pub expr: Expr,
1990 pub alias: Ident,
1992}
1993
1994impl fmt::Display for Measure {
1995 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1996 write!(f, "{} AS {}", self.expr, self.alias)
1997 }
1998}
1999
2000#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2004#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2005#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2006pub enum RowsPerMatch {
2007 OneRow,
2009 AllRows(Option<EmptyMatchesMode>),
2011}
2012
2013impl fmt::Display for RowsPerMatch {
2014 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2015 match self {
2016 RowsPerMatch::OneRow => write!(f, "ONE ROW PER MATCH"),
2017 RowsPerMatch::AllRows(mode) => {
2018 write!(f, "ALL ROWS PER MATCH")?;
2019 if let Some(mode) = mode {
2020 write!(f, " {mode}")?;
2021 }
2022 Ok(())
2023 }
2024 }
2025 }
2026}
2027
2028#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2032#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2033#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2034pub enum AfterMatchSkip {
2035 PastLastRow,
2037 ToNextRow,
2039 ToFirst(Ident),
2041 ToLast(Ident),
2043}
2044
2045impl fmt::Display for AfterMatchSkip {
2046 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2047 write!(f, "AFTER MATCH SKIP ")?;
2048 match self {
2049 AfterMatchSkip::PastLastRow => write!(f, "PAST LAST ROW"),
2050 AfterMatchSkip::ToNextRow => write!(f, " TO NEXT ROW"),
2051 AfterMatchSkip::ToFirst(symbol) => write!(f, "TO FIRST {symbol}"),
2052 AfterMatchSkip::ToLast(symbol) => write!(f, "TO LAST {symbol}"),
2053 }
2054 }
2055}
2056
2057#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2058#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2059#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2060pub enum EmptyMatchesMode {
2062 Show,
2064 Omit,
2066 WithUnmatched,
2068}
2069
2070impl fmt::Display for EmptyMatchesMode {
2071 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2072 match self {
2073 EmptyMatchesMode::Show => write!(f, "SHOW EMPTY MATCHES"),
2074 EmptyMatchesMode::Omit => write!(f, "OMIT EMPTY MATCHES"),
2075 EmptyMatchesMode::WithUnmatched => write!(f, "WITH UNMATCHED ROWS"),
2076 }
2077 }
2078}
2079
2080#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2084#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2085#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2086pub struct SymbolDefinition {
2088 pub symbol: Ident,
2090 pub definition: Expr,
2092}
2093
2094impl fmt::Display for SymbolDefinition {
2095 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2096 write!(f, "{} AS {}", self.symbol, self.definition)
2097 }
2098}
2099
2100#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2102#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2103#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2104pub enum MatchRecognizeSymbol {
2105 Named(Ident),
2107 Start,
2109 End,
2111}
2112
2113impl fmt::Display for MatchRecognizeSymbol {
2114 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2115 match self {
2116 MatchRecognizeSymbol::Named(symbol) => write!(f, "{symbol}"),
2117 MatchRecognizeSymbol::Start => write!(f, "^"),
2118 MatchRecognizeSymbol::End => write!(f, "$"),
2119 }
2120 }
2121}
2122
2123#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2127#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2128#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2129pub enum MatchRecognizePattern {
2130 Symbol(MatchRecognizeSymbol),
2132 Exclude(MatchRecognizeSymbol),
2134 Permute(Vec<MatchRecognizeSymbol>),
2136 Concat(Vec<MatchRecognizePattern>),
2138 Group(Box<MatchRecognizePattern>),
2140 Alternation(Vec<MatchRecognizePattern>),
2142 Repetition(Box<MatchRecognizePattern>, RepetitionQuantifier),
2144}
2145
2146impl fmt::Display for MatchRecognizePattern {
2147 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2148 use MatchRecognizePattern::*;
2149 match self {
2150 Symbol(symbol) => write!(f, "{symbol}"),
2151 Exclude(symbol) => write!(f, "{{- {symbol} -}}"),
2152 Permute(symbols) => write!(f, "PERMUTE({})", display_comma_separated(symbols)),
2153 Concat(patterns) => write!(f, "{}", display_separated(patterns, " ")),
2154 Group(pattern) => write!(f, "( {pattern} )"),
2155 Alternation(patterns) => write!(f, "{}", display_separated(patterns, " | ")),
2156 Repetition(pattern, op) => write!(f, "{pattern}{op}"),
2157 }
2158 }
2159}
2160
2161#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2164#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2165#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2166pub enum RepetitionQuantifier {
2167 ZeroOrMore,
2169 OneOrMore,
2171 AtMostOne,
2173 Exactly(u32),
2175 AtLeast(u32),
2177 AtMost(u32),
2179 Range(u32, u32),
2181}
2182
2183impl fmt::Display for RepetitionQuantifier {
2184 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2185 use RepetitionQuantifier::*;
2186 match self {
2187 ZeroOrMore => write!(f, "*"),
2188 OneOrMore => write!(f, "+"),
2189 AtMostOne => write!(f, "?"),
2190 Exactly(n) => write!(f, "{{{n}}}"),
2191 AtLeast(n) => write!(f, "{{{n},}}"),
2192 AtMost(n) => write!(f, "{{,{n}}}"),
2193 Range(n, m) => write!(f, "{{{n},{m}}}"),
2194 }
2195 }
2196}
2197
2198impl fmt::Display for TableFactor {
2199 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2200 match self {
2201 TableFactor::Table {
2202 name,
2203 alias,
2204 args,
2205 with_hints,
2206 version,
2207 partitions,
2208 with_ordinality,
2209 json_path,
2210 sample,
2211 index_hints,
2212 } => {
2213 name.fmt(f)?;
2214 if let Some(json_path) = json_path {
2215 json_path.fmt(f)?;
2216 }
2217 if !partitions.is_empty() {
2218 write!(f, "PARTITION ({})", display_comma_separated(partitions))?;
2219 }
2220 if let Some(args) = args {
2221 write!(f, "(")?;
2222 write!(f, "{}", display_comma_separated(&args.args))?;
2223 if let Some(ref settings) = args.settings {
2224 if !args.args.is_empty() {
2225 write!(f, ", ")?;
2226 }
2227 write!(f, "SETTINGS {}", display_comma_separated(settings))?;
2228 }
2229 write!(f, ")")?;
2230 }
2231 if *with_ordinality {
2232 write!(f, " WITH ORDINALITY")?;
2233 }
2234 if let Some(TableSampleKind::BeforeTableAlias(sample)) = sample {
2235 write!(f, " {sample}")?;
2236 }
2237 if let Some(alias) = alias {
2238 write!(f, " {alias}")?;
2239 }
2240 if !index_hints.is_empty() {
2241 write!(f, " {}", display_separated(index_hints, " "))?;
2242 }
2243 if !with_hints.is_empty() {
2244 write!(f, " WITH ({})", display_comma_separated(with_hints))?;
2245 }
2246 if let Some(version) = version {
2247 write!(f, " {version}")?;
2248 }
2249 if let Some(TableSampleKind::AfterTableAlias(sample)) = sample {
2250 write!(f, " {sample}")?;
2251 }
2252 Ok(())
2253 }
2254 TableFactor::Derived {
2255 lateral,
2256 subquery,
2257 alias,
2258 sample,
2259 } => {
2260 if *lateral {
2261 write!(f, "LATERAL ")?;
2262 }
2263 f.write_str("(")?;
2264 NewLine.fmt(f)?;
2265 Indent(subquery).fmt(f)?;
2266 NewLine.fmt(f)?;
2267 f.write_str(")")?;
2268 if let Some(alias) = alias {
2269 write!(f, " {alias}")?;
2270 }
2271 if let Some(TableSampleKind::AfterTableAlias(sample)) = sample {
2272 write!(f, " {sample}")?;
2273 }
2274 Ok(())
2275 }
2276 TableFactor::Function {
2277 lateral,
2278 name,
2279 args,
2280 alias,
2281 } => {
2282 if *lateral {
2283 write!(f, "LATERAL ")?;
2284 }
2285 write!(f, "{name}")?;
2286 write!(f, "({})", display_comma_separated(args))?;
2287 if let Some(alias) = alias {
2288 write!(f, " {alias}")?;
2289 }
2290 Ok(())
2291 }
2292 TableFactor::TableFunction { expr, alias } => {
2293 write!(f, "TABLE({expr})")?;
2294 if let Some(alias) = alias {
2295 write!(f, " {alias}")?;
2296 }
2297 Ok(())
2298 }
2299 TableFactor::UNNEST {
2300 alias,
2301 array_exprs,
2302 with_offset,
2303 with_offset_alias,
2304 with_ordinality,
2305 } => {
2306 write!(f, "UNNEST({})", display_comma_separated(array_exprs))?;
2307
2308 if *with_ordinality {
2309 write!(f, " WITH ORDINALITY")?;
2310 }
2311
2312 if let Some(alias) = alias {
2313 write!(f, " {alias}")?;
2314 }
2315 if *with_offset {
2316 write!(f, " WITH OFFSET")?;
2317 }
2318 if let Some(alias) = with_offset_alias {
2319 write!(f, " {alias}")?;
2320 }
2321 Ok(())
2322 }
2323 TableFactor::JsonTable {
2324 json_expr,
2325 json_path,
2326 columns,
2327 alias,
2328 } => {
2329 write!(
2330 f,
2331 "JSON_TABLE({json_expr}, {json_path} COLUMNS({columns}))",
2332 columns = display_comma_separated(columns)
2333 )?;
2334 if let Some(alias) = alias {
2335 write!(f, " {alias}")?;
2336 }
2337 Ok(())
2338 }
2339 TableFactor::OpenJsonTable {
2340 json_expr,
2341 json_path,
2342 columns,
2343 alias,
2344 } => {
2345 write!(f, "OPENJSON({json_expr}")?;
2346 if let Some(json_path) = json_path {
2347 write!(f, ", {json_path}")?;
2348 }
2349 write!(f, ")")?;
2350 if !columns.is_empty() {
2351 write!(f, " WITH ({})", display_comma_separated(columns))?;
2352 }
2353 if let Some(alias) = alias {
2354 write!(f, " {alias}")?;
2355 }
2356 Ok(())
2357 }
2358 TableFactor::NestedJoin {
2359 table_with_joins,
2360 alias,
2361 } => {
2362 write!(f, "({table_with_joins})")?;
2363 if let Some(alias) = alias {
2364 write!(f, " {alias}")?;
2365 }
2366 Ok(())
2367 }
2368 TableFactor::Pivot {
2369 table,
2370 aggregate_functions,
2371 value_column,
2372 value_source,
2373 default_on_null,
2374 alias,
2375 } => {
2376 write!(
2377 f,
2378 "{table} PIVOT({} FOR ",
2379 display_comma_separated(aggregate_functions),
2380 )?;
2381 if value_column.len() == 1 {
2382 write!(f, "{}", value_column[0])?;
2383 } else {
2384 write!(f, "({})", display_comma_separated(value_column))?;
2385 }
2386 write!(f, " IN ({value_source})")?;
2387 if let Some(expr) = default_on_null {
2388 write!(f, " DEFAULT ON NULL ({expr})")?;
2389 }
2390 write!(f, ")")?;
2391 if let Some(alias) = alias {
2392 write!(f, " {alias}")?;
2393 }
2394 Ok(())
2395 }
2396 TableFactor::Unpivot {
2397 table,
2398 null_inclusion,
2399 value,
2400 name,
2401 columns,
2402 alias,
2403 } => {
2404 write!(f, "{table} UNPIVOT")?;
2405 if let Some(null_inclusion) = null_inclusion {
2406 write!(f, " {null_inclusion} ")?;
2407 }
2408 write!(
2409 f,
2410 "({} FOR {} IN ({}))",
2411 value,
2412 name,
2413 display_comma_separated(columns)
2414 )?;
2415 if let Some(alias) = alias {
2416 write!(f, " {alias}")?;
2417 }
2418 Ok(())
2419 }
2420 TableFactor::MatchRecognize {
2421 table,
2422 partition_by,
2423 order_by,
2424 measures,
2425 rows_per_match,
2426 after_match_skip,
2427 pattern,
2428 symbols,
2429 alias,
2430 } => {
2431 write!(f, "{table} MATCH_RECOGNIZE(")?;
2432 if !partition_by.is_empty() {
2433 write!(f, "PARTITION BY {} ", display_comma_separated(partition_by))?;
2434 }
2435 if !order_by.is_empty() {
2436 write!(f, "ORDER BY {} ", display_comma_separated(order_by))?;
2437 }
2438 if !measures.is_empty() {
2439 write!(f, "MEASURES {} ", display_comma_separated(measures))?;
2440 }
2441 if let Some(rows_per_match) = rows_per_match {
2442 write!(f, "{rows_per_match} ")?;
2443 }
2444 if let Some(after_match_skip) = after_match_skip {
2445 write!(f, "{after_match_skip} ")?;
2446 }
2447 write!(f, "PATTERN ({pattern}) ")?;
2448 write!(f, "DEFINE {})", display_comma_separated(symbols))?;
2449 if let Some(alias) = alias {
2450 write!(f, " {alias}")?;
2451 }
2452 Ok(())
2453 }
2454 TableFactor::XmlTable {
2455 row_expression,
2456 passing,
2457 columns,
2458 alias,
2459 namespaces,
2460 } => {
2461 write!(f, "XMLTABLE(")?;
2462 if !namespaces.is_empty() {
2463 write!(
2464 f,
2465 "XMLNAMESPACES({}), ",
2466 display_comma_separated(namespaces)
2467 )?;
2468 }
2469 write!(
2470 f,
2471 "{row_expression}{passing} COLUMNS {columns})",
2472 columns = display_comma_separated(columns)
2473 )?;
2474 if let Some(alias) = alias {
2475 write!(f, " {alias}")?;
2476 }
2477 Ok(())
2478 }
2479 TableFactor::SemanticView {
2480 name,
2481 dimensions,
2482 metrics,
2483 facts,
2484 where_clause,
2485 alias,
2486 } => {
2487 write!(f, "SEMANTIC_VIEW({name}")?;
2488
2489 if !dimensions.is_empty() {
2490 write!(f, " DIMENSIONS {}", display_comma_separated(dimensions))?;
2491 }
2492
2493 if !metrics.is_empty() {
2494 write!(f, " METRICS {}", display_comma_separated(metrics))?;
2495 }
2496
2497 if !facts.is_empty() {
2498 write!(f, " FACTS {}", display_comma_separated(facts))?;
2499 }
2500
2501 if let Some(where_clause) = where_clause {
2502 write!(f, " WHERE {where_clause}")?;
2503 }
2504
2505 write!(f, ")")?;
2506
2507 if let Some(alias) = alias {
2508 write!(f, " {alias}")?;
2509 }
2510
2511 Ok(())
2512 }
2513 }
2514 }
2515}
2516
2517#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2518#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2519#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2520pub struct TableAlias {
2522 pub explicit: bool,
2526 pub name: Ident,
2528 pub columns: Vec<TableAliasColumnDef>,
2530}
2531
2532impl fmt::Display for TableAlias {
2533 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2534 write!(f, "{}{}", if self.explicit { "AS " } else { "" }, self.name)?;
2535 if !self.columns.is_empty() {
2536 write!(f, " ({})", display_comma_separated(&self.columns))?;
2537 }
2538 Ok(())
2539 }
2540}
2541
2542#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2548#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2549#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2550pub struct TableAliasColumnDef {
2551 pub name: Ident,
2553 pub data_type: Option<DataType>,
2555}
2556
2557impl TableAliasColumnDef {
2558 pub fn from_name<S: Into<String>>(name: S) -> Self {
2560 TableAliasColumnDef {
2561 name: Ident::new(name),
2562 data_type: None,
2563 }
2564 }
2565}
2566
2567impl fmt::Display for TableAliasColumnDef {
2568 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2569 write!(f, "{}", self.name)?;
2570 if let Some(ref data_type) = self.data_type {
2571 write!(f, " {data_type}")?;
2572 }
2573 Ok(())
2574 }
2575}
2576
2577#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2578#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2579#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2580pub enum TableVersion {
2582 ForSystemTimeAsOf(Expr),
2585 TimestampAsOf(Expr),
2589 VersionAsOf(Expr),
2593 Function(Expr),
2596 Changes {
2606 changes: Expr,
2608 at: Expr,
2610 end: Option<Expr>,
2612 },
2613}
2614
2615impl Display for TableVersion {
2616 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2617 match self {
2618 TableVersion::ForSystemTimeAsOf(e) => write!(f, "FOR SYSTEM_TIME AS OF {e}")?,
2619 TableVersion::TimestampAsOf(e) => write!(f, "TIMESTAMP AS OF {e}")?,
2620 TableVersion::VersionAsOf(e) => write!(f, "VERSION AS OF {e}")?,
2621 TableVersion::Function(func) => write!(f, "{func}")?,
2622 TableVersion::Changes { changes, at, end } => {
2623 write!(f, "{changes} {at}")?;
2624 if let Some(end) = end {
2625 write!(f, " {end}")?;
2626 }
2627 }
2628 }
2629 Ok(())
2630 }
2631}
2632
2633#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2634#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2635#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2636pub struct Join {
2638 pub relation: TableFactor,
2640 pub global: bool,
2643 pub join_operator: JoinOperator,
2645}
2646
2647impl fmt::Display for Join {
2648 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2649 fn prefix(constraint: &JoinConstraint) -> &'static str {
2650 match constraint {
2651 JoinConstraint::Natural => "NATURAL ",
2652 _ => "",
2653 }
2654 }
2655 fn suffix(constraint: &'_ JoinConstraint) -> impl fmt::Display + '_ {
2656 struct Suffix<'a>(&'a JoinConstraint);
2657 impl fmt::Display for Suffix<'_> {
2658 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2659 match self.0 {
2660 JoinConstraint::On(expr) => write!(f, " ON {expr}"),
2661 JoinConstraint::Using(attrs) => {
2662 write!(f, " USING({})", display_comma_separated(attrs))
2663 }
2664 _ => Ok(()),
2665 }
2666 }
2667 }
2668 Suffix(constraint)
2669 }
2670 if self.global {
2671 write!(f, "GLOBAL ")?;
2672 }
2673
2674 match &self.join_operator {
2675 JoinOperator::Join(constraint) => f.write_fmt(format_args!(
2676 "{}JOIN {}{}",
2677 prefix(constraint),
2678 self.relation,
2679 suffix(constraint)
2680 )),
2681 JoinOperator::Inner(constraint) => f.write_fmt(format_args!(
2682 "{}INNER JOIN {}{}",
2683 prefix(constraint),
2684 self.relation,
2685 suffix(constraint)
2686 )),
2687 JoinOperator::Left(constraint) => f.write_fmt(format_args!(
2688 "{}LEFT JOIN {}{}",
2689 prefix(constraint),
2690 self.relation,
2691 suffix(constraint)
2692 )),
2693 JoinOperator::LeftOuter(constraint) => f.write_fmt(format_args!(
2694 "{}LEFT OUTER JOIN {}{}",
2695 prefix(constraint),
2696 self.relation,
2697 suffix(constraint)
2698 )),
2699 JoinOperator::Right(constraint) => f.write_fmt(format_args!(
2700 "{}RIGHT JOIN {}{}",
2701 prefix(constraint),
2702 self.relation,
2703 suffix(constraint)
2704 )),
2705 JoinOperator::RightOuter(constraint) => f.write_fmt(format_args!(
2706 "{}RIGHT OUTER JOIN {}{}",
2707 prefix(constraint),
2708 self.relation,
2709 suffix(constraint)
2710 )),
2711 JoinOperator::FullOuter(constraint) => f.write_fmt(format_args!(
2712 "{}FULL JOIN {}{}",
2713 prefix(constraint),
2714 self.relation,
2715 suffix(constraint)
2716 )),
2717 JoinOperator::CrossJoin(constraint) => f.write_fmt(format_args!(
2718 "CROSS JOIN {}{}",
2719 self.relation,
2720 suffix(constraint)
2721 )),
2722 JoinOperator::Semi(constraint) => f.write_fmt(format_args!(
2723 "{}SEMI JOIN {}{}",
2724 prefix(constraint),
2725 self.relation,
2726 suffix(constraint)
2727 )),
2728 JoinOperator::LeftSemi(constraint) => f.write_fmt(format_args!(
2729 "{}LEFT SEMI JOIN {}{}",
2730 prefix(constraint),
2731 self.relation,
2732 suffix(constraint)
2733 )),
2734 JoinOperator::RightSemi(constraint) => f.write_fmt(format_args!(
2735 "{}RIGHT SEMI JOIN {}{}",
2736 prefix(constraint),
2737 self.relation,
2738 suffix(constraint)
2739 )),
2740 JoinOperator::Anti(constraint) => f.write_fmt(format_args!(
2741 "{}ANTI JOIN {}{}",
2742 prefix(constraint),
2743 self.relation,
2744 suffix(constraint)
2745 )),
2746 JoinOperator::LeftAnti(constraint) => f.write_fmt(format_args!(
2747 "{}LEFT ANTI JOIN {}{}",
2748 prefix(constraint),
2749 self.relation,
2750 suffix(constraint)
2751 )),
2752 JoinOperator::RightAnti(constraint) => f.write_fmt(format_args!(
2753 "{}RIGHT ANTI JOIN {}{}",
2754 prefix(constraint),
2755 self.relation,
2756 suffix(constraint)
2757 )),
2758 JoinOperator::CrossApply => f.write_fmt(format_args!("CROSS APPLY {}", self.relation)),
2759 JoinOperator::OuterApply => f.write_fmt(format_args!("OUTER APPLY {}", self.relation)),
2760 JoinOperator::AsOf {
2761 match_condition,
2762 constraint,
2763 } => f.write_fmt(format_args!(
2764 "ASOF JOIN {} MATCH_CONDITION ({match_condition}){}",
2765 self.relation,
2766 suffix(constraint)
2767 )),
2768 JoinOperator::StraightJoin(constraint) => f.write_fmt(format_args!(
2769 "STRAIGHT_JOIN {}{}",
2770 self.relation,
2771 suffix(constraint)
2772 )),
2773 }
2774 }
2775}
2776
2777#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2778#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2779#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2780pub enum JoinOperator {
2782 Join(JoinConstraint),
2784 Inner(JoinConstraint),
2786 Left(JoinConstraint),
2788 LeftOuter(JoinConstraint),
2790 Right(JoinConstraint),
2792 RightOuter(JoinConstraint),
2794 FullOuter(JoinConstraint),
2796 CrossJoin(JoinConstraint),
2798 Semi(JoinConstraint),
2800 LeftSemi(JoinConstraint),
2802 RightSemi(JoinConstraint),
2804 Anti(JoinConstraint),
2806 LeftAnti(JoinConstraint),
2808 RightAnti(JoinConstraint),
2810 CrossApply,
2812 OuterApply,
2814 AsOf {
2818 match_condition: Expr,
2820 constraint: JoinConstraint,
2822 },
2823 StraightJoin(JoinConstraint),
2827}
2828
2829#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2830#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2831#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2832pub enum JoinConstraint {
2834 On(Expr),
2836 Using(Vec<ObjectName>),
2838 Natural,
2840 None,
2842}
2843
2844#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2845#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2846#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2847pub enum OrderByKind {
2849 All(OrderByOptions),
2854
2855 Expressions(Vec<OrderByExpr>),
2857}
2858
2859#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2860#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2861#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2862pub struct OrderBy {
2864 pub kind: OrderByKind,
2866
2867 pub interpolate: Option<Interpolate>,
2869}
2870
2871impl fmt::Display for OrderBy {
2872 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2873 write!(f, "ORDER BY")?;
2874 match &self.kind {
2875 OrderByKind::Expressions(exprs) => {
2876 write!(f, " {}", display_comma_separated(exprs))?;
2877 }
2878 OrderByKind::All(all) => {
2879 write!(f, " ALL{all}")?;
2880 }
2881 }
2882
2883 if let Some(ref interpolate) = self.interpolate {
2884 match &interpolate.exprs {
2885 Some(exprs) => write!(f, " INTERPOLATE ({})", display_comma_separated(exprs))?,
2886 None => write!(f, " INTERPOLATE")?,
2887 }
2888 }
2889
2890 Ok(())
2891 }
2892}
2893
2894#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2896#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2897#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2898pub struct OrderByExpr {
2899 pub expr: Expr,
2901 pub options: OrderByOptions,
2903 pub with_fill: Option<WithFill>,
2905}
2906
2907impl From<Ident> for OrderByExpr {
2908 fn from(ident: Ident) -> Self {
2909 OrderByExpr {
2910 expr: Expr::Identifier(ident),
2911 options: OrderByOptions::default(),
2912 with_fill: None,
2913 }
2914 }
2915}
2916
2917impl fmt::Display for OrderByExpr {
2918 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2919 write!(f, "{}{}", self.expr, self.options)?;
2920 if let Some(ref with_fill) = self.with_fill {
2921 write!(f, " {with_fill}")?
2922 }
2923 Ok(())
2924 }
2925}
2926
2927#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2932#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2933#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2934pub struct WithFill {
2936 pub from: Option<Expr>,
2938 pub to: Option<Expr>,
2940 pub step: Option<Expr>,
2942}
2943
2944impl fmt::Display for WithFill {
2945 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2946 write!(f, "WITH FILL")?;
2947 if let Some(ref from) = self.from {
2948 write!(f, " FROM {from}")?;
2949 }
2950 if let Some(ref to) = self.to {
2951 write!(f, " TO {to}")?;
2952 }
2953 if let Some(ref step) = self.step {
2954 write!(f, " STEP {step}")?;
2955 }
2956 Ok(())
2957 }
2958}
2959
2960#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2965#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2966#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2967pub struct InterpolateExpr {
2969 pub column: Ident,
2971 pub expr: Option<Expr>,
2973}
2974
2975#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2976#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2977#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2978pub struct Interpolate {
2980 pub exprs: Option<Vec<InterpolateExpr>>,
2982}
2983
2984impl fmt::Display for InterpolateExpr {
2985 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2986 write!(f, "{}", self.column)?;
2987 if let Some(ref expr) = self.expr {
2988 write!(f, " AS {expr}")?;
2989 }
2990 Ok(())
2991 }
2992}
2993
2994#[derive(Default, Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2995#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2996#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2997pub struct OrderByOptions {
2999 pub asc: Option<bool>,
3001 pub nulls_first: Option<bool>,
3003}
3004
3005impl fmt::Display for OrderByOptions {
3006 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3007 match self.asc {
3008 Some(true) => write!(f, " ASC")?,
3009 Some(false) => write!(f, " DESC")?,
3010 None => (),
3011 }
3012 match self.nulls_first {
3013 Some(true) => write!(f, " NULLS FIRST")?,
3014 Some(false) => write!(f, " NULLS LAST")?,
3015 None => (),
3016 }
3017 Ok(())
3018 }
3019}
3020
3021#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3022#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3023#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3024pub enum LimitClause {
3026 LimitOffset {
3030 limit: Option<Expr>,
3032 offset: Option<Offset>,
3034 limit_by: Vec<Expr>,
3036 },
3037 OffsetCommaLimit {
3039 offset: Expr,
3041 limit: Expr,
3043 },
3044}
3045
3046impl fmt::Display for LimitClause {
3047 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3048 match self {
3049 LimitClause::LimitOffset {
3050 limit,
3051 limit_by,
3052 offset,
3053 } => {
3054 if let Some(ref limit) = limit {
3055 write!(f, " LIMIT {limit}")?;
3056 }
3057 if let Some(ref offset) = offset {
3058 write!(f, " {offset}")?;
3059 }
3060 if !limit_by.is_empty() {
3061 debug_assert!(limit.is_some());
3062 write!(f, " BY {}", display_separated(limit_by, ", "))?;
3063 }
3064 Ok(())
3065 }
3066 LimitClause::OffsetCommaLimit { offset, limit } => {
3067 write!(f, " LIMIT {offset}, {limit}")
3068 }
3069 }
3070 }
3071}
3072
3073#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3074#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3075#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3076pub struct Offset {
3078 pub value: Expr,
3080 pub rows: OffsetRows,
3082}
3083
3084impl fmt::Display for Offset {
3085 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3086 write!(f, "OFFSET {}{}", self.value, self.rows)
3087 }
3088}
3089
3090#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3092#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3093#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3094pub enum OffsetRows {
3095 None,
3097 Row,
3099 Rows,
3101}
3102
3103impl fmt::Display for OffsetRows {
3104 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3105 match self {
3106 OffsetRows::None => Ok(()),
3107 OffsetRows::Row => write!(f, " ROW"),
3108 OffsetRows::Rows => write!(f, " ROWS"),
3109 }
3110 }
3111}
3112
3113#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3125#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3126#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3127pub enum PipeOperator {
3128 Limit {
3134 expr: Expr,
3136 offset: Option<Expr>,
3138 },
3139 Where {
3145 expr: Expr,
3147 },
3148 OrderBy {
3150 exprs: Vec<OrderByExpr>,
3152 },
3153 Select {
3159 exprs: Vec<SelectItem>,
3161 },
3162 Extend {
3168 exprs: Vec<SelectItem>,
3170 },
3171 Set {
3177 assignments: Vec<Assignment>,
3179 },
3180 Drop {
3186 columns: Vec<Ident>,
3188 },
3189 As {
3195 alias: Ident,
3197 },
3198 Aggregate {
3210 full_table_exprs: Vec<ExprWithAliasAndOrderBy>,
3212 group_by_expr: Vec<ExprWithAliasAndOrderBy>,
3214 },
3215 TableSample {
3219 sample: Box<TableSample>,
3221 },
3222 Rename {
3228 mappings: Vec<IdentWithAlias>,
3230 },
3231 Union {
3237 set_quantifier: SetQuantifier,
3239 queries: Vec<Query>,
3241 },
3242 Intersect {
3248 set_quantifier: SetQuantifier,
3250 queries: Vec<Query>,
3252 },
3253 Except {
3259 set_quantifier: SetQuantifier,
3261 queries: Vec<Query>,
3263 },
3264 Call {
3270 function: Function,
3272 alias: Option<Ident>,
3274 },
3275 Pivot {
3281 aggregate_functions: Vec<ExprWithAlias>,
3283 value_column: Vec<Ident>,
3285 value_source: PivotValueSource,
3287 alias: Option<Ident>,
3289 },
3290 Unpivot {
3299 value_column: Ident,
3301 name_column: Ident,
3303 unpivot_columns: Vec<Ident>,
3305 alias: Option<Ident>,
3307 },
3308 Join(Join),
3314}
3315
3316impl fmt::Display for PipeOperator {
3317 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3318 match self {
3319 PipeOperator::Select { exprs } => {
3320 write!(f, "SELECT {}", display_comma_separated(exprs.as_slice()))
3321 }
3322 PipeOperator::Extend { exprs } => {
3323 write!(f, "EXTEND {}", display_comma_separated(exprs.as_slice()))
3324 }
3325 PipeOperator::Set { assignments } => {
3326 write!(f, "SET {}", display_comma_separated(assignments.as_slice()))
3327 }
3328 PipeOperator::Drop { columns } => {
3329 write!(f, "DROP {}", display_comma_separated(columns.as_slice()))
3330 }
3331 PipeOperator::As { alias } => {
3332 write!(f, "AS {alias}")
3333 }
3334 PipeOperator::Limit { expr, offset } => {
3335 write!(f, "LIMIT {expr}")?;
3336 if let Some(offset) = offset {
3337 write!(f, " OFFSET {offset}")?;
3338 }
3339 Ok(())
3340 }
3341 PipeOperator::Aggregate {
3342 full_table_exprs,
3343 group_by_expr,
3344 } => {
3345 write!(f, "AGGREGATE")?;
3346 if !full_table_exprs.is_empty() {
3347 write!(
3348 f,
3349 " {}",
3350 display_comma_separated(full_table_exprs.as_slice())
3351 )?;
3352 }
3353 if !group_by_expr.is_empty() {
3354 write!(f, " GROUP BY {}", display_comma_separated(group_by_expr))?;
3355 }
3356 Ok(())
3357 }
3358
3359 PipeOperator::Where { expr } => {
3360 write!(f, "WHERE {expr}")
3361 }
3362 PipeOperator::OrderBy { exprs } => {
3363 write!(f, "ORDER BY {}", display_comma_separated(exprs.as_slice()))
3364 }
3365
3366 PipeOperator::TableSample { sample } => {
3367 write!(f, "{sample}")
3368 }
3369 PipeOperator::Rename { mappings } => {
3370 write!(f, "RENAME {}", display_comma_separated(mappings))
3371 }
3372 PipeOperator::Union {
3373 set_quantifier,
3374 queries,
3375 } => Self::fmt_set_operation(f, "UNION", set_quantifier, queries),
3376 PipeOperator::Intersect {
3377 set_quantifier,
3378 queries,
3379 } => Self::fmt_set_operation(f, "INTERSECT", set_quantifier, queries),
3380 PipeOperator::Except {
3381 set_quantifier,
3382 queries,
3383 } => Self::fmt_set_operation(f, "EXCEPT", set_quantifier, queries),
3384 PipeOperator::Call { function, alias } => {
3385 write!(f, "CALL {function}")?;
3386 Self::fmt_optional_alias(f, alias)
3387 }
3388 PipeOperator::Pivot {
3389 aggregate_functions,
3390 value_column,
3391 value_source,
3392 alias,
3393 } => {
3394 write!(
3395 f,
3396 "PIVOT({} FOR {} IN ({}))",
3397 display_comma_separated(aggregate_functions),
3398 Expr::CompoundIdentifier(value_column.to_vec()),
3399 value_source
3400 )?;
3401 Self::fmt_optional_alias(f, alias)
3402 }
3403 PipeOperator::Unpivot {
3404 value_column,
3405 name_column,
3406 unpivot_columns,
3407 alias,
3408 } => {
3409 write!(
3410 f,
3411 "UNPIVOT({} FOR {} IN ({}))",
3412 value_column,
3413 name_column,
3414 display_comma_separated(unpivot_columns)
3415 )?;
3416 Self::fmt_optional_alias(f, alias)
3417 }
3418 PipeOperator::Join(join) => write!(f, "{join}"),
3419 }
3420 }
3421}
3422
3423impl PipeOperator {
3424 fn fmt_optional_alias(f: &mut fmt::Formatter<'_>, alias: &Option<Ident>) -> fmt::Result {
3426 if let Some(alias) = alias {
3427 write!(f, " AS {alias}")?;
3428 }
3429 Ok(())
3430 }
3431
3432 fn fmt_set_operation(
3434 f: &mut fmt::Formatter<'_>,
3435 operation: &str,
3436 set_quantifier: &SetQuantifier,
3437 queries: &[Query],
3438 ) -> fmt::Result {
3439 write!(f, "{operation}")?;
3440 match set_quantifier {
3441 SetQuantifier::None => {}
3442 _ => {
3443 write!(f, " {set_quantifier}")?;
3444 }
3445 }
3446 write!(f, " ")?;
3447 let parenthesized_queries: Vec<String> =
3448 queries.iter().map(|query| format!("({query})")).collect();
3449 write!(f, "{}", display_comma_separated(&parenthesized_queries))
3450 }
3451}
3452
3453#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3454#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3455#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3456pub struct Fetch {
3458 pub with_ties: bool,
3460 pub percent: bool,
3462 pub quantity: Option<Expr>,
3464}
3465
3466impl fmt::Display for Fetch {
3467 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3468 let extension = if self.with_ties { "WITH TIES" } else { "ONLY" };
3469 if let Some(ref quantity) = self.quantity {
3470 let percent = if self.percent { " PERCENT" } else { "" };
3471 write!(f, "FETCH FIRST {quantity}{percent} ROWS {extension}")
3472 } else {
3473 write!(f, "FETCH FIRST ROWS {extension}")
3474 }
3475 }
3476}
3477
3478#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3479#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3480#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3481pub struct LockClause {
3483 pub lock_type: LockType,
3485 pub of: Option<ObjectName>,
3487 pub nonblock: Option<NonBlock>,
3489}
3490
3491impl fmt::Display for LockClause {
3492 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3493 write!(f, "FOR {}", &self.lock_type)?;
3494 if let Some(ref of) = self.of {
3495 write!(f, " OF {of}")?;
3496 }
3497 if let Some(ref nb) = self.nonblock {
3498 write!(f, " {nb}")?;
3499 }
3500 Ok(())
3501 }
3502}
3503
3504#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3505#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3506#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3507pub enum LockType {
3509 Share,
3511 Update,
3513}
3514
3515impl fmt::Display for LockType {
3516 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3517 let select_lock = match self {
3518 LockType::Share => "SHARE",
3519 LockType::Update => "UPDATE",
3520 };
3521 write!(f, "{select_lock}")
3522 }
3523}
3524
3525#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3526#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3527#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3528pub enum NonBlock {
3530 Nowait,
3532 SkipLocked,
3534}
3535
3536impl fmt::Display for NonBlock {
3537 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3538 let nonblock = match self {
3539 NonBlock::Nowait => "NOWAIT",
3540 NonBlock::SkipLocked => "SKIP LOCKED",
3541 };
3542 write!(f, "{nonblock}")
3543 }
3544}
3545
3546#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3547#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3548#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3549pub enum Distinct {
3551 All,
3556
3557 Distinct,
3559
3560 On(Vec<Expr>),
3562}
3563
3564impl fmt::Display for Distinct {
3565 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3566 match self {
3567 Distinct::All => write!(f, "ALL"),
3568 Distinct::Distinct => write!(f, "DISTINCT"),
3569 Distinct::On(col_names) => {
3570 let col_names = display_comma_separated(col_names);
3571 write!(f, "DISTINCT ON ({col_names})")
3572 }
3573 }
3574 }
3575}
3576
3577#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3578#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3579#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3580pub struct Top {
3582 pub with_ties: bool,
3585 pub percent: bool,
3587 pub quantity: Option<TopQuantity>,
3589}
3590
3591#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3592#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3593#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3594pub enum TopQuantity {
3596 Expr(Expr),
3598 Constant(u64),
3600}
3601
3602impl fmt::Display for Top {
3603 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3604 let extension = if self.with_ties { " WITH TIES" } else { "" };
3605 if let Some(ref quantity) = self.quantity {
3606 let percent = if self.percent { " PERCENT" } else { "" };
3607 match quantity {
3608 TopQuantity::Expr(quantity) => write!(f, "TOP ({quantity}){percent}{extension}"),
3609 TopQuantity::Constant(quantity) => {
3610 write!(f, "TOP {quantity}{percent}{extension}")
3611 }
3612 }
3613 } else {
3614 write!(f, "TOP{extension}")
3615 }
3616 }
3617}
3618
3619#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3620#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3621#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3622pub struct Values {
3624 pub explicit_row: bool,
3627 pub value_keyword: bool,
3630 pub rows: Vec<Vec<Expr>>,
3632}
3633
3634impl fmt::Display for Values {
3635 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3636 match self.value_keyword {
3637 true => f.write_str("VALUE")?,
3638 false => f.write_str("VALUES")?,
3639 };
3640 let prefix = if self.explicit_row { "ROW" } else { "" };
3641 let mut delim = "";
3642 for row in &self.rows {
3643 f.write_str(delim)?;
3644 delim = ",";
3645 SpaceOrNewline.fmt(f)?;
3646 Indent(format_args!("{prefix}({})", display_comma_separated(row))).fmt(f)?;
3647 }
3648 Ok(())
3649 }
3650}
3651
3652#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3653#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3654#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3655pub struct SelectInto {
3657 pub temporary: bool,
3659 pub unlogged: bool,
3661 pub table: bool,
3663 pub name: ObjectName,
3665}
3666
3667impl fmt::Display for SelectInto {
3668 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3669 let temporary = if self.temporary { " TEMPORARY" } else { "" };
3670 let unlogged = if self.unlogged { " UNLOGGED" } else { "" };
3671 let table = if self.table { " TABLE" } else { "" };
3672
3673 write!(f, "INTO{}{}{} {}", temporary, unlogged, table, self.name)
3674 }
3675}
3676
3677#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3682#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3683#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3684pub enum GroupByWithModifier {
3686 Rollup,
3688 Cube,
3690 Totals,
3692 GroupingSets(Expr),
3696}
3697
3698impl fmt::Display for GroupByWithModifier {
3699 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3700 match self {
3701 GroupByWithModifier::Rollup => write!(f, "WITH ROLLUP"),
3702 GroupByWithModifier::Cube => write!(f, "WITH CUBE"),
3703 GroupByWithModifier::Totals => write!(f, "WITH TOTALS"),
3704 GroupByWithModifier::GroupingSets(expr) => {
3705 write!(f, "{expr}")
3706 }
3707 }
3708 }
3709}
3710
3711#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3712#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3713#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3714pub enum GroupByExpr {
3717 All(Vec<GroupByWithModifier>),
3727 Expressions(Vec<Expr>, Vec<GroupByWithModifier>),
3729}
3730
3731impl fmt::Display for GroupByExpr {
3732 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3733 match self {
3734 GroupByExpr::All(modifiers) => {
3735 write!(f, "GROUP BY ALL")?;
3736 if !modifiers.is_empty() {
3737 write!(f, " {}", display_separated(modifiers, " "))?;
3738 }
3739 Ok(())
3740 }
3741 GroupByExpr::Expressions(col_names, modifiers) => {
3742 f.write_str("GROUP BY")?;
3743 SpaceOrNewline.fmt(f)?;
3744 Indent(display_comma_separated(col_names)).fmt(f)?;
3745 if !modifiers.is_empty() {
3746 write!(f, " {}", display_separated(modifiers, " "))?;
3747 }
3748 Ok(())
3749 }
3750 }
3751 }
3752}
3753
3754#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3758#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3759#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3760pub enum FormatClause {
3761 Identifier(Ident),
3763 Null,
3765}
3766
3767impl fmt::Display for FormatClause {
3768 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3769 match self {
3770 FormatClause::Identifier(ident) => write!(f, "FORMAT {ident}"),
3771 FormatClause::Null => write!(f, "FORMAT NULL"),
3772 }
3773 }
3774}
3775
3776#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3780#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3781#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3782pub struct InputFormatClause {
3783 pub ident: Ident,
3785 pub values: Vec<Expr>,
3787}
3788
3789impl fmt::Display for InputFormatClause {
3790 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3791 write!(f, "FORMAT {}", self.ident)?;
3792
3793 if !self.values.is_empty() {
3794 write!(f, " {}", display_comma_separated(self.values.as_slice()))?;
3795 }
3796
3797 Ok(())
3798 }
3799}
3800
3801#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3803#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3804#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3805pub enum ForClause {
3806 Browse,
3808 Json {
3810 for_json: ForJson,
3812 root: Option<String>,
3814 include_null_values: bool,
3816 without_array_wrapper: bool,
3818 },
3819 Xml {
3821 for_xml: ForXml,
3823 elements: bool,
3825 binary_base64: bool,
3827 root: Option<String>,
3829 r#type: bool,
3831 },
3832}
3833
3834impl fmt::Display for ForClause {
3835 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3836 match self {
3837 ForClause::Browse => write!(f, "FOR BROWSE"),
3838 ForClause::Json {
3839 for_json,
3840 root,
3841 include_null_values,
3842 without_array_wrapper,
3843 } => {
3844 write!(f, "FOR JSON ")?;
3845 write!(f, "{for_json}")?;
3846 if let Some(root) = root {
3847 write!(f, ", ROOT('{root}')")?;
3848 }
3849 if *include_null_values {
3850 write!(f, ", INCLUDE_NULL_VALUES")?;
3851 }
3852 if *without_array_wrapper {
3853 write!(f, ", WITHOUT_ARRAY_WRAPPER")?;
3854 }
3855 Ok(())
3856 }
3857 ForClause::Xml {
3858 for_xml,
3859 elements,
3860 binary_base64,
3861 root,
3862 r#type,
3863 } => {
3864 write!(f, "FOR XML ")?;
3865 write!(f, "{for_xml}")?;
3866 if *binary_base64 {
3867 write!(f, ", BINARY BASE64")?;
3868 }
3869 if *r#type {
3870 write!(f, ", TYPE")?;
3871 }
3872 if let Some(root) = root {
3873 write!(f, ", ROOT('{root}')")?;
3874 }
3875 if *elements {
3876 write!(f, ", ELEMENTS")?;
3877 }
3878 Ok(())
3879 }
3880 }
3881 }
3882}
3883
3884#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3885#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3886#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3887pub enum ForXml {
3889 Raw(Option<String>),
3891 Auto,
3893 Explicit,
3895 Path(Option<String>),
3897}
3898
3899impl fmt::Display for ForXml {
3900 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3901 match self {
3902 ForXml::Raw(root) => {
3903 write!(f, "RAW")?;
3904 if let Some(root) = root {
3905 write!(f, "('{root}')")?;
3906 }
3907 Ok(())
3908 }
3909 ForXml::Auto => write!(f, "AUTO"),
3910 ForXml::Explicit => write!(f, "EXPLICIT"),
3911 ForXml::Path(root) => {
3912 write!(f, "PATH")?;
3913 if let Some(root) = root {
3914 write!(f, "('{root}')")?;
3915 }
3916 Ok(())
3917 }
3918 }
3919 }
3920}
3921
3922#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3923#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3924#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3925pub enum ForJson {
3927 Auto,
3929 Path,
3931}
3932
3933impl fmt::Display for ForJson {
3934 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3935 match self {
3936 ForJson::Auto => write!(f, "AUTO"),
3937 ForJson::Path => write!(f, "PATH"),
3938 }
3939 }
3940}
3941
3942#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3963#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3964#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3965pub enum JsonTableColumn {
3966 Named(JsonTableNamedColumn),
3968 ForOrdinality(Ident),
3970 Nested(JsonTableNestedColumn),
3972}
3973
3974impl fmt::Display for JsonTableColumn {
3975 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3976 match self {
3977 JsonTableColumn::Named(json_table_named_column) => {
3978 write!(f, "{json_table_named_column}")
3979 }
3980 JsonTableColumn::ForOrdinality(ident) => write!(f, "{ident} FOR ORDINALITY"),
3981 JsonTableColumn::Nested(json_table_nested_column) => {
3982 write!(f, "{json_table_nested_column}")
3983 }
3984 }
3985 }
3986}
3987
3988#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3992#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3993#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3994pub struct JsonTableNestedColumn {
3996 pub path: ValueWithSpan,
3998 pub columns: Vec<JsonTableColumn>,
4000}
4001
4002impl fmt::Display for JsonTableNestedColumn {
4003 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4004 write!(
4005 f,
4006 "NESTED PATH {} COLUMNS ({})",
4007 self.path,
4008 display_comma_separated(&self.columns)
4009 )
4010 }
4011}
4012
4013#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4021#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4022#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4023pub struct JsonTableNamedColumn {
4024 pub name: Ident,
4026 pub r#type: DataType,
4028 pub path: ValueWithSpan,
4030 pub exists: bool,
4032 pub on_empty: Option<JsonTableColumnErrorHandling>,
4034 pub on_error: Option<JsonTableColumnErrorHandling>,
4036}
4037
4038impl fmt::Display for JsonTableNamedColumn {
4039 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4040 write!(
4041 f,
4042 "{} {}{} PATH {}",
4043 self.name,
4044 self.r#type,
4045 if self.exists { " EXISTS" } else { "" },
4046 self.path
4047 )?;
4048 if let Some(on_empty) = &self.on_empty {
4049 write!(f, " {on_empty} ON EMPTY")?;
4050 }
4051 if let Some(on_error) = &self.on_error {
4052 write!(f, " {on_error} ON ERROR")?;
4053 }
4054 Ok(())
4055 }
4056}
4057
4058#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4061#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4062#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4063pub enum JsonTableColumnErrorHandling {
4065 Null,
4067 Default(ValueWithSpan),
4069 Error,
4071}
4072
4073impl fmt::Display for JsonTableColumnErrorHandling {
4074 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4075 match self {
4076 JsonTableColumnErrorHandling::Null => write!(f, "NULL"),
4077 JsonTableColumnErrorHandling::Default(json_string) => {
4078 write!(f, "DEFAULT {json_string}")
4079 }
4080 JsonTableColumnErrorHandling::Error => write!(f, "ERROR"),
4081 }
4082 }
4083}
4084
4085#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4093#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4094#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4095pub struct OpenJsonTableColumn {
4096 pub name: Ident,
4098 pub r#type: DataType,
4100 pub path: Option<String>,
4102 pub as_json: bool,
4104}
4105
4106impl fmt::Display for OpenJsonTableColumn {
4107 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4108 write!(f, "{} {}", self.name, self.r#type)?;
4109 if let Some(path) = &self.path {
4110 write!(f, " '{}'", value::escape_single_quote_string(path))?;
4111 }
4112 if self.as_json {
4113 write!(f, " AS JSON")?;
4114 }
4115 Ok(())
4116 }
4117}
4118
4119#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4126#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4127#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4128pub enum ValueTableMode {
4130 AsStruct,
4132 AsValue,
4134 DistinctAsStruct,
4136 DistinctAsValue,
4138}
4139
4140impl fmt::Display for ValueTableMode {
4141 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4142 match self {
4143 ValueTableMode::AsStruct => write!(f, "AS STRUCT"),
4144 ValueTableMode::AsValue => write!(f, "AS VALUE"),
4145 ValueTableMode::DistinctAsStruct => write!(f, "DISTINCT AS STRUCT"),
4146 ValueTableMode::DistinctAsValue => write!(f, "DISTINCT AS VALUE"),
4147 }
4148 }
4149}
4150
4151#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4153#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4154#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4155pub enum UpdateTableFromKind {
4156 BeforeSet(Vec<TableWithJoins>),
4159 AfterSet(Vec<TableWithJoins>),
4162}
4163
4164#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4166#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4167#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4168pub enum XmlTableColumnOption {
4169 NamedInfo {
4171 r#type: DataType,
4173 path: Option<Expr>,
4175 default: Option<Expr>,
4177 nullable: bool,
4179 },
4180 ForOrdinality,
4182}
4183
4184#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4197#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4198#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4199pub struct XmlTableColumn {
4200 pub name: Ident,
4202 pub option: XmlTableColumnOption,
4204}
4205
4206impl fmt::Display for XmlTableColumn {
4207 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4208 write!(f, "{}", self.name)?;
4209 match &self.option {
4210 XmlTableColumnOption::NamedInfo {
4211 r#type,
4212 path,
4213 default,
4214 nullable,
4215 } => {
4216 write!(f, " {type}")?;
4217 if let Some(p) = path {
4218 write!(f, " PATH {p}")?;
4219 }
4220 if let Some(d) = default {
4221 write!(f, " DEFAULT {d}")?;
4222 }
4223 if !*nullable {
4224 write!(f, " NOT NULL")?;
4225 }
4226 Ok(())
4227 }
4228 XmlTableColumnOption::ForOrdinality => {
4229 write!(f, " FOR ORDINALITY")
4230 }
4231 }
4232 }
4233}
4234
4235#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4237#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4238#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4239pub struct XmlPassingArgument {
4241 pub expr: Expr,
4243 pub alias: Option<Ident>,
4245 pub by_value: bool,
4247}
4248
4249impl fmt::Display for XmlPassingArgument {
4250 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4251 if self.by_value {
4252 write!(f, "BY VALUE ")?;
4253 }
4254 write!(f, "{}", self.expr)?;
4255 if let Some(alias) = &self.alias {
4256 write!(f, " AS {alias}")?;
4257 }
4258 Ok(())
4259 }
4260}
4261
4262#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4264#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4265#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4266pub struct XmlPassingClause {
4268 pub arguments: Vec<XmlPassingArgument>,
4270}
4271
4272impl fmt::Display for XmlPassingClause {
4273 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4274 if !self.arguments.is_empty() {
4275 write!(f, " PASSING {}", display_comma_separated(&self.arguments))?;
4276 }
4277 Ok(())
4278 }
4279}
4280
4281#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4285#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4286#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4287pub struct XmlNamespaceDefinition {
4288 pub uri: Expr,
4290 pub name: Ident,
4292}
4293
4294impl fmt::Display for XmlNamespaceDefinition {
4295 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4296 write!(f, "{} AS {}", self.uri, self.name)
4297 }
4298}