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 {
301 pub table_name: Option<Ident>,
303 pub schema_name: Option<Ident>,
305}
306
307impl fmt::Display for Table {
308 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
309 if let Some(ref table_name) = self.table_name {
310 if let Some(ref schema_name) = self.schema_name {
311 write!(f, "TABLE {}.{}", schema_name, table_name,)?;
312 } else {
313 write!(f, "TABLE {}", table_name)?;
314 }
315 } else {
316 write!(f, "TABLE")?;
317 }
318 Ok(())
319 }
320}
321
322#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
324#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
325#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
326pub enum SelectFlavor {
327 Standard,
329 FromFirst,
331 FromFirstNoSelect,
333}
334
335#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Default)]
353#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
354#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
355pub struct SelectModifiers {
356 pub high_priority: bool,
360 pub straight_join: bool,
364 pub sql_small_result: bool,
368 pub sql_big_result: bool,
372 pub sql_buffer_result: bool,
376 pub sql_no_cache: bool,
380 pub sql_calc_found_rows: bool,
385}
386
387impl fmt::Display for SelectModifiers {
388 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
389 if self.high_priority {
390 f.write_str(" HIGH_PRIORITY")?;
391 }
392 if self.straight_join {
393 f.write_str(" STRAIGHT_JOIN")?;
394 }
395 if self.sql_small_result {
396 f.write_str(" SQL_SMALL_RESULT")?;
397 }
398 if self.sql_big_result {
399 f.write_str(" SQL_BIG_RESULT")?;
400 }
401 if self.sql_buffer_result {
402 f.write_str(" SQL_BUFFER_RESULT")?;
403 }
404 if self.sql_no_cache {
405 f.write_str(" SQL_NO_CACHE")?;
406 }
407 if self.sql_calc_found_rows {
408 f.write_str(" SQL_CALC_FOUND_ROWS")?;
409 }
410 Ok(())
411 }
412}
413
414impl SelectModifiers {
415 pub fn is_any_set(&self) -> bool {
417 let Self {
419 high_priority,
420 straight_join,
421 sql_small_result,
422 sql_big_result,
423 sql_buffer_result,
424 sql_no_cache,
425 sql_calc_found_rows,
426 } = self;
427 *high_priority
428 || *straight_join
429 || *sql_small_result
430 || *sql_big_result
431 || *sql_buffer_result
432 || *sql_no_cache
433 || *sql_calc_found_rows
434 }
435}
436
437#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
441#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
442#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
443#[cfg_attr(feature = "visitor", visit(with = "visit_select"))]
444pub struct Select {
445 pub select_token: AttachedToken,
447 pub optimizer_hints: Vec<OptimizerHint>,
452 pub distinct: Option<Distinct>,
454 pub select_modifiers: Option<SelectModifiers>,
458 pub top: Option<Top>,
460 pub top_before_distinct: bool,
462 pub projection: Vec<SelectItem>,
464 pub exclude: Option<ExcludeSelectItem>,
469 pub into: Option<SelectInto>,
471 pub from: Vec<TableWithJoins>,
473 pub lateral_views: Vec<LateralView>,
475 pub prewhere: Option<Expr>,
480 pub selection: Option<Expr>,
482 pub connect_by: Vec<ConnectByKind>,
484 pub group_by: GroupByExpr,
486 pub cluster_by: Vec<Expr>,
488 pub distribute_by: Vec<Expr>,
490 pub sort_by: Vec<OrderByExpr>,
492 pub having: Option<Expr>,
494 pub named_window: Vec<NamedWindowDefinition>,
496 pub qualify: Option<Expr>,
498 pub window_before_qualify: bool,
503 pub value_table_mode: Option<ValueTableMode>,
505 pub flavor: SelectFlavor,
507}
508
509impl fmt::Display for Select {
510 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
511 match self.flavor {
512 SelectFlavor::Standard => {
513 write!(f, "SELECT")?;
514 }
515 SelectFlavor::FromFirst => {
516 write!(f, "FROM {} SELECT", display_comma_separated(&self.from))?;
517 }
518 SelectFlavor::FromFirstNoSelect => {
519 write!(f, "FROM {}", display_comma_separated(&self.from))?;
520 }
521 }
522
523 for hint in &self.optimizer_hints {
524 f.write_str(" ")?;
525 hint.fmt(f)?;
526 }
527
528 if let Some(value_table_mode) = self.value_table_mode {
529 f.write_str(" ")?;
530 value_table_mode.fmt(f)?;
531 }
532
533 if let Some(ref top) = self.top {
534 if self.top_before_distinct {
535 f.write_str(" ")?;
536 top.fmt(f)?;
537 }
538 }
539 if let Some(ref distinct) = self.distinct {
540 f.write_str(" ")?;
541 distinct.fmt(f)?;
542 }
543 if let Some(ref top) = self.top {
544 if !self.top_before_distinct {
545 f.write_str(" ")?;
546 top.fmt(f)?;
547 }
548 }
549
550 if let Some(ref select_modifiers) = self.select_modifiers {
551 select_modifiers.fmt(f)?;
552 }
553
554 if !self.projection.is_empty() {
555 indented_list(f, &self.projection)?;
556 }
557
558 if let Some(exclude) = &self.exclude {
559 write!(f, " {exclude}")?;
560 }
561
562 if let Some(ref into) = self.into {
563 f.write_str(" ")?;
564 into.fmt(f)?;
565 }
566
567 if self.flavor == SelectFlavor::Standard && !self.from.is_empty() {
568 SpaceOrNewline.fmt(f)?;
569 f.write_str("FROM")?;
570 indented_list(f, &self.from)?;
571 }
572 if !self.lateral_views.is_empty() {
573 for lv in &self.lateral_views {
574 lv.fmt(f)?;
575 }
576 }
577 if let Some(ref prewhere) = self.prewhere {
578 f.write_str(" PREWHERE ")?;
579 prewhere.fmt(f)?;
580 }
581 if let Some(ref selection) = self.selection {
582 SpaceOrNewline.fmt(f)?;
583 f.write_str("WHERE")?;
584 SpaceOrNewline.fmt(f)?;
585 Indent(selection).fmt(f)?;
586 }
587 for clause in &self.connect_by {
588 SpaceOrNewline.fmt(f)?;
589 clause.fmt(f)?;
590 }
591 match &self.group_by {
592 GroupByExpr::All(_) => {
593 SpaceOrNewline.fmt(f)?;
594 self.group_by.fmt(f)?;
595 }
596 GroupByExpr::Expressions(exprs, _) => {
597 if !exprs.is_empty() {
598 SpaceOrNewline.fmt(f)?;
599 self.group_by.fmt(f)?;
600 }
601 }
602 }
603 if !self.cluster_by.is_empty() {
604 SpaceOrNewline.fmt(f)?;
605 f.write_str("CLUSTER BY")?;
606 SpaceOrNewline.fmt(f)?;
607 Indent(display_comma_separated(&self.cluster_by)).fmt(f)?;
608 }
609 if !self.distribute_by.is_empty() {
610 SpaceOrNewline.fmt(f)?;
611 f.write_str("DISTRIBUTE BY")?;
612 SpaceOrNewline.fmt(f)?;
613 display_comma_separated(&self.distribute_by).fmt(f)?;
614 }
615 if !self.sort_by.is_empty() {
616 SpaceOrNewline.fmt(f)?;
617 f.write_str("SORT BY")?;
618 SpaceOrNewline.fmt(f)?;
619 Indent(display_comma_separated(&self.sort_by)).fmt(f)?;
620 }
621 if let Some(ref having) = self.having {
622 SpaceOrNewline.fmt(f)?;
623 f.write_str("HAVING")?;
624 SpaceOrNewline.fmt(f)?;
625 Indent(having).fmt(f)?;
626 }
627 if self.window_before_qualify {
628 if !self.named_window.is_empty() {
629 SpaceOrNewline.fmt(f)?;
630 f.write_str("WINDOW")?;
631 SpaceOrNewline.fmt(f)?;
632 display_comma_separated(&self.named_window).fmt(f)?;
633 }
634 if let Some(ref qualify) = self.qualify {
635 SpaceOrNewline.fmt(f)?;
636 f.write_str("QUALIFY")?;
637 SpaceOrNewline.fmt(f)?;
638 qualify.fmt(f)?;
639 }
640 } else {
641 if let Some(ref qualify) = self.qualify {
642 SpaceOrNewline.fmt(f)?;
643 f.write_str("QUALIFY")?;
644 SpaceOrNewline.fmt(f)?;
645 qualify.fmt(f)?;
646 }
647 if !self.named_window.is_empty() {
648 SpaceOrNewline.fmt(f)?;
649 f.write_str("WINDOW")?;
650 SpaceOrNewline.fmt(f)?;
651 display_comma_separated(&self.named_window).fmt(f)?;
652 }
653 }
654 Ok(())
655 }
656}
657
658#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
660#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
661#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
662pub struct LateralView {
663 pub lateral_view: Expr,
665 pub lateral_view_name: ObjectName,
667 pub lateral_col_alias: Vec<Ident>,
669 pub outer: bool,
671}
672
673impl fmt::Display for LateralView {
674 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
675 write!(
676 f,
677 " LATERAL VIEW{outer} {} {}",
678 self.lateral_view,
679 self.lateral_view_name,
680 outer = if self.outer { " OUTER" } else { "" }
681 )?;
682 if !self.lateral_col_alias.is_empty() {
683 write!(
684 f,
685 " AS {}",
686 display_comma_separated(&self.lateral_col_alias)
687 )?;
688 }
689 Ok(())
690 }
691}
692
693#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
699#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
700#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
701pub enum NamedWindowExpr {
702 NamedWindow(Ident),
712 WindowSpec(WindowSpec),
719}
720
721impl fmt::Display for NamedWindowExpr {
722 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
723 match self {
724 NamedWindowExpr::NamedWindow(named_window) => {
725 write!(f, "{named_window}")?;
726 }
727 NamedWindowExpr::WindowSpec(window_spec) => {
728 write!(f, "({window_spec})")?;
729 }
730 };
731 Ok(())
732 }
733}
734
735#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
736#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
737#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
738pub struct NamedWindowDefinition(pub Ident, pub NamedWindowExpr);
740
741impl fmt::Display for NamedWindowDefinition {
742 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
743 write!(f, "{} AS {}", self.0, self.1)
744 }
745}
746
747#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
748#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
749#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
750pub struct With {
752 pub with_token: AttachedToken,
754 pub recursive: bool,
756 pub cte_tables: Vec<Cte>,
758}
759
760impl fmt::Display for With {
761 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
762 f.write_str("WITH ")?;
763 if self.recursive {
764 f.write_str("RECURSIVE ")?;
765 }
766 display_comma_separated(&self.cte_tables).fmt(f)?;
767 Ok(())
768 }
769}
770
771#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
772#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
773#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
774pub enum CteAsMaterialized {
776 Materialized,
778 NotMaterialized,
780}
781
782impl fmt::Display for CteAsMaterialized {
783 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
784 match *self {
785 CteAsMaterialized::Materialized => {
786 write!(f, "MATERIALIZED")?;
787 }
788 CteAsMaterialized::NotMaterialized => {
789 write!(f, "NOT MATERIALIZED")?;
790 }
791 };
792 Ok(())
793 }
794}
795
796#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
801#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
802#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
803pub struct Cte {
804 pub alias: TableAlias,
806 pub query: Box<Query>,
808 pub from: Option<Ident>,
810 pub materialized: Option<CteAsMaterialized>,
812 pub closing_paren_token: AttachedToken,
814}
815
816impl fmt::Display for Cte {
817 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
818 match self.materialized.as_ref() {
819 None => {
820 self.alias.fmt(f)?;
821 f.write_str(" AS (")?;
822 NewLine.fmt(f)?;
823 Indent(&self.query).fmt(f)?;
824 NewLine.fmt(f)?;
825 f.write_str(")")?;
826 }
827 Some(materialized) => {
828 self.alias.fmt(f)?;
829 f.write_str(" AS ")?;
830 materialized.fmt(f)?;
831 f.write_str(" (")?;
832 NewLine.fmt(f)?;
833 Indent(&self.query).fmt(f)?;
834 NewLine.fmt(f)?;
835 f.write_str(")")?;
836 }
837 };
838 if let Some(ref fr) = self.from {
839 write!(f, " FROM {fr}")?;
840 }
841 Ok(())
842 }
843}
844
845#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
848#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
849#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
850pub enum SelectItemQualifiedWildcardKind {
851 ObjectName(ObjectName),
854 Expr(Expr),
857}
858
859#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
861#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
862#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
863pub enum SelectItem {
864 UnnamedExpr(Expr),
866 ExprWithAlias {
868 expr: Expr,
870 alias: Ident,
872 },
873 ExprWithAliases {
877 expr: Expr,
879 aliases: Vec<Ident>,
881 },
882 QualifiedWildcard(SelectItemQualifiedWildcardKind, WildcardAdditionalOptions),
885 Wildcard(WildcardAdditionalOptions),
887}
888
889impl fmt::Display for SelectItemQualifiedWildcardKind {
890 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
891 match &self {
892 SelectItemQualifiedWildcardKind::ObjectName(object_name) => {
893 write!(f, "{object_name}.*")
894 }
895 SelectItemQualifiedWildcardKind::Expr(expr) => write!(f, "{expr}.*"),
896 }
897 }
898}
899
900#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
907#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
908#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
909pub struct IdentWithAlias {
910 pub ident: Ident,
912 pub alias: Ident,
914}
915
916impl fmt::Display for IdentWithAlias {
917 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
918 write!(f, "{} AS {}", self.ident, self.alias)
919 }
920}
921
922#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
924#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
925#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
926pub struct WildcardAdditionalOptions {
927 pub wildcard_token: AttachedToken,
929 pub opt_ilike: Option<IlikeSelectItem>,
932 pub opt_exclude: Option<ExcludeSelectItem>,
934 pub opt_except: Option<ExceptSelectItem>,
937 pub opt_replace: Option<ReplaceSelectItem>,
942 pub opt_rename: Option<RenameSelectItem>,
944 pub opt_alias: Option<Ident>,
947}
948
949impl Default for WildcardAdditionalOptions {
950 fn default() -> Self {
951 Self {
952 wildcard_token: TokenWithSpan::wrap(Token::Mul).into(),
953 opt_ilike: None,
954 opt_exclude: None,
955 opt_except: None,
956 opt_replace: None,
957 opt_rename: None,
958 opt_alias: None,
959 }
960 }
961}
962
963impl fmt::Display for WildcardAdditionalOptions {
964 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
965 if let Some(ilike) = &self.opt_ilike {
966 write!(f, " {ilike}")?;
967 }
968 if let Some(exclude) = &self.opt_exclude {
969 write!(f, " {exclude}")?;
970 }
971 if let Some(except) = &self.opt_except {
972 write!(f, " {except}")?;
973 }
974 if let Some(replace) = &self.opt_replace {
975 write!(f, " {replace}")?;
976 }
977 if let Some(rename) = &self.opt_rename {
978 write!(f, " {rename}")?;
979 }
980 if let Some(alias) = &self.opt_alias {
981 write!(f, " AS {alias}")?;
982 }
983 Ok(())
984 }
985}
986
987#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
994#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
995#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
996pub struct IlikeSelectItem {
997 pub pattern: String,
999}
1000
1001impl fmt::Display for IlikeSelectItem {
1002 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1003 write!(
1004 f,
1005 "ILIKE '{}'",
1006 value::escape_single_quote_string(&self.pattern)
1007 )?;
1008 Ok(())
1009 }
1010}
1011#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1019#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1020#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1021pub enum ExcludeSelectItem {
1022 Single(ObjectName),
1029 Multiple(Vec<ObjectName>),
1035}
1036
1037impl fmt::Display for ExcludeSelectItem {
1038 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1039 write!(f, "EXCLUDE")?;
1040 match self {
1041 Self::Single(column) => {
1042 write!(f, " {column}")?;
1043 }
1044 Self::Multiple(columns) => {
1045 write!(f, " ({})", display_comma_separated(columns))?;
1046 }
1047 }
1048 Ok(())
1049 }
1050}
1051
1052#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1060#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1061#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1062pub enum RenameSelectItem {
1063 Single(IdentWithAlias),
1070 Multiple(Vec<IdentWithAlias>),
1076}
1077
1078impl fmt::Display for RenameSelectItem {
1079 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1080 write!(f, "RENAME")?;
1081 match self {
1082 Self::Single(column) => {
1083 write!(f, " {column}")?;
1084 }
1085 Self::Multiple(columns) => {
1086 write!(f, " ({})", display_comma_separated(columns))?;
1087 }
1088 }
1089 Ok(())
1090 }
1091}
1092
1093#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1100#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1101#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1102pub struct ExceptSelectItem {
1103 pub first_element: Ident,
1105 pub additional_elements: Vec<Ident>,
1107}
1108
1109impl fmt::Display for ExceptSelectItem {
1110 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1111 write!(f, "EXCEPT ")?;
1112 if self.additional_elements.is_empty() {
1113 write!(f, "({})", self.first_element)?;
1114 } else {
1115 write!(
1116 f,
1117 "({}, {})",
1118 self.first_element,
1119 display_comma_separated(&self.additional_elements)
1120 )?;
1121 }
1122 Ok(())
1123 }
1124}
1125
1126#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1134#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1135#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1136pub struct ReplaceSelectItem {
1137 pub items: Vec<Box<ReplaceSelectElement>>,
1139}
1140
1141impl fmt::Display for ReplaceSelectItem {
1142 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1143 write!(f, "REPLACE")?;
1144 write!(f, " ({})", display_comma_separated(&self.items))?;
1145 Ok(())
1146 }
1147}
1148
1149#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1154#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1155#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1156pub struct ReplaceSelectElement {
1157 pub expr: Expr,
1159 pub column_name: Ident,
1161 pub as_keyword: bool,
1163}
1164
1165impl fmt::Display for ReplaceSelectElement {
1166 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1167 if self.as_keyword {
1168 write!(f, "{} AS {}", self.expr, self.column_name)
1169 } else {
1170 write!(f, "{} {}", self.expr, self.column_name)
1171 }
1172 }
1173}
1174
1175impl fmt::Display for SelectItem {
1176 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1177 use core::fmt::Write;
1178 match &self {
1179 SelectItem::UnnamedExpr(expr) => expr.fmt(f),
1180 SelectItem::ExprWithAlias { expr, alias } => {
1181 expr.fmt(f)?;
1182 f.write_str(" AS ")?;
1183 alias.fmt(f)
1184 }
1185 SelectItem::ExprWithAliases { expr, aliases } => {
1186 expr.fmt(f)?;
1187 f.write_str(" AS (")?;
1188 display_comma_separated(aliases).fmt(f)?;
1189 f.write_str(")")
1190 }
1191 SelectItem::QualifiedWildcard(kind, additional_options) => {
1192 kind.fmt(f)?;
1193 additional_options.fmt(f)
1194 }
1195 SelectItem::Wildcard(additional_options) => {
1196 f.write_char('*')?;
1197 additional_options.fmt(f)
1198 }
1199 }
1200 }
1201}
1202
1203#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1204#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1205#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1206pub struct TableWithJoins {
1208 pub relation: TableFactor,
1210 pub joins: Vec<Join>,
1212}
1213
1214impl fmt::Display for TableWithJoins {
1215 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1216 self.relation.fmt(f)?;
1217 for join in &self.joins {
1218 SpaceOrNewline.fmt(f)?;
1219 join.fmt(f)?;
1220 }
1221 Ok(())
1222 }
1223}
1224
1225#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1230#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1231#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1232pub enum ConnectByKind {
1233 ConnectBy {
1235 connect_token: AttachedToken,
1237
1238 nocycle: bool,
1242
1243 relationships: Vec<Expr>,
1245 },
1246
1247 StartWith {
1252 start_token: AttachedToken,
1254
1255 condition: Box<Expr>,
1257 },
1258}
1259
1260impl fmt::Display for ConnectByKind {
1261 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1262 match self {
1263 ConnectByKind::ConnectBy {
1264 connect_token: _,
1265 nocycle,
1266 relationships,
1267 } => {
1268 write!(
1269 f,
1270 "CONNECT BY {nocycle}{relationships}",
1271 nocycle = if *nocycle { "NOCYCLE " } else { "" },
1272 relationships = display_comma_separated(relationships)
1273 )
1274 }
1275 ConnectByKind::StartWith {
1276 start_token: _,
1277 condition,
1278 } => {
1279 write!(f, "START WITH {condition}")
1280 }
1281 }
1282 }
1283}
1284
1285#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1286#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1287#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1288pub struct Setting {
1290 pub key: Ident,
1292 pub value: Expr,
1294}
1295
1296impl fmt::Display for Setting {
1297 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1298 write!(f, "{} = {}", self.key, self.value)
1299 }
1300}
1301
1302#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1309#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1310#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1311pub struct ExprWithAlias {
1312 pub expr: Expr,
1314 pub alias: Option<Ident>,
1316}
1317
1318impl fmt::Display for ExprWithAlias {
1319 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1320 let ExprWithAlias { expr, alias } = self;
1321 write!(f, "{expr}")?;
1322 if let Some(alias) = alias {
1323 write!(f, " AS {alias}")?;
1324 }
1325 Ok(())
1326 }
1327}
1328
1329#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1336#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1337#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1338pub struct ExprWithAliasAndOrderBy {
1339 pub expr: ExprWithAlias,
1341 pub order_by: OrderByOptions,
1343}
1344
1345impl fmt::Display for ExprWithAliasAndOrderBy {
1346 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1347 write!(f, "{}{}", self.expr, self.order_by)
1348 }
1349}
1350
1351#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1353#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1354#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1355pub struct TableFunctionArgs {
1356 pub args: Vec<FunctionArg>,
1358 pub settings: Option<Vec<Setting>>,
1363}
1364
1365#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1366#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1367#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1368pub enum TableIndexHintType {
1370 Use,
1372 Ignore,
1374 Force,
1376}
1377
1378impl fmt::Display for TableIndexHintType {
1379 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1380 f.write_str(match self {
1381 TableIndexHintType::Use => "USE",
1382 TableIndexHintType::Ignore => "IGNORE",
1383 TableIndexHintType::Force => "FORCE",
1384 })
1385 }
1386}
1387
1388#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1389#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1390#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1391pub enum TableIndexType {
1393 Index,
1395 Key,
1397}
1398
1399impl fmt::Display for TableIndexType {
1400 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1401 f.write_str(match self {
1402 TableIndexType::Index => "INDEX",
1403 TableIndexType::Key => "KEY",
1404 })
1405 }
1406}
1407
1408#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1409#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1410#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1411pub enum TableIndexHintForClause {
1413 Join,
1415 OrderBy,
1417 GroupBy,
1419}
1420
1421impl fmt::Display for TableIndexHintForClause {
1422 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1423 f.write_str(match self {
1424 TableIndexHintForClause::Join => "JOIN",
1425 TableIndexHintForClause::OrderBy => "ORDER BY",
1426 TableIndexHintForClause::GroupBy => "GROUP BY",
1427 })
1428 }
1429}
1430
1431#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1432#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1433#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1434pub struct TableIndexHints {
1436 pub hint_type: TableIndexHintType,
1438 pub index_type: TableIndexType,
1440 pub for_clause: Option<TableIndexHintForClause>,
1442 pub index_names: Vec<Ident>,
1444}
1445
1446impl fmt::Display for TableIndexHints {
1447 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1448 write!(f, "{} {} ", self.hint_type, self.index_type)?;
1449 if let Some(for_clause) = &self.for_clause {
1450 write!(f, "FOR {for_clause} ")?;
1451 }
1452 write!(f, "({})", display_comma_separated(&self.index_names))
1453 }
1454}
1455
1456#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1458#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1459#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1460#[cfg_attr(feature = "visitor", visit(with = "visit_table_factor"))]
1461pub enum TableFactor {
1462 Table {
1464 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
1465 name: ObjectName,
1467 alias: Option<TableAlias>,
1469 args: Option<TableFunctionArgs>,
1477 with_hints: Vec<Expr>,
1479 version: Option<TableVersion>,
1482 with_ordinality: bool,
1486 partitions: Vec<Ident>,
1488 json_path: Option<JsonPath>,
1490 sample: Option<TableSampleKind>,
1493 index_hints: Vec<TableIndexHints>,
1496 },
1497 Derived {
1499 lateral: bool,
1501 subquery: Box<Query>,
1503 alias: Option<TableAlias>,
1505 sample: Option<TableSampleKind>,
1507 },
1508 TableFunction {
1510 expr: Expr,
1512 alias: Option<TableAlias>,
1514 },
1515 Function {
1517 lateral: bool,
1519 name: ObjectName,
1521 args: Vec<FunctionArg>,
1523 with_ordinality: bool,
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 UnpivotExpr {
1663 expression: Expr,
1665 value_alias: Ident,
1667 attribute_alias: Option<Ident>,
1669 },
1670 MatchRecognize {
1674 table: Box<TableFactor>,
1676 partition_by: Vec<Expr>,
1678 order_by: Vec<OrderByExpr>,
1680 measures: Vec<Measure>,
1682 rows_per_match: Option<RowsPerMatch>,
1684 after_match_skip: Option<AfterMatchSkip>,
1686 pattern: MatchRecognizePattern,
1688 symbols: Vec<SymbolDefinition>,
1690 alias: Option<TableAlias>,
1692 },
1693 XmlTable {
1713 namespaces: Vec<XmlNamespaceDefinition>,
1715 row_expression: Expr,
1717 passing: XmlPassingClause,
1719 columns: Vec<XmlTableColumn>,
1721 alias: Option<TableAlias>,
1723 },
1724 SemanticView {
1736 name: ObjectName,
1738 dimensions: Vec<Expr>,
1740 metrics: Vec<Expr>,
1742 facts: Vec<Expr>,
1744 where_clause: Option<Expr>,
1746 alias: Option<TableAlias>,
1748 },
1749}
1750
1751#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1753#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1754#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1755pub enum TableSampleKind {
1756 BeforeTableAlias(Box<TableSample>),
1758 AfterTableAlias(Box<TableSample>),
1760}
1761
1762#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1763#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1764#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1765pub struct TableSample {
1767 pub modifier: TableSampleModifier,
1769 pub name: Option<TableSampleMethod>,
1771 pub quantity: Option<TableSampleQuantity>,
1773 pub seed: Option<TableSampleSeed>,
1775 pub bucket: Option<TableSampleBucket>,
1777 pub offset: Option<Expr>,
1779}
1780
1781#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1782#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1783#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1784pub enum TableSampleModifier {
1786 Sample,
1788 TableSample,
1790}
1791
1792impl fmt::Display for TableSampleModifier {
1793 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1794 match self {
1795 TableSampleModifier::Sample => write!(f, "SAMPLE")?,
1796 TableSampleModifier::TableSample => write!(f, "TABLESAMPLE")?,
1797 }
1798 Ok(())
1799 }
1800}
1801
1802#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1803#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1804#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1805pub struct TableSampleQuantity {
1807 pub parenthesized: bool,
1809 pub value: Expr,
1811 pub unit: Option<TableSampleUnit>,
1813}
1814
1815impl fmt::Display for TableSampleQuantity {
1816 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1817 if self.parenthesized {
1818 write!(f, "(")?;
1819 }
1820 write!(f, "{}", self.value)?;
1821 if let Some(unit) = &self.unit {
1822 write!(f, " {unit}")?;
1823 }
1824 if self.parenthesized {
1825 write!(f, ")")?;
1826 }
1827 Ok(())
1828 }
1829}
1830
1831#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1833#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1834#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1835pub enum TableSampleMethod {
1837 Row,
1839 Bernoulli,
1841 System,
1843 Block,
1845}
1846
1847impl fmt::Display for TableSampleMethod {
1848 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1849 match self {
1850 TableSampleMethod::Bernoulli => write!(f, "BERNOULLI"),
1851 TableSampleMethod::Row => write!(f, "ROW"),
1852 TableSampleMethod::System => write!(f, "SYSTEM"),
1853 TableSampleMethod::Block => write!(f, "BLOCK"),
1854 }
1855 }
1856}
1857
1858#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1859#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1860#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1861pub struct TableSampleSeed {
1863 pub modifier: TableSampleSeedModifier,
1865 pub value: ValueWithSpan,
1867}
1868
1869impl fmt::Display for TableSampleSeed {
1870 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1871 write!(f, "{} ({})", self.modifier, self.value)?;
1872 Ok(())
1873 }
1874}
1875
1876#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1877#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1878#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1879pub enum TableSampleSeedModifier {
1881 Repeatable,
1883 Seed,
1885}
1886
1887impl fmt::Display for TableSampleSeedModifier {
1888 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1889 match self {
1890 TableSampleSeedModifier::Repeatable => write!(f, "REPEATABLE"),
1891 TableSampleSeedModifier::Seed => write!(f, "SEED"),
1892 }
1893 }
1894}
1895
1896#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1897#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1898#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1899pub enum TableSampleUnit {
1901 Rows,
1903 Percent,
1905}
1906
1907impl fmt::Display for TableSampleUnit {
1908 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1909 match self {
1910 TableSampleUnit::Percent => write!(f, "PERCENT"),
1911 TableSampleUnit::Rows => write!(f, "ROWS"),
1912 }
1913 }
1914}
1915
1916#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1917#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1918#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1919pub struct TableSampleBucket {
1921 pub bucket: ValueWithSpan,
1923 pub total: ValueWithSpan,
1925 pub on: Option<Expr>,
1927}
1928
1929impl fmt::Display for TableSampleBucket {
1930 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1931 write!(f, "BUCKET {} OUT OF {}", self.bucket, self.total)?;
1932 if let Some(on) = &self.on {
1933 write!(f, " ON {on}")?;
1934 }
1935 Ok(())
1936 }
1937}
1938impl fmt::Display for TableSample {
1939 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1940 write!(f, "{}", self.modifier)?;
1941 if let Some(name) = &self.name {
1942 write!(f, " {name}")?;
1943 }
1944 if let Some(quantity) = &self.quantity {
1945 write!(f, " {quantity}")?;
1946 }
1947 if let Some(seed) = &self.seed {
1948 write!(f, " {seed}")?;
1949 }
1950 if let Some(bucket) = &self.bucket {
1951 write!(f, " ({bucket})")?;
1952 }
1953 if let Some(offset) = &self.offset {
1954 write!(f, " OFFSET {offset}")?;
1955 }
1956 Ok(())
1957 }
1958}
1959
1960#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1962#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1963#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1964pub enum PivotValueSource {
1965 List(Vec<ExprWithAlias>),
1969 Any(Vec<OrderByExpr>),
1973 Subquery(Box<Query>),
1977}
1978
1979impl fmt::Display for PivotValueSource {
1980 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1981 match self {
1982 PivotValueSource::List(values) => write!(f, "{}", display_comma_separated(values)),
1983 PivotValueSource::Any(order_by) => {
1984 write!(f, "ANY")?;
1985 if !order_by.is_empty() {
1986 write!(f, " ORDER BY {}", display_comma_separated(order_by))?;
1987 }
1988 Ok(())
1989 }
1990 PivotValueSource::Subquery(query) => write!(f, "{query}"),
1991 }
1992 }
1993}
1994
1995#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1999#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2000#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2001pub struct Measure {
2003 pub expr: Expr,
2005 pub alias: Ident,
2007}
2008
2009impl fmt::Display for Measure {
2010 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2011 write!(f, "{} AS {}", self.expr, self.alias)
2012 }
2013}
2014
2015#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2019#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2020#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2021pub enum RowsPerMatch {
2022 OneRow,
2024 AllRows(Option<EmptyMatchesMode>),
2026}
2027
2028impl fmt::Display for RowsPerMatch {
2029 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2030 match self {
2031 RowsPerMatch::OneRow => write!(f, "ONE ROW PER MATCH"),
2032 RowsPerMatch::AllRows(mode) => {
2033 write!(f, "ALL ROWS PER MATCH")?;
2034 if let Some(mode) = mode {
2035 write!(f, " {mode}")?;
2036 }
2037 Ok(())
2038 }
2039 }
2040 }
2041}
2042
2043#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2047#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2048#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2049pub enum AfterMatchSkip {
2050 PastLastRow,
2052 ToNextRow,
2054 ToFirst(Ident),
2056 ToLast(Ident),
2058}
2059
2060impl fmt::Display for AfterMatchSkip {
2061 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2062 write!(f, "AFTER MATCH SKIP ")?;
2063 match self {
2064 AfterMatchSkip::PastLastRow => write!(f, "PAST LAST ROW"),
2065 AfterMatchSkip::ToNextRow => write!(f, " TO NEXT ROW"),
2066 AfterMatchSkip::ToFirst(symbol) => write!(f, "TO FIRST {symbol}"),
2067 AfterMatchSkip::ToLast(symbol) => write!(f, "TO LAST {symbol}"),
2068 }
2069 }
2070}
2071
2072#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2073#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2074#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2075pub enum EmptyMatchesMode {
2077 Show,
2079 Omit,
2081 WithUnmatched,
2083}
2084
2085impl fmt::Display for EmptyMatchesMode {
2086 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2087 match self {
2088 EmptyMatchesMode::Show => write!(f, "SHOW EMPTY MATCHES"),
2089 EmptyMatchesMode::Omit => write!(f, "OMIT EMPTY MATCHES"),
2090 EmptyMatchesMode::WithUnmatched => write!(f, "WITH UNMATCHED ROWS"),
2091 }
2092 }
2093}
2094
2095#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2099#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2100#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2101pub struct SymbolDefinition {
2103 pub symbol: Ident,
2105 pub definition: Expr,
2107}
2108
2109impl fmt::Display for SymbolDefinition {
2110 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2111 write!(f, "{} AS {}", self.symbol, self.definition)
2112 }
2113}
2114
2115#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2117#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2118#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2119pub enum MatchRecognizeSymbol {
2120 Named(Ident),
2122 Start,
2124 End,
2126}
2127
2128impl fmt::Display for MatchRecognizeSymbol {
2129 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2130 match self {
2131 MatchRecognizeSymbol::Named(symbol) => write!(f, "{symbol}"),
2132 MatchRecognizeSymbol::Start => write!(f, "^"),
2133 MatchRecognizeSymbol::End => write!(f, "$"),
2134 }
2135 }
2136}
2137
2138#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2142#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2143#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2144pub enum MatchRecognizePattern {
2145 Symbol(MatchRecognizeSymbol),
2147 Exclude(MatchRecognizeSymbol),
2149 Permute(Vec<MatchRecognizeSymbol>),
2151 Concat(Vec<MatchRecognizePattern>),
2153 Group(Box<MatchRecognizePattern>),
2155 Alternation(Vec<MatchRecognizePattern>),
2157 Repetition(Box<MatchRecognizePattern>, RepetitionQuantifier),
2159}
2160
2161impl fmt::Display for MatchRecognizePattern {
2162 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2163 use MatchRecognizePattern::*;
2164 match self {
2165 Symbol(symbol) => write!(f, "{symbol}"),
2166 Exclude(symbol) => write!(f, "{{- {symbol} -}}"),
2167 Permute(symbols) => write!(f, "PERMUTE({})", display_comma_separated(symbols)),
2168 Concat(patterns) => write!(f, "{}", display_separated(patterns, " ")),
2169 Group(pattern) => write!(f, "( {pattern} )"),
2170 Alternation(patterns) => write!(f, "{}", display_separated(patterns, " | ")),
2171 Repetition(pattern, op) => write!(f, "{pattern}{op}"),
2172 }
2173 }
2174}
2175
2176#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2179#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2180#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2181pub enum RepetitionQuantifier {
2182 ZeroOrMore,
2184 OneOrMore,
2186 AtMostOne,
2188 Exactly(u32),
2190 AtLeast(u32),
2192 AtMost(u32),
2194 Range(u32, u32),
2196 Reluctant(Box<RepetitionQuantifier>),
2198}
2199
2200impl fmt::Display for RepetitionQuantifier {
2201 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2202 use RepetitionQuantifier::*;
2203 match self {
2204 ZeroOrMore => write!(f, "*"),
2205 OneOrMore => write!(f, "+"),
2206 AtMostOne => write!(f, "?"),
2207 Exactly(n) => write!(f, "{{{n}}}"),
2208 AtLeast(n) => write!(f, "{{{n},}}"),
2209 AtMost(n) => write!(f, "{{,{n}}}"),
2210 Range(n, m) => write!(f, "{{{n},{m}}}"),
2211 Reluctant(quantifier) => write!(f, "{quantifier}?"),
2212 }
2213 }
2214}
2215
2216impl fmt::Display for TableFactor {
2217 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2218 match self {
2219 TableFactor::Table {
2220 name,
2221 alias,
2222 args,
2223 with_hints,
2224 version,
2225 partitions,
2226 with_ordinality,
2227 json_path,
2228 sample,
2229 index_hints,
2230 } => {
2231 name.fmt(f)?;
2232 if let Some(json_path) = json_path {
2233 json_path.fmt(f)?;
2234 }
2235 if !partitions.is_empty() {
2236 write!(f, " PARTITION ({})", display_comma_separated(partitions))?;
2237 }
2238 if let Some(args) = args {
2239 write!(f, "(")?;
2240 write!(f, "{}", display_comma_separated(&args.args))?;
2241 if let Some(ref settings) = args.settings {
2242 if !args.args.is_empty() {
2243 write!(f, ", ")?;
2244 }
2245 write!(f, "SETTINGS {}", display_comma_separated(settings))?;
2246 }
2247 write!(f, ")")?;
2248 }
2249 if *with_ordinality {
2250 write!(f, " WITH ORDINALITY")?;
2251 }
2252 if let Some(TableSampleKind::BeforeTableAlias(sample)) = sample {
2253 write!(f, " {sample}")?;
2254 }
2255 if let Some(alias) = alias {
2256 write!(f, " {alias}")?;
2257 }
2258 if !index_hints.is_empty() {
2259 write!(f, " {}", display_separated(index_hints, " "))?;
2260 }
2261 if !with_hints.is_empty() {
2262 write!(f, " WITH ({})", display_comma_separated(with_hints))?;
2263 }
2264 if let Some(version) = version {
2265 write!(f, " {version}")?;
2266 }
2267 if let Some(TableSampleKind::AfterTableAlias(sample)) = sample {
2268 write!(f, " {sample}")?;
2269 }
2270 Ok(())
2271 }
2272 TableFactor::Derived {
2273 lateral,
2274 subquery,
2275 alias,
2276 sample,
2277 } => {
2278 if *lateral {
2279 write!(f, "LATERAL ")?;
2280 }
2281 f.write_str("(")?;
2282 NewLine.fmt(f)?;
2283 Indent(subquery).fmt(f)?;
2284 NewLine.fmt(f)?;
2285 f.write_str(")")?;
2286 if let Some(alias) = alias {
2287 write!(f, " {alias}")?;
2288 }
2289 if let Some(TableSampleKind::AfterTableAlias(sample)) = sample {
2290 write!(f, " {sample}")?;
2291 }
2292 Ok(())
2293 }
2294 TableFactor::Function {
2295 lateral,
2296 name,
2297 args,
2298 with_ordinality,
2299 alias,
2300 } => {
2301 if *lateral {
2302 write!(f, "LATERAL ")?;
2303 }
2304 write!(f, "{name}")?;
2305 write!(f, "({})", display_comma_separated(args))?;
2306 if *with_ordinality {
2307 write!(f, " WITH ORDINALITY")?;
2308 }
2309 if let Some(alias) = alias {
2310 write!(f, " {alias}")?;
2311 }
2312 Ok(())
2313 }
2314 TableFactor::TableFunction { expr, alias } => {
2315 write!(f, "TABLE({expr})")?;
2316 if let Some(alias) = alias {
2317 write!(f, " {alias}")?;
2318 }
2319 Ok(())
2320 }
2321 TableFactor::UNNEST {
2322 alias,
2323 array_exprs,
2324 with_offset,
2325 with_offset_alias,
2326 with_ordinality,
2327 } => {
2328 write!(f, "UNNEST({})", display_comma_separated(array_exprs))?;
2329
2330 if *with_ordinality {
2331 write!(f, " WITH ORDINALITY")?;
2332 }
2333
2334 if let Some(alias) = alias {
2335 write!(f, " {alias}")?;
2336 }
2337 if *with_offset {
2338 write!(f, " WITH OFFSET")?;
2339 }
2340 if let Some(alias) = with_offset_alias {
2341 write!(f, " {alias}")?;
2342 }
2343 Ok(())
2344 }
2345 TableFactor::JsonTable {
2346 json_expr,
2347 json_path,
2348 columns,
2349 alias,
2350 } => {
2351 write!(
2352 f,
2353 "JSON_TABLE({json_expr}, {json_path} COLUMNS({columns}))",
2354 columns = display_comma_separated(columns)
2355 )?;
2356 if let Some(alias) = alias {
2357 write!(f, " {alias}")?;
2358 }
2359 Ok(())
2360 }
2361 TableFactor::OpenJsonTable {
2362 json_expr,
2363 json_path,
2364 columns,
2365 alias,
2366 } => {
2367 write!(f, "OPENJSON({json_expr}")?;
2368 if let Some(json_path) = json_path {
2369 write!(f, ", {json_path}")?;
2370 }
2371 write!(f, ")")?;
2372 if !columns.is_empty() {
2373 write!(f, " WITH ({})", display_comma_separated(columns))?;
2374 }
2375 if let Some(alias) = alias {
2376 write!(f, " {alias}")?;
2377 }
2378 Ok(())
2379 }
2380 TableFactor::NestedJoin {
2381 table_with_joins,
2382 alias,
2383 } => {
2384 write!(f, "({table_with_joins})")?;
2385 if let Some(alias) = alias {
2386 write!(f, " {alias}")?;
2387 }
2388 Ok(())
2389 }
2390 TableFactor::Pivot {
2391 table,
2392 aggregate_functions,
2393 value_column,
2394 value_source,
2395 default_on_null,
2396 alias,
2397 } => {
2398 write!(
2399 f,
2400 "{table} PIVOT({} FOR ",
2401 display_comma_separated(aggregate_functions),
2402 )?;
2403 if value_column.len() == 1 {
2404 write!(f, "{}", value_column[0])?;
2405 } else {
2406 write!(f, "({})", display_comma_separated(value_column))?;
2407 }
2408 write!(f, " IN ({value_source})")?;
2409 if let Some(expr) = default_on_null {
2410 write!(f, " DEFAULT ON NULL ({expr})")?;
2411 }
2412 write!(f, ")")?;
2413 if let Some(alias) = alias {
2414 write!(f, " {alias}")?;
2415 }
2416 Ok(())
2417 }
2418 TableFactor::Unpivot {
2419 table,
2420 null_inclusion,
2421 value,
2422 name,
2423 columns,
2424 alias,
2425 } => {
2426 write!(f, "{table} UNPIVOT")?;
2427 if let Some(null_inclusion) = null_inclusion {
2428 write!(f, " {null_inclusion} ")?;
2429 }
2430 write!(
2431 f,
2432 "({} FOR {} IN ({}))",
2433 value,
2434 name,
2435 display_comma_separated(columns)
2436 )?;
2437 if let Some(alias) = alias {
2438 write!(f, " {alias}")?;
2439 }
2440 Ok(())
2441 }
2442 TableFactor::UnpivotExpr {
2443 expression,
2444 value_alias,
2445 attribute_alias,
2446 } => {
2447 write!(f, "UNPIVOT {expression} AS {value_alias}")?;
2448 if let Some(attribute_alias) = attribute_alias {
2449 write!(f, " AT {attribute_alias}")?;
2450 }
2451 Ok(())
2452 }
2453 TableFactor::MatchRecognize {
2454 table,
2455 partition_by,
2456 order_by,
2457 measures,
2458 rows_per_match,
2459 after_match_skip,
2460 pattern,
2461 symbols,
2462 alias,
2463 } => {
2464 write!(f, "{table} MATCH_RECOGNIZE(")?;
2465 if !partition_by.is_empty() {
2466 write!(f, "PARTITION BY {} ", display_comma_separated(partition_by))?;
2467 }
2468 if !order_by.is_empty() {
2469 write!(f, "ORDER BY {} ", display_comma_separated(order_by))?;
2470 }
2471 if !measures.is_empty() {
2472 write!(f, "MEASURES {} ", display_comma_separated(measures))?;
2473 }
2474 if let Some(rows_per_match) = rows_per_match {
2475 write!(f, "{rows_per_match} ")?;
2476 }
2477 if let Some(after_match_skip) = after_match_skip {
2478 write!(f, "{after_match_skip} ")?;
2479 }
2480 write!(f, "PATTERN ({pattern}) ")?;
2481 write!(f, "DEFINE {})", display_comma_separated(symbols))?;
2482 if let Some(alias) = alias {
2483 write!(f, " {alias}")?;
2484 }
2485 Ok(())
2486 }
2487 TableFactor::XmlTable {
2488 row_expression,
2489 passing,
2490 columns,
2491 alias,
2492 namespaces,
2493 } => {
2494 write!(f, "XMLTABLE(")?;
2495 if !namespaces.is_empty() {
2496 write!(
2497 f,
2498 "XMLNAMESPACES({}), ",
2499 display_comma_separated(namespaces)
2500 )?;
2501 }
2502 write!(
2503 f,
2504 "{row_expression}{passing} COLUMNS {columns})",
2505 columns = display_comma_separated(columns)
2506 )?;
2507 if let Some(alias) = alias {
2508 write!(f, " {alias}")?;
2509 }
2510 Ok(())
2511 }
2512 TableFactor::SemanticView {
2513 name,
2514 dimensions,
2515 metrics,
2516 facts,
2517 where_clause,
2518 alias,
2519 } => {
2520 write!(f, "SEMANTIC_VIEW({name}")?;
2521
2522 if !dimensions.is_empty() {
2523 write!(f, " DIMENSIONS {}", display_comma_separated(dimensions))?;
2524 }
2525
2526 if !metrics.is_empty() {
2527 write!(f, " METRICS {}", display_comma_separated(metrics))?;
2528 }
2529
2530 if !facts.is_empty() {
2531 write!(f, " FACTS {}", display_comma_separated(facts))?;
2532 }
2533
2534 if let Some(where_clause) = where_clause {
2535 write!(f, " WHERE {where_clause}")?;
2536 }
2537
2538 write!(f, ")")?;
2539
2540 if let Some(alias) = alias {
2541 write!(f, " {alias}")?;
2542 }
2543
2544 Ok(())
2545 }
2546 }
2547 }
2548}
2549
2550#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2551#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2552#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2553pub struct TableAlias {
2555 pub explicit: bool,
2559 pub name: Ident,
2561 pub columns: Vec<TableAliasColumnDef>,
2563 pub at: Option<Ident>,
2569}
2570
2571impl fmt::Display for TableAlias {
2572 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2573 write!(f, "{}{}", if self.explicit { "AS " } else { "" }, self.name)?;
2574 if !self.columns.is_empty() {
2575 write!(f, " ({})", display_comma_separated(&self.columns))?;
2576 }
2577 if let Some(at) = &self.at {
2578 write!(f, " AT {at}")?;
2579 }
2580 Ok(())
2581 }
2582}
2583
2584#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2590#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2591#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2592pub struct TableAliasColumnDef {
2593 pub name: Ident,
2595 pub data_type: Option<DataType>,
2597}
2598
2599impl TableAliasColumnDef {
2600 pub fn from_name<S: Into<String>>(name: S) -> Self {
2602 TableAliasColumnDef {
2603 name: Ident::new(name),
2604 data_type: None,
2605 }
2606 }
2607}
2608
2609impl fmt::Display for TableAliasColumnDef {
2610 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2611 write!(f, "{}", self.name)?;
2612 if let Some(ref data_type) = self.data_type {
2613 write!(f, " {data_type}")?;
2614 }
2615 Ok(())
2616 }
2617}
2618
2619#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2620#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2621#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2622pub enum TableVersion {
2624 ForSystemTimeAsOf(Expr),
2627 TimestampAsOf(Expr),
2631 VersionAsOf(Expr),
2635 Function(Expr),
2638 Changes {
2648 changes: Expr,
2650 at: Expr,
2652 end: Option<Expr>,
2654 },
2655}
2656
2657impl Display for TableVersion {
2658 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2659 match self {
2660 TableVersion::ForSystemTimeAsOf(e) => write!(f, "FOR SYSTEM_TIME AS OF {e}")?,
2661 TableVersion::TimestampAsOf(e) => write!(f, "TIMESTAMP AS OF {e}")?,
2662 TableVersion::VersionAsOf(e) => write!(f, "VERSION AS OF {e}")?,
2663 TableVersion::Function(func) => write!(f, "{func}")?,
2664 TableVersion::Changes { changes, at, end } => {
2665 write!(f, "{changes} {at}")?;
2666 if let Some(end) = end {
2667 write!(f, " {end}")?;
2668 }
2669 }
2670 }
2671 Ok(())
2672 }
2673}
2674
2675#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2676#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2677#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2678pub struct Join {
2680 pub relation: TableFactor,
2682 pub global: bool,
2685 pub join_operator: JoinOperator,
2687}
2688
2689impl fmt::Display for Join {
2690 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2691 fn prefix(constraint: &JoinConstraint) -> &'static str {
2692 match constraint {
2693 JoinConstraint::Natural => "NATURAL ",
2694 _ => "",
2695 }
2696 }
2697 fn suffix(constraint: &'_ JoinConstraint) -> impl fmt::Display + '_ {
2698 struct Suffix<'a>(&'a JoinConstraint);
2699 impl fmt::Display for Suffix<'_> {
2700 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2701 match self.0 {
2702 JoinConstraint::On(expr) => write!(f, " ON {expr}"),
2703 JoinConstraint::Using(attrs) => {
2704 write!(f, " USING({})", display_comma_separated(attrs))
2705 }
2706 _ => Ok(()),
2707 }
2708 }
2709 }
2710 Suffix(constraint)
2711 }
2712 if self.global {
2713 write!(f, "GLOBAL ")?;
2714 }
2715
2716 match &self.join_operator {
2717 JoinOperator::Join(constraint) => f.write_fmt(format_args!(
2718 "{}JOIN {}{}",
2719 prefix(constraint),
2720 self.relation,
2721 suffix(constraint)
2722 )),
2723 JoinOperator::Inner(constraint) => f.write_fmt(format_args!(
2724 "{}INNER JOIN {}{}",
2725 prefix(constraint),
2726 self.relation,
2727 suffix(constraint)
2728 )),
2729 JoinOperator::Left(constraint) => f.write_fmt(format_args!(
2730 "{}LEFT JOIN {}{}",
2731 prefix(constraint),
2732 self.relation,
2733 suffix(constraint)
2734 )),
2735 JoinOperator::LeftOuter(constraint) => f.write_fmt(format_args!(
2736 "{}LEFT OUTER JOIN {}{}",
2737 prefix(constraint),
2738 self.relation,
2739 suffix(constraint)
2740 )),
2741 JoinOperator::Right(constraint) => f.write_fmt(format_args!(
2742 "{}RIGHT JOIN {}{}",
2743 prefix(constraint),
2744 self.relation,
2745 suffix(constraint)
2746 )),
2747 JoinOperator::RightOuter(constraint) => f.write_fmt(format_args!(
2748 "{}RIGHT OUTER JOIN {}{}",
2749 prefix(constraint),
2750 self.relation,
2751 suffix(constraint)
2752 )),
2753 JoinOperator::FullOuter(constraint) => f.write_fmt(format_args!(
2754 "{}FULL JOIN {}{}",
2755 prefix(constraint),
2756 self.relation,
2757 suffix(constraint)
2758 )),
2759 JoinOperator::CrossJoin(constraint) => f.write_fmt(format_args!(
2760 "CROSS JOIN {}{}",
2761 self.relation,
2762 suffix(constraint)
2763 )),
2764 JoinOperator::Semi(constraint) => f.write_fmt(format_args!(
2765 "{}SEMI JOIN {}{}",
2766 prefix(constraint),
2767 self.relation,
2768 suffix(constraint)
2769 )),
2770 JoinOperator::LeftSemi(constraint) => f.write_fmt(format_args!(
2771 "{}LEFT SEMI JOIN {}{}",
2772 prefix(constraint),
2773 self.relation,
2774 suffix(constraint)
2775 )),
2776 JoinOperator::RightSemi(constraint) => f.write_fmt(format_args!(
2777 "{}RIGHT SEMI JOIN {}{}",
2778 prefix(constraint),
2779 self.relation,
2780 suffix(constraint)
2781 )),
2782 JoinOperator::Anti(constraint) => f.write_fmt(format_args!(
2783 "{}ANTI JOIN {}{}",
2784 prefix(constraint),
2785 self.relation,
2786 suffix(constraint)
2787 )),
2788 JoinOperator::LeftAnti(constraint) => f.write_fmt(format_args!(
2789 "{}LEFT ANTI JOIN {}{}",
2790 prefix(constraint),
2791 self.relation,
2792 suffix(constraint)
2793 )),
2794 JoinOperator::RightAnti(constraint) => f.write_fmt(format_args!(
2795 "{}RIGHT ANTI JOIN {}{}",
2796 prefix(constraint),
2797 self.relation,
2798 suffix(constraint)
2799 )),
2800 JoinOperator::CrossApply => f.write_fmt(format_args!("CROSS APPLY {}", self.relation)),
2801 JoinOperator::OuterApply => f.write_fmt(format_args!("OUTER APPLY {}", self.relation)),
2802 JoinOperator::AsOf {
2803 match_condition,
2804 constraint,
2805 } => f.write_fmt(format_args!(
2806 "ASOF JOIN {} MATCH_CONDITION ({match_condition}){}",
2807 self.relation,
2808 suffix(constraint)
2809 )),
2810 JoinOperator::StraightJoin(constraint) => f.write_fmt(format_args!(
2811 "STRAIGHT_JOIN {}{}",
2812 self.relation,
2813 suffix(constraint)
2814 )),
2815 JoinOperator::ArrayJoin => f.write_fmt(format_args!("ARRAY JOIN {}", self.relation)),
2816 JoinOperator::LeftArrayJoin => {
2817 f.write_fmt(format_args!("LEFT ARRAY JOIN {}", self.relation))
2818 }
2819 JoinOperator::InnerArrayJoin => {
2820 f.write_fmt(format_args!("INNER ARRAY JOIN {}", self.relation))
2821 }
2822 }
2823 }
2824}
2825
2826#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2827#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2828#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2829pub enum JoinOperator {
2831 Join(JoinConstraint),
2833 Inner(JoinConstraint),
2835 Left(JoinConstraint),
2837 LeftOuter(JoinConstraint),
2839 Right(JoinConstraint),
2841 RightOuter(JoinConstraint),
2843 FullOuter(JoinConstraint),
2845 CrossJoin(JoinConstraint),
2847 Semi(JoinConstraint),
2849 LeftSemi(JoinConstraint),
2851 RightSemi(JoinConstraint),
2853 Anti(JoinConstraint),
2855 LeftAnti(JoinConstraint),
2857 RightAnti(JoinConstraint),
2859 CrossApply,
2861 OuterApply,
2863 AsOf {
2867 match_condition: Expr,
2869 constraint: JoinConstraint,
2871 },
2872 StraightJoin(JoinConstraint),
2876 ArrayJoin,
2880 LeftArrayJoin,
2882 InnerArrayJoin,
2884}
2885
2886#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2887#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2888#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2889pub enum JoinConstraint {
2891 On(Expr),
2893 Using(Vec<ObjectName>),
2895 Natural,
2897 None,
2899}
2900
2901#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2902#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2903#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2904pub enum OrderByKind {
2906 All(OrderByOptions),
2911
2912 Expressions(Vec<OrderByExpr>),
2914}
2915
2916#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2917#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2918#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2919#[cfg_attr(feature = "visitor", visit(with = "visit_order_by"))]
2920pub struct OrderBy {
2922 pub kind: OrderByKind,
2924
2925 pub interpolate: Option<Interpolate>,
2927}
2928
2929impl fmt::Display for OrderBy {
2930 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2931 write!(f, "ORDER BY")?;
2932 match &self.kind {
2933 OrderByKind::Expressions(exprs) => {
2934 write!(f, " {}", display_comma_separated(exprs))?;
2935 }
2936 OrderByKind::All(all) => {
2937 write!(f, " ALL{all}")?;
2938 }
2939 }
2940
2941 if let Some(ref interpolate) = self.interpolate {
2942 match &interpolate.exprs {
2943 Some(exprs) => write!(f, " INTERPOLATE ({})", display_comma_separated(exprs))?,
2944 None => write!(f, " INTERPOLATE")?,
2945 }
2946 }
2947
2948 Ok(())
2949 }
2950}
2951
2952#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2954#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2955#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2956#[cfg_attr(feature = "visitor", visit(with = "visit_order_by_expr"))]
2957pub struct OrderByExpr {
2958 pub expr: Expr,
2960 pub options: OrderByOptions,
2962 pub with_fill: Option<WithFill>,
2964}
2965
2966impl From<Ident> for OrderByExpr {
2967 fn from(ident: Ident) -> Self {
2968 OrderByExpr {
2969 expr: Expr::Identifier(ident),
2970 options: OrderByOptions::default(),
2971 with_fill: None,
2972 }
2973 }
2974}
2975
2976impl fmt::Display for OrderByExpr {
2977 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2978 write!(f, "{}", self.expr)?;
2979 write!(f, "{}", self.options)?;
2980 if let Some(ref with_fill) = self.with_fill {
2981 write!(f, " {with_fill}")?
2982 }
2983 Ok(())
2984 }
2985}
2986
2987#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2992#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2993#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2994pub struct WithFill {
2996 pub from: Option<Expr>,
2998 pub to: Option<Expr>,
3000 pub step: Option<Expr>,
3002}
3003
3004impl fmt::Display for WithFill {
3005 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3006 write!(f, "WITH FILL")?;
3007 if let Some(ref from) = self.from {
3008 write!(f, " FROM {from}")?;
3009 }
3010 if let Some(ref to) = self.to {
3011 write!(f, " TO {to}")?;
3012 }
3013 if let Some(ref step) = self.step {
3014 write!(f, " STEP {step}")?;
3015 }
3016 Ok(())
3017 }
3018}
3019
3020#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3025#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3026#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3027pub struct InterpolateExpr {
3029 pub column: Ident,
3031 pub expr: Option<Expr>,
3033}
3034
3035#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3036#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3037#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3038pub struct Interpolate {
3040 pub exprs: Option<Vec<InterpolateExpr>>,
3042}
3043
3044impl fmt::Display for InterpolateExpr {
3045 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3046 write!(f, "{}", self.column)?;
3047 if let Some(ref expr) = self.expr {
3048 write!(f, " AS {expr}")?;
3049 }
3050 Ok(())
3051 }
3052}
3053
3054#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3059#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3060#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3061pub enum OrderBySort {
3062 Asc,
3064 Desc,
3066 Using(ObjectName),
3070}
3071
3072#[derive(Default, Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3073#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3074#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3075pub struct OrderByOptions {
3077 pub sort: Option<OrderBySort>,
3079 pub nulls_first: Option<bool>,
3081}
3082
3083impl fmt::Display for OrderByOptions {
3084 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3085 match &self.sort {
3086 Some(OrderBySort::Asc) => write!(f, " ASC")?,
3087 Some(OrderBySort::Desc) => write!(f, " DESC")?,
3088 Some(OrderBySort::Using(op)) => {
3089 if op.0.len() > 1 {
3090 write!(f, " USING OPERATOR({op})")?;
3091 } else {
3092 write!(f, " USING {op}")?;
3093 }
3094 }
3095 None => (),
3096 }
3097 match self.nulls_first {
3098 Some(true) => write!(f, " NULLS FIRST")?,
3099 Some(false) => write!(f, " NULLS LAST")?,
3100 None => (),
3101 }
3102 Ok(())
3103 }
3104}
3105
3106#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3107#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3108#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3109pub enum LimitClause {
3111 LimitOffset {
3115 limit: Option<Expr>,
3117 offset: Option<Offset>,
3119 limit_by: Vec<Expr>,
3121 },
3122 OffsetCommaLimit {
3124 offset: Expr,
3126 limit: Expr,
3128 },
3129}
3130
3131impl fmt::Display for LimitClause {
3132 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3133 match self {
3134 LimitClause::LimitOffset {
3135 limit,
3136 limit_by,
3137 offset,
3138 } => {
3139 if let Some(ref limit) = limit {
3140 write!(f, " LIMIT {limit}")?;
3141 }
3142 if let Some(ref offset) = offset {
3143 write!(f, " {offset}")?;
3144 }
3145 if !limit_by.is_empty() {
3146 debug_assert!(limit.is_some());
3147 write!(f, " BY {}", display_separated(limit_by, ", "))?;
3148 }
3149 Ok(())
3150 }
3151 LimitClause::OffsetCommaLimit { offset, limit } => {
3152 write!(f, " LIMIT {offset}, {limit}")
3153 }
3154 }
3155 }
3156}
3157
3158#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3159#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3160#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3161pub struct Offset {
3163 pub value: Expr,
3165 pub rows: OffsetRows,
3167}
3168
3169impl fmt::Display for Offset {
3170 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3171 write!(f, "OFFSET {}{}", self.value, self.rows)
3172 }
3173}
3174
3175#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3177#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3178#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3179pub enum OffsetRows {
3180 None,
3182 Row,
3184 Rows,
3186}
3187
3188impl fmt::Display for OffsetRows {
3189 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3190 match self {
3191 OffsetRows::None => Ok(()),
3192 OffsetRows::Row => write!(f, " ROW"),
3193 OffsetRows::Rows => write!(f, " ROWS"),
3194 }
3195 }
3196}
3197
3198#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3210#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3211#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3212pub enum PipeOperator {
3213 Limit {
3219 expr: Expr,
3221 offset: Option<Expr>,
3223 },
3224 Where {
3230 expr: Expr,
3232 },
3233 OrderBy {
3235 exprs: Vec<OrderByExpr>,
3237 },
3238 Select {
3244 exprs: Vec<SelectItem>,
3246 },
3247 Extend {
3253 exprs: Vec<SelectItem>,
3255 },
3256 Set {
3262 assignments: Vec<Assignment>,
3264 },
3265 Drop {
3271 columns: Vec<Ident>,
3273 },
3274 As {
3280 alias: Ident,
3282 },
3283 Aggregate {
3295 full_table_exprs: Vec<ExprWithAliasAndOrderBy>,
3297 group_by_expr: Vec<ExprWithAliasAndOrderBy>,
3299 },
3300 TableSample {
3304 sample: Box<TableSample>,
3306 },
3307 Rename {
3313 mappings: Vec<IdentWithAlias>,
3315 },
3316 Union {
3322 set_quantifier: SetQuantifier,
3324 queries: Vec<Query>,
3326 },
3327 Intersect {
3333 set_quantifier: SetQuantifier,
3335 queries: Vec<Query>,
3337 },
3338 Except {
3344 set_quantifier: SetQuantifier,
3346 queries: Vec<Query>,
3348 },
3349 Call {
3355 function: Function,
3357 alias: Option<Ident>,
3359 },
3360 Pivot {
3366 aggregate_functions: Vec<ExprWithAlias>,
3368 value_column: Vec<Ident>,
3370 value_source: PivotValueSource,
3372 alias: Option<Ident>,
3374 },
3375 Unpivot {
3384 value_column: Ident,
3386 name_column: Ident,
3388 unpivot_columns: Vec<Ident>,
3390 alias: Option<Ident>,
3392 },
3393 Join(Join),
3399}
3400
3401impl fmt::Display for PipeOperator {
3402 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3403 match self {
3404 PipeOperator::Select { exprs } => {
3405 write!(f, "SELECT {}", display_comma_separated(exprs.as_slice()))
3406 }
3407 PipeOperator::Extend { exprs } => {
3408 write!(f, "EXTEND {}", display_comma_separated(exprs.as_slice()))
3409 }
3410 PipeOperator::Set { assignments } => {
3411 write!(f, "SET {}", display_comma_separated(assignments.as_slice()))
3412 }
3413 PipeOperator::Drop { columns } => {
3414 write!(f, "DROP {}", display_comma_separated(columns.as_slice()))
3415 }
3416 PipeOperator::As { alias } => {
3417 write!(f, "AS {alias}")
3418 }
3419 PipeOperator::Limit { expr, offset } => {
3420 write!(f, "LIMIT {expr}")?;
3421 if let Some(offset) = offset {
3422 write!(f, " OFFSET {offset}")?;
3423 }
3424 Ok(())
3425 }
3426 PipeOperator::Aggregate {
3427 full_table_exprs,
3428 group_by_expr,
3429 } => {
3430 write!(f, "AGGREGATE")?;
3431 if !full_table_exprs.is_empty() {
3432 write!(
3433 f,
3434 " {}",
3435 display_comma_separated(full_table_exprs.as_slice())
3436 )?;
3437 }
3438 if !group_by_expr.is_empty() {
3439 write!(f, " GROUP BY {}", display_comma_separated(group_by_expr))?;
3440 }
3441 Ok(())
3442 }
3443
3444 PipeOperator::Where { expr } => {
3445 write!(f, "WHERE {expr}")
3446 }
3447 PipeOperator::OrderBy { exprs } => {
3448 write!(f, "ORDER BY {}", display_comma_separated(exprs.as_slice()))
3449 }
3450
3451 PipeOperator::TableSample { sample } => {
3452 write!(f, "{sample}")
3453 }
3454 PipeOperator::Rename { mappings } => {
3455 write!(f, "RENAME {}", display_comma_separated(mappings))
3456 }
3457 PipeOperator::Union {
3458 set_quantifier,
3459 queries,
3460 } => Self::fmt_set_operation(f, "UNION", set_quantifier, queries),
3461 PipeOperator::Intersect {
3462 set_quantifier,
3463 queries,
3464 } => Self::fmt_set_operation(f, "INTERSECT", set_quantifier, queries),
3465 PipeOperator::Except {
3466 set_quantifier,
3467 queries,
3468 } => Self::fmt_set_operation(f, "EXCEPT", set_quantifier, queries),
3469 PipeOperator::Call { function, alias } => {
3470 write!(f, "CALL {function}")?;
3471 Self::fmt_optional_alias(f, alias)
3472 }
3473 PipeOperator::Pivot {
3474 aggregate_functions,
3475 value_column,
3476 value_source,
3477 alias,
3478 } => {
3479 write!(
3480 f,
3481 "PIVOT({} FOR {} IN ({}))",
3482 display_comma_separated(aggregate_functions),
3483 Expr::CompoundIdentifier(value_column.to_vec()),
3484 value_source
3485 )?;
3486 Self::fmt_optional_alias(f, alias)
3487 }
3488 PipeOperator::Unpivot {
3489 value_column,
3490 name_column,
3491 unpivot_columns,
3492 alias,
3493 } => {
3494 write!(
3495 f,
3496 "UNPIVOT({} FOR {} IN ({}))",
3497 value_column,
3498 name_column,
3499 display_comma_separated(unpivot_columns)
3500 )?;
3501 Self::fmt_optional_alias(f, alias)
3502 }
3503 PipeOperator::Join(join) => write!(f, "{join}"),
3504 }
3505 }
3506}
3507
3508impl PipeOperator {
3509 fn fmt_optional_alias(f: &mut fmt::Formatter<'_>, alias: &Option<Ident>) -> fmt::Result {
3511 if let Some(alias) = alias {
3512 write!(f, " AS {alias}")?;
3513 }
3514 Ok(())
3515 }
3516
3517 fn fmt_set_operation(
3519 f: &mut fmt::Formatter<'_>,
3520 operation: &str,
3521 set_quantifier: &SetQuantifier,
3522 queries: &[Query],
3523 ) -> fmt::Result {
3524 write!(f, "{operation}")?;
3525 match set_quantifier {
3526 SetQuantifier::None => {}
3527 _ => {
3528 write!(f, " {set_quantifier}")?;
3529 }
3530 }
3531 write!(f, " ")?;
3532 let parenthesized_queries: Vec<String> =
3533 queries.iter().map(|query| format!("({query})")).collect();
3534 write!(f, "{}", display_comma_separated(&parenthesized_queries))
3535 }
3536}
3537
3538#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3539#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3540#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3541pub struct Fetch {
3543 pub with_ties: bool,
3545 pub percent: bool,
3547 pub quantity: Option<Expr>,
3549}
3550
3551impl fmt::Display for Fetch {
3552 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3553 let extension = if self.with_ties { "WITH TIES" } else { "ONLY" };
3554 if let Some(ref quantity) = self.quantity {
3555 let percent = if self.percent { " PERCENT" } else { "" };
3556 write!(f, "FETCH FIRST {quantity}{percent} ROWS {extension}")
3557 } else {
3558 write!(f, "FETCH FIRST ROWS {extension}")
3559 }
3560 }
3561}
3562
3563#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3564#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3565#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3566pub struct LockClause {
3568 pub lock_type: LockType,
3570 pub of: Vec<ObjectName>,
3572 pub nonblock: Option<NonBlock>,
3574}
3575
3576impl fmt::Display for LockClause {
3577 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3578 write!(f, "FOR {}", self.lock_type)?;
3579 if !self.of.is_empty() {
3580 write!(f, " OF {}", display_comma_separated(&self.of))?;
3581 }
3582 if let Some(ref nb) = self.nonblock {
3583 write!(f, " {nb}")?;
3584 }
3585 Ok(())
3586 }
3587}
3588
3589#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3590#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3591#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3592pub enum LockType {
3594 KeyShare,
3596 Share,
3598 NoKeyUpdate,
3600 Update,
3602}
3603
3604impl fmt::Display for LockType {
3605 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3606 let select_lock = match self {
3607 LockType::KeyShare => "KEY SHARE",
3608 LockType::Share => "SHARE",
3609 LockType::NoKeyUpdate => "NO KEY UPDATE",
3610 LockType::Update => "UPDATE",
3611 };
3612 write!(f, "{select_lock}")
3613 }
3614}
3615
3616#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3617#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3618#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3619pub enum NonBlock {
3621 Nowait,
3623 SkipLocked,
3625}
3626
3627impl fmt::Display for NonBlock {
3628 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3629 let nonblock = match self {
3630 NonBlock::Nowait => "NOWAIT",
3631 NonBlock::SkipLocked => "SKIP LOCKED",
3632 };
3633 write!(f, "{nonblock}")
3634 }
3635}
3636
3637#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3638#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3639#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3640pub enum Distinct {
3642 All,
3647
3648 Distinct,
3650
3651 On(Vec<Expr>),
3653}
3654
3655impl fmt::Display for Distinct {
3656 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3657 match self {
3658 Distinct::All => write!(f, "ALL"),
3659 Distinct::Distinct => write!(f, "DISTINCT"),
3660 Distinct::On(col_names) => {
3661 let col_names = display_comma_separated(col_names);
3662 write!(f, "DISTINCT ON ({col_names})")
3663 }
3664 }
3665 }
3666}
3667
3668#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3669#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3670#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3671pub struct Top {
3673 pub with_ties: bool,
3676 pub percent: bool,
3678 pub quantity: Option<TopQuantity>,
3680}
3681
3682#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3683#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3684#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3685pub enum TopQuantity {
3687 Expr(Expr),
3689 Constant(u64),
3691}
3692
3693impl fmt::Display for Top {
3694 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3695 let extension = if self.with_ties { " WITH TIES" } else { "" };
3696 if let Some(ref quantity) = self.quantity {
3697 let percent = if self.percent { " PERCENT" } else { "" };
3698 match quantity {
3699 TopQuantity::Expr(quantity) => write!(f, "TOP ({quantity}){percent}{extension}"),
3700 TopQuantity::Constant(quantity) => {
3701 write!(f, "TOP {quantity}{percent}{extension}")
3702 }
3703 }
3704 } else {
3705 write!(f, "TOP{extension}")
3706 }
3707 }
3708}
3709
3710#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3711#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3712#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3713pub struct Values {
3715 pub explicit_row: bool,
3718 pub value_keyword: bool,
3721 pub rows: Vec<Parens<Vec<Expr>>>,
3723}
3724
3725impl fmt::Display for Values {
3726 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3727 match self.value_keyword {
3728 true => f.write_str("VALUE")?,
3729 false => f.write_str("VALUES")?,
3730 };
3731 let prefix = if self.explicit_row { "ROW" } else { "" };
3732 let mut delim = "";
3733 for row in &self.rows {
3734 f.write_str(delim)?;
3735 delim = ",";
3736 SpaceOrNewline.fmt(f)?;
3737 Indent(format_args!("{prefix}({})", display_comma_separated(row))).fmt(f)?;
3738 }
3739 Ok(())
3740 }
3741}
3742
3743#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3744#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3745#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3746pub struct SelectInto {
3748 pub temporary: bool,
3750 pub unlogged: bool,
3752 pub table: bool,
3754 pub targets: Vec<Expr>,
3759}
3760
3761impl fmt::Display for SelectInto {
3762 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3763 let temporary = if self.temporary { " TEMPORARY" } else { "" };
3764 let unlogged = if self.unlogged { " UNLOGGED" } else { "" };
3765 let table = if self.table { " TABLE" } else { "" };
3766
3767 write!(
3768 f,
3769 "INTO{}{}{} {}",
3770 temporary,
3771 unlogged,
3772 table,
3773 display_comma_separated(&self.targets)
3774 )
3775 }
3776}
3777
3778#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3783#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3784#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3785pub enum GroupByWithModifier {
3787 Rollup,
3789 Cube,
3791 Totals,
3793 GroupingSets(Expr),
3797}
3798
3799impl fmt::Display for GroupByWithModifier {
3800 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3801 match self {
3802 GroupByWithModifier::Rollup => write!(f, "WITH ROLLUP"),
3803 GroupByWithModifier::Cube => write!(f, "WITH CUBE"),
3804 GroupByWithModifier::Totals => write!(f, "WITH TOTALS"),
3805 GroupByWithModifier::GroupingSets(expr) => {
3806 write!(f, "{expr}")
3807 }
3808 }
3809 }
3810}
3811
3812#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3813#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3814#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3815#[cfg_attr(feature = "visitor", visit(with = "visit_group_by"))]
3816pub enum GroupByExpr {
3819 All(Vec<GroupByWithModifier>),
3829 Expressions(Vec<Expr>, Vec<GroupByWithModifier>),
3831}
3832
3833impl fmt::Display for GroupByExpr {
3834 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3835 match self {
3836 GroupByExpr::All(modifiers) => {
3837 write!(f, "GROUP BY ALL")?;
3838 if !modifiers.is_empty() {
3839 write!(f, " {}", display_separated(modifiers, " "))?;
3840 }
3841 Ok(())
3842 }
3843 GroupByExpr::Expressions(col_names, modifiers) => {
3844 f.write_str("GROUP BY")?;
3845 SpaceOrNewline.fmt(f)?;
3846 Indent(display_comma_separated(col_names)).fmt(f)?;
3847 if !modifiers.is_empty() {
3848 write!(f, " {}", display_separated(modifiers, " "))?;
3849 }
3850 Ok(())
3851 }
3852 }
3853 }
3854}
3855
3856#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3860#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3861#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3862pub enum FormatClause {
3863 Identifier(Ident),
3865 Null,
3867}
3868
3869impl fmt::Display for FormatClause {
3870 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3871 match self {
3872 FormatClause::Identifier(ident) => write!(f, "FORMAT {ident}"),
3873 FormatClause::Null => write!(f, "FORMAT NULL"),
3874 }
3875 }
3876}
3877
3878#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3882#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3883#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3884pub struct InputFormatClause {
3885 pub ident: Ident,
3887 pub values: Vec<Expr>,
3889}
3890
3891impl fmt::Display for InputFormatClause {
3892 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3893 write!(f, "FORMAT {}", self.ident)?;
3894
3895 if !self.values.is_empty() {
3896 write!(f, " {}", display_comma_separated(self.values.as_slice()))?;
3897 }
3898
3899 Ok(())
3900 }
3901}
3902
3903#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3905#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3906#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3907pub enum ForClause {
3908 Browse,
3910 Json {
3912 for_json: ForJson,
3914 root: Option<String>,
3916 include_null_values: bool,
3918 without_array_wrapper: bool,
3920 },
3921 Xml {
3923 for_xml: ForXml,
3925 elements: bool,
3927 binary_base64: bool,
3929 root: Option<String>,
3931 r#type: bool,
3933 },
3934}
3935
3936impl fmt::Display for ForClause {
3937 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3938 match self {
3939 ForClause::Browse => write!(f, "FOR BROWSE"),
3940 ForClause::Json {
3941 for_json,
3942 root,
3943 include_null_values,
3944 without_array_wrapper,
3945 } => {
3946 write!(f, "FOR JSON ")?;
3947 write!(f, "{for_json}")?;
3948 if let Some(root) = root {
3949 write!(f, ", ROOT('{root}')")?;
3950 }
3951 if *include_null_values {
3952 write!(f, ", INCLUDE_NULL_VALUES")?;
3953 }
3954 if *without_array_wrapper {
3955 write!(f, ", WITHOUT_ARRAY_WRAPPER")?;
3956 }
3957 Ok(())
3958 }
3959 ForClause::Xml {
3960 for_xml,
3961 elements,
3962 binary_base64,
3963 root,
3964 r#type,
3965 } => {
3966 write!(f, "FOR XML ")?;
3967 write!(f, "{for_xml}")?;
3968 if *binary_base64 {
3969 write!(f, ", BINARY BASE64")?;
3970 }
3971 if *r#type {
3972 write!(f, ", TYPE")?;
3973 }
3974 if let Some(root) = root {
3975 write!(f, ", ROOT('{root}')")?;
3976 }
3977 if *elements {
3978 write!(f, ", ELEMENTS")?;
3979 }
3980 Ok(())
3981 }
3982 }
3983 }
3984}
3985
3986#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3987#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3988#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3989pub enum ForXml {
3991 Raw(Option<String>),
3993 Auto,
3995 Explicit,
3997 Path(Option<String>),
3999}
4000
4001impl fmt::Display for ForXml {
4002 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4003 match self {
4004 ForXml::Raw(root) => {
4005 write!(f, "RAW")?;
4006 if let Some(root) = root {
4007 write!(f, "('{root}')")?;
4008 }
4009 Ok(())
4010 }
4011 ForXml::Auto => write!(f, "AUTO"),
4012 ForXml::Explicit => write!(f, "EXPLICIT"),
4013 ForXml::Path(root) => {
4014 write!(f, "PATH")?;
4015 if let Some(root) = root {
4016 write!(f, "('{root}')")?;
4017 }
4018 Ok(())
4019 }
4020 }
4021 }
4022}
4023
4024#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4025#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4026#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4027pub enum ForJson {
4029 Auto,
4031 Path,
4033}
4034
4035impl fmt::Display for ForJson {
4036 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4037 match self {
4038 ForJson::Auto => write!(f, "AUTO"),
4039 ForJson::Path => write!(f, "PATH"),
4040 }
4041 }
4042}
4043
4044#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4065#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4066#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4067pub enum JsonTableColumn {
4068 Named(JsonTableNamedColumn),
4070 ForOrdinality(Ident),
4072 Nested(JsonTableNestedColumn),
4074}
4075
4076impl fmt::Display for JsonTableColumn {
4077 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4078 match self {
4079 JsonTableColumn::Named(json_table_named_column) => {
4080 write!(f, "{json_table_named_column}")
4081 }
4082 JsonTableColumn::ForOrdinality(ident) => write!(f, "{ident} FOR ORDINALITY"),
4083 JsonTableColumn::Nested(json_table_nested_column) => {
4084 write!(f, "{json_table_nested_column}")
4085 }
4086 }
4087 }
4088}
4089
4090#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4094#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4095#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4096pub struct JsonTableNestedColumn {
4098 pub path: ValueWithSpan,
4100 pub columns: Vec<JsonTableColumn>,
4102}
4103
4104impl fmt::Display for JsonTableNestedColumn {
4105 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4106 write!(
4107 f,
4108 "NESTED PATH {} COLUMNS ({})",
4109 self.path,
4110 display_comma_separated(&self.columns)
4111 )
4112 }
4113}
4114
4115#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4123#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4124#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4125pub struct JsonTableNamedColumn {
4126 pub name: Ident,
4128 pub r#type: DataType,
4130 pub path: ValueWithSpan,
4132 pub exists: bool,
4134 pub on_empty: Option<JsonTableColumnErrorHandling>,
4136 pub on_error: Option<JsonTableColumnErrorHandling>,
4138}
4139
4140impl fmt::Display for JsonTableNamedColumn {
4141 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4142 write!(
4143 f,
4144 "{} {}{} PATH {}",
4145 self.name,
4146 self.r#type,
4147 if self.exists { " EXISTS" } else { "" },
4148 self.path
4149 )?;
4150 if let Some(on_empty) = &self.on_empty {
4151 write!(f, " {on_empty} ON EMPTY")?;
4152 }
4153 if let Some(on_error) = &self.on_error {
4154 write!(f, " {on_error} ON ERROR")?;
4155 }
4156 Ok(())
4157 }
4158}
4159
4160#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4163#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4164#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4165pub enum JsonTableColumnErrorHandling {
4167 Null,
4169 Default(ValueWithSpan),
4171 Error,
4173}
4174
4175impl fmt::Display for JsonTableColumnErrorHandling {
4176 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4177 match self {
4178 JsonTableColumnErrorHandling::Null => write!(f, "NULL"),
4179 JsonTableColumnErrorHandling::Default(json_string) => {
4180 write!(f, "DEFAULT {json_string}")
4181 }
4182 JsonTableColumnErrorHandling::Error => write!(f, "ERROR"),
4183 }
4184 }
4185}
4186
4187#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4195#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4196#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4197pub struct OpenJsonTableColumn {
4198 pub name: Ident,
4200 pub r#type: DataType,
4202 pub path: Option<String>,
4204 pub as_json: bool,
4206}
4207
4208impl fmt::Display for OpenJsonTableColumn {
4209 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4210 write!(f, "{} {}", self.name, self.r#type)?;
4211 if let Some(path) = &self.path {
4212 write!(f, " '{}'", value::escape_single_quote_string(path))?;
4213 }
4214 if self.as_json {
4215 write!(f, " AS JSON")?;
4216 }
4217 Ok(())
4218 }
4219}
4220
4221#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4228#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4229#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4230pub enum ValueTableMode {
4232 AsStruct,
4234 AsValue,
4236 DistinctAsStruct,
4238 DistinctAsValue,
4240}
4241
4242impl fmt::Display for ValueTableMode {
4243 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4244 match self {
4245 ValueTableMode::AsStruct => write!(f, "AS STRUCT"),
4246 ValueTableMode::AsValue => write!(f, "AS VALUE"),
4247 ValueTableMode::DistinctAsStruct => write!(f, "DISTINCT AS STRUCT"),
4248 ValueTableMode::DistinctAsValue => write!(f, "DISTINCT AS VALUE"),
4249 }
4250 }
4251}
4252
4253#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4255#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4256#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4257pub enum UpdateTableFromKind {
4258 BeforeSet(Vec<TableWithJoins>),
4261 AfterSet(Vec<TableWithJoins>),
4264}
4265
4266#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4268#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4269#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4270pub enum XmlTableColumnOption {
4271 NamedInfo {
4273 r#type: DataType,
4275 path: Option<Expr>,
4277 default: Option<Expr>,
4279 nullable: bool,
4281 },
4282 ForOrdinality,
4284}
4285
4286#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4299#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4300#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4301pub struct XmlTableColumn {
4302 pub name: Ident,
4304 pub option: XmlTableColumnOption,
4306}
4307
4308impl fmt::Display for XmlTableColumn {
4309 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4310 write!(f, "{}", self.name)?;
4311 match &self.option {
4312 XmlTableColumnOption::NamedInfo {
4313 r#type,
4314 path,
4315 default,
4316 nullable,
4317 } => {
4318 write!(f, " {type}")?;
4319 if let Some(p) = path {
4320 write!(f, " PATH {p}")?;
4321 }
4322 if let Some(d) = default {
4323 write!(f, " DEFAULT {d}")?;
4324 }
4325 if !*nullable {
4326 write!(f, " NOT NULL")?;
4327 }
4328 Ok(())
4329 }
4330 XmlTableColumnOption::ForOrdinality => {
4331 write!(f, " FOR ORDINALITY")
4332 }
4333 }
4334 }
4335}
4336
4337#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4339#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4340#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4341pub struct XmlPassingArgument {
4343 pub expr: Expr,
4345 pub alias: Option<Ident>,
4347 pub by_value: bool,
4349}
4350
4351impl fmt::Display for XmlPassingArgument {
4352 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4353 if self.by_value {
4354 write!(f, "BY VALUE ")?;
4355 }
4356 write!(f, "{}", self.expr)?;
4357 if let Some(alias) = &self.alias {
4358 write!(f, " AS {alias}")?;
4359 }
4360 Ok(())
4361 }
4362}
4363
4364#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4366#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4367#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4368pub struct XmlPassingClause {
4370 pub arguments: Vec<XmlPassingArgument>,
4372}
4373
4374impl fmt::Display for XmlPassingClause {
4375 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4376 if !self.arguments.is_empty() {
4377 write!(f, " PASSING {}", display_comma_separated(&self.arguments))?;
4378 }
4379 Ok(())
4380 }
4381}
4382
4383#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4387#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4388#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4389pub struct XmlNamespaceDefinition {
4390 pub uri: Expr,
4392 pub name: Ident,
4394}
4395
4396impl fmt::Display for XmlNamespaceDefinition {
4397 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4398 write!(f, "{} AS {}", self.uri, self.name)
4399 }
4400}