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>,
124 pub order_by: Option<OrderBy>,
125 pub group_by: Option<GroupByExpr>,
126}
127
128impl fmt::Display for ProjectionSelect {
129 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
130 write!(f, "SELECT {}", display_comma_separated(&self.projection))?;
131 if let Some(ref group_by) = self.group_by {
132 write!(f, " {group_by}")?;
133 }
134 if let Some(ref order_by) = self.order_by {
135 write!(f, " {order_by}")?;
136 }
137 Ok(())
138 }
139}
140
141#[allow(clippy::large_enum_variant)]
144#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
145#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
146#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
147pub enum SetExpr {
148 Select(Box<Select>),
150 Query(Box<Query>),
153 SetOperation {
155 op: SetOperator,
156 set_quantifier: SetQuantifier,
157 left: Box<SetExpr>,
158 right: Box<SetExpr>,
159 },
160 Values(Values),
161 Insert(Statement),
162 Update(Statement),
163 Delete(Statement),
164 Table(Box<Table>),
165}
166
167impl SetExpr {
168 pub fn as_select(&self) -> Option<&Select> {
170 if let Self::Select(select) = self {
171 Some(&**select)
172 } else {
173 None
174 }
175 }
176}
177
178impl fmt::Display for SetExpr {
179 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
180 match self {
181 SetExpr::Select(s) => s.fmt(f),
182 SetExpr::Query(q) => {
183 f.write_str("(")?;
184 q.fmt(f)?;
185 f.write_str(")")
186 }
187 SetExpr::Values(v) => v.fmt(f),
188 SetExpr::Insert(v) => v.fmt(f),
189 SetExpr::Update(v) => v.fmt(f),
190 SetExpr::Delete(v) => v.fmt(f),
191 SetExpr::Table(t) => t.fmt(f),
192 SetExpr::SetOperation {
193 left,
194 right,
195 op,
196 set_quantifier,
197 } => {
198 left.fmt(f)?;
199 SpaceOrNewline.fmt(f)?;
200 op.fmt(f)?;
201 match set_quantifier {
202 SetQuantifier::All
203 | SetQuantifier::Distinct
204 | SetQuantifier::ByName
205 | SetQuantifier::AllByName
206 | SetQuantifier::DistinctByName => {
207 f.write_str(" ")?;
208 set_quantifier.fmt(f)?;
209 }
210 SetQuantifier::None => {}
211 }
212 SpaceOrNewline.fmt(f)?;
213 right.fmt(f)?;
214 Ok(())
215 }
216 }
217 }
218}
219
220#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
221#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
222#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
223pub enum SetOperator {
224 Union,
225 Except,
226 Intersect,
227 Minus,
228}
229
230impl fmt::Display for SetOperator {
231 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
232 f.write_str(match self {
233 SetOperator::Union => "UNION",
234 SetOperator::Except => "EXCEPT",
235 SetOperator::Intersect => "INTERSECT",
236 SetOperator::Minus => "MINUS",
237 })
238 }
239}
240
241#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
245#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
246#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
247pub enum SetQuantifier {
248 All,
249 Distinct,
250 ByName,
251 AllByName,
252 DistinctByName,
253 None,
254}
255
256impl fmt::Display for SetQuantifier {
257 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
258 match self {
259 SetQuantifier::All => write!(f, "ALL"),
260 SetQuantifier::Distinct => write!(f, "DISTINCT"),
261 SetQuantifier::ByName => write!(f, "BY NAME"),
262 SetQuantifier::AllByName => write!(f, "ALL BY NAME"),
263 SetQuantifier::DistinctByName => write!(f, "DISTINCT BY NAME"),
264 SetQuantifier::None => Ok(()),
265 }
266 }
267}
268
269#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
270#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
271#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
273pub struct Table {
274 pub table_name: Option<String>,
275 pub schema_name: Option<String>,
276}
277
278impl fmt::Display for Table {
279 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
280 if let Some(ref schema_name) = self.schema_name {
281 write!(
282 f,
283 "TABLE {}.{}",
284 schema_name,
285 self.table_name.as_ref().unwrap(),
286 )?;
287 } else {
288 write!(f, "TABLE {}", self.table_name.as_ref().unwrap(),)?;
289 }
290 Ok(())
291 }
292}
293
294#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
296#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
297#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
298pub enum SelectFlavor {
299 Standard,
301 FromFirst,
303 FromFirstNoSelect,
305}
306
307#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
311#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
312#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
313pub struct Select {
314 pub select_token: AttachedToken,
316 pub distinct: Option<Distinct>,
318 pub top: Option<Top>,
320 pub top_before_distinct: bool,
322 pub projection: Vec<SelectItem>,
324 pub into: Option<SelectInto>,
326 pub from: Vec<TableWithJoins>,
328 pub lateral_views: Vec<LateralView>,
330 pub prewhere: Option<Expr>,
335 pub selection: Option<Expr>,
337 pub group_by: GroupByExpr,
339 pub cluster_by: Vec<Expr>,
341 pub distribute_by: Vec<Expr>,
343 pub sort_by: Vec<OrderByExpr>,
345 pub having: Option<Expr>,
347 pub named_window: Vec<NamedWindowDefinition>,
349 pub qualify: Option<Expr>,
351 pub window_before_qualify: bool,
356 pub value_table_mode: Option<ValueTableMode>,
358 pub connect_by: Option<ConnectBy>,
360 pub flavor: SelectFlavor,
362}
363
364impl fmt::Display for Select {
365 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
366 match self.flavor {
367 SelectFlavor::Standard => {
368 write!(f, "SELECT")?;
369 }
370 SelectFlavor::FromFirst => {
371 write!(f, "FROM {} SELECT", display_comma_separated(&self.from))?;
372 }
373 SelectFlavor::FromFirstNoSelect => {
374 write!(f, "FROM {}", display_comma_separated(&self.from))?;
375 }
376 }
377
378 if let Some(value_table_mode) = self.value_table_mode {
379 f.write_str(" ")?;
380 value_table_mode.fmt(f)?;
381 }
382
383 if let Some(ref top) = self.top {
384 if self.top_before_distinct {
385 f.write_str(" ")?;
386 top.fmt(f)?;
387 }
388 }
389 if let Some(ref distinct) = self.distinct {
390 f.write_str(" ")?;
391 distinct.fmt(f)?;
392 }
393 if let Some(ref top) = self.top {
394 if !self.top_before_distinct {
395 f.write_str(" ")?;
396 top.fmt(f)?;
397 }
398 }
399
400 if !self.projection.is_empty() {
401 indented_list(f, &self.projection)?;
402 }
403
404 if let Some(ref into) = self.into {
405 f.write_str(" ")?;
406 into.fmt(f)?;
407 }
408
409 if self.flavor == SelectFlavor::Standard && !self.from.is_empty() {
410 SpaceOrNewline.fmt(f)?;
411 f.write_str("FROM")?;
412 indented_list(f, &self.from)?;
413 }
414 if !self.lateral_views.is_empty() {
415 for lv in &self.lateral_views {
416 lv.fmt(f)?;
417 }
418 }
419 if let Some(ref prewhere) = self.prewhere {
420 f.write_str(" PREWHERE ")?;
421 prewhere.fmt(f)?;
422 }
423 if let Some(ref selection) = self.selection {
424 SpaceOrNewline.fmt(f)?;
425 f.write_str("WHERE")?;
426 SpaceOrNewline.fmt(f)?;
427 Indent(selection).fmt(f)?;
428 }
429 match &self.group_by {
430 GroupByExpr::All(_) => {
431 SpaceOrNewline.fmt(f)?;
432 self.group_by.fmt(f)?;
433 }
434 GroupByExpr::Expressions(exprs, _) => {
435 if !exprs.is_empty() {
436 SpaceOrNewline.fmt(f)?;
437 self.group_by.fmt(f)?;
438 }
439 }
440 }
441 if !self.cluster_by.is_empty() {
442 SpaceOrNewline.fmt(f)?;
443 f.write_str("CLUSTER BY")?;
444 SpaceOrNewline.fmt(f)?;
445 Indent(display_comma_separated(&self.cluster_by)).fmt(f)?;
446 }
447 if !self.distribute_by.is_empty() {
448 SpaceOrNewline.fmt(f)?;
449 f.write_str("DISTRIBUTE BY")?;
450 SpaceOrNewline.fmt(f)?;
451 display_comma_separated(&self.distribute_by).fmt(f)?;
452 }
453 if !self.sort_by.is_empty() {
454 SpaceOrNewline.fmt(f)?;
455 f.write_str("SORT BY")?;
456 SpaceOrNewline.fmt(f)?;
457 Indent(display_comma_separated(&self.sort_by)).fmt(f)?;
458 }
459 if let Some(ref having) = self.having {
460 SpaceOrNewline.fmt(f)?;
461 f.write_str("HAVING")?;
462 SpaceOrNewline.fmt(f)?;
463 Indent(having).fmt(f)?;
464 }
465 if self.window_before_qualify {
466 if !self.named_window.is_empty() {
467 SpaceOrNewline.fmt(f)?;
468 f.write_str("WINDOW")?;
469 SpaceOrNewline.fmt(f)?;
470 display_comma_separated(&self.named_window).fmt(f)?;
471 }
472 if let Some(ref qualify) = self.qualify {
473 SpaceOrNewline.fmt(f)?;
474 f.write_str("QUALIFY")?;
475 SpaceOrNewline.fmt(f)?;
476 qualify.fmt(f)?;
477 }
478 } else {
479 if let Some(ref qualify) = self.qualify {
480 SpaceOrNewline.fmt(f)?;
481 f.write_str("QUALIFY")?;
482 SpaceOrNewline.fmt(f)?;
483 qualify.fmt(f)?;
484 }
485 if !self.named_window.is_empty() {
486 SpaceOrNewline.fmt(f)?;
487 f.write_str("WINDOW")?;
488 SpaceOrNewline.fmt(f)?;
489 display_comma_separated(&self.named_window).fmt(f)?;
490 }
491 }
492 if let Some(ref connect_by) = self.connect_by {
493 SpaceOrNewline.fmt(f)?;
494 connect_by.fmt(f)?;
495 }
496 Ok(())
497 }
498}
499
500#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
502#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
503#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
504pub struct LateralView {
505 pub lateral_view: Expr,
507 pub lateral_view_name: ObjectName,
509 pub lateral_col_alias: Vec<Ident>,
511 pub outer: bool,
513}
514
515impl fmt::Display for LateralView {
516 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
517 write!(
518 f,
519 " LATERAL VIEW{outer} {} {}",
520 self.lateral_view,
521 self.lateral_view_name,
522 outer = if self.outer { " OUTER" } else { "" }
523 )?;
524 if !self.lateral_col_alias.is_empty() {
525 write!(
526 f,
527 " AS {}",
528 display_comma_separated(&self.lateral_col_alias)
529 )?;
530 }
531 Ok(())
532 }
533}
534
535#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
541#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
542#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
543pub enum NamedWindowExpr {
544 NamedWindow(Ident),
554 WindowSpec(WindowSpec),
561}
562
563impl fmt::Display for NamedWindowExpr {
564 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
565 match self {
566 NamedWindowExpr::NamedWindow(named_window) => {
567 write!(f, "{named_window}")?;
568 }
569 NamedWindowExpr::WindowSpec(window_spec) => {
570 write!(f, "({window_spec})")?;
571 }
572 };
573 Ok(())
574 }
575}
576
577#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
578#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
579#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
580pub struct NamedWindowDefinition(pub Ident, pub NamedWindowExpr);
581
582impl fmt::Display for NamedWindowDefinition {
583 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
584 write!(f, "{} AS {}", self.0, self.1)
585 }
586}
587
588#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
589#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
590#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
591pub struct With {
592 pub with_token: AttachedToken,
594 pub recursive: bool,
595 pub cte_tables: Vec<Cte>,
596}
597
598impl fmt::Display for With {
599 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
600 f.write_str("WITH ")?;
601 if self.recursive {
602 f.write_str("RECURSIVE ")?;
603 }
604 display_comma_separated(&self.cte_tables).fmt(f)?;
605 Ok(())
606 }
607}
608
609#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
610#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
611#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
612pub enum CteAsMaterialized {
613 Materialized,
615 NotMaterialized,
617}
618
619impl fmt::Display for CteAsMaterialized {
620 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
621 match *self {
622 CteAsMaterialized::Materialized => {
623 write!(f, "MATERIALIZED")?;
624 }
625 CteAsMaterialized::NotMaterialized => {
626 write!(f, "NOT MATERIALIZED")?;
627 }
628 };
629 Ok(())
630 }
631}
632
633#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
638#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
639#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
640pub struct Cte {
641 pub alias: TableAlias,
642 pub query: Box<Query>,
643 pub from: Option<Ident>,
644 pub materialized: Option<CteAsMaterialized>,
645 pub closing_paren_token: AttachedToken,
647}
648
649impl fmt::Display for Cte {
650 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
651 match self.materialized.as_ref() {
652 None => {
653 self.alias.fmt(f)?;
654 f.write_str(" AS (")?;
655 NewLine.fmt(f)?;
656 Indent(&self.query).fmt(f)?;
657 NewLine.fmt(f)?;
658 f.write_str(")")?;
659 }
660 Some(materialized) => {
661 self.alias.fmt(f)?;
662 f.write_str(" AS ")?;
663 materialized.fmt(f)?;
664 f.write_str(" (")?;
665 NewLine.fmt(f)?;
666 Indent(&self.query).fmt(f)?;
667 NewLine.fmt(f)?;
668 f.write_str(")")?;
669 }
670 };
671 if let Some(ref fr) = self.from {
672 write!(f, " FROM {fr}")?;
673 }
674 Ok(())
675 }
676}
677
678#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
681#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
682#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
683pub enum SelectItemQualifiedWildcardKind {
684 ObjectName(ObjectName),
687 Expr(Expr),
690}
691
692#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
694#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
695#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
696pub enum SelectItem {
697 UnnamedExpr(Expr),
699 ExprWithAlias { expr: Expr, alias: Ident },
701 QualifiedWildcard(SelectItemQualifiedWildcardKind, WildcardAdditionalOptions),
704 Wildcard(WildcardAdditionalOptions),
706}
707
708impl fmt::Display for SelectItemQualifiedWildcardKind {
709 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
710 match &self {
711 SelectItemQualifiedWildcardKind::ObjectName(object_name) => {
712 write!(f, "{object_name}.*")
713 }
714 SelectItemQualifiedWildcardKind::Expr(expr) => write!(f, "{expr}.*"),
715 }
716 }
717}
718
719#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
726#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
727#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
728pub struct IdentWithAlias {
729 pub ident: Ident,
730 pub alias: Ident,
731}
732
733impl fmt::Display for IdentWithAlias {
734 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
735 write!(f, "{} AS {}", self.ident, self.alias)
736 }
737}
738
739#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
741#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
742#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
743pub struct WildcardAdditionalOptions {
744 pub wildcard_token: AttachedToken,
746 pub opt_ilike: Option<IlikeSelectItem>,
749 pub opt_exclude: Option<ExcludeSelectItem>,
751 pub opt_except: Option<ExceptSelectItem>,
754 pub opt_replace: Option<ReplaceSelectItem>,
759 pub opt_rename: Option<RenameSelectItem>,
761}
762
763impl Default for WildcardAdditionalOptions {
764 fn default() -> Self {
765 Self {
766 wildcard_token: TokenWithSpan::wrap(Token::Mul).into(),
767 opt_ilike: None,
768 opt_exclude: None,
769 opt_except: None,
770 opt_replace: None,
771 opt_rename: None,
772 }
773 }
774}
775
776impl fmt::Display for WildcardAdditionalOptions {
777 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
778 if let Some(ilike) = &self.opt_ilike {
779 write!(f, " {ilike}")?;
780 }
781 if let Some(exclude) = &self.opt_exclude {
782 write!(f, " {exclude}")?;
783 }
784 if let Some(except) = &self.opt_except {
785 write!(f, " {except}")?;
786 }
787 if let Some(replace) = &self.opt_replace {
788 write!(f, " {replace}")?;
789 }
790 if let Some(rename) = &self.opt_rename {
791 write!(f, " {rename}")?;
792 }
793 Ok(())
794 }
795}
796
797#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
804#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
805#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
806pub struct IlikeSelectItem {
807 pub pattern: String,
808}
809
810impl fmt::Display for IlikeSelectItem {
811 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
812 write!(
813 f,
814 "ILIKE '{}'",
815 value::escape_single_quote_string(&self.pattern)
816 )?;
817 Ok(())
818 }
819}
820#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
828#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
829#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
830pub enum ExcludeSelectItem {
831 Single(Ident),
838 Multiple(Vec<Ident>),
844}
845
846impl fmt::Display for ExcludeSelectItem {
847 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
848 write!(f, "EXCLUDE")?;
849 match self {
850 Self::Single(column) => {
851 write!(f, " {column}")?;
852 }
853 Self::Multiple(columns) => {
854 write!(f, " ({})", display_comma_separated(columns))?;
855 }
856 }
857 Ok(())
858 }
859}
860
861#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
869#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
870#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
871pub enum RenameSelectItem {
872 Single(IdentWithAlias),
879 Multiple(Vec<IdentWithAlias>),
885}
886
887impl fmt::Display for RenameSelectItem {
888 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
889 write!(f, "RENAME")?;
890 match self {
891 Self::Single(column) => {
892 write!(f, " {column}")?;
893 }
894 Self::Multiple(columns) => {
895 write!(f, " ({})", display_comma_separated(columns))?;
896 }
897 }
898 Ok(())
899 }
900}
901
902#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
909#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
910#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
911pub struct ExceptSelectItem {
912 pub first_element: Ident,
914 pub additional_elements: Vec<Ident>,
916}
917
918impl fmt::Display for ExceptSelectItem {
919 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
920 write!(f, "EXCEPT ")?;
921 if self.additional_elements.is_empty() {
922 write!(f, "({})", self.first_element)?;
923 } else {
924 write!(
925 f,
926 "({}, {})",
927 self.first_element,
928 display_comma_separated(&self.additional_elements)
929 )?;
930 }
931 Ok(())
932 }
933}
934
935#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
943#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
944#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
945pub struct ReplaceSelectItem {
946 pub items: Vec<Box<ReplaceSelectElement>>,
947}
948
949impl fmt::Display for ReplaceSelectItem {
950 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
951 write!(f, "REPLACE")?;
952 write!(f, " ({})", display_comma_separated(&self.items))?;
953 Ok(())
954 }
955}
956
957#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
962#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
963#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
964pub struct ReplaceSelectElement {
965 pub expr: Expr,
966 pub column_name: Ident,
967 pub as_keyword: bool,
968}
969
970impl fmt::Display for ReplaceSelectElement {
971 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
972 if self.as_keyword {
973 write!(f, "{} AS {}", self.expr, self.column_name)
974 } else {
975 write!(f, "{} {}", self.expr, self.column_name)
976 }
977 }
978}
979
980impl fmt::Display for SelectItem {
981 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
982 use core::fmt::Write;
983 match &self {
984 SelectItem::UnnamedExpr(expr) => expr.fmt(f),
985 SelectItem::ExprWithAlias { expr, alias } => {
986 expr.fmt(f)?;
987 f.write_str(" AS ")?;
988 alias.fmt(f)
989 }
990 SelectItem::QualifiedWildcard(kind, additional_options) => {
991 kind.fmt(f)?;
992 additional_options.fmt(f)
993 }
994 SelectItem::Wildcard(additional_options) => {
995 f.write_char('*')?;
996 additional_options.fmt(f)
997 }
998 }
999 }
1000}
1001
1002#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1003#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1004#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1005pub struct TableWithJoins {
1006 pub relation: TableFactor,
1007 pub joins: Vec<Join>,
1008}
1009
1010impl fmt::Display for TableWithJoins {
1011 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1012 self.relation.fmt(f)?;
1013 for join in &self.joins {
1014 SpaceOrNewline.fmt(f)?;
1015 join.fmt(f)?;
1016 }
1017 Ok(())
1018 }
1019}
1020
1021#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1025#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1026#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1027pub struct ConnectBy {
1028 pub condition: Expr,
1030 pub relationships: Vec<Expr>,
1032}
1033
1034impl fmt::Display for ConnectBy {
1035 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1036 write!(
1037 f,
1038 "START WITH {condition} CONNECT BY {relationships}",
1039 condition = self.condition,
1040 relationships = display_comma_separated(&self.relationships)
1041 )
1042 }
1043}
1044
1045#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1046#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1047#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1048pub struct Setting {
1049 pub key: Ident,
1050 pub value: Value,
1051}
1052
1053impl fmt::Display for Setting {
1054 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1055 write!(f, "{} = {}", self.key, self.value)
1056 }
1057}
1058
1059#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1066#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1067#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1068pub struct ExprWithAlias {
1069 pub expr: Expr,
1070 pub alias: Option<Ident>,
1071}
1072
1073impl fmt::Display for ExprWithAlias {
1074 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1075 let ExprWithAlias { expr, alias } = self;
1076 write!(f, "{expr}")?;
1077 if let Some(alias) = alias {
1078 write!(f, " AS {alias}")?;
1079 }
1080 Ok(())
1081 }
1082}
1083
1084#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1091#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1092#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1093pub struct ExprWithAliasAndOrderBy {
1094 pub expr: ExprWithAlias,
1095 pub order_by: OrderByOptions,
1096}
1097
1098impl fmt::Display for ExprWithAliasAndOrderBy {
1099 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1100 write!(f, "{}{}", self.expr, self.order_by)
1101 }
1102}
1103
1104#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1106#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1107#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1108pub struct TableFunctionArgs {
1109 pub args: Vec<FunctionArg>,
1110 pub settings: Option<Vec<Setting>>,
1115}
1116
1117#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1118#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1119#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1120pub enum TableIndexHintType {
1121 Use,
1122 Ignore,
1123 Force,
1124}
1125
1126impl fmt::Display for TableIndexHintType {
1127 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1128 f.write_str(match self {
1129 TableIndexHintType::Use => "USE",
1130 TableIndexHintType::Ignore => "IGNORE",
1131 TableIndexHintType::Force => "FORCE",
1132 })
1133 }
1134}
1135
1136#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1137#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1138#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1139pub enum TableIndexType {
1140 Index,
1141 Key,
1142}
1143
1144impl fmt::Display for TableIndexType {
1145 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1146 f.write_str(match self {
1147 TableIndexType::Index => "INDEX",
1148 TableIndexType::Key => "KEY",
1149 })
1150 }
1151}
1152
1153#[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 enum TableIndexHintForClause {
1157 Join,
1158 OrderBy,
1159 GroupBy,
1160}
1161
1162impl fmt::Display for TableIndexHintForClause {
1163 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1164 f.write_str(match self {
1165 TableIndexHintForClause::Join => "JOIN",
1166 TableIndexHintForClause::OrderBy => "ORDER BY",
1167 TableIndexHintForClause::GroupBy => "GROUP BY",
1168 })
1169 }
1170}
1171
1172#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1173#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1174#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1175pub struct TableIndexHints {
1176 pub hint_type: TableIndexHintType,
1177 pub index_type: TableIndexType,
1178 pub for_clause: Option<TableIndexHintForClause>,
1179 pub index_names: Vec<Ident>,
1180}
1181
1182impl fmt::Display for TableIndexHints {
1183 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1184 write!(f, "{} {} ", self.hint_type, self.index_type)?;
1185 if let Some(for_clause) = &self.for_clause {
1186 write!(f, "FOR {} ", for_clause)?;
1187 }
1188 write!(f, "({})", display_comma_separated(&self.index_names))
1189 }
1190}
1191
1192#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1194#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1195#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1196#[cfg_attr(feature = "visitor", visit(with = "visit_table_factor"))]
1197pub enum TableFactor {
1198 Table {
1199 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
1200 name: ObjectName,
1201 alias: Option<TableAlias>,
1202 args: Option<TableFunctionArgs>,
1210 with_hints: Vec<Expr>,
1212 version: Option<TableVersion>,
1215 with_ordinality: bool,
1219 partitions: Vec<Ident>,
1221 json_path: Option<JsonPath>,
1223 sample: Option<TableSampleKind>,
1226 index_hints: Vec<TableIndexHints>,
1229 },
1230 Derived {
1231 lateral: bool,
1232 subquery: Box<Query>,
1233 alias: Option<TableAlias>,
1234 },
1235 TableFunction {
1237 expr: Expr,
1238 alias: Option<TableAlias>,
1239 },
1240 Function {
1242 lateral: bool,
1243 name: ObjectName,
1244 args: Vec<FunctionArg>,
1245 alias: Option<TableAlias>,
1246 },
1247 UNNEST {
1258 alias: Option<TableAlias>,
1259 array_exprs: Vec<Expr>,
1260 with_offset: bool,
1261 with_offset_alias: Option<Ident>,
1262 with_ordinality: bool,
1263 },
1264 JsonTable {
1280 json_expr: Expr,
1282 json_path: Value,
1285 columns: Vec<JsonTableColumn>,
1288 alias: Option<TableAlias>,
1290 },
1291 OpenJsonTable {
1301 json_expr: Expr,
1303 json_path: Option<Value>,
1306 columns: Vec<OpenJsonTableColumn>,
1309 alias: Option<TableAlias>,
1311 },
1312 NestedJoin {
1319 table_with_joins: Box<TableWithJoins>,
1320 alias: Option<TableAlias>,
1321 },
1322 Pivot {
1328 table: Box<TableFactor>,
1329 aggregate_functions: Vec<ExprWithAlias>, value_column: Vec<Ident>,
1331 value_source: PivotValueSource,
1332 default_on_null: Option<Expr>,
1333 alias: Option<TableAlias>,
1334 },
1335 Unpivot {
1344 table: Box<TableFactor>,
1345 value: Ident,
1346 name: Ident,
1347 columns: Vec<Ident>,
1348 null_inclusion: Option<NullInclusion>,
1349 alias: Option<TableAlias>,
1350 },
1351 MatchRecognize {
1355 table: Box<TableFactor>,
1356 partition_by: Vec<Expr>,
1358 order_by: Vec<OrderByExpr>,
1360 measures: Vec<Measure>,
1362 rows_per_match: Option<RowsPerMatch>,
1364 after_match_skip: Option<AfterMatchSkip>,
1366 pattern: MatchRecognizePattern,
1368 symbols: Vec<SymbolDefinition>,
1370 alias: Option<TableAlias>,
1371 },
1372 XmlTable {
1392 namespaces: Vec<XmlNamespaceDefinition>,
1394 row_expression: Expr,
1396 passing: XmlPassingClause,
1398 columns: Vec<XmlTableColumn>,
1400 alias: Option<TableAlias>,
1402 },
1403}
1404
1405#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1407#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1408#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1409pub enum TableSampleKind {
1410 BeforeTableAlias(Box<TableSample>),
1412 AfterTableAlias(Box<TableSample>),
1414}
1415
1416#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1417#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1418#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1419pub struct TableSample {
1420 pub modifier: TableSampleModifier,
1421 pub name: Option<TableSampleMethod>,
1422 pub quantity: Option<TableSampleQuantity>,
1423 pub seed: Option<TableSampleSeed>,
1424 pub bucket: Option<TableSampleBucket>,
1425 pub offset: Option<Expr>,
1426}
1427
1428#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1429#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1430#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1431pub enum TableSampleModifier {
1432 Sample,
1433 TableSample,
1434}
1435
1436impl fmt::Display for TableSampleModifier {
1437 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1438 match self {
1439 TableSampleModifier::Sample => write!(f, "SAMPLE")?,
1440 TableSampleModifier::TableSample => write!(f, "TABLESAMPLE")?,
1441 }
1442 Ok(())
1443 }
1444}
1445
1446#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1447#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1448#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1449pub struct TableSampleQuantity {
1450 pub parenthesized: bool,
1451 pub value: Expr,
1452 pub unit: Option<TableSampleUnit>,
1453}
1454
1455impl fmt::Display for TableSampleQuantity {
1456 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1457 if self.parenthesized {
1458 write!(f, "(")?;
1459 }
1460 write!(f, "{}", self.value)?;
1461 if let Some(unit) = &self.unit {
1462 write!(f, " {}", unit)?;
1463 }
1464 if self.parenthesized {
1465 write!(f, ")")?;
1466 }
1467 Ok(())
1468 }
1469}
1470
1471#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1473#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1474#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1475pub enum TableSampleMethod {
1476 Row,
1477 Bernoulli,
1478 System,
1479 Block,
1480}
1481
1482impl fmt::Display for TableSampleMethod {
1483 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1484 match self {
1485 TableSampleMethod::Bernoulli => write!(f, "BERNOULLI"),
1486 TableSampleMethod::Row => write!(f, "ROW"),
1487 TableSampleMethod::System => write!(f, "SYSTEM"),
1488 TableSampleMethod::Block => write!(f, "BLOCK"),
1489 }
1490 }
1491}
1492
1493#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1494#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1495#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1496pub struct TableSampleSeed {
1497 pub modifier: TableSampleSeedModifier,
1498 pub value: Value,
1499}
1500
1501impl fmt::Display for TableSampleSeed {
1502 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1503 write!(f, "{} ({})", self.modifier, self.value)?;
1504 Ok(())
1505 }
1506}
1507
1508#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1509#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1510#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1511pub enum TableSampleSeedModifier {
1512 Repeatable,
1513 Seed,
1514}
1515
1516impl fmt::Display for TableSampleSeedModifier {
1517 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1518 match self {
1519 TableSampleSeedModifier::Repeatable => write!(f, "REPEATABLE"),
1520 TableSampleSeedModifier::Seed => write!(f, "SEED"),
1521 }
1522 }
1523}
1524
1525#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1526#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1527#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1528pub enum TableSampleUnit {
1529 Rows,
1530 Percent,
1531}
1532
1533impl fmt::Display for TableSampleUnit {
1534 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1535 match self {
1536 TableSampleUnit::Percent => write!(f, "PERCENT"),
1537 TableSampleUnit::Rows => write!(f, "ROWS"),
1538 }
1539 }
1540}
1541
1542#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1543#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1544#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1545pub struct TableSampleBucket {
1546 pub bucket: Value,
1547 pub total: Value,
1548 pub on: Option<Expr>,
1549}
1550
1551impl fmt::Display for TableSampleBucket {
1552 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1553 write!(f, "BUCKET {} OUT OF {}", self.bucket, self.total)?;
1554 if let Some(on) = &self.on {
1555 write!(f, " ON {}", on)?;
1556 }
1557 Ok(())
1558 }
1559}
1560impl fmt::Display for TableSample {
1561 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1562 write!(f, "{}", self.modifier)?;
1563 if let Some(name) = &self.name {
1564 write!(f, " {}", name)?;
1565 }
1566 if let Some(quantity) = &self.quantity {
1567 write!(f, " {}", quantity)?;
1568 }
1569 if let Some(seed) = &self.seed {
1570 write!(f, " {}", seed)?;
1571 }
1572 if let Some(bucket) = &self.bucket {
1573 write!(f, " ({})", bucket)?;
1574 }
1575 if let Some(offset) = &self.offset {
1576 write!(f, " OFFSET {}", offset)?;
1577 }
1578 Ok(())
1579 }
1580}
1581
1582#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1584#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1585#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1586pub enum PivotValueSource {
1587 List(Vec<ExprWithAlias>),
1591 Any(Vec<OrderByExpr>),
1595 Subquery(Box<Query>),
1599}
1600
1601impl fmt::Display for PivotValueSource {
1602 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1603 match self {
1604 PivotValueSource::List(values) => write!(f, "{}", display_comma_separated(values)),
1605 PivotValueSource::Any(order_by) => {
1606 write!(f, "ANY")?;
1607 if !order_by.is_empty() {
1608 write!(f, " ORDER BY {}", display_comma_separated(order_by))?;
1609 }
1610 Ok(())
1611 }
1612 PivotValueSource::Subquery(query) => write!(f, "{query}"),
1613 }
1614 }
1615}
1616
1617#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1621#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1622#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1623pub struct Measure {
1624 pub expr: Expr,
1625 pub alias: Ident,
1626}
1627
1628impl fmt::Display for Measure {
1629 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1630 write!(f, "{} AS {}", self.expr, self.alias)
1631 }
1632}
1633
1634#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1638#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1639#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1640pub enum RowsPerMatch {
1641 OneRow,
1643 AllRows(Option<EmptyMatchesMode>),
1645}
1646
1647impl fmt::Display for RowsPerMatch {
1648 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1649 match self {
1650 RowsPerMatch::OneRow => write!(f, "ONE ROW PER MATCH"),
1651 RowsPerMatch::AllRows(mode) => {
1652 write!(f, "ALL ROWS PER MATCH")?;
1653 if let Some(mode) = mode {
1654 write!(f, " {}", mode)?;
1655 }
1656 Ok(())
1657 }
1658 }
1659 }
1660}
1661
1662#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1666#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1667#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1668pub enum AfterMatchSkip {
1669 PastLastRow,
1671 ToNextRow,
1673 ToFirst(Ident),
1675 ToLast(Ident),
1677}
1678
1679impl fmt::Display for AfterMatchSkip {
1680 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1681 write!(f, "AFTER MATCH SKIP ")?;
1682 match self {
1683 AfterMatchSkip::PastLastRow => write!(f, "PAST LAST ROW"),
1684 AfterMatchSkip::ToNextRow => write!(f, " TO NEXT ROW"),
1685 AfterMatchSkip::ToFirst(symbol) => write!(f, "TO FIRST {symbol}"),
1686 AfterMatchSkip::ToLast(symbol) => write!(f, "TO LAST {symbol}"),
1687 }
1688 }
1689}
1690
1691#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1692#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1693#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1694pub enum EmptyMatchesMode {
1695 Show,
1697 Omit,
1699 WithUnmatched,
1701}
1702
1703impl fmt::Display for EmptyMatchesMode {
1704 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1705 match self {
1706 EmptyMatchesMode::Show => write!(f, "SHOW EMPTY MATCHES"),
1707 EmptyMatchesMode::Omit => write!(f, "OMIT EMPTY MATCHES"),
1708 EmptyMatchesMode::WithUnmatched => write!(f, "WITH UNMATCHED ROWS"),
1709 }
1710 }
1711}
1712
1713#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1717#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1718#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1719pub struct SymbolDefinition {
1720 pub symbol: Ident,
1721 pub definition: Expr,
1722}
1723
1724impl fmt::Display for SymbolDefinition {
1725 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1726 write!(f, "{} AS {}", self.symbol, self.definition)
1727 }
1728}
1729
1730#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1732#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1733#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1734pub enum MatchRecognizeSymbol {
1735 Named(Ident),
1737 Start,
1739 End,
1741}
1742
1743impl fmt::Display for MatchRecognizeSymbol {
1744 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1745 match self {
1746 MatchRecognizeSymbol::Named(symbol) => write!(f, "{symbol}"),
1747 MatchRecognizeSymbol::Start => write!(f, "^"),
1748 MatchRecognizeSymbol::End => write!(f, "$"),
1749 }
1750 }
1751}
1752
1753#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1757#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1758#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1759pub enum MatchRecognizePattern {
1760 Symbol(MatchRecognizeSymbol),
1762 Exclude(MatchRecognizeSymbol),
1764 Permute(Vec<MatchRecognizeSymbol>),
1766 Concat(Vec<MatchRecognizePattern>),
1768 Group(Box<MatchRecognizePattern>),
1770 Alternation(Vec<MatchRecognizePattern>),
1772 Repetition(Box<MatchRecognizePattern>, RepetitionQuantifier),
1774}
1775
1776impl fmt::Display for MatchRecognizePattern {
1777 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1778 use MatchRecognizePattern::*;
1779 match self {
1780 Symbol(symbol) => write!(f, "{}", symbol),
1781 Exclude(symbol) => write!(f, "{{- {symbol} -}}"),
1782 Permute(symbols) => write!(f, "PERMUTE({})", display_comma_separated(symbols)),
1783 Concat(patterns) => write!(f, "{}", display_separated(patterns, " ")),
1784 Group(pattern) => write!(f, "( {pattern} )"),
1785 Alternation(patterns) => write!(f, "{}", display_separated(patterns, " | ")),
1786 Repetition(pattern, op) => write!(f, "{pattern}{op}"),
1787 }
1788 }
1789}
1790
1791#[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 RepetitionQuantifier {
1797 ZeroOrMore,
1799 OneOrMore,
1801 AtMostOne,
1803 Exactly(u32),
1805 AtLeast(u32),
1807 AtMost(u32),
1809 Range(u32, u32),
1811}
1812
1813impl fmt::Display for RepetitionQuantifier {
1814 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1815 use RepetitionQuantifier::*;
1816 match self {
1817 ZeroOrMore => write!(f, "*"),
1818 OneOrMore => write!(f, "+"),
1819 AtMostOne => write!(f, "?"),
1820 Exactly(n) => write!(f, "{{{n}}}"),
1821 AtLeast(n) => write!(f, "{{{n},}}"),
1822 AtMost(n) => write!(f, "{{,{n}}}"),
1823 Range(n, m) => write!(f, "{{{n},{m}}}"),
1824 }
1825 }
1826}
1827
1828impl fmt::Display for TableFactor {
1829 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1830 match self {
1831 TableFactor::Table {
1832 name,
1833 alias,
1834 args,
1835 with_hints,
1836 version,
1837 partitions,
1838 with_ordinality,
1839 json_path,
1840 sample,
1841 index_hints,
1842 } => {
1843 name.fmt(f)?;
1844 if let Some(json_path) = json_path {
1845 json_path.fmt(f)?;
1846 }
1847 if !partitions.is_empty() {
1848 write!(f, "PARTITION ({})", display_comma_separated(partitions))?;
1849 }
1850 if let Some(args) = args {
1851 write!(f, "(")?;
1852 write!(f, "{}", display_comma_separated(&args.args))?;
1853 if let Some(ref settings) = args.settings {
1854 if !args.args.is_empty() {
1855 write!(f, ", ")?;
1856 }
1857 write!(f, "SETTINGS {}", display_comma_separated(settings))?;
1858 }
1859 write!(f, ")")?;
1860 }
1861 if *with_ordinality {
1862 write!(f, " WITH ORDINALITY")?;
1863 }
1864 if let Some(TableSampleKind::BeforeTableAlias(sample)) = sample {
1865 write!(f, " {sample}")?;
1866 }
1867 if let Some(alias) = alias {
1868 write!(f, " AS {alias}")?;
1869 }
1870 if !index_hints.is_empty() {
1871 write!(f, " {}", display_separated(index_hints, " "))?;
1872 }
1873 if !with_hints.is_empty() {
1874 write!(f, " WITH ({})", display_comma_separated(with_hints))?;
1875 }
1876 if let Some(version) = version {
1877 write!(f, "{version}")?;
1878 }
1879 if let Some(TableSampleKind::AfterTableAlias(sample)) = sample {
1880 write!(f, " {sample}")?;
1881 }
1882 Ok(())
1883 }
1884 TableFactor::Derived {
1885 lateral,
1886 subquery,
1887 alias,
1888 } => {
1889 if *lateral {
1890 write!(f, "LATERAL ")?;
1891 }
1892 f.write_str("(")?;
1893 NewLine.fmt(f)?;
1894 Indent(subquery).fmt(f)?;
1895 NewLine.fmt(f)?;
1896 f.write_str(")")?;
1897 if let Some(alias) = alias {
1898 write!(f, " AS {alias}")?;
1899 }
1900 Ok(())
1901 }
1902 TableFactor::Function {
1903 lateral,
1904 name,
1905 args,
1906 alias,
1907 } => {
1908 if *lateral {
1909 write!(f, "LATERAL ")?;
1910 }
1911 write!(f, "{name}")?;
1912 write!(f, "({})", display_comma_separated(args))?;
1913 if let Some(alias) = alias {
1914 write!(f, " AS {alias}")?;
1915 }
1916 Ok(())
1917 }
1918 TableFactor::TableFunction { expr, alias } => {
1919 write!(f, "TABLE({expr})")?;
1920 if let Some(alias) = alias {
1921 write!(f, " AS {alias}")?;
1922 }
1923 Ok(())
1924 }
1925 TableFactor::UNNEST {
1926 alias,
1927 array_exprs,
1928 with_offset,
1929 with_offset_alias,
1930 with_ordinality,
1931 } => {
1932 write!(f, "UNNEST({})", display_comma_separated(array_exprs))?;
1933
1934 if *with_ordinality {
1935 write!(f, " WITH ORDINALITY")?;
1936 }
1937
1938 if let Some(alias) = alias {
1939 write!(f, " AS {alias}")?;
1940 }
1941 if *with_offset {
1942 write!(f, " WITH OFFSET")?;
1943 }
1944 if let Some(alias) = with_offset_alias {
1945 write!(f, " AS {alias}")?;
1946 }
1947 Ok(())
1948 }
1949 TableFactor::JsonTable {
1950 json_expr,
1951 json_path,
1952 columns,
1953 alias,
1954 } => {
1955 write!(
1956 f,
1957 "JSON_TABLE({json_expr}, {json_path} COLUMNS({columns}))",
1958 columns = display_comma_separated(columns)
1959 )?;
1960 if let Some(alias) = alias {
1961 write!(f, " AS {alias}")?;
1962 }
1963 Ok(())
1964 }
1965 TableFactor::OpenJsonTable {
1966 json_expr,
1967 json_path,
1968 columns,
1969 alias,
1970 } => {
1971 write!(f, "OPENJSON({json_expr}")?;
1972 if let Some(json_path) = json_path {
1973 write!(f, ", {json_path}")?;
1974 }
1975 write!(f, ")")?;
1976 if !columns.is_empty() {
1977 write!(f, " WITH ({})", display_comma_separated(columns))?;
1978 }
1979 if let Some(alias) = alias {
1980 write!(f, " AS {alias}")?;
1981 }
1982 Ok(())
1983 }
1984 TableFactor::NestedJoin {
1985 table_with_joins,
1986 alias,
1987 } => {
1988 write!(f, "({table_with_joins})")?;
1989 if let Some(alias) = alias {
1990 write!(f, " AS {alias}")?;
1991 }
1992 Ok(())
1993 }
1994 TableFactor::Pivot {
1995 table,
1996 aggregate_functions,
1997 value_column,
1998 value_source,
1999 default_on_null,
2000 alias,
2001 } => {
2002 write!(
2003 f,
2004 "{table} PIVOT({} FOR {} IN ({value_source})",
2005 display_comma_separated(aggregate_functions),
2006 Expr::CompoundIdentifier(value_column.to_vec()),
2007 )?;
2008 if let Some(expr) = default_on_null {
2009 write!(f, " DEFAULT ON NULL ({expr})")?;
2010 }
2011 write!(f, ")")?;
2012 if alias.is_some() {
2013 write!(f, " AS {}", alias.as_ref().unwrap())?;
2014 }
2015 Ok(())
2016 }
2017 TableFactor::Unpivot {
2018 table,
2019 null_inclusion,
2020 value,
2021 name,
2022 columns,
2023 alias,
2024 } => {
2025 write!(f, "{table} UNPIVOT")?;
2026 if let Some(null_inclusion) = null_inclusion {
2027 write!(f, " {null_inclusion} ")?;
2028 }
2029 write!(
2030 f,
2031 "({} FOR {} IN ({}))",
2032 value,
2033 name,
2034 display_comma_separated(columns)
2035 )?;
2036 if alias.is_some() {
2037 write!(f, " AS {}", alias.as_ref().unwrap())?;
2038 }
2039 Ok(())
2040 }
2041 TableFactor::MatchRecognize {
2042 table,
2043 partition_by,
2044 order_by,
2045 measures,
2046 rows_per_match,
2047 after_match_skip,
2048 pattern,
2049 symbols,
2050 alias,
2051 } => {
2052 write!(f, "{table} MATCH_RECOGNIZE(")?;
2053 if !partition_by.is_empty() {
2054 write!(f, "PARTITION BY {} ", display_comma_separated(partition_by))?;
2055 }
2056 if !order_by.is_empty() {
2057 write!(f, "ORDER BY {} ", display_comma_separated(order_by))?;
2058 }
2059 if !measures.is_empty() {
2060 write!(f, "MEASURES {} ", display_comma_separated(measures))?;
2061 }
2062 if let Some(rows_per_match) = rows_per_match {
2063 write!(f, "{rows_per_match} ")?;
2064 }
2065 if let Some(after_match_skip) = after_match_skip {
2066 write!(f, "{after_match_skip} ")?;
2067 }
2068 write!(f, "PATTERN ({pattern}) ")?;
2069 write!(f, "DEFINE {})", display_comma_separated(symbols))?;
2070 if alias.is_some() {
2071 write!(f, " AS {}", alias.as_ref().unwrap())?;
2072 }
2073 Ok(())
2074 }
2075 TableFactor::XmlTable {
2076 row_expression,
2077 passing,
2078 columns,
2079 alias,
2080 namespaces,
2081 } => {
2082 write!(f, "XMLTABLE(")?;
2083 if !namespaces.is_empty() {
2084 write!(
2085 f,
2086 "XMLNAMESPACES({}), ",
2087 display_comma_separated(namespaces)
2088 )?;
2089 }
2090 write!(
2091 f,
2092 "{row_expression}{passing} COLUMNS {columns})",
2093 columns = display_comma_separated(columns)
2094 )?;
2095 if let Some(alias) = alias {
2096 write!(f, " AS {alias}")?;
2097 }
2098 Ok(())
2099 }
2100 }
2101 }
2102}
2103
2104#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2105#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2106#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2107pub struct TableAlias {
2108 pub name: Ident,
2109 pub columns: Vec<TableAliasColumnDef>,
2110}
2111
2112impl fmt::Display for TableAlias {
2113 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2114 write!(f, "{}", self.name)?;
2115 if !self.columns.is_empty() {
2116 write!(f, " ({})", display_comma_separated(&self.columns))?;
2117 }
2118 Ok(())
2119 }
2120}
2121
2122#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2128#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2129#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2130pub struct TableAliasColumnDef {
2131 pub name: Ident,
2133 pub data_type: Option<DataType>,
2135}
2136
2137impl TableAliasColumnDef {
2138 pub fn from_name<S: Into<String>>(name: S) -> Self {
2140 TableAliasColumnDef {
2141 name: Ident::new(name),
2142 data_type: None,
2143 }
2144 }
2145}
2146
2147impl fmt::Display for TableAliasColumnDef {
2148 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2149 write!(f, "{}", self.name)?;
2150 if let Some(ref data_type) = self.data_type {
2151 write!(f, " {}", data_type)?;
2152 }
2153 Ok(())
2154 }
2155}
2156
2157#[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 TableVersion {
2161 ForSystemTimeAsOf(Expr),
2164 Function(Expr),
2167}
2168
2169impl Display for TableVersion {
2170 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2171 match self {
2172 TableVersion::ForSystemTimeAsOf(e) => write!(f, " FOR SYSTEM_TIME AS OF {e}")?,
2173 TableVersion::Function(func) => write!(f, " {func}")?,
2174 }
2175 Ok(())
2176 }
2177}
2178
2179#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2180#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2181#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2182pub struct Join {
2183 pub relation: TableFactor,
2184 pub global: bool,
2187 pub join_operator: JoinOperator,
2188}
2189
2190impl fmt::Display for Join {
2191 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2192 fn prefix(constraint: &JoinConstraint) -> &'static str {
2193 match constraint {
2194 JoinConstraint::Natural => "NATURAL ",
2195 _ => "",
2196 }
2197 }
2198 fn suffix(constraint: &'_ JoinConstraint) -> impl fmt::Display + '_ {
2199 struct Suffix<'a>(&'a JoinConstraint);
2200 impl fmt::Display for Suffix<'_> {
2201 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2202 match self.0 {
2203 JoinConstraint::On(expr) => write!(f, " ON {expr}"),
2204 JoinConstraint::Using(attrs) => {
2205 write!(f, " USING({})", display_comma_separated(attrs))
2206 }
2207 _ => Ok(()),
2208 }
2209 }
2210 }
2211 Suffix(constraint)
2212 }
2213 if self.global {
2214 write!(f, "GLOBAL ")?;
2215 }
2216
2217 match &self.join_operator {
2218 JoinOperator::Join(constraint) => f.write_fmt(format_args!(
2219 "{}JOIN {}{}",
2220 prefix(constraint),
2221 self.relation,
2222 suffix(constraint)
2223 )),
2224 JoinOperator::Inner(constraint) => f.write_fmt(format_args!(
2225 "{}INNER JOIN {}{}",
2226 prefix(constraint),
2227 self.relation,
2228 suffix(constraint)
2229 )),
2230 JoinOperator::Left(constraint) => f.write_fmt(format_args!(
2231 "{}LEFT JOIN {}{}",
2232 prefix(constraint),
2233 self.relation,
2234 suffix(constraint)
2235 )),
2236 JoinOperator::LeftOuter(constraint) => f.write_fmt(format_args!(
2237 "{}LEFT OUTER JOIN {}{}",
2238 prefix(constraint),
2239 self.relation,
2240 suffix(constraint)
2241 )),
2242 JoinOperator::Right(constraint) => f.write_fmt(format_args!(
2243 "{}RIGHT JOIN {}{}",
2244 prefix(constraint),
2245 self.relation,
2246 suffix(constraint)
2247 )),
2248 JoinOperator::RightOuter(constraint) => f.write_fmt(format_args!(
2249 "{}RIGHT OUTER JOIN {}{}",
2250 prefix(constraint),
2251 self.relation,
2252 suffix(constraint)
2253 )),
2254 JoinOperator::FullOuter(constraint) => f.write_fmt(format_args!(
2255 "{}FULL JOIN {}{}",
2256 prefix(constraint),
2257 self.relation,
2258 suffix(constraint)
2259 )),
2260 JoinOperator::CrossJoin => f.write_fmt(format_args!("CROSS JOIN {}", self.relation)),
2261 JoinOperator::Semi(constraint) => f.write_fmt(format_args!(
2262 "{}SEMI JOIN {}{}",
2263 prefix(constraint),
2264 self.relation,
2265 suffix(constraint)
2266 )),
2267 JoinOperator::LeftSemi(constraint) => f.write_fmt(format_args!(
2268 "{}LEFT SEMI JOIN {}{}",
2269 prefix(constraint),
2270 self.relation,
2271 suffix(constraint)
2272 )),
2273 JoinOperator::RightSemi(constraint) => f.write_fmt(format_args!(
2274 "{}RIGHT SEMI JOIN {}{}",
2275 prefix(constraint),
2276 self.relation,
2277 suffix(constraint)
2278 )),
2279 JoinOperator::Anti(constraint) => f.write_fmt(format_args!(
2280 "{}ANTI JOIN {}{}",
2281 prefix(constraint),
2282 self.relation,
2283 suffix(constraint)
2284 )),
2285 JoinOperator::LeftAnti(constraint) => f.write_fmt(format_args!(
2286 "{}LEFT ANTI JOIN {}{}",
2287 prefix(constraint),
2288 self.relation,
2289 suffix(constraint)
2290 )),
2291 JoinOperator::RightAnti(constraint) => f.write_fmt(format_args!(
2292 "{}RIGHT ANTI JOIN {}{}",
2293 prefix(constraint),
2294 self.relation,
2295 suffix(constraint)
2296 )),
2297 JoinOperator::CrossApply => f.write_fmt(format_args!("CROSS APPLY {}", self.relation)),
2298 JoinOperator::OuterApply => f.write_fmt(format_args!("OUTER APPLY {}", self.relation)),
2299 JoinOperator::AsOf {
2300 match_condition,
2301 constraint,
2302 } => f.write_fmt(format_args!(
2303 "ASOF JOIN {} MATCH_CONDITION ({match_condition}){}",
2304 self.relation,
2305 suffix(constraint)
2306 )),
2307 JoinOperator::StraightJoin(constraint) => f.write_fmt(format_args!(
2308 "STRAIGHT_JOIN {}{}",
2309 self.relation,
2310 suffix(constraint)
2311 )),
2312 }
2313 }
2314}
2315
2316#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2317#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2318#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2319pub enum JoinOperator {
2320 Join(JoinConstraint),
2321 Inner(JoinConstraint),
2322 Left(JoinConstraint),
2323 LeftOuter(JoinConstraint),
2324 Right(JoinConstraint),
2325 RightOuter(JoinConstraint),
2326 FullOuter(JoinConstraint),
2327 CrossJoin,
2328 Semi(JoinConstraint),
2330 LeftSemi(JoinConstraint),
2332 RightSemi(JoinConstraint),
2334 Anti(JoinConstraint),
2336 LeftAnti(JoinConstraint),
2338 RightAnti(JoinConstraint),
2340 CrossApply,
2342 OuterApply,
2344 AsOf {
2349 match_condition: Expr,
2350 constraint: JoinConstraint,
2351 },
2352 StraightJoin(JoinConstraint),
2356}
2357
2358#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2359#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2360#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2361pub enum JoinConstraint {
2362 On(Expr),
2363 Using(Vec<ObjectName>),
2364 Natural,
2365 None,
2366}
2367
2368#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2369#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2370#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2371pub enum OrderByKind {
2372 All(OrderByOptions),
2377
2378 Expressions(Vec<OrderByExpr>),
2380}
2381
2382#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2383#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2384#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2385pub struct OrderBy {
2386 pub kind: OrderByKind,
2387
2388 pub interpolate: Option<Interpolate>,
2391}
2392
2393impl fmt::Display for OrderBy {
2394 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2395 write!(f, "ORDER BY")?;
2396 match &self.kind {
2397 OrderByKind::Expressions(exprs) => {
2398 write!(f, " {}", display_comma_separated(exprs))?;
2399 }
2400 OrderByKind::All(all) => {
2401 write!(f, " ALL{}", all)?;
2402 }
2403 }
2404
2405 if let Some(ref interpolate) = self.interpolate {
2406 match &interpolate.exprs {
2407 Some(exprs) => write!(f, " INTERPOLATE ({})", display_comma_separated(exprs))?,
2408 None => write!(f, " INTERPOLATE")?,
2409 }
2410 }
2411
2412 Ok(())
2413 }
2414}
2415
2416#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2418#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2419#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2420pub struct OrderByExpr {
2421 pub expr: Expr,
2422 pub options: OrderByOptions,
2423 pub with_fill: Option<WithFill>,
2426}
2427
2428impl fmt::Display for OrderByExpr {
2429 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2430 write!(f, "{}{}", self.expr, self.options)?;
2431 if let Some(ref with_fill) = self.with_fill {
2432 write!(f, " {}", with_fill)?
2433 }
2434 Ok(())
2435 }
2436}
2437
2438#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2443#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2444#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2445pub struct WithFill {
2446 pub from: Option<Expr>,
2447 pub to: Option<Expr>,
2448 pub step: Option<Expr>,
2449}
2450
2451impl fmt::Display for WithFill {
2452 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2453 write!(f, "WITH FILL")?;
2454 if let Some(ref from) = self.from {
2455 write!(f, " FROM {}", from)?;
2456 }
2457 if let Some(ref to) = self.to {
2458 write!(f, " TO {}", to)?;
2459 }
2460 if let Some(ref step) = self.step {
2461 write!(f, " STEP {}", step)?;
2462 }
2463 Ok(())
2464 }
2465}
2466
2467#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2472#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2473#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2474pub struct InterpolateExpr {
2475 pub column: Ident,
2476 pub expr: Option<Expr>,
2477}
2478
2479#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2480#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2481#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2482pub struct Interpolate {
2483 pub exprs: Option<Vec<InterpolateExpr>>,
2484}
2485
2486impl fmt::Display for InterpolateExpr {
2487 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2488 write!(f, "{}", self.column)?;
2489 if let Some(ref expr) = self.expr {
2490 write!(f, " AS {}", expr)?;
2491 }
2492 Ok(())
2493 }
2494}
2495
2496#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2497#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2498#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2499pub struct OrderByOptions {
2500 pub asc: Option<bool>,
2502 pub nulls_first: Option<bool>,
2504}
2505
2506impl fmt::Display for OrderByOptions {
2507 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2508 match self.asc {
2509 Some(true) => write!(f, " ASC")?,
2510 Some(false) => write!(f, " DESC")?,
2511 None => (),
2512 }
2513 match self.nulls_first {
2514 Some(true) => write!(f, " NULLS FIRST")?,
2515 Some(false) => write!(f, " NULLS LAST")?,
2516 None => (),
2517 }
2518 Ok(())
2519 }
2520}
2521
2522#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2523#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2524#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2525pub enum LimitClause {
2526 LimitOffset {
2530 limit: Option<Expr>,
2532 offset: Option<Offset>,
2534 limit_by: Vec<Expr>,
2538 },
2539 OffsetCommaLimit { offset: Expr, limit: Expr },
2545}
2546
2547impl fmt::Display for LimitClause {
2548 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2549 match self {
2550 LimitClause::LimitOffset {
2551 limit,
2552 limit_by,
2553 offset,
2554 } => {
2555 if let Some(ref limit) = limit {
2556 write!(f, " LIMIT {limit}")?;
2557 }
2558 if let Some(ref offset) = offset {
2559 write!(f, " {offset}")?;
2560 }
2561 if !limit_by.is_empty() {
2562 debug_assert!(limit.is_some());
2563 write!(f, " BY {}", display_separated(limit_by, ", "))?;
2564 }
2565 Ok(())
2566 }
2567 LimitClause::OffsetCommaLimit { offset, limit } => {
2568 write!(f, " LIMIT {}, {}", offset, limit)
2569 }
2570 }
2571 }
2572}
2573
2574#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2575#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2576#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2577pub struct Offset {
2578 pub value: Expr,
2579 pub rows: OffsetRows,
2580}
2581
2582impl fmt::Display for Offset {
2583 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2584 write!(f, "OFFSET {}{}", self.value, self.rows)
2585 }
2586}
2587
2588#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2590#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2591#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2592pub enum OffsetRows {
2593 None,
2595 Row,
2596 Rows,
2597}
2598
2599impl fmt::Display for OffsetRows {
2600 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2601 match self {
2602 OffsetRows::None => Ok(()),
2603 OffsetRows::Row => write!(f, " ROW"),
2604 OffsetRows::Rows => write!(f, " ROWS"),
2605 }
2606 }
2607}
2608
2609#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2621#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2622#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2623pub enum PipeOperator {
2624 Limit { expr: Expr, offset: Option<Expr> },
2630 Where { expr: Expr },
2636 OrderBy { exprs: Vec<OrderByExpr> },
2638 Select { exprs: Vec<SelectItem> },
2644 Extend { exprs: Vec<SelectItem> },
2650 Set { assignments: Vec<Assignment> },
2656 Drop { columns: Vec<Ident> },
2662 As { alias: Ident },
2668 Aggregate {
2680 full_table_exprs: Vec<ExprWithAliasAndOrderBy>,
2681 group_by_expr: Vec<ExprWithAliasAndOrderBy>,
2682 },
2683 TableSample { sample: Box<TableSample> },
2687}
2688
2689impl fmt::Display for PipeOperator {
2690 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2691 match self {
2692 PipeOperator::Select { exprs } => {
2693 write!(f, "SELECT {}", display_comma_separated(exprs.as_slice()))
2694 }
2695 PipeOperator::Extend { exprs } => {
2696 write!(f, "EXTEND {}", display_comma_separated(exprs.as_slice()))
2697 }
2698 PipeOperator::Set { assignments } => {
2699 write!(f, "SET {}", display_comma_separated(assignments.as_slice()))
2700 }
2701 PipeOperator::Drop { columns } => {
2702 write!(f, "DROP {}", display_comma_separated(columns.as_slice()))
2703 }
2704 PipeOperator::As { alias } => {
2705 write!(f, "AS {}", alias)
2706 }
2707 PipeOperator::Limit { expr, offset } => {
2708 write!(f, "LIMIT {}", expr)?;
2709 if let Some(offset) = offset {
2710 write!(f, " OFFSET {}", offset)?;
2711 }
2712 Ok(())
2713 }
2714 PipeOperator::Aggregate {
2715 full_table_exprs,
2716 group_by_expr,
2717 } => {
2718 write!(f, "AGGREGATE")?;
2719 if !full_table_exprs.is_empty() {
2720 write!(
2721 f,
2722 " {}",
2723 display_comma_separated(full_table_exprs.as_slice())
2724 )?;
2725 }
2726 if !group_by_expr.is_empty() {
2727 write!(f, " GROUP BY {}", display_comma_separated(group_by_expr))?;
2728 }
2729 Ok(())
2730 }
2731
2732 PipeOperator::Where { expr } => {
2733 write!(f, "WHERE {}", expr)
2734 }
2735 PipeOperator::OrderBy { exprs } => {
2736 write!(f, "ORDER BY {}", display_comma_separated(exprs.as_slice()))
2737 }
2738
2739 PipeOperator::TableSample { sample } => {
2740 write!(f, "{}", sample)
2741 }
2742 }
2743 }
2744}
2745
2746#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2747#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2748#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2749pub struct Fetch {
2750 pub with_ties: bool,
2751 pub percent: bool,
2752 pub quantity: Option<Expr>,
2753}
2754
2755impl fmt::Display for Fetch {
2756 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2757 let extension = if self.with_ties { "WITH TIES" } else { "ONLY" };
2758 if let Some(ref quantity) = self.quantity {
2759 let percent = if self.percent { " PERCENT" } else { "" };
2760 write!(f, "FETCH FIRST {quantity}{percent} ROWS {extension}")
2761 } else {
2762 write!(f, "FETCH FIRST ROWS {extension}")
2763 }
2764 }
2765}
2766
2767#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2768#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2769#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2770pub struct LockClause {
2771 pub lock_type: LockType,
2772 pub of: Option<ObjectName>,
2773 pub nonblock: Option<NonBlock>,
2774}
2775
2776impl fmt::Display for LockClause {
2777 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2778 write!(f, "FOR {}", &self.lock_type)?;
2779 if let Some(ref of) = self.of {
2780 write!(f, " OF {of}")?;
2781 }
2782 if let Some(ref nb) = self.nonblock {
2783 write!(f, " {nb}")?;
2784 }
2785 Ok(())
2786 }
2787}
2788
2789#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2790#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2791#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2792pub enum LockType {
2793 Share,
2794 Update,
2795}
2796
2797impl fmt::Display for LockType {
2798 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2799 let select_lock = match self {
2800 LockType::Share => "SHARE",
2801 LockType::Update => "UPDATE",
2802 };
2803 write!(f, "{select_lock}")
2804 }
2805}
2806
2807#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2808#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2809#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2810pub enum NonBlock {
2811 Nowait,
2812 SkipLocked,
2813}
2814
2815impl fmt::Display for NonBlock {
2816 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2817 let nonblock = match self {
2818 NonBlock::Nowait => "NOWAIT",
2819 NonBlock::SkipLocked => "SKIP LOCKED",
2820 };
2821 write!(f, "{nonblock}")
2822 }
2823}
2824
2825#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2826#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2827#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2828pub enum Distinct {
2829 Distinct,
2831
2832 On(Vec<Expr>),
2834}
2835
2836impl fmt::Display for Distinct {
2837 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2838 match self {
2839 Distinct::Distinct => write!(f, "DISTINCT"),
2840 Distinct::On(col_names) => {
2841 let col_names = display_comma_separated(col_names);
2842 write!(f, "DISTINCT ON ({col_names})")
2843 }
2844 }
2845 }
2846}
2847
2848#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2849#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2850#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2851pub struct Top {
2852 pub with_ties: bool,
2855 pub percent: bool,
2857 pub quantity: Option<TopQuantity>,
2858}
2859
2860#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2861#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2862#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2863pub enum TopQuantity {
2864 Expr(Expr),
2866 Constant(u64),
2868}
2869
2870impl fmt::Display for Top {
2871 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2872 let extension = if self.with_ties { " WITH TIES" } else { "" };
2873 if let Some(ref quantity) = self.quantity {
2874 let percent = if self.percent { " PERCENT" } else { "" };
2875 match quantity {
2876 TopQuantity::Expr(quantity) => write!(f, "TOP ({quantity}){percent}{extension}"),
2877 TopQuantity::Constant(quantity) => {
2878 write!(f, "TOP {quantity}{percent}{extension}")
2879 }
2880 }
2881 } else {
2882 write!(f, "TOP{extension}")
2883 }
2884 }
2885}
2886
2887#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2888#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2889#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2890pub struct Values {
2891 pub explicit_row: bool,
2894 pub rows: Vec<Vec<Expr>>,
2895}
2896
2897impl fmt::Display for Values {
2898 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2899 f.write_str("VALUES")?;
2900 let prefix = if self.explicit_row { "ROW" } else { "" };
2901 let mut delim = "";
2902 for row in &self.rows {
2903 f.write_str(delim)?;
2904 delim = ",";
2905 SpaceOrNewline.fmt(f)?;
2906 Indent(format_args!("{prefix}({})", display_comma_separated(row))).fmt(f)?;
2907 }
2908 Ok(())
2909 }
2910}
2911
2912#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2913#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2914#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2915pub struct SelectInto {
2916 pub temporary: bool,
2917 pub unlogged: bool,
2918 pub table: bool,
2919 pub name: ObjectName,
2920}
2921
2922impl fmt::Display for SelectInto {
2923 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2924 let temporary = if self.temporary { " TEMPORARY" } else { "" };
2925 let unlogged = if self.unlogged { " UNLOGGED" } else { "" };
2926 let table = if self.table { " TABLE" } else { "" };
2927
2928 write!(f, "INTO{}{}{} {}", temporary, unlogged, table, self.name)
2929 }
2930}
2931
2932#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2937#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2938#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2939pub enum GroupByWithModifier {
2940 Rollup,
2941 Cube,
2942 Totals,
2943 GroupingSets(Expr),
2948}
2949
2950impl fmt::Display for GroupByWithModifier {
2951 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2952 match self {
2953 GroupByWithModifier::Rollup => write!(f, "WITH ROLLUP"),
2954 GroupByWithModifier::Cube => write!(f, "WITH CUBE"),
2955 GroupByWithModifier::Totals => write!(f, "WITH TOTALS"),
2956 GroupByWithModifier::GroupingSets(expr) => {
2957 write!(f, "{expr}")
2958 }
2959 }
2960 }
2961}
2962
2963#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2964#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2965#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2966pub enum GroupByExpr {
2967 All(Vec<GroupByWithModifier>),
2977
2978 Expressions(Vec<Expr>, Vec<GroupByWithModifier>),
2980}
2981
2982impl fmt::Display for GroupByExpr {
2983 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2984 match self {
2985 GroupByExpr::All(modifiers) => {
2986 write!(f, "GROUP BY ALL")?;
2987 if !modifiers.is_empty() {
2988 write!(f, " {}", display_separated(modifiers, " "))?;
2989 }
2990 Ok(())
2991 }
2992 GroupByExpr::Expressions(col_names, modifiers) => {
2993 f.write_str("GROUP BY")?;
2994 SpaceOrNewline.fmt(f)?;
2995 Indent(display_comma_separated(col_names)).fmt(f)?;
2996 if !modifiers.is_empty() {
2997 write!(f, " {}", display_separated(modifiers, " "))?;
2998 }
2999 Ok(())
3000 }
3001 }
3002 }
3003}
3004
3005#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3009#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3010#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3011pub enum FormatClause {
3012 Identifier(Ident),
3013 Null,
3014}
3015
3016impl fmt::Display for FormatClause {
3017 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3018 match self {
3019 FormatClause::Identifier(ident) => write!(f, "FORMAT {}", ident),
3020 FormatClause::Null => write!(f, "FORMAT NULL"),
3021 }
3022 }
3023}
3024
3025#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3029#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3030#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3031pub struct InputFormatClause {
3032 pub ident: Ident,
3033 pub values: Vec<Expr>,
3034}
3035
3036impl fmt::Display for InputFormatClause {
3037 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3038 write!(f, "FORMAT {}", self.ident)?;
3039
3040 if !self.values.is_empty() {
3041 write!(f, " {}", display_comma_separated(self.values.as_slice()))?;
3042 }
3043
3044 Ok(())
3045 }
3046}
3047
3048#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3051#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3052#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3053pub enum ForClause {
3054 Browse,
3055 Json {
3056 for_json: ForJson,
3057 root: Option<String>,
3058 include_null_values: bool,
3059 without_array_wrapper: bool,
3060 },
3061 Xml {
3062 for_xml: ForXml,
3063 elements: bool,
3064 binary_base64: bool,
3065 root: Option<String>,
3066 r#type: bool,
3067 },
3068}
3069
3070impl fmt::Display for ForClause {
3071 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3072 match self {
3073 ForClause::Browse => write!(f, "FOR BROWSE"),
3074 ForClause::Json {
3075 for_json,
3076 root,
3077 include_null_values,
3078 without_array_wrapper,
3079 } => {
3080 write!(f, "FOR JSON ")?;
3081 write!(f, "{}", for_json)?;
3082 if let Some(root) = root {
3083 write!(f, ", ROOT('{}')", root)?;
3084 }
3085 if *include_null_values {
3086 write!(f, ", INCLUDE_NULL_VALUES")?;
3087 }
3088 if *without_array_wrapper {
3089 write!(f, ", WITHOUT_ARRAY_WRAPPER")?;
3090 }
3091 Ok(())
3092 }
3093 ForClause::Xml {
3094 for_xml,
3095 elements,
3096 binary_base64,
3097 root,
3098 r#type,
3099 } => {
3100 write!(f, "FOR XML ")?;
3101 write!(f, "{}", for_xml)?;
3102 if *binary_base64 {
3103 write!(f, ", BINARY BASE64")?;
3104 }
3105 if *r#type {
3106 write!(f, ", TYPE")?;
3107 }
3108 if let Some(root) = root {
3109 write!(f, ", ROOT('{}')", root)?;
3110 }
3111 if *elements {
3112 write!(f, ", ELEMENTS")?;
3113 }
3114 Ok(())
3115 }
3116 }
3117 }
3118}
3119
3120#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3121#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3122#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3123pub enum ForXml {
3124 Raw(Option<String>),
3125 Auto,
3126 Explicit,
3127 Path(Option<String>),
3128}
3129
3130impl fmt::Display for ForXml {
3131 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3132 match self {
3133 ForXml::Raw(root) => {
3134 write!(f, "RAW")?;
3135 if let Some(root) = root {
3136 write!(f, "('{}')", root)?;
3137 }
3138 Ok(())
3139 }
3140 ForXml::Auto => write!(f, "AUTO"),
3141 ForXml::Explicit => write!(f, "EXPLICIT"),
3142 ForXml::Path(root) => {
3143 write!(f, "PATH")?;
3144 if let Some(root) = root {
3145 write!(f, "('{}')", root)?;
3146 }
3147 Ok(())
3148 }
3149 }
3150 }
3151}
3152
3153#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3154#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3155#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3156pub enum ForJson {
3157 Auto,
3158 Path,
3159}
3160
3161impl fmt::Display for ForJson {
3162 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3163 match self {
3164 ForJson::Auto => write!(f, "AUTO"),
3165 ForJson::Path => write!(f, "PATH"),
3166 }
3167 }
3168}
3169
3170#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3191#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3192#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3193pub enum JsonTableColumn {
3194 Named(JsonTableNamedColumn),
3196 ForOrdinality(Ident),
3198 Nested(JsonTableNestedColumn),
3200}
3201
3202impl fmt::Display for JsonTableColumn {
3203 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3204 match self {
3205 JsonTableColumn::Named(json_table_named_column) => {
3206 write!(f, "{json_table_named_column}")
3207 }
3208 JsonTableColumn::ForOrdinality(ident) => write!(f, "{} FOR ORDINALITY", ident),
3209 JsonTableColumn::Nested(json_table_nested_column) => {
3210 write!(f, "{json_table_nested_column}")
3211 }
3212 }
3213 }
3214}
3215
3216#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3220#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3221#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3222pub struct JsonTableNestedColumn {
3223 pub path: Value,
3224 pub columns: Vec<JsonTableColumn>,
3225}
3226
3227impl fmt::Display for JsonTableNestedColumn {
3228 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3229 write!(
3230 f,
3231 "NESTED PATH {} COLUMNS ({})",
3232 self.path,
3233 display_comma_separated(&self.columns)
3234 )
3235 }
3236}
3237
3238#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3246#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3247#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3248pub struct JsonTableNamedColumn {
3249 pub name: Ident,
3251 pub r#type: DataType,
3253 pub path: Value,
3255 pub exists: bool,
3257 pub on_empty: Option<JsonTableColumnErrorHandling>,
3259 pub on_error: Option<JsonTableColumnErrorHandling>,
3261}
3262
3263impl fmt::Display for JsonTableNamedColumn {
3264 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3265 write!(
3266 f,
3267 "{} {}{} PATH {}",
3268 self.name,
3269 self.r#type,
3270 if self.exists { " EXISTS" } else { "" },
3271 self.path
3272 )?;
3273 if let Some(on_empty) = &self.on_empty {
3274 write!(f, " {} ON EMPTY", on_empty)?;
3275 }
3276 if let Some(on_error) = &self.on_error {
3277 write!(f, " {} ON ERROR", on_error)?;
3278 }
3279 Ok(())
3280 }
3281}
3282
3283#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3286#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3287#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3288pub enum JsonTableColumnErrorHandling {
3289 Null,
3290 Default(Value),
3291 Error,
3292}
3293
3294impl fmt::Display for JsonTableColumnErrorHandling {
3295 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3296 match self {
3297 JsonTableColumnErrorHandling::Null => write!(f, "NULL"),
3298 JsonTableColumnErrorHandling::Default(json_string) => {
3299 write!(f, "DEFAULT {}", json_string)
3300 }
3301 JsonTableColumnErrorHandling::Error => write!(f, "ERROR"),
3302 }
3303 }
3304}
3305
3306#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3314#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3315#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3316pub struct OpenJsonTableColumn {
3317 pub name: Ident,
3319 pub r#type: DataType,
3321 pub path: Option<String>,
3323 pub as_json: bool,
3325}
3326
3327impl fmt::Display for OpenJsonTableColumn {
3328 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3329 write!(f, "{} {}", self.name, self.r#type)?;
3330 if let Some(path) = &self.path {
3331 write!(f, " '{}'", value::escape_single_quote_string(path))?;
3332 }
3333 if self.as_json {
3334 write!(f, " AS JSON")?;
3335 }
3336 Ok(())
3337 }
3338}
3339
3340#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3347#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3348#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3349pub enum ValueTableMode {
3350 AsStruct,
3351 AsValue,
3352 DistinctAsStruct,
3353 DistinctAsValue,
3354}
3355
3356impl fmt::Display for ValueTableMode {
3357 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3358 match self {
3359 ValueTableMode::AsStruct => write!(f, "AS STRUCT"),
3360 ValueTableMode::AsValue => write!(f, "AS VALUE"),
3361 ValueTableMode::DistinctAsStruct => write!(f, "DISTINCT AS STRUCT"),
3362 ValueTableMode::DistinctAsValue => write!(f, "DISTINCT AS VALUE"),
3363 }
3364 }
3365}
3366
3367#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3369#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3370#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3371pub enum UpdateTableFromKind {
3372 BeforeSet(Vec<TableWithJoins>),
3375 AfterSet(Vec<TableWithJoins>),
3378}
3379
3380#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3382#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3383#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3384pub enum XmlTableColumnOption {
3385 NamedInfo {
3387 r#type: DataType,
3389 path: Option<Expr>,
3391 default: Option<Expr>,
3393 nullable: bool,
3395 },
3396 ForOrdinality,
3398}
3399
3400#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3413#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3414#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3415pub struct XmlTableColumn {
3416 pub name: Ident,
3418 pub option: XmlTableColumnOption,
3420}
3421
3422impl fmt::Display for XmlTableColumn {
3423 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3424 write!(f, "{}", self.name)?;
3425 match &self.option {
3426 XmlTableColumnOption::NamedInfo {
3427 r#type,
3428 path,
3429 default,
3430 nullable,
3431 } => {
3432 write!(f, " {}", r#type)?;
3433 if let Some(p) = path {
3434 write!(f, " PATH {}", p)?;
3435 }
3436 if let Some(d) = default {
3437 write!(f, " DEFAULT {}", d)?;
3438 }
3439 if !*nullable {
3440 write!(f, " NOT NULL")?;
3441 }
3442 Ok(())
3443 }
3444 XmlTableColumnOption::ForOrdinality => {
3445 write!(f, " FOR ORDINALITY")
3446 }
3447 }
3448 }
3449}
3450
3451#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3453#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3454#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3455pub struct XmlPassingArgument {
3456 pub expr: Expr,
3457 pub alias: Option<Ident>,
3458 pub by_value: bool, }
3460
3461impl fmt::Display for XmlPassingArgument {
3462 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3463 if self.by_value {
3464 write!(f, "BY VALUE ")?;
3465 }
3466 write!(f, "{}", self.expr)?;
3467 if let Some(alias) = &self.alias {
3468 write!(f, " AS {}", alias)?;
3469 }
3470 Ok(())
3471 }
3472}
3473
3474#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3476#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3477#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3478pub struct XmlPassingClause {
3479 pub arguments: Vec<XmlPassingArgument>,
3480}
3481
3482impl fmt::Display for XmlPassingClause {
3483 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3484 if !self.arguments.is_empty() {
3485 write!(f, " PASSING {}", display_comma_separated(&self.arguments))?;
3486 }
3487 Ok(())
3488 }
3489}
3490
3491#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3495#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3496#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3497pub struct XmlNamespaceDefinition {
3498 pub uri: Expr,
3500 pub name: Ident,
3502}
3503
3504impl fmt::Display for XmlNamespaceDefinition {
3505 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3506 write!(f, "{} AS {}", self.uri, self.name)
3507 }
3508}