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#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
147#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
148#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
149pub enum SetExpr {
150 Select(Box<Select>),
152 Query(Box<Query>),
155 SetOperation {
158 left: Box<SetExpr>,
160 op: SetOperator,
162 set_quantifier: SetQuantifier,
164 right: Box<SetExpr>,
166 },
167 Values(Values),
169 Insert(Statement),
171 Update(Statement),
173 Delete(Statement),
175 Merge(Statement),
177 Table(Box<Table>),
179}
180
181impl SetExpr {
182 pub fn as_select(&self) -> Option<&Select> {
184 if let Self::Select(select) = self {
185 Some(&**select)
186 } else {
187 None
188 }
189 }
190}
191
192impl fmt::Display for SetExpr {
193 #[cfg_attr(feature = "recursive-protection", recursive::recursive)]
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))]
1460pub enum TableIndexDirective {
1461 MySql(TableIndexHints),
1464 IndexedBy(Ident),
1466 NotIndexed,
1468}
1469
1470impl fmt::Display for TableIndexDirective {
1471 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1472 match self {
1473 TableIndexDirective::MySql(hint) => hint.fmt(f),
1474 TableIndexDirective::IndexedBy(name) => write!(f, "INDEXED BY {name}"),
1475 TableIndexDirective::NotIndexed => f.write_str("NOT INDEXED"),
1476 }
1477 }
1478}
1479
1480#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1484#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1485#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1486pub enum TableInheritance {
1487 Only {
1489 parenthesized: bool,
1491 },
1492 IncludeDescendants,
1494}
1495
1496#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1498#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1499#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1500#[cfg_attr(feature = "visitor", visit(with = "visit_table_factor"))]
1501pub enum TableFactor {
1502 Table {
1504 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
1505 name: ObjectName,
1507 alias: Option<TableAlias>,
1509 args: Option<TableFunctionArgs>,
1517 with_hints: Vec<Expr>,
1519 version: Option<TableVersion>,
1522 with_ordinality: bool,
1526 partitions: Vec<Ident>,
1528 json_path: Option<JsonPath>,
1530 sample: Option<TableSampleKind>,
1533 index_directives: Vec<TableIndexDirective>,
1535 inheritance: Option<TableInheritance>,
1537 },
1538 Derived {
1540 lateral: bool,
1542 subquery: Box<Query>,
1544 alias: Option<TableAlias>,
1546 sample: Option<TableSampleKind>,
1548 },
1549 TableFunction {
1551 expr: Expr,
1553 alias: Option<TableAlias>,
1555 },
1556 Function {
1558 lateral: bool,
1560 name: ObjectName,
1562 args: Vec<FunctionArg>,
1564 with_ordinality: bool,
1566 alias: Option<TableAlias>,
1568 },
1569 UNNEST {
1580 alias: Option<TableAlias>,
1582 array_exprs: Vec<Expr>,
1584 with_offset: bool,
1586 with_offset_alias: Option<Ident>,
1588 with_ordinality: bool,
1590 },
1591 JsonTable {
1607 json_expr: Expr,
1609 json_path: ValueWithSpan,
1612 columns: Vec<JsonTableColumn>,
1615 alias: Option<TableAlias>,
1617 },
1618 OpenJsonTable {
1628 json_expr: Expr,
1630 json_path: Option<ValueWithSpan>,
1633 columns: Vec<OpenJsonTableColumn>,
1636 alias: Option<TableAlias>,
1638 },
1639 NestedJoin {
1646 table_with_joins: Box<TableWithJoins>,
1648 alias: Option<TableAlias>,
1650 },
1651 Pivot {
1658 table: Box<TableFactor>,
1660 aggregate_functions: Vec<ExprWithAlias>, value_column: Vec<Expr>,
1664 value_source: PivotValueSource,
1666 default_on_null: Option<Expr>,
1668 alias: Option<TableAlias>,
1670 },
1671 Unpivot {
1683 table: Box<TableFactor>,
1685 value: Expr,
1687 name: Ident,
1689 columns: Vec<ExprWithAlias>,
1691 null_inclusion: Option<NullInclusion>,
1693 alias: Option<TableAlias>,
1695 },
1696 UnpivotExpr {
1704 expression: Expr,
1706 value_alias: Ident,
1708 attribute_alias: Option<Ident>,
1710 },
1711 MatchRecognize {
1715 table: Box<TableFactor>,
1717 partition_by: Vec<Expr>,
1719 order_by: Vec<OrderByExpr>,
1721 measures: Vec<Measure>,
1723 rows_per_match: Option<RowsPerMatch>,
1725 after_match_skip: Option<AfterMatchSkip>,
1727 pattern: MatchRecognizePattern,
1729 symbols: Vec<SymbolDefinition>,
1731 alias: Option<TableAlias>,
1733 },
1734 XmlTable {
1754 namespaces: Vec<XmlNamespaceDefinition>,
1756 row_expression: Expr,
1758 passing: XmlPassingClause,
1760 columns: Vec<XmlTableColumn>,
1762 alias: Option<TableAlias>,
1764 },
1765 SemanticView {
1777 name: ObjectName,
1779 dimensions: Vec<Expr>,
1781 metrics: Vec<Expr>,
1783 facts: Vec<Expr>,
1785 where_clause: Option<Expr>,
1787 alias: Option<TableAlias>,
1789 },
1790}
1791
1792#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1794#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1795#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1796pub enum TableSampleKind {
1797 BeforeTableAlias(Box<TableSample>),
1799 AfterTableAlias(Box<TableSample>),
1801}
1802
1803#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1804#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1805#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1806pub struct TableSample {
1808 pub modifier: TableSampleModifier,
1810 pub name: Option<TableSampleMethod>,
1812 pub quantity: Option<TableSampleQuantity>,
1814 pub seed: Option<TableSampleSeed>,
1816 pub bucket: Option<TableSampleBucket>,
1818 pub offset: Option<Expr>,
1820}
1821
1822#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1823#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1824#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1825pub enum TableSampleModifier {
1827 Sample,
1829 TableSample,
1831}
1832
1833impl fmt::Display for TableSampleModifier {
1834 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1835 match self {
1836 TableSampleModifier::Sample => write!(f, "SAMPLE")?,
1837 TableSampleModifier::TableSample => write!(f, "TABLESAMPLE")?,
1838 }
1839 Ok(())
1840 }
1841}
1842
1843#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1844#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1845#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1846pub struct TableSampleQuantity {
1848 pub parenthesized: bool,
1850 pub value: Expr,
1852 pub unit: Option<TableSampleUnit>,
1854}
1855
1856impl fmt::Display for TableSampleQuantity {
1857 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1858 if self.parenthesized {
1859 write!(f, "(")?;
1860 }
1861 write!(f, "{}", self.value)?;
1862 if let Some(unit) = &self.unit {
1863 write!(f, " {unit}")?;
1864 }
1865 if self.parenthesized {
1866 write!(f, ")")?;
1867 }
1868 Ok(())
1869 }
1870}
1871
1872#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1874#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1875#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1876pub enum TableSampleMethod {
1878 Row,
1880 Bernoulli,
1882 System,
1884 Block,
1886}
1887
1888impl fmt::Display for TableSampleMethod {
1889 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1890 match self {
1891 TableSampleMethod::Bernoulli => write!(f, "BERNOULLI"),
1892 TableSampleMethod::Row => write!(f, "ROW"),
1893 TableSampleMethod::System => write!(f, "SYSTEM"),
1894 TableSampleMethod::Block => write!(f, "BLOCK"),
1895 }
1896 }
1897}
1898
1899#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1900#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1901#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1902pub struct TableSampleSeed {
1904 pub modifier: TableSampleSeedModifier,
1906 pub value: ValueWithSpan,
1908}
1909
1910impl fmt::Display for TableSampleSeed {
1911 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1912 write!(f, "{} ({})", self.modifier, self.value)?;
1913 Ok(())
1914 }
1915}
1916
1917#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1918#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1919#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1920pub enum TableSampleSeedModifier {
1922 Repeatable,
1924 Seed,
1926}
1927
1928impl fmt::Display for TableSampleSeedModifier {
1929 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1930 match self {
1931 TableSampleSeedModifier::Repeatable => write!(f, "REPEATABLE"),
1932 TableSampleSeedModifier::Seed => write!(f, "SEED"),
1933 }
1934 }
1935}
1936
1937#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1938#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1939#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1940pub enum TableSampleUnit {
1942 Rows,
1944 Percent,
1946}
1947
1948impl fmt::Display for TableSampleUnit {
1949 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1950 match self {
1951 TableSampleUnit::Percent => write!(f, "PERCENT"),
1952 TableSampleUnit::Rows => write!(f, "ROWS"),
1953 }
1954 }
1955}
1956
1957#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1958#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1959#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1960pub struct TableSampleBucket {
1962 pub bucket: ValueWithSpan,
1964 pub total: ValueWithSpan,
1966 pub on: Option<Expr>,
1968}
1969
1970impl fmt::Display for TableSampleBucket {
1971 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1972 write!(f, "BUCKET {} OUT OF {}", self.bucket, self.total)?;
1973 if let Some(on) = &self.on {
1974 write!(f, " ON {on}")?;
1975 }
1976 Ok(())
1977 }
1978}
1979impl fmt::Display for TableSample {
1980 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1981 write!(f, "{}", self.modifier)?;
1982 if let Some(name) = &self.name {
1983 write!(f, " {name}")?;
1984 }
1985 if let Some(quantity) = &self.quantity {
1986 write!(f, " {quantity}")?;
1987 }
1988 if let Some(seed) = &self.seed {
1989 write!(f, " {seed}")?;
1990 }
1991 if let Some(bucket) = &self.bucket {
1992 write!(f, " ({bucket})")?;
1993 }
1994 if let Some(offset) = &self.offset {
1995 write!(f, " OFFSET {offset}")?;
1996 }
1997 Ok(())
1998 }
1999}
2000
2001#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2003#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2004#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2005pub enum PivotValueSource {
2006 List(Vec<ExprWithAlias>),
2010 Any(Vec<OrderByExpr>),
2014 Subquery(Box<Query>),
2018}
2019
2020impl fmt::Display for PivotValueSource {
2021 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2022 match self {
2023 PivotValueSource::List(values) => write!(f, "{}", display_comma_separated(values)),
2024 PivotValueSource::Any(order_by) => {
2025 write!(f, "ANY")?;
2026 if !order_by.is_empty() {
2027 write!(f, " ORDER BY {}", display_comma_separated(order_by))?;
2028 }
2029 Ok(())
2030 }
2031 PivotValueSource::Subquery(query) => write!(f, "{query}"),
2032 }
2033 }
2034}
2035
2036#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2040#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2041#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2042pub struct Measure {
2044 pub expr: Expr,
2046 pub alias: Ident,
2048}
2049
2050impl fmt::Display for Measure {
2051 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2052 write!(f, "{} AS {}", self.expr, self.alias)
2053 }
2054}
2055
2056#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2060#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2061#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2062pub enum RowsPerMatch {
2063 OneRow,
2065 AllRows(Option<EmptyMatchesMode>),
2067}
2068
2069impl fmt::Display for RowsPerMatch {
2070 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2071 match self {
2072 RowsPerMatch::OneRow => write!(f, "ONE ROW PER MATCH"),
2073 RowsPerMatch::AllRows(mode) => {
2074 write!(f, "ALL ROWS PER MATCH")?;
2075 if let Some(mode) = mode {
2076 write!(f, " {mode}")?;
2077 }
2078 Ok(())
2079 }
2080 }
2081 }
2082}
2083
2084#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2088#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2089#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2090pub enum AfterMatchSkip {
2091 PastLastRow,
2093 ToNextRow,
2095 ToFirst(Ident),
2097 ToLast(Ident),
2099}
2100
2101impl fmt::Display for AfterMatchSkip {
2102 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2103 write!(f, "AFTER MATCH SKIP ")?;
2104 match self {
2105 AfterMatchSkip::PastLastRow => write!(f, "PAST LAST ROW"),
2106 AfterMatchSkip::ToNextRow => write!(f, " TO NEXT ROW"),
2107 AfterMatchSkip::ToFirst(symbol) => write!(f, "TO FIRST {symbol}"),
2108 AfterMatchSkip::ToLast(symbol) => write!(f, "TO LAST {symbol}"),
2109 }
2110 }
2111}
2112
2113#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2114#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2115#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2116pub enum EmptyMatchesMode {
2118 Show,
2120 Omit,
2122 WithUnmatched,
2124}
2125
2126impl fmt::Display for EmptyMatchesMode {
2127 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2128 match self {
2129 EmptyMatchesMode::Show => write!(f, "SHOW EMPTY MATCHES"),
2130 EmptyMatchesMode::Omit => write!(f, "OMIT EMPTY MATCHES"),
2131 EmptyMatchesMode::WithUnmatched => write!(f, "WITH UNMATCHED ROWS"),
2132 }
2133 }
2134}
2135
2136#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2140#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2141#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2142pub struct SymbolDefinition {
2144 pub symbol: Ident,
2146 pub definition: Expr,
2148}
2149
2150impl fmt::Display for SymbolDefinition {
2151 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2152 write!(f, "{} AS {}", self.symbol, self.definition)
2153 }
2154}
2155
2156#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2158#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2159#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2160pub enum MatchRecognizeSymbol {
2161 Named(Ident),
2163 Start,
2165 End,
2167}
2168
2169impl fmt::Display for MatchRecognizeSymbol {
2170 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2171 match self {
2172 MatchRecognizeSymbol::Named(symbol) => write!(f, "{symbol}"),
2173 MatchRecognizeSymbol::Start => write!(f, "^"),
2174 MatchRecognizeSymbol::End => write!(f, "$"),
2175 }
2176 }
2177}
2178
2179#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2183#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2184#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2185pub enum MatchRecognizePattern {
2186 Symbol(MatchRecognizeSymbol),
2188 Exclude(MatchRecognizeSymbol),
2190 Permute(Vec<MatchRecognizeSymbol>),
2192 Concat(Vec<MatchRecognizePattern>),
2194 Group(Box<MatchRecognizePattern>),
2196 Alternation(Vec<MatchRecognizePattern>),
2198 Repetition(Box<MatchRecognizePattern>, RepetitionQuantifier),
2200}
2201
2202impl fmt::Display for MatchRecognizePattern {
2203 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2204 use MatchRecognizePattern::*;
2205 match self {
2206 Symbol(symbol) => write!(f, "{symbol}"),
2207 Exclude(symbol) => write!(f, "{{- {symbol} -}}"),
2208 Permute(symbols) => write!(f, "PERMUTE({})", display_comma_separated(symbols)),
2209 Concat(patterns) => write!(f, "{}", display_separated(patterns, " ")),
2210 Group(pattern) => write!(f, "( {pattern} )"),
2211 Alternation(patterns) => write!(f, "{}", display_separated(patterns, " | ")),
2212 Repetition(pattern, op) => write!(f, "{pattern}{op}"),
2213 }
2214 }
2215}
2216
2217#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2220#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2221#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2222pub enum RepetitionQuantifier {
2223 ZeroOrMore,
2225 OneOrMore,
2227 AtMostOne,
2229 Exactly(u32),
2231 AtLeast(u32),
2233 AtMost(u32),
2235 Range(u32, u32),
2237 Reluctant(Box<RepetitionQuantifier>),
2239}
2240
2241impl fmt::Display for RepetitionQuantifier {
2242 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2243 use RepetitionQuantifier::*;
2244 match self {
2245 ZeroOrMore => write!(f, "*"),
2246 OneOrMore => write!(f, "+"),
2247 AtMostOne => write!(f, "?"),
2248 Exactly(n) => write!(f, "{{{n}}}"),
2249 AtLeast(n) => write!(f, "{{{n},}}"),
2250 AtMost(n) => write!(f, "{{,{n}}}"),
2251 Range(n, m) => write!(f, "{{{n},{m}}}"),
2252 Reluctant(quantifier) => write!(f, "{quantifier}?"),
2253 }
2254 }
2255}
2256
2257impl fmt::Display for TableFactor {
2258 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2259 match self {
2260 TableFactor::Table {
2261 name,
2262 alias,
2263 args,
2264 with_hints,
2265 version,
2266 partitions,
2267 with_ordinality,
2268 json_path,
2269 sample,
2270 index_directives,
2271 inheritance,
2272 } => {
2273 match inheritance {
2274 Some(TableInheritance::Only {
2275 parenthesized: true,
2276 }) => write!(f, "ONLY ({name})")?,
2277 Some(TableInheritance::Only {
2278 parenthesized: false,
2279 }) => write!(f, "ONLY {name}")?,
2280 Some(TableInheritance::IncludeDescendants) => write!(f, "{name} *")?,
2281 None => name.fmt(f)?,
2282 }
2283 if let Some(json_path) = json_path {
2284 json_path.fmt(f)?;
2285 }
2286 if !partitions.is_empty() {
2287 write!(f, " PARTITION ({})", display_comma_separated(partitions))?;
2288 }
2289 if let Some(args) = args {
2290 write!(f, "(")?;
2291 write!(f, "{}", display_comma_separated(&args.args))?;
2292 if let Some(ref settings) = args.settings {
2293 if !args.args.is_empty() {
2294 write!(f, ", ")?;
2295 }
2296 write!(f, "SETTINGS {}", display_comma_separated(settings))?;
2297 }
2298 write!(f, ")")?;
2299 }
2300 if *with_ordinality {
2301 write!(f, " WITH ORDINALITY")?;
2302 }
2303 if let Some(TableSampleKind::BeforeTableAlias(sample)) = sample {
2304 write!(f, " {sample}")?;
2305 }
2306 if let Some(alias) = alias {
2307 write!(f, " {alias}")?;
2308 }
2309 if !index_directives.is_empty() {
2310 write!(f, " {}", display_separated(index_directives, " "))?;
2311 }
2312 if !with_hints.is_empty() {
2313 write!(f, " WITH ({})", display_comma_separated(with_hints))?;
2314 }
2315 if let Some(version) = version {
2316 write!(f, " {version}")?;
2317 }
2318 if let Some(TableSampleKind::AfterTableAlias(sample)) = sample {
2319 write!(f, " {sample}")?;
2320 }
2321 Ok(())
2322 }
2323 TableFactor::Derived {
2324 lateral,
2325 subquery,
2326 alias,
2327 sample,
2328 } => {
2329 if *lateral {
2330 write!(f, "LATERAL ")?;
2331 }
2332 f.write_str("(")?;
2333 NewLine.fmt(f)?;
2334 Indent(subquery).fmt(f)?;
2335 NewLine.fmt(f)?;
2336 f.write_str(")")?;
2337 if let Some(alias) = alias {
2338 write!(f, " {alias}")?;
2339 }
2340 if let Some(TableSampleKind::AfterTableAlias(sample)) = sample {
2341 write!(f, " {sample}")?;
2342 }
2343 Ok(())
2344 }
2345 TableFactor::Function {
2346 lateral,
2347 name,
2348 args,
2349 with_ordinality,
2350 alias,
2351 } => {
2352 if *lateral {
2353 write!(f, "LATERAL ")?;
2354 }
2355 write!(f, "{name}")?;
2356 write!(f, "({})", display_comma_separated(args))?;
2357 if *with_ordinality {
2358 write!(f, " WITH ORDINALITY")?;
2359 }
2360 if let Some(alias) = alias {
2361 write!(f, " {alias}")?;
2362 }
2363 Ok(())
2364 }
2365 TableFactor::TableFunction { expr, alias } => {
2366 write!(f, "TABLE({expr})")?;
2367 if let Some(alias) = alias {
2368 write!(f, " {alias}")?;
2369 }
2370 Ok(())
2371 }
2372 TableFactor::UNNEST {
2373 alias,
2374 array_exprs,
2375 with_offset,
2376 with_offset_alias,
2377 with_ordinality,
2378 } => {
2379 write!(f, "UNNEST({})", display_comma_separated(array_exprs))?;
2380
2381 if *with_ordinality {
2382 write!(f, " WITH ORDINALITY")?;
2383 }
2384
2385 if let Some(alias) = alias {
2386 write!(f, " {alias}")?;
2387 }
2388 if *with_offset {
2389 write!(f, " WITH OFFSET")?;
2390 }
2391 if let Some(alias) = with_offset_alias {
2392 write!(f, " {alias}")?;
2393 }
2394 Ok(())
2395 }
2396 TableFactor::JsonTable {
2397 json_expr,
2398 json_path,
2399 columns,
2400 alias,
2401 } => {
2402 write!(
2403 f,
2404 "JSON_TABLE({json_expr}, {json_path} COLUMNS({columns}))",
2405 columns = display_comma_separated(columns)
2406 )?;
2407 if let Some(alias) = alias {
2408 write!(f, " {alias}")?;
2409 }
2410 Ok(())
2411 }
2412 TableFactor::OpenJsonTable {
2413 json_expr,
2414 json_path,
2415 columns,
2416 alias,
2417 } => {
2418 write!(f, "OPENJSON({json_expr}")?;
2419 if let Some(json_path) = json_path {
2420 write!(f, ", {json_path}")?;
2421 }
2422 write!(f, ")")?;
2423 if !columns.is_empty() {
2424 write!(f, " WITH ({})", display_comma_separated(columns))?;
2425 }
2426 if let Some(alias) = alias {
2427 write!(f, " {alias}")?;
2428 }
2429 Ok(())
2430 }
2431 TableFactor::NestedJoin {
2432 table_with_joins,
2433 alias,
2434 } => {
2435 write!(f, "({table_with_joins})")?;
2436 if let Some(alias) = alias {
2437 write!(f, " {alias}")?;
2438 }
2439 Ok(())
2440 }
2441 TableFactor::Pivot {
2442 table,
2443 aggregate_functions,
2444 value_column,
2445 value_source,
2446 default_on_null,
2447 alias,
2448 } => {
2449 write!(
2450 f,
2451 "{table} PIVOT({} FOR ",
2452 display_comma_separated(aggregate_functions),
2453 )?;
2454 if value_column.len() == 1 {
2455 write!(f, "{}", value_column[0])?;
2456 } else {
2457 write!(f, "({})", display_comma_separated(value_column))?;
2458 }
2459 write!(f, " IN ({value_source})")?;
2460 if let Some(expr) = default_on_null {
2461 write!(f, " DEFAULT ON NULL ({expr})")?;
2462 }
2463 write!(f, ")")?;
2464 if let Some(alias) = alias {
2465 write!(f, " {alias}")?;
2466 }
2467 Ok(())
2468 }
2469 TableFactor::Unpivot {
2470 table,
2471 null_inclusion,
2472 value,
2473 name,
2474 columns,
2475 alias,
2476 } => {
2477 write!(f, "{table} UNPIVOT")?;
2478 if let Some(null_inclusion) = null_inclusion {
2479 write!(f, " {null_inclusion} ")?;
2480 }
2481 write!(
2482 f,
2483 "({} FOR {} IN ({}))",
2484 value,
2485 name,
2486 display_comma_separated(columns)
2487 )?;
2488 if let Some(alias) = alias {
2489 write!(f, " {alias}")?;
2490 }
2491 Ok(())
2492 }
2493 TableFactor::UnpivotExpr {
2494 expression,
2495 value_alias,
2496 attribute_alias,
2497 } => {
2498 write!(f, "UNPIVOT {expression} AS {value_alias}")?;
2499 if let Some(attribute_alias) = attribute_alias {
2500 write!(f, " AT {attribute_alias}")?;
2501 }
2502 Ok(())
2503 }
2504 TableFactor::MatchRecognize {
2505 table,
2506 partition_by,
2507 order_by,
2508 measures,
2509 rows_per_match,
2510 after_match_skip,
2511 pattern,
2512 symbols,
2513 alias,
2514 } => {
2515 write!(f, "{table} MATCH_RECOGNIZE(")?;
2516 if !partition_by.is_empty() {
2517 write!(f, "PARTITION BY {} ", display_comma_separated(partition_by))?;
2518 }
2519 if !order_by.is_empty() {
2520 write!(f, "ORDER BY {} ", display_comma_separated(order_by))?;
2521 }
2522 if !measures.is_empty() {
2523 write!(f, "MEASURES {} ", display_comma_separated(measures))?;
2524 }
2525 if let Some(rows_per_match) = rows_per_match {
2526 write!(f, "{rows_per_match} ")?;
2527 }
2528 if let Some(after_match_skip) = after_match_skip {
2529 write!(f, "{after_match_skip} ")?;
2530 }
2531 write!(f, "PATTERN ({pattern}) ")?;
2532 write!(f, "DEFINE {})", display_comma_separated(symbols))?;
2533 if let Some(alias) = alias {
2534 write!(f, " {alias}")?;
2535 }
2536 Ok(())
2537 }
2538 TableFactor::XmlTable {
2539 row_expression,
2540 passing,
2541 columns,
2542 alias,
2543 namespaces,
2544 } => {
2545 write!(f, "XMLTABLE(")?;
2546 if !namespaces.is_empty() {
2547 write!(
2548 f,
2549 "XMLNAMESPACES({}), ",
2550 display_comma_separated(namespaces)
2551 )?;
2552 }
2553 write!(
2554 f,
2555 "{row_expression}{passing} COLUMNS {columns})",
2556 columns = display_comma_separated(columns)
2557 )?;
2558 if let Some(alias) = alias {
2559 write!(f, " {alias}")?;
2560 }
2561 Ok(())
2562 }
2563 TableFactor::SemanticView {
2564 name,
2565 dimensions,
2566 metrics,
2567 facts,
2568 where_clause,
2569 alias,
2570 } => {
2571 write!(f, "SEMANTIC_VIEW({name}")?;
2572
2573 if !dimensions.is_empty() {
2574 write!(f, " DIMENSIONS {}", display_comma_separated(dimensions))?;
2575 }
2576
2577 if !metrics.is_empty() {
2578 write!(f, " METRICS {}", display_comma_separated(metrics))?;
2579 }
2580
2581 if !facts.is_empty() {
2582 write!(f, " FACTS {}", display_comma_separated(facts))?;
2583 }
2584
2585 if let Some(where_clause) = where_clause {
2586 write!(f, " WHERE {where_clause}")?;
2587 }
2588
2589 write!(f, ")")?;
2590
2591 if let Some(alias) = alias {
2592 write!(f, " {alias}")?;
2593 }
2594
2595 Ok(())
2596 }
2597 }
2598 }
2599}
2600
2601#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2602#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2603#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2604pub struct TableAlias {
2606 pub explicit: bool,
2610 pub name: Ident,
2612 pub columns: Vec<TableAliasColumnDef>,
2614 pub at: Option<Ident>,
2620}
2621
2622impl fmt::Display for TableAlias {
2623 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2624 write!(f, "{}{}", if self.explicit { "AS " } else { "" }, self.name)?;
2625 if !self.columns.is_empty() {
2626 write!(f, " ({})", display_comma_separated(&self.columns))?;
2627 }
2628 if let Some(at) = &self.at {
2629 write!(f, " AT {at}")?;
2630 }
2631 Ok(())
2632 }
2633}
2634
2635#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2641#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2642#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2643pub struct TableAliasColumnDef {
2644 pub name: Ident,
2646 pub data_type: Option<DataType>,
2648}
2649
2650impl TableAliasColumnDef {
2651 pub fn from_name<S: Into<String>>(name: S) -> Self {
2653 TableAliasColumnDef {
2654 name: Ident::new(name),
2655 data_type: None,
2656 }
2657 }
2658}
2659
2660impl fmt::Display for TableAliasColumnDef {
2661 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2662 write!(f, "{}", self.name)?;
2663 if let Some(ref data_type) = self.data_type {
2664 write!(f, " {data_type}")?;
2665 }
2666 Ok(())
2667 }
2668}
2669
2670#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2671#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2672#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2673pub enum TableVersion {
2675 ForSystemTimeAsOf(Expr),
2678 TimestampAsOf(Expr),
2682 VersionAsOf(Expr),
2686 Function(Expr),
2689 Changes {
2699 changes: Expr,
2701 at: Expr,
2703 end: Option<Expr>,
2705 },
2706}
2707
2708impl Display for TableVersion {
2709 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2710 match self {
2711 TableVersion::ForSystemTimeAsOf(e) => write!(f, "FOR SYSTEM_TIME AS OF {e}")?,
2712 TableVersion::TimestampAsOf(e) => write!(f, "TIMESTAMP AS OF {e}")?,
2713 TableVersion::VersionAsOf(e) => write!(f, "VERSION AS OF {e}")?,
2714 TableVersion::Function(func) => write!(f, "{func}")?,
2715 TableVersion::Changes { changes, at, end } => {
2716 write!(f, "{changes} {at}")?;
2717 if let Some(end) = end {
2718 write!(f, " {end}")?;
2719 }
2720 }
2721 }
2722 Ok(())
2723 }
2724}
2725
2726#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2727#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2728#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2729pub struct Join {
2731 pub relation: TableFactor,
2733 pub global: bool,
2736 pub join_operator: JoinOperator,
2738}
2739
2740impl fmt::Display for Join {
2741 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2742 fn prefix(constraint: &JoinConstraint) -> &'static str {
2743 match constraint {
2744 JoinConstraint::Natural => "NATURAL ",
2745 _ => "",
2746 }
2747 }
2748 fn suffix(constraint: &'_ JoinConstraint) -> impl fmt::Display + '_ {
2749 struct Suffix<'a>(&'a JoinConstraint);
2750 impl fmt::Display for Suffix<'_> {
2751 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2752 match self.0 {
2753 JoinConstraint::On(expr) => write!(f, " ON {expr}"),
2754 JoinConstraint::Using(attrs) => {
2755 write!(f, " USING({})", display_comma_separated(attrs))
2756 }
2757 JoinConstraint::UsingAlias(columns, alias) => {
2758 write!(f, " USING({}) AS {alias}", display_comma_separated(columns))
2759 }
2760 _ => Ok(()),
2761 }
2762 }
2763 }
2764 Suffix(constraint)
2765 }
2766 if self.global {
2767 write!(f, "GLOBAL ")?;
2768 }
2769
2770 match &self.join_operator {
2771 JoinOperator::Join(constraint) => f.write_fmt(format_args!(
2772 "{}JOIN {}{}",
2773 prefix(constraint),
2774 self.relation,
2775 suffix(constraint)
2776 )),
2777 JoinOperator::Inner(constraint) => f.write_fmt(format_args!(
2778 "{}INNER JOIN {}{}",
2779 prefix(constraint),
2780 self.relation,
2781 suffix(constraint)
2782 )),
2783 JoinOperator::Left(constraint) => f.write_fmt(format_args!(
2784 "{}LEFT JOIN {}{}",
2785 prefix(constraint),
2786 self.relation,
2787 suffix(constraint)
2788 )),
2789 JoinOperator::LeftOuter(constraint) => f.write_fmt(format_args!(
2790 "{}LEFT OUTER JOIN {}{}",
2791 prefix(constraint),
2792 self.relation,
2793 suffix(constraint)
2794 )),
2795 JoinOperator::Right(constraint) => f.write_fmt(format_args!(
2796 "{}RIGHT JOIN {}{}",
2797 prefix(constraint),
2798 self.relation,
2799 suffix(constraint)
2800 )),
2801 JoinOperator::RightOuter(constraint) => f.write_fmt(format_args!(
2802 "{}RIGHT OUTER JOIN {}{}",
2803 prefix(constraint),
2804 self.relation,
2805 suffix(constraint)
2806 )),
2807 JoinOperator::FullOuter(constraint) => f.write_fmt(format_args!(
2808 "{}FULL JOIN {}{}",
2809 prefix(constraint),
2810 self.relation,
2811 suffix(constraint)
2812 )),
2813 JoinOperator::CrossJoin(constraint) => f.write_fmt(format_args!(
2814 "CROSS JOIN {}{}",
2815 self.relation,
2816 suffix(constraint)
2817 )),
2818 JoinOperator::Semi(constraint) => f.write_fmt(format_args!(
2819 "{}SEMI JOIN {}{}",
2820 prefix(constraint),
2821 self.relation,
2822 suffix(constraint)
2823 )),
2824 JoinOperator::LeftSemi(constraint) => f.write_fmt(format_args!(
2825 "{}LEFT SEMI JOIN {}{}",
2826 prefix(constraint),
2827 self.relation,
2828 suffix(constraint)
2829 )),
2830 JoinOperator::RightSemi(constraint) => f.write_fmt(format_args!(
2831 "{}RIGHT SEMI JOIN {}{}",
2832 prefix(constraint),
2833 self.relation,
2834 suffix(constraint)
2835 )),
2836 JoinOperator::Anti(constraint) => f.write_fmt(format_args!(
2837 "{}ANTI JOIN {}{}",
2838 prefix(constraint),
2839 self.relation,
2840 suffix(constraint)
2841 )),
2842 JoinOperator::LeftAnti(constraint) => f.write_fmt(format_args!(
2843 "{}LEFT ANTI JOIN {}{}",
2844 prefix(constraint),
2845 self.relation,
2846 suffix(constraint)
2847 )),
2848 JoinOperator::RightAnti(constraint) => f.write_fmt(format_args!(
2849 "{}RIGHT ANTI JOIN {}{}",
2850 prefix(constraint),
2851 self.relation,
2852 suffix(constraint)
2853 )),
2854 JoinOperator::CrossApply => f.write_fmt(format_args!("CROSS APPLY {}", self.relation)),
2855 JoinOperator::OuterApply => f.write_fmt(format_args!("OUTER APPLY {}", self.relation)),
2856 JoinOperator::AsOf {
2857 match_condition,
2858 constraint,
2859 } => f.write_fmt(format_args!(
2860 "ASOF JOIN {} MATCH_CONDITION ({match_condition}){}",
2861 self.relation,
2862 suffix(constraint)
2863 )),
2864 JoinOperator::StraightJoin(constraint) => f.write_fmt(format_args!(
2865 "STRAIGHT_JOIN {}{}",
2866 self.relation,
2867 suffix(constraint)
2868 )),
2869 JoinOperator::ArrayJoin => f.write_fmt(format_args!("ARRAY JOIN {}", self.relation)),
2870 JoinOperator::LeftArrayJoin => {
2871 f.write_fmt(format_args!("LEFT ARRAY JOIN {}", self.relation))
2872 }
2873 JoinOperator::InnerArrayJoin => {
2874 f.write_fmt(format_args!("INNER ARRAY JOIN {}", self.relation))
2875 }
2876 }
2877 }
2878}
2879
2880#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2881#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2882#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2883pub enum JoinOperator {
2885 Join(JoinConstraint),
2887 Inner(JoinConstraint),
2889 Left(JoinConstraint),
2891 LeftOuter(JoinConstraint),
2893 Right(JoinConstraint),
2895 RightOuter(JoinConstraint),
2897 FullOuter(JoinConstraint),
2899 CrossJoin(JoinConstraint),
2901 Semi(JoinConstraint),
2903 LeftSemi(JoinConstraint),
2905 RightSemi(JoinConstraint),
2907 Anti(JoinConstraint),
2909 LeftAnti(JoinConstraint),
2911 RightAnti(JoinConstraint),
2913 CrossApply,
2915 OuterApply,
2917 AsOf {
2921 match_condition: Expr,
2923 constraint: JoinConstraint,
2925 },
2926 StraightJoin(JoinConstraint),
2930 ArrayJoin,
2934 LeftArrayJoin,
2936 InnerArrayJoin,
2938}
2939
2940#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2941#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2942#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2943pub enum JoinConstraint {
2945 On(Expr),
2947 Using(Vec<ObjectName>),
2949 UsingAlias(Vec<ObjectName>, Ident),
2951 Natural,
2953 None,
2955}
2956
2957#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2958#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2959#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2960pub enum OrderByKind {
2962 All(OrderByOptions),
2967
2968 Expressions(Vec<OrderByExpr>),
2970}
2971
2972#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2973#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2974#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2975#[cfg_attr(feature = "visitor", visit(with = "visit_order_by"))]
2976pub struct OrderBy {
2978 pub kind: OrderByKind,
2980
2981 pub interpolate: Option<Interpolate>,
2983}
2984
2985impl fmt::Display for OrderBy {
2986 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2987 write!(f, "ORDER BY")?;
2988 match &self.kind {
2989 OrderByKind::Expressions(exprs) => {
2990 write!(f, " {}", display_comma_separated(exprs))?;
2991 }
2992 OrderByKind::All(all) => {
2993 write!(f, " ALL{all}")?;
2994 }
2995 }
2996
2997 if let Some(ref interpolate) = self.interpolate {
2998 match &interpolate.exprs {
2999 Some(exprs) => write!(f, " INTERPOLATE ({})", display_comma_separated(exprs))?,
3000 None => write!(f, " INTERPOLATE")?,
3001 }
3002 }
3003
3004 Ok(())
3005 }
3006}
3007
3008#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3010#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3011#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3012#[cfg_attr(feature = "visitor", visit(with = "visit_order_by_expr"))]
3013pub struct OrderByExpr {
3014 pub expr: Expr,
3016 pub options: OrderByOptions,
3018 pub with_fill: Option<WithFill>,
3020}
3021
3022impl From<Ident> for OrderByExpr {
3023 fn from(ident: Ident) -> Self {
3024 OrderByExpr {
3025 expr: Expr::Identifier(ident),
3026 options: OrderByOptions::default(),
3027 with_fill: None,
3028 }
3029 }
3030}
3031
3032impl fmt::Display for OrderByExpr {
3033 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3034 write!(f, "{}", self.expr)?;
3035 self.fmt_sort_modifiers(f)
3036 }
3037}
3038
3039impl OrderByExpr {
3040 pub(crate) fn fmt_sort_modifiers(&self, f: &mut fmt::Formatter) -> fmt::Result {
3042 write!(f, "{}", self.options)?;
3043 if let Some(with_fill) = &self.with_fill {
3044 write!(f, " {with_fill}")?
3045 }
3046 Ok(())
3047 }
3048}
3049
3050#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3055#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3056#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3057pub struct WithFill {
3059 pub from: Option<Expr>,
3061 pub to: Option<Expr>,
3063 pub step: Option<Expr>,
3065}
3066
3067impl fmt::Display for WithFill {
3068 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3069 write!(f, "WITH FILL")?;
3070 if let Some(ref from) = self.from {
3071 write!(f, " FROM {from}")?;
3072 }
3073 if let Some(ref to) = self.to {
3074 write!(f, " TO {to}")?;
3075 }
3076 if let Some(ref step) = self.step {
3077 write!(f, " STEP {step}")?;
3078 }
3079 Ok(())
3080 }
3081}
3082
3083#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3088#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3089#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3090pub struct InterpolateExpr {
3092 pub column: Ident,
3094 pub expr: Option<Expr>,
3096}
3097
3098#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3099#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3100#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3101pub struct Interpolate {
3103 pub exprs: Option<Vec<InterpolateExpr>>,
3105}
3106
3107impl fmt::Display for InterpolateExpr {
3108 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3109 write!(f, "{}", self.column)?;
3110 if let Some(ref expr) = self.expr {
3111 write!(f, " AS {expr}")?;
3112 }
3113 Ok(())
3114 }
3115}
3116
3117#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3122#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3123#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3124pub enum OrderBySort {
3125 Asc,
3127 Desc,
3129 Using(ObjectName),
3133}
3134
3135#[derive(Default, Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3136#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3137#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3138pub struct OrderByOptions {
3140 pub sort: Option<OrderBySort>,
3142 pub nulls_first: Option<bool>,
3144}
3145
3146impl fmt::Display for OrderByOptions {
3147 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3148 match &self.sort {
3149 Some(OrderBySort::Asc) => write!(f, " ASC")?,
3150 Some(OrderBySort::Desc) => write!(f, " DESC")?,
3151 Some(OrderBySort::Using(op)) => {
3152 if op.0.len() > 1 {
3153 write!(f, " USING OPERATOR({op})")?;
3154 } else {
3155 write!(f, " USING {op}")?;
3156 }
3157 }
3158 None => (),
3159 }
3160 match self.nulls_first {
3161 Some(true) => write!(f, " NULLS FIRST")?,
3162 Some(false) => write!(f, " NULLS LAST")?,
3163 None => (),
3164 }
3165 Ok(())
3166 }
3167}
3168
3169#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3170#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3171#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3172pub enum LimitClause {
3174 LimitOffset {
3178 limit: Option<Expr>,
3180 offset: Option<Offset>,
3182 limit_by: Vec<Expr>,
3184 },
3185 OffsetCommaLimit {
3187 offset: Expr,
3189 limit: Expr,
3191 },
3192}
3193
3194impl fmt::Display for LimitClause {
3195 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3196 match self {
3197 LimitClause::LimitOffset {
3198 limit,
3199 limit_by,
3200 offset,
3201 } => {
3202 if let Some(ref limit) = limit {
3203 write!(f, " LIMIT {limit}")?;
3204 }
3205 if let Some(ref offset) = offset {
3206 write!(f, " {offset}")?;
3207 }
3208 if !limit_by.is_empty() {
3209 debug_assert!(limit.is_some());
3210 write!(f, " BY {}", display_separated(limit_by, ", "))?;
3211 }
3212 Ok(())
3213 }
3214 LimitClause::OffsetCommaLimit { offset, limit } => {
3215 write!(f, " LIMIT {offset}, {limit}")
3216 }
3217 }
3218 }
3219}
3220
3221#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3222#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3223#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3224pub struct Offset {
3226 pub value: Expr,
3228 pub rows: OffsetRows,
3230}
3231
3232impl fmt::Display for Offset {
3233 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3234 write!(f, "OFFSET {}{}", self.value, self.rows)
3235 }
3236}
3237
3238#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3240#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3241#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3242pub enum OffsetRows {
3243 None,
3245 Row,
3247 Rows,
3249}
3250
3251impl fmt::Display for OffsetRows {
3252 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3253 match self {
3254 OffsetRows::None => Ok(()),
3255 OffsetRows::Row => write!(f, " ROW"),
3256 OffsetRows::Rows => write!(f, " ROWS"),
3257 }
3258 }
3259}
3260
3261#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3273#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3274#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3275pub enum PipeOperator {
3276 Limit {
3282 expr: Expr,
3284 offset: Option<Expr>,
3286 },
3287 Where {
3293 expr: Expr,
3295 },
3296 OrderBy {
3298 exprs: Vec<OrderByExpr>,
3300 },
3301 Select {
3307 exprs: Vec<SelectItem>,
3309 },
3310 Extend {
3316 exprs: Vec<SelectItem>,
3318 },
3319 Set {
3325 assignments: Vec<Assignment>,
3327 },
3328 Drop {
3334 columns: Vec<Ident>,
3336 },
3337 As {
3343 alias: Ident,
3345 },
3346 Aggregate {
3358 full_table_exprs: Vec<ExprWithAliasAndOrderBy>,
3360 group_by_expr: Vec<ExprWithAliasAndOrderBy>,
3362 },
3363 TableSample {
3367 sample: Box<TableSample>,
3369 },
3370 Rename {
3376 mappings: Vec<IdentWithAlias>,
3378 },
3379 Union {
3385 set_quantifier: SetQuantifier,
3387 queries: Vec<Query>,
3389 },
3390 Intersect {
3396 set_quantifier: SetQuantifier,
3398 queries: Vec<Query>,
3400 },
3401 Except {
3407 set_quantifier: SetQuantifier,
3409 queries: Vec<Query>,
3411 },
3412 Call {
3418 function: Function,
3420 alias: Option<Ident>,
3422 },
3423 Pivot {
3429 aggregate_functions: Vec<ExprWithAlias>,
3431 value_column: Vec<Ident>,
3433 value_source: PivotValueSource,
3435 alias: Option<Ident>,
3437 },
3438 Unpivot {
3447 value_column: Ident,
3449 name_column: Ident,
3451 unpivot_columns: Vec<Ident>,
3453 alias: Option<Ident>,
3455 },
3456 Join(Join),
3462}
3463
3464impl fmt::Display for PipeOperator {
3465 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3466 match self {
3467 PipeOperator::Select { exprs } => {
3468 write!(f, "SELECT {}", display_comma_separated(exprs.as_slice()))
3469 }
3470 PipeOperator::Extend { exprs } => {
3471 write!(f, "EXTEND {}", display_comma_separated(exprs.as_slice()))
3472 }
3473 PipeOperator::Set { assignments } => {
3474 write!(f, "SET {}", display_comma_separated(assignments.as_slice()))
3475 }
3476 PipeOperator::Drop { columns } => {
3477 write!(f, "DROP {}", display_comma_separated(columns.as_slice()))
3478 }
3479 PipeOperator::As { alias } => {
3480 write!(f, "AS {alias}")
3481 }
3482 PipeOperator::Limit { expr, offset } => {
3483 write!(f, "LIMIT {expr}")?;
3484 if let Some(offset) = offset {
3485 write!(f, " OFFSET {offset}")?;
3486 }
3487 Ok(())
3488 }
3489 PipeOperator::Aggregate {
3490 full_table_exprs,
3491 group_by_expr,
3492 } => {
3493 write!(f, "AGGREGATE")?;
3494 if !full_table_exprs.is_empty() {
3495 write!(
3496 f,
3497 " {}",
3498 display_comma_separated(full_table_exprs.as_slice())
3499 )?;
3500 }
3501 if !group_by_expr.is_empty() {
3502 write!(f, " GROUP BY {}", display_comma_separated(group_by_expr))?;
3503 }
3504 Ok(())
3505 }
3506
3507 PipeOperator::Where { expr } => {
3508 write!(f, "WHERE {expr}")
3509 }
3510 PipeOperator::OrderBy { exprs } => {
3511 write!(f, "ORDER BY {}", display_comma_separated(exprs.as_slice()))
3512 }
3513
3514 PipeOperator::TableSample { sample } => {
3515 write!(f, "{sample}")
3516 }
3517 PipeOperator::Rename { mappings } => {
3518 write!(f, "RENAME {}", display_comma_separated(mappings))
3519 }
3520 PipeOperator::Union {
3521 set_quantifier,
3522 queries,
3523 } => Self::fmt_set_operation(f, "UNION", set_quantifier, queries),
3524 PipeOperator::Intersect {
3525 set_quantifier,
3526 queries,
3527 } => Self::fmt_set_operation(f, "INTERSECT", set_quantifier, queries),
3528 PipeOperator::Except {
3529 set_quantifier,
3530 queries,
3531 } => Self::fmt_set_operation(f, "EXCEPT", set_quantifier, queries),
3532 PipeOperator::Call { function, alias } => {
3533 write!(f, "CALL {function}")?;
3534 Self::fmt_optional_alias(f, alias)
3535 }
3536 PipeOperator::Pivot {
3537 aggregate_functions,
3538 value_column,
3539 value_source,
3540 alias,
3541 } => {
3542 write!(
3543 f,
3544 "PIVOT({} FOR {} IN ({}))",
3545 display_comma_separated(aggregate_functions),
3546 Expr::CompoundIdentifier(value_column.to_vec()),
3547 value_source
3548 )?;
3549 Self::fmt_optional_alias(f, alias)
3550 }
3551 PipeOperator::Unpivot {
3552 value_column,
3553 name_column,
3554 unpivot_columns,
3555 alias,
3556 } => {
3557 write!(
3558 f,
3559 "UNPIVOT({} FOR {} IN ({}))",
3560 value_column,
3561 name_column,
3562 display_comma_separated(unpivot_columns)
3563 )?;
3564 Self::fmt_optional_alias(f, alias)
3565 }
3566 PipeOperator::Join(join) => write!(f, "{join}"),
3567 }
3568 }
3569}
3570
3571impl PipeOperator {
3572 fn fmt_optional_alias(f: &mut fmt::Formatter<'_>, alias: &Option<Ident>) -> fmt::Result {
3574 if let Some(alias) = alias {
3575 write!(f, " AS {alias}")?;
3576 }
3577 Ok(())
3578 }
3579
3580 fn fmt_set_operation(
3582 f: &mut fmt::Formatter<'_>,
3583 operation: &str,
3584 set_quantifier: &SetQuantifier,
3585 queries: &[Query],
3586 ) -> fmt::Result {
3587 write!(f, "{operation}")?;
3588 match set_quantifier {
3589 SetQuantifier::None => {}
3590 _ => {
3591 write!(f, " {set_quantifier}")?;
3592 }
3593 }
3594 write!(f, " ")?;
3595 let parenthesized_queries: Vec<String> =
3596 queries.iter().map(|query| format!("({query})")).collect();
3597 write!(f, "{}", display_comma_separated(&parenthesized_queries))
3598 }
3599}
3600
3601#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3602#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3603#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3604pub struct Fetch {
3606 pub with_ties: bool,
3608 pub percent: bool,
3610 pub quantity: Option<Expr>,
3612}
3613
3614impl fmt::Display for Fetch {
3615 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3616 let extension = if self.with_ties { "WITH TIES" } else { "ONLY" };
3617 if let Some(ref quantity) = self.quantity {
3618 let percent = if self.percent { " PERCENT" } else { "" };
3619 write!(f, "FETCH FIRST {quantity}{percent} ROWS {extension}")
3620 } else {
3621 write!(f, "FETCH FIRST ROWS {extension}")
3622 }
3623 }
3624}
3625
3626#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3627#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3628#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3629pub struct LockClause {
3631 pub lock_type: LockType,
3633 pub of: Option<ObjectName>,
3635 pub nonblock: Option<NonBlock>,
3637}
3638
3639impl fmt::Display for LockClause {
3640 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3641 write!(f, "FOR {}", self.lock_type)?;
3642 if let Some(ref of) = self.of {
3643 write!(f, " OF {of}")?;
3644 }
3645 if let Some(ref nb) = self.nonblock {
3646 write!(f, " {nb}")?;
3647 }
3648 Ok(())
3649 }
3650}
3651
3652#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3653#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3654#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3655pub enum LockType {
3657 Share,
3659 Update,
3661}
3662
3663impl fmt::Display for LockType {
3664 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3665 let select_lock = match self {
3666 LockType::Share => "SHARE",
3667 LockType::Update => "UPDATE",
3668 };
3669 write!(f, "{select_lock}")
3670 }
3671}
3672
3673#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3674#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3675#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3676pub enum NonBlock {
3678 Nowait,
3680 SkipLocked,
3682}
3683
3684impl fmt::Display for NonBlock {
3685 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3686 let nonblock = match self {
3687 NonBlock::Nowait => "NOWAIT",
3688 NonBlock::SkipLocked => "SKIP LOCKED",
3689 };
3690 write!(f, "{nonblock}")
3691 }
3692}
3693
3694#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3695#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3696#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3697pub enum Distinct {
3699 All,
3704
3705 Distinct,
3707
3708 On(Vec<Expr>),
3710}
3711
3712impl fmt::Display for Distinct {
3713 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3714 match self {
3715 Distinct::All => write!(f, "ALL"),
3716 Distinct::Distinct => write!(f, "DISTINCT"),
3717 Distinct::On(col_names) => {
3718 let col_names = display_comma_separated(col_names);
3719 write!(f, "DISTINCT ON ({col_names})")
3720 }
3721 }
3722 }
3723}
3724
3725#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3726#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3727#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3728pub struct Top {
3730 pub with_ties: bool,
3733 pub percent: bool,
3735 pub quantity: Option<TopQuantity>,
3737}
3738
3739#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3740#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3741#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3742pub enum TopQuantity {
3744 Expr(Expr),
3746 Constant(u64),
3748}
3749
3750impl fmt::Display for Top {
3751 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3752 let extension = if self.with_ties { " WITH TIES" } else { "" };
3753 if let Some(ref quantity) = self.quantity {
3754 let percent = if self.percent { " PERCENT" } else { "" };
3755 match quantity {
3756 TopQuantity::Expr(quantity) => write!(f, "TOP ({quantity}){percent}{extension}"),
3757 TopQuantity::Constant(quantity) => {
3758 write!(f, "TOP {quantity}{percent}{extension}")
3759 }
3760 }
3761 } else {
3762 write!(f, "TOP{extension}")
3763 }
3764 }
3765}
3766
3767#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3768#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3769#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3770pub struct Values {
3772 pub explicit_row: bool,
3775 pub value_keyword: bool,
3778 pub rows: Vec<Parens<Vec<Expr>>>,
3780}
3781
3782impl fmt::Display for Values {
3783 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3784 match self.value_keyword {
3785 true => f.write_str("VALUE")?,
3786 false => f.write_str("VALUES")?,
3787 };
3788 let prefix = if self.explicit_row { "ROW" } else { "" };
3789 let mut delim = "";
3790 for row in &self.rows {
3791 f.write_str(delim)?;
3792 delim = ",";
3793 SpaceOrNewline.fmt(f)?;
3794 Indent(format_args!("{prefix}({})", display_comma_separated(row))).fmt(f)?;
3795 }
3796 Ok(())
3797 }
3798}
3799
3800#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3801#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3802#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3803pub struct SelectInto {
3805 pub temporary: bool,
3807 pub unlogged: bool,
3809 pub table: bool,
3811 pub targets: Vec<Expr>,
3816}
3817
3818impl fmt::Display for SelectInto {
3819 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3820 let temporary = if self.temporary { " TEMPORARY" } else { "" };
3821 let unlogged = if self.unlogged { " UNLOGGED" } else { "" };
3822 let table = if self.table { " TABLE" } else { "" };
3823
3824 write!(
3825 f,
3826 "INTO{}{}{} {}",
3827 temporary,
3828 unlogged,
3829 table,
3830 display_comma_separated(&self.targets)
3831 )
3832 }
3833}
3834
3835#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3840#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3841#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3842pub enum GroupByWithModifier {
3844 Rollup,
3846 Cube,
3848 Totals,
3850 GroupingSets(Expr),
3854}
3855
3856impl fmt::Display for GroupByWithModifier {
3857 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3858 match self {
3859 GroupByWithModifier::Rollup => write!(f, "WITH ROLLUP"),
3860 GroupByWithModifier::Cube => write!(f, "WITH CUBE"),
3861 GroupByWithModifier::Totals => write!(f, "WITH TOTALS"),
3862 GroupByWithModifier::GroupingSets(expr) => {
3863 write!(f, "{expr}")
3864 }
3865 }
3866 }
3867}
3868
3869#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3870#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3871#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3872#[cfg_attr(feature = "visitor", visit(with = "visit_group_by"))]
3873pub enum GroupByExpr {
3876 All(Vec<GroupByWithModifier>),
3886 Expressions(Vec<Expr>, Vec<GroupByWithModifier>),
3888}
3889
3890impl fmt::Display for GroupByExpr {
3891 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3892 match self {
3893 GroupByExpr::All(modifiers) => {
3894 write!(f, "GROUP BY ALL")?;
3895 if !modifiers.is_empty() {
3896 write!(f, " {}", display_separated(modifiers, " "))?;
3897 }
3898 Ok(())
3899 }
3900 GroupByExpr::Expressions(col_names, modifiers) => {
3901 f.write_str("GROUP BY")?;
3902 SpaceOrNewline.fmt(f)?;
3903 Indent(display_comma_separated(col_names)).fmt(f)?;
3904 if !modifiers.is_empty() {
3905 write!(f, " {}", display_separated(modifiers, " "))?;
3906 }
3907 Ok(())
3908 }
3909 }
3910 }
3911}
3912
3913#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3917#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3918#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3919pub enum FormatClause {
3920 Identifier(Ident),
3922 Null,
3924}
3925
3926impl fmt::Display for FormatClause {
3927 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3928 match self {
3929 FormatClause::Identifier(ident) => write!(f, "FORMAT {ident}"),
3930 FormatClause::Null => write!(f, "FORMAT NULL"),
3931 }
3932 }
3933}
3934
3935#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3939#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3940#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3941pub struct InputFormatClause {
3942 pub ident: Ident,
3944 pub values: Vec<Expr>,
3946}
3947
3948impl fmt::Display for InputFormatClause {
3949 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3950 write!(f, "FORMAT {}", self.ident)?;
3951
3952 if !self.values.is_empty() {
3953 write!(f, " {}", display_comma_separated(self.values.as_slice()))?;
3954 }
3955
3956 Ok(())
3957 }
3958}
3959
3960#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3962#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3963#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3964pub enum ForClause {
3965 Browse,
3967 Json {
3969 for_json: ForJson,
3971 root: Option<String>,
3973 include_null_values: bool,
3975 without_array_wrapper: bool,
3977 },
3978 Xml {
3980 for_xml: ForXml,
3982 elements: bool,
3984 binary_base64: bool,
3986 root: Option<String>,
3988 r#type: bool,
3990 },
3991}
3992
3993impl fmt::Display for ForClause {
3994 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3995 match self {
3996 ForClause::Browse => write!(f, "FOR BROWSE"),
3997 ForClause::Json {
3998 for_json,
3999 root,
4000 include_null_values,
4001 without_array_wrapper,
4002 } => {
4003 write!(f, "FOR JSON ")?;
4004 write!(f, "{for_json}")?;
4005 if let Some(root) = root {
4006 write!(f, ", ROOT('{root}')")?;
4007 }
4008 if *include_null_values {
4009 write!(f, ", INCLUDE_NULL_VALUES")?;
4010 }
4011 if *without_array_wrapper {
4012 write!(f, ", WITHOUT_ARRAY_WRAPPER")?;
4013 }
4014 Ok(())
4015 }
4016 ForClause::Xml {
4017 for_xml,
4018 elements,
4019 binary_base64,
4020 root,
4021 r#type,
4022 } => {
4023 write!(f, "FOR XML ")?;
4024 write!(f, "{for_xml}")?;
4025 if *binary_base64 {
4026 write!(f, ", BINARY BASE64")?;
4027 }
4028 if *r#type {
4029 write!(f, ", TYPE")?;
4030 }
4031 if let Some(root) = root {
4032 write!(f, ", ROOT('{root}')")?;
4033 }
4034 if *elements {
4035 write!(f, ", ELEMENTS")?;
4036 }
4037 Ok(())
4038 }
4039 }
4040 }
4041}
4042
4043#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4044#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4045#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4046pub enum ForXml {
4048 Raw(Option<String>),
4050 Auto,
4052 Explicit,
4054 Path(Option<String>),
4056}
4057
4058impl fmt::Display for ForXml {
4059 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4060 match self {
4061 ForXml::Raw(root) => {
4062 write!(f, "RAW")?;
4063 if let Some(root) = root {
4064 write!(f, "('{root}')")?;
4065 }
4066 Ok(())
4067 }
4068 ForXml::Auto => write!(f, "AUTO"),
4069 ForXml::Explicit => write!(f, "EXPLICIT"),
4070 ForXml::Path(root) => {
4071 write!(f, "PATH")?;
4072 if let Some(root) = root {
4073 write!(f, "('{root}')")?;
4074 }
4075 Ok(())
4076 }
4077 }
4078 }
4079}
4080
4081#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4082#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4083#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4084pub enum ForJson {
4086 Auto,
4088 Path,
4090}
4091
4092impl fmt::Display for ForJson {
4093 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4094 match self {
4095 ForJson::Auto => write!(f, "AUTO"),
4096 ForJson::Path => write!(f, "PATH"),
4097 }
4098 }
4099}
4100
4101#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4122#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4123#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4124pub enum JsonTableColumn {
4125 Named(JsonTableNamedColumn),
4127 ForOrdinality(Ident),
4129 Nested(JsonTableNestedColumn),
4131}
4132
4133impl fmt::Display for JsonTableColumn {
4134 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4135 match self {
4136 JsonTableColumn::Named(json_table_named_column) => {
4137 write!(f, "{json_table_named_column}")
4138 }
4139 JsonTableColumn::ForOrdinality(ident) => write!(f, "{ident} FOR ORDINALITY"),
4140 JsonTableColumn::Nested(json_table_nested_column) => {
4141 write!(f, "{json_table_nested_column}")
4142 }
4143 }
4144 }
4145}
4146
4147#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4151#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4152#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4153pub struct JsonTableNestedColumn {
4155 pub path: ValueWithSpan,
4157 pub columns: Vec<JsonTableColumn>,
4159}
4160
4161impl fmt::Display for JsonTableNestedColumn {
4162 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4163 write!(
4164 f,
4165 "NESTED PATH {} COLUMNS ({})",
4166 self.path,
4167 display_comma_separated(&self.columns)
4168 )
4169 }
4170}
4171
4172#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4180#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4181#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4182pub struct JsonTableNamedColumn {
4183 pub name: Ident,
4185 pub r#type: DataType,
4187 pub path: ValueWithSpan,
4189 pub exists: bool,
4191 pub on_empty: Option<JsonTableColumnErrorHandling>,
4193 pub on_error: Option<JsonTableColumnErrorHandling>,
4195}
4196
4197impl fmt::Display for JsonTableNamedColumn {
4198 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4199 write!(
4200 f,
4201 "{} {}{} PATH {}",
4202 self.name,
4203 self.r#type,
4204 if self.exists { " EXISTS" } else { "" },
4205 self.path
4206 )?;
4207 if let Some(on_empty) = &self.on_empty {
4208 write!(f, " {on_empty} ON EMPTY")?;
4209 }
4210 if let Some(on_error) = &self.on_error {
4211 write!(f, " {on_error} ON ERROR")?;
4212 }
4213 Ok(())
4214 }
4215}
4216
4217#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4220#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4221#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4222pub enum JsonTableColumnErrorHandling {
4224 Null,
4226 Default(ValueWithSpan),
4228 Error,
4230}
4231
4232impl fmt::Display for JsonTableColumnErrorHandling {
4233 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4234 match self {
4235 JsonTableColumnErrorHandling::Null => write!(f, "NULL"),
4236 JsonTableColumnErrorHandling::Default(json_string) => {
4237 write!(f, "DEFAULT {json_string}")
4238 }
4239 JsonTableColumnErrorHandling::Error => write!(f, "ERROR"),
4240 }
4241 }
4242}
4243
4244#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4252#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4253#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4254pub struct OpenJsonTableColumn {
4255 pub name: Ident,
4257 pub r#type: DataType,
4259 pub path: Option<String>,
4261 pub as_json: bool,
4263}
4264
4265impl fmt::Display for OpenJsonTableColumn {
4266 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4267 write!(f, "{} {}", self.name, self.r#type)?;
4268 if let Some(path) = &self.path {
4269 write!(f, " '{}'", value::escape_single_quote_string(path))?;
4270 }
4271 if self.as_json {
4272 write!(f, " AS JSON")?;
4273 }
4274 Ok(())
4275 }
4276}
4277
4278#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4285#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4286#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4287pub enum ValueTableMode {
4289 AsStruct,
4291 AsValue,
4293 DistinctAsStruct,
4295 DistinctAsValue,
4297}
4298
4299impl fmt::Display for ValueTableMode {
4300 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4301 match self {
4302 ValueTableMode::AsStruct => write!(f, "AS STRUCT"),
4303 ValueTableMode::AsValue => write!(f, "AS VALUE"),
4304 ValueTableMode::DistinctAsStruct => write!(f, "DISTINCT AS STRUCT"),
4305 ValueTableMode::DistinctAsValue => write!(f, "DISTINCT AS VALUE"),
4306 }
4307 }
4308}
4309
4310#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4312#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4313#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4314pub enum UpdateTableFromKind {
4315 BeforeSet(Vec<TableWithJoins>),
4318 AfterSet(Vec<TableWithJoins>),
4321}
4322
4323#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4325#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4326#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4327pub enum XmlTableColumnOption {
4328 NamedInfo {
4330 r#type: DataType,
4332 path: Option<Expr>,
4334 default: Option<Expr>,
4336 nullable: bool,
4338 },
4339 ForOrdinality,
4341}
4342
4343#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4356#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4357#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4358pub struct XmlTableColumn {
4359 pub name: Ident,
4361 pub option: XmlTableColumnOption,
4363}
4364
4365impl fmt::Display for XmlTableColumn {
4366 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4367 write!(f, "{}", self.name)?;
4368 match &self.option {
4369 XmlTableColumnOption::NamedInfo {
4370 r#type,
4371 path,
4372 default,
4373 nullable,
4374 } => {
4375 write!(f, " {type}")?;
4376 if let Some(p) = path {
4377 write!(f, " PATH {p}")?;
4378 }
4379 if let Some(d) = default {
4380 write!(f, " DEFAULT {d}")?;
4381 }
4382 if !*nullable {
4383 write!(f, " NOT NULL")?;
4384 }
4385 Ok(())
4386 }
4387 XmlTableColumnOption::ForOrdinality => {
4388 write!(f, " FOR ORDINALITY")
4389 }
4390 }
4391 }
4392}
4393
4394#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4396#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4397#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4398pub struct XmlPassingArgument {
4400 pub expr: Expr,
4402 pub alias: Option<Ident>,
4404 pub by_value: bool,
4406}
4407
4408impl fmt::Display for XmlPassingArgument {
4409 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4410 if self.by_value {
4411 write!(f, "BY VALUE ")?;
4412 }
4413 write!(f, "{}", self.expr)?;
4414 if let Some(alias) = &self.alias {
4415 write!(f, " AS {alias}")?;
4416 }
4417 Ok(())
4418 }
4419}
4420
4421#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4423#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4424#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4425pub struct XmlPassingClause {
4427 pub arguments: Vec<XmlPassingArgument>,
4429}
4430
4431impl fmt::Display for XmlPassingClause {
4432 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4433 if !self.arguments.is_empty() {
4434 write!(f, " PASSING {}", display_comma_separated(&self.arguments))?;
4435 }
4436 Ok(())
4437 }
4438}
4439
4440#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4444#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4445#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4446pub struct XmlNamespaceDefinition {
4447 pub uri: Expr,
4449 pub name: Ident,
4451}
4452
4453impl fmt::Display for XmlNamespaceDefinition {
4454 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4455 write!(f, "{} AS {}", self.uri, self.name)
4456 }
4457}