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 sqltk_parser_derive::{Visit, VisitMut};
27
28use crate::{
29 ast::*,
30 tokenizer::{Token, TokenWithSpan},
31};
32
33#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
36#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
37#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
38#[cfg_attr(feature = "visitor", visit(with = "visit_query"))]
39pub struct Query {
40 pub with: Option<With>,
42 pub body: Box<SetExpr>,
44 pub order_by: Option<OrderBy>,
46 pub limit: Option<Expr>,
48
49 pub limit_by: Vec<Expr>,
51
52 pub offset: Option<Offset>,
54 pub fetch: Option<Fetch>,
56 pub locks: Vec<LockClause>,
58 pub for_clause: Option<ForClause>,
62 pub settings: Option<Vec<Setting>>,
66 pub format_clause: Option<FormatClause>,
71}
72
73impl fmt::Display for Query {
74 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
75 if let Some(ref with) = self.with {
76 write!(f, "{with} ")?;
77 }
78 write!(f, "{}", self.body)?;
79 if let Some(ref order_by) = self.order_by {
80 write!(f, " {order_by}")?;
81 }
82 if let Some(ref limit) = self.limit {
83 write!(f, " LIMIT {limit}")?;
84 }
85 if let Some(ref offset) = self.offset {
86 write!(f, " {offset}")?;
87 }
88 if !self.limit_by.is_empty() {
89 write!(f, " BY {}", display_separated(&self.limit_by, ", "))?;
90 }
91 if let Some(ref settings) = self.settings {
92 write!(f, " SETTINGS {}", display_comma_separated(settings))?;
93 }
94 if let Some(ref fetch) = self.fetch {
95 write!(f, " {fetch}")?;
96 }
97 if !self.locks.is_empty() {
98 write!(f, " {}", display_separated(&self.locks, " "))?;
99 }
100 if let Some(ref for_clause) = self.for_clause {
101 write!(f, " {}", for_clause)?;
102 }
103 if let Some(ref format) = self.format_clause {
104 write!(f, " {}", format)?;
105 }
106 Ok(())
107 }
108}
109
110#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
116#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
117#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
118pub struct ProjectionSelect {
119 pub projection: Vec<SelectItem>,
120 pub order_by: Option<OrderBy>,
121 pub group_by: Option<GroupByExpr>,
122}
123
124impl fmt::Display for ProjectionSelect {
125 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
126 write!(f, "SELECT {}", display_comma_separated(&self.projection))?;
127 if let Some(ref group_by) = self.group_by {
128 write!(f, " {group_by}")?;
129 }
130 if let Some(ref order_by) = self.order_by {
131 write!(f, " {order_by}")?;
132 }
133 Ok(())
134 }
135}
136
137#[allow(clippy::large_enum_variant)]
140#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
141#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
142#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
143pub enum SetExpr {
144 Select(Box<Select>),
146 Query(Box<Query>),
149 SetOperation {
151 op: SetOperator,
152 set_quantifier: SetQuantifier,
153 left: Box<SetExpr>,
154 right: Box<SetExpr>,
155 },
156 Values(Values),
157 Insert(Statement),
158 Update(Statement),
159 Table(Box<Table>),
160}
161
162impl SetExpr {
163 pub fn as_select(&self) -> Option<&Select> {
165 if let Self::Select(select) = self {
166 Some(&**select)
167 } else {
168 None
169 }
170 }
171}
172
173impl fmt::Display for SetExpr {
174 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
175 match self {
176 SetExpr::Select(s) => write!(f, "{s}"),
177 SetExpr::Query(q) => write!(f, "({q})"),
178 SetExpr::Values(v) => write!(f, "{v}"),
179 SetExpr::Insert(v) => write!(f, "{v}"),
180 SetExpr::Update(v) => write!(f, "{v}"),
181 SetExpr::Table(t) => write!(f, "{t}"),
182 SetExpr::SetOperation {
183 left,
184 right,
185 op,
186 set_quantifier,
187 } => {
188 write!(f, "{left} {op}")?;
189 match set_quantifier {
190 SetQuantifier::All
191 | SetQuantifier::Distinct
192 | SetQuantifier::ByName
193 | SetQuantifier::AllByName
194 | SetQuantifier::DistinctByName => write!(f, " {set_quantifier}")?,
195 SetQuantifier::None => write!(f, "{set_quantifier}")?,
196 }
197 write!(f, " {right}")?;
198 Ok(())
199 }
200 }
201 }
202}
203
204#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
205#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
206#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
207pub enum SetOperator {
208 Union,
209 Except,
210 Intersect,
211}
212
213impl fmt::Display for SetOperator {
214 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
215 f.write_str(match self {
216 SetOperator::Union => "UNION",
217 SetOperator::Except => "EXCEPT",
218 SetOperator::Intersect => "INTERSECT",
219 })
220 }
221}
222
223#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
227#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
228#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
229pub enum SetQuantifier {
230 All,
231 Distinct,
232 ByName,
233 AllByName,
234 DistinctByName,
235 None,
236}
237
238impl fmt::Display for SetQuantifier {
239 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
240 match self {
241 SetQuantifier::All => write!(f, "ALL"),
242 SetQuantifier::Distinct => write!(f, "DISTINCT"),
243 SetQuantifier::ByName => write!(f, "BY NAME"),
244 SetQuantifier::AllByName => write!(f, "ALL BY NAME"),
245 SetQuantifier::DistinctByName => write!(f, "DISTINCT BY NAME"),
246 SetQuantifier::None => write!(f, ""),
247 }
248 }
249}
250
251#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
252#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
253#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
255pub struct Table {
256 pub table_name: Option<String>,
257 pub schema_name: Option<String>,
258}
259
260impl fmt::Display for Table {
261 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
262 if let Some(ref schema_name) = self.schema_name {
263 write!(
264 f,
265 "TABLE {}.{}",
266 schema_name,
267 self.table_name.as_ref().unwrap(),
268 )?;
269 } else {
270 write!(f, "TABLE {}", self.table_name.as_ref().unwrap(),)?;
271 }
272 Ok(())
273 }
274}
275
276#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
280#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
281#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
282pub struct Select {
283 pub select_token: AttachedToken,
285 pub distinct: Option<Distinct>,
287 pub top: Option<Top>,
289 pub top_before_distinct: bool,
291 pub projection: Vec<SelectItem>,
293 pub into: Option<SelectInto>,
295 pub from: Vec<TableWithJoins>,
297 pub lateral_views: Vec<LateralView>,
299 pub prewhere: Option<Expr>,
304 pub selection: Option<Expr>,
306 pub group_by: GroupByExpr,
308 pub cluster_by: Vec<Expr>,
310 pub distribute_by: Vec<Expr>,
312 pub sort_by: Vec<Expr>,
314 pub having: Option<Expr>,
316 pub named_window: Vec<NamedWindowDefinition>,
318 pub qualify: Option<Expr>,
320 pub window_before_qualify: bool,
325 pub value_table_mode: Option<ValueTableMode>,
327 pub connect_by: Option<ConnectBy>,
329}
330
331impl fmt::Display for Select {
332 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
333 write!(f, "SELECT")?;
334
335 if let Some(value_table_mode) = self.value_table_mode {
336 write!(f, " {value_table_mode}")?;
337 }
338
339 if let Some(ref top) = self.top {
340 if self.top_before_distinct {
341 write!(f, " {top}")?;
342 }
343 }
344 if let Some(ref distinct) = self.distinct {
345 write!(f, " {distinct}")?;
346 }
347 if let Some(ref top) = self.top {
348 if !self.top_before_distinct {
349 write!(f, " {top}")?;
350 }
351 }
352
353 write!(f, " {}", display_comma_separated(&self.projection))?;
354
355 if let Some(ref into) = self.into {
356 write!(f, " {into}")?;
357 }
358
359 if !self.from.is_empty() {
360 write!(f, " FROM {}", display_comma_separated(&self.from))?;
361 }
362 if !self.lateral_views.is_empty() {
363 for lv in &self.lateral_views {
364 write!(f, "{lv}")?;
365 }
366 }
367 if let Some(ref prewhere) = self.prewhere {
368 write!(f, " PREWHERE {prewhere}")?;
369 }
370 if let Some(ref selection) = self.selection {
371 write!(f, " WHERE {selection}")?;
372 }
373 match &self.group_by {
374 GroupByExpr::All(_) => write!(f, " {}", self.group_by)?,
375 GroupByExpr::Expressions(exprs, _) => {
376 if !exprs.is_empty() {
377 write!(f, " {}", self.group_by)?
378 }
379 }
380 }
381 if !self.cluster_by.is_empty() {
382 write!(
383 f,
384 " CLUSTER BY {}",
385 display_comma_separated(&self.cluster_by)
386 )?;
387 }
388 if !self.distribute_by.is_empty() {
389 write!(
390 f,
391 " DISTRIBUTE BY {}",
392 display_comma_separated(&self.distribute_by)
393 )?;
394 }
395 if !self.sort_by.is_empty() {
396 write!(f, " SORT BY {}", display_comma_separated(&self.sort_by))?;
397 }
398 if let Some(ref having) = self.having {
399 write!(f, " HAVING {having}")?;
400 }
401 if self.window_before_qualify {
402 if !self.named_window.is_empty() {
403 write!(f, " WINDOW {}", display_comma_separated(&self.named_window))?;
404 }
405 if let Some(ref qualify) = self.qualify {
406 write!(f, " QUALIFY {qualify}")?;
407 }
408 } else {
409 if let Some(ref qualify) = self.qualify {
410 write!(f, " QUALIFY {qualify}")?;
411 }
412 if !self.named_window.is_empty() {
413 write!(f, " WINDOW {}", display_comma_separated(&self.named_window))?;
414 }
415 }
416 if let Some(ref connect_by) = self.connect_by {
417 write!(f, " {connect_by}")?;
418 }
419 Ok(())
420 }
421}
422
423#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
425#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
426#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
427pub struct LateralView {
428 pub lateral_view: Expr,
430 pub lateral_view_name: ObjectName,
432 pub lateral_col_alias: Vec<Ident>,
434 pub outer: bool,
436}
437
438impl fmt::Display for LateralView {
439 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
440 write!(
441 f,
442 " LATERAL VIEW{outer} {} {}",
443 self.lateral_view,
444 self.lateral_view_name,
445 outer = if self.outer { " OUTER" } else { "" }
446 )?;
447 if !self.lateral_col_alias.is_empty() {
448 write!(
449 f,
450 " AS {}",
451 display_comma_separated(&self.lateral_col_alias)
452 )?;
453 }
454 Ok(())
455 }
456}
457
458#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
464#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
465#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
466pub enum NamedWindowExpr {
467 NamedWindow(Ident),
477 WindowSpec(WindowSpec),
484}
485
486impl fmt::Display for NamedWindowExpr {
487 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
488 match self {
489 NamedWindowExpr::NamedWindow(named_window) => {
490 write!(f, "{named_window}")?;
491 }
492 NamedWindowExpr::WindowSpec(window_spec) => {
493 write!(f, "({window_spec})")?;
494 }
495 };
496 Ok(())
497 }
498}
499
500#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
501#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
502#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
503pub struct NamedWindowDefinition(pub Ident, pub NamedWindowExpr);
504
505impl fmt::Display for NamedWindowDefinition {
506 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
507 write!(f, "{} AS {}", self.0, self.1)
508 }
509}
510
511#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
512#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
513#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
514pub struct With {
515 pub with_token: AttachedToken,
517 pub recursive: bool,
518 pub cte_tables: Vec<Cte>,
519}
520
521impl fmt::Display for With {
522 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
523 write!(
524 f,
525 "WITH {}{}",
526 if self.recursive { "RECURSIVE " } else { "" },
527 display_comma_separated(&self.cte_tables)
528 )
529 }
530}
531
532#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
533#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
534#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
535pub enum CteAsMaterialized {
536 Materialized,
538 NotMaterialized,
540}
541
542impl fmt::Display for CteAsMaterialized {
543 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
544 match *self {
545 CteAsMaterialized::Materialized => {
546 write!(f, "MATERIALIZED")?;
547 }
548 CteAsMaterialized::NotMaterialized => {
549 write!(f, "NOT MATERIALIZED")?;
550 }
551 };
552 Ok(())
553 }
554}
555
556#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
561#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
562#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
563pub struct Cte {
564 pub alias: TableAlias,
565 pub query: Box<Query>,
566 pub from: Option<Ident>,
567 pub materialized: Option<CteAsMaterialized>,
568 pub closing_paren_token: AttachedToken,
570}
571
572impl fmt::Display for Cte {
573 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
574 match self.materialized.as_ref() {
575 None => write!(f, "{} AS ({})", self.alias, self.query)?,
576 Some(materialized) => write!(f, "{} AS {materialized} ({})", self.alias, self.query)?,
577 };
578 if let Some(ref fr) = self.from {
579 write!(f, " FROM {fr}")?;
580 }
581 Ok(())
582 }
583}
584
585#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
587#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
588#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
589pub enum SelectItem {
590 UnnamedExpr(Expr),
592 ExprWithAlias { expr: Expr, alias: Ident },
594 QualifiedWildcard(ObjectName, WildcardAdditionalOptions),
596 Wildcard(WildcardAdditionalOptions),
598}
599
600#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
607#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
608#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
609pub struct IdentWithAlias {
610 pub ident: Ident,
611 pub alias: Ident,
612}
613
614impl fmt::Display for IdentWithAlias {
615 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
616 write!(f, "{} AS {}", self.ident, self.alias)
617 }
618}
619
620#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
622#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
623#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
624pub struct WildcardAdditionalOptions {
625 pub wildcard_token: AttachedToken,
627 pub opt_ilike: Option<IlikeSelectItem>,
630 pub opt_exclude: Option<ExcludeSelectItem>,
632 pub opt_except: Option<ExceptSelectItem>,
635 pub opt_replace: Option<ReplaceSelectItem>,
640 pub opt_rename: Option<RenameSelectItem>,
642}
643
644impl Default for WildcardAdditionalOptions {
645 fn default() -> Self {
646 Self {
647 wildcard_token: TokenWithSpan::wrap(Token::Mul).into(),
648 opt_ilike: None,
649 opt_exclude: None,
650 opt_except: None,
651 opt_replace: None,
652 opt_rename: None,
653 }
654 }
655}
656
657impl fmt::Display for WildcardAdditionalOptions {
658 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
659 if let Some(ilike) = &self.opt_ilike {
660 write!(f, " {ilike}")?;
661 }
662 if let Some(exclude) = &self.opt_exclude {
663 write!(f, " {exclude}")?;
664 }
665 if let Some(except) = &self.opt_except {
666 write!(f, " {except}")?;
667 }
668 if let Some(replace) = &self.opt_replace {
669 write!(f, " {replace}")?;
670 }
671 if let Some(rename) = &self.opt_rename {
672 write!(f, " {rename}")?;
673 }
674 Ok(())
675 }
676}
677
678#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
685#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
686#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
687pub struct IlikeSelectItem {
688 pub pattern: String,
689}
690
691impl fmt::Display for IlikeSelectItem {
692 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
693 write!(
694 f,
695 "ILIKE '{}'",
696 value::escape_single_quote_string(&self.pattern)
697 )?;
698 Ok(())
699 }
700}
701#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
709#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
710#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
711pub enum ExcludeSelectItem {
712 Single(Ident),
719 Multiple(Vec<Ident>),
725}
726
727impl fmt::Display for ExcludeSelectItem {
728 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
729 write!(f, "EXCLUDE")?;
730 match self {
731 Self::Single(column) => {
732 write!(f, " {column}")?;
733 }
734 Self::Multiple(columns) => {
735 write!(f, " ({})", display_comma_separated(columns))?;
736 }
737 }
738 Ok(())
739 }
740}
741
742#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
750#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
751#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
752pub enum RenameSelectItem {
753 Single(IdentWithAlias),
760 Multiple(Vec<IdentWithAlias>),
766}
767
768impl fmt::Display for RenameSelectItem {
769 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
770 write!(f, "RENAME")?;
771 match self {
772 Self::Single(column) => {
773 write!(f, " {column}")?;
774 }
775 Self::Multiple(columns) => {
776 write!(f, " ({})", display_comma_separated(columns))?;
777 }
778 }
779 Ok(())
780 }
781}
782
783#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
790#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
791#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
792pub struct ExceptSelectItem {
793 pub first_element: Ident,
795 pub additional_elements: Vec<Ident>,
797}
798
799impl fmt::Display for ExceptSelectItem {
800 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
801 write!(f, "EXCEPT ")?;
802 if self.additional_elements.is_empty() {
803 write!(f, "({})", self.first_element)?;
804 } else {
805 write!(
806 f,
807 "({}, {})",
808 self.first_element,
809 display_comma_separated(&self.additional_elements)
810 )?;
811 }
812 Ok(())
813 }
814}
815
816#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
824#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
825#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
826pub struct ReplaceSelectItem {
827 pub items: Vec<Box<ReplaceSelectElement>>,
828}
829
830impl fmt::Display for ReplaceSelectItem {
831 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
832 write!(f, "REPLACE")?;
833 write!(f, " ({})", display_comma_separated(&self.items))?;
834 Ok(())
835 }
836}
837
838#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
843#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
844#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
845pub struct ReplaceSelectElement {
846 pub expr: Expr,
847 pub column_name: Ident,
848 pub as_keyword: bool,
849}
850
851impl fmt::Display for ReplaceSelectElement {
852 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
853 if self.as_keyword {
854 write!(f, "{} AS {}", self.expr, self.column_name)
855 } else {
856 write!(f, "{} {}", self.expr, self.column_name)
857 }
858 }
859}
860
861impl fmt::Display for SelectItem {
862 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
863 match &self {
864 SelectItem::UnnamedExpr(expr) => write!(f, "{expr}"),
865 SelectItem::ExprWithAlias { expr, alias } => write!(f, "{expr} AS {alias}"),
866 SelectItem::QualifiedWildcard(prefix, additional_options) => {
867 write!(f, "{prefix}.*")?;
868 write!(f, "{additional_options}")?;
869 Ok(())
870 }
871 SelectItem::Wildcard(additional_options) => {
872 write!(f, "*")?;
873 write!(f, "{additional_options}")?;
874 Ok(())
875 }
876 }
877 }
878}
879
880#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
881#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
882#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
883pub struct TableWithJoins {
884 pub relation: TableFactor,
885 pub joins: Vec<Join>,
886}
887
888impl fmt::Display for TableWithJoins {
889 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
890 write!(f, "{}", self.relation)?;
891 for join in &self.joins {
892 write!(f, "{join}")?;
893 }
894 Ok(())
895 }
896}
897
898#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
902#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
903#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
904pub struct ConnectBy {
905 pub condition: Expr,
907 pub relationships: Vec<Expr>,
909}
910
911impl fmt::Display for ConnectBy {
912 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
913 write!(
914 f,
915 "START WITH {condition} CONNECT BY {relationships}",
916 condition = self.condition,
917 relationships = display_comma_separated(&self.relationships)
918 )
919 }
920}
921
922#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
923#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
924#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
925pub struct Setting {
926 pub key: Ident,
927 pub value: Value,
928}
929
930impl fmt::Display for Setting {
931 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
932 write!(f, "{} = {}", self.key, self.value)
933 }
934}
935
936#[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 ExprWithAlias {
946 pub expr: Expr,
947 pub alias: Option<Ident>,
948}
949
950impl fmt::Display for ExprWithAlias {
951 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
952 let ExprWithAlias { expr, alias } = self;
953 write!(f, "{expr}")?;
954 if let Some(alias) = alias {
955 write!(f, " AS {alias}")?;
956 }
957 Ok(())
958 }
959}
960
961#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
963#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
964#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
965pub struct TableFunctionArgs {
966 pub args: Vec<FunctionArg>,
967 pub settings: Option<Vec<Setting>>,
972}
973
974#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
976#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
977#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
978#[cfg_attr(feature = "visitor", visit(with = "visit_table_factor"))]
979pub enum TableFactor {
980 Table {
981 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
982 name: ObjectName,
983 alias: Option<TableAlias>,
984 args: Option<TableFunctionArgs>,
992 with_hints: Vec<Expr>,
994 version: Option<TableVersion>,
997 with_ordinality: bool,
1001 partitions: Vec<Ident>,
1003 json_path: Option<JsonPath>,
1005 },
1006 Derived {
1007 lateral: bool,
1008 subquery: Box<Query>,
1009 alias: Option<TableAlias>,
1010 },
1011 TableFunction {
1013 expr: Expr,
1014 alias: Option<TableAlias>,
1015 },
1016 Function {
1018 lateral: bool,
1019 name: ObjectName,
1020 args: Vec<FunctionArg>,
1021 alias: Option<TableAlias>,
1022 },
1023 UNNEST {
1034 alias: Option<TableAlias>,
1035 array_exprs: Vec<Expr>,
1036 with_offset: bool,
1037 with_offset_alias: Option<Ident>,
1038 with_ordinality: bool,
1039 },
1040 JsonTable {
1056 json_expr: Expr,
1058 json_path: Value,
1061 columns: Vec<JsonTableColumn>,
1064 alias: Option<TableAlias>,
1066 },
1067 OpenJsonTable {
1077 json_expr: Expr,
1079 json_path: Option<Value>,
1082 columns: Vec<OpenJsonTableColumn>,
1085 alias: Option<TableAlias>,
1087 },
1088 NestedJoin {
1095 table_with_joins: Box<TableWithJoins>,
1096 alias: Option<TableAlias>,
1097 },
1098 Pivot {
1104 table: Box<TableFactor>,
1105 aggregate_functions: Vec<ExprWithAlias>, value_column: Vec<Ident>,
1107 value_source: PivotValueSource,
1108 default_on_null: Option<Expr>,
1109 alias: Option<TableAlias>,
1110 },
1111 Unpivot {
1120 table: Box<TableFactor>,
1121 value: Ident,
1122 name: Ident,
1123 columns: Vec<Ident>,
1124 alias: Option<TableAlias>,
1125 },
1126 MatchRecognize {
1130 table: Box<TableFactor>,
1131 partition_by: Vec<Expr>,
1133 order_by: Vec<OrderByExpr>,
1135 measures: Vec<Measure>,
1137 rows_per_match: Option<RowsPerMatch>,
1139 after_match_skip: Option<AfterMatchSkip>,
1141 pattern: MatchRecognizePattern,
1143 symbols: Vec<SymbolDefinition>,
1145 alias: Option<TableAlias>,
1146 },
1147}
1148
1149#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1151#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1152#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1153pub enum PivotValueSource {
1154 List(Vec<ExprWithAlias>),
1158 Any(Vec<OrderByExpr>),
1162 Subquery(Box<Query>),
1166}
1167
1168impl fmt::Display for PivotValueSource {
1169 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1170 match self {
1171 PivotValueSource::List(values) => write!(f, "{}", display_comma_separated(values)),
1172 PivotValueSource::Any(order_by) => {
1173 write!(f, "ANY")?;
1174 if !order_by.is_empty() {
1175 write!(f, " ORDER BY {}", display_comma_separated(order_by))?;
1176 }
1177 Ok(())
1178 }
1179 PivotValueSource::Subquery(query) => write!(f, "{query}"),
1180 }
1181 }
1182}
1183
1184#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1188#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1189#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1190pub struct Measure {
1191 pub expr: Expr,
1192 pub alias: Ident,
1193}
1194
1195impl fmt::Display for Measure {
1196 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1197 write!(f, "{} AS {}", self.expr, self.alias)
1198 }
1199}
1200
1201#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1205#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1206#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1207pub enum RowsPerMatch {
1208 OneRow,
1210 AllRows(Option<EmptyMatchesMode>),
1212}
1213
1214impl fmt::Display for RowsPerMatch {
1215 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1216 match self {
1217 RowsPerMatch::OneRow => write!(f, "ONE ROW PER MATCH"),
1218 RowsPerMatch::AllRows(mode) => {
1219 write!(f, "ALL ROWS PER MATCH")?;
1220 if let Some(mode) = mode {
1221 write!(f, " {}", mode)?;
1222 }
1223 Ok(())
1224 }
1225 }
1226 }
1227}
1228
1229#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1233#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1234#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1235pub enum AfterMatchSkip {
1236 PastLastRow,
1238 ToNextRow,
1240 ToFirst(Ident),
1242 ToLast(Ident),
1244}
1245
1246impl fmt::Display for AfterMatchSkip {
1247 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1248 write!(f, "AFTER MATCH SKIP ")?;
1249 match self {
1250 AfterMatchSkip::PastLastRow => write!(f, "PAST LAST ROW"),
1251 AfterMatchSkip::ToNextRow => write!(f, " TO NEXT ROW"),
1252 AfterMatchSkip::ToFirst(symbol) => write!(f, "TO FIRST {symbol}"),
1253 AfterMatchSkip::ToLast(symbol) => write!(f, "TO LAST {symbol}"),
1254 }
1255 }
1256}
1257
1258#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1259#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1260#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1261pub enum EmptyMatchesMode {
1262 Show,
1264 Omit,
1266 WithUnmatched,
1268}
1269
1270impl fmt::Display for EmptyMatchesMode {
1271 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1272 match self {
1273 EmptyMatchesMode::Show => write!(f, "SHOW EMPTY MATCHES"),
1274 EmptyMatchesMode::Omit => write!(f, "OMIT EMPTY MATCHES"),
1275 EmptyMatchesMode::WithUnmatched => write!(f, "WITH UNMATCHED ROWS"),
1276 }
1277 }
1278}
1279
1280#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1284#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1285#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1286pub struct SymbolDefinition {
1287 pub symbol: Ident,
1288 pub definition: Expr,
1289}
1290
1291impl fmt::Display for SymbolDefinition {
1292 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1293 write!(f, "{} AS {}", self.symbol, self.definition)
1294 }
1295}
1296
1297#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1299#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1300#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1301pub enum MatchRecognizeSymbol {
1302 Named(Ident),
1304 Start,
1306 End,
1308}
1309
1310impl fmt::Display for MatchRecognizeSymbol {
1311 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1312 match self {
1313 MatchRecognizeSymbol::Named(symbol) => write!(f, "{symbol}"),
1314 MatchRecognizeSymbol::Start => write!(f, "^"),
1315 MatchRecognizeSymbol::End => write!(f, "$"),
1316 }
1317 }
1318}
1319
1320#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1324#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1325#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1326pub enum MatchRecognizePattern {
1327 Symbol(MatchRecognizeSymbol),
1329 Exclude(MatchRecognizeSymbol),
1331 Permute(Vec<MatchRecognizeSymbol>),
1333 Concat(Vec<MatchRecognizePattern>),
1335 Group(Box<MatchRecognizePattern>),
1337 Alternation(Vec<MatchRecognizePattern>),
1339 Repetition(Box<MatchRecognizePattern>, RepetitionQuantifier),
1341}
1342
1343impl fmt::Display for MatchRecognizePattern {
1344 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1345 use MatchRecognizePattern::*;
1346 match self {
1347 Symbol(symbol) => write!(f, "{}", symbol),
1348 Exclude(symbol) => write!(f, "{{- {symbol} -}}"),
1349 Permute(symbols) => write!(f, "PERMUTE({})", display_comma_separated(symbols)),
1350 Concat(patterns) => write!(f, "{}", display_separated(patterns, " ")),
1351 Group(pattern) => write!(f, "( {pattern} )"),
1352 Alternation(patterns) => write!(f, "{}", display_separated(patterns, " | ")),
1353 Repetition(pattern, op) => write!(f, "{pattern}{op}"),
1354 }
1355 }
1356}
1357
1358#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1361#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1362#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1363pub enum RepetitionQuantifier {
1364 ZeroOrMore,
1366 OneOrMore,
1368 AtMostOne,
1370 Exactly(u32),
1372 AtLeast(u32),
1374 AtMost(u32),
1376 Range(u32, u32),
1378}
1379
1380impl fmt::Display for RepetitionQuantifier {
1381 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1382 use RepetitionQuantifier::*;
1383 match self {
1384 ZeroOrMore => write!(f, "*"),
1385 OneOrMore => write!(f, "+"),
1386 AtMostOne => write!(f, "?"),
1387 Exactly(n) => write!(f, "{{{n}}}"),
1388 AtLeast(n) => write!(f, "{{{n},}}"),
1389 AtMost(n) => write!(f, "{{,{n}}}"),
1390 Range(n, m) => write!(f, "{{{n},{m}}}"),
1391 }
1392 }
1393}
1394
1395impl fmt::Display for TableFactor {
1396 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1397 match self {
1398 TableFactor::Table {
1399 name,
1400 alias,
1401 args,
1402 with_hints,
1403 version,
1404 partitions,
1405 with_ordinality,
1406 json_path,
1407 } => {
1408 write!(f, "{name}")?;
1409 if let Some(json_path) = json_path {
1410 write!(f, "{json_path}")?;
1411 }
1412 if !partitions.is_empty() {
1413 write!(f, "PARTITION ({})", display_comma_separated(partitions))?;
1414 }
1415 if let Some(args) = args {
1416 write!(f, "(")?;
1417 write!(f, "{}", display_comma_separated(&args.args))?;
1418 if let Some(ref settings) = args.settings {
1419 if !args.args.is_empty() {
1420 write!(f, ", ")?;
1421 }
1422 write!(f, "SETTINGS {}", display_comma_separated(settings))?;
1423 }
1424 write!(f, ")")?;
1425 }
1426 if *with_ordinality {
1427 write!(f, " WITH ORDINALITY")?;
1428 }
1429 if let Some(alias) = alias {
1430 write!(f, " AS {alias}")?;
1431 }
1432 if !with_hints.is_empty() {
1433 write!(f, " WITH ({})", display_comma_separated(with_hints))?;
1434 }
1435 if let Some(version) = version {
1436 write!(f, "{version}")?;
1437 }
1438 Ok(())
1439 }
1440 TableFactor::Derived {
1441 lateral,
1442 subquery,
1443 alias,
1444 } => {
1445 if *lateral {
1446 write!(f, "LATERAL ")?;
1447 }
1448 write!(f, "({subquery})")?;
1449 if let Some(alias) = alias {
1450 write!(f, " AS {alias}")?;
1451 }
1452 Ok(())
1453 }
1454 TableFactor::Function {
1455 lateral,
1456 name,
1457 args,
1458 alias,
1459 } => {
1460 if *lateral {
1461 write!(f, "LATERAL ")?;
1462 }
1463 write!(f, "{name}")?;
1464 write!(f, "({})", display_comma_separated(args))?;
1465 if let Some(alias) = alias {
1466 write!(f, " AS {alias}")?;
1467 }
1468 Ok(())
1469 }
1470 TableFactor::TableFunction { expr, alias } => {
1471 write!(f, "TABLE({expr})")?;
1472 if let Some(alias) = alias {
1473 write!(f, " AS {alias}")?;
1474 }
1475 Ok(())
1476 }
1477 TableFactor::UNNEST {
1478 alias,
1479 array_exprs,
1480 with_offset,
1481 with_offset_alias,
1482 with_ordinality,
1483 } => {
1484 write!(f, "UNNEST({})", display_comma_separated(array_exprs))?;
1485
1486 if *with_ordinality {
1487 write!(f, " WITH ORDINALITY")?;
1488 }
1489
1490 if let Some(alias) = alias {
1491 write!(f, " AS {alias}")?;
1492 }
1493 if *with_offset {
1494 write!(f, " WITH OFFSET")?;
1495 }
1496 if let Some(alias) = with_offset_alias {
1497 write!(f, " AS {alias}")?;
1498 }
1499 Ok(())
1500 }
1501 TableFactor::JsonTable {
1502 json_expr,
1503 json_path,
1504 columns,
1505 alias,
1506 } => {
1507 write!(
1508 f,
1509 "JSON_TABLE({json_expr}, {json_path} COLUMNS({columns}))",
1510 columns = display_comma_separated(columns)
1511 )?;
1512 if let Some(alias) = alias {
1513 write!(f, " AS {alias}")?;
1514 }
1515 Ok(())
1516 }
1517 TableFactor::OpenJsonTable {
1518 json_expr,
1519 json_path,
1520 columns,
1521 alias,
1522 } => {
1523 write!(f, "OPENJSON({json_expr}")?;
1524 if let Some(json_path) = json_path {
1525 write!(f, ", {json_path}")?;
1526 }
1527 write!(f, ")")?;
1528 if !columns.is_empty() {
1529 write!(f, " WITH ({})", display_comma_separated(columns))?;
1530 }
1531 if let Some(alias) = alias {
1532 write!(f, " AS {alias}")?;
1533 }
1534 Ok(())
1535 }
1536 TableFactor::NestedJoin {
1537 table_with_joins,
1538 alias,
1539 } => {
1540 write!(f, "({table_with_joins})")?;
1541 if let Some(alias) = alias {
1542 write!(f, " AS {alias}")?;
1543 }
1544 Ok(())
1545 }
1546 TableFactor::Pivot {
1547 table,
1548 aggregate_functions,
1549 value_column,
1550 value_source,
1551 default_on_null,
1552 alias,
1553 } => {
1554 write!(
1555 f,
1556 "{table} PIVOT({} FOR {} IN ({value_source})",
1557 display_comma_separated(aggregate_functions),
1558 Expr::CompoundIdentifier(value_column.to_vec()),
1559 )?;
1560 if let Some(expr) = default_on_null {
1561 write!(f, " DEFAULT ON NULL ({expr})")?;
1562 }
1563 write!(f, ")")?;
1564 if alias.is_some() {
1565 write!(f, " AS {}", alias.as_ref().unwrap())?;
1566 }
1567 Ok(())
1568 }
1569 TableFactor::Unpivot {
1570 table,
1571 value,
1572 name,
1573 columns,
1574 alias,
1575 } => {
1576 write!(
1577 f,
1578 "{} UNPIVOT({} FOR {} IN ({}))",
1579 table,
1580 value,
1581 name,
1582 display_comma_separated(columns)
1583 )?;
1584 if alias.is_some() {
1585 write!(f, " AS {}", alias.as_ref().unwrap())?;
1586 }
1587 Ok(())
1588 }
1589 TableFactor::MatchRecognize {
1590 table,
1591 partition_by,
1592 order_by,
1593 measures,
1594 rows_per_match,
1595 after_match_skip,
1596 pattern,
1597 symbols,
1598 alias,
1599 } => {
1600 write!(f, "{table} MATCH_RECOGNIZE(")?;
1601 if !partition_by.is_empty() {
1602 write!(f, "PARTITION BY {} ", display_comma_separated(partition_by))?;
1603 }
1604 if !order_by.is_empty() {
1605 write!(f, "ORDER BY {} ", display_comma_separated(order_by))?;
1606 }
1607 if !measures.is_empty() {
1608 write!(f, "MEASURES {} ", display_comma_separated(measures))?;
1609 }
1610 if let Some(rows_per_match) = rows_per_match {
1611 write!(f, "{rows_per_match} ")?;
1612 }
1613 if let Some(after_match_skip) = after_match_skip {
1614 write!(f, "{after_match_skip} ")?;
1615 }
1616 write!(f, "PATTERN ({pattern}) ")?;
1617 write!(f, "DEFINE {})", display_comma_separated(symbols))?;
1618 if alias.is_some() {
1619 write!(f, " AS {}", alias.as_ref().unwrap())?;
1620 }
1621 Ok(())
1622 }
1623 }
1624 }
1625}
1626
1627#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1628#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1629#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1630pub struct TableAlias {
1631 pub name: Ident,
1632 pub columns: Vec<TableAliasColumnDef>,
1633}
1634
1635impl fmt::Display for TableAlias {
1636 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1637 write!(f, "{}", self.name)?;
1638 if !self.columns.is_empty() {
1639 write!(f, " ({})", display_comma_separated(&self.columns))?;
1640 }
1641 Ok(())
1642 }
1643}
1644
1645#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1651#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1652#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1653pub struct TableAliasColumnDef {
1654 pub name: Ident,
1656 pub data_type: Option<DataType>,
1658}
1659
1660impl TableAliasColumnDef {
1661 pub fn from_name<S: Into<String>>(name: S) -> Self {
1663 TableAliasColumnDef {
1664 name: Ident::new(name),
1665 data_type: None,
1666 }
1667 }
1668}
1669
1670impl fmt::Display for TableAliasColumnDef {
1671 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1672 write!(f, "{}", self.name)?;
1673 if let Some(ref data_type) = self.data_type {
1674 write!(f, " {}", data_type)?;
1675 }
1676 Ok(())
1677 }
1678}
1679
1680#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1681#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1682#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1683pub enum TableVersion {
1684 ForSystemTimeAsOf(Expr),
1685}
1686
1687impl Display for TableVersion {
1688 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1689 match self {
1690 TableVersion::ForSystemTimeAsOf(e) => write!(f, " FOR SYSTEM_TIME AS OF {e}")?,
1691 }
1692 Ok(())
1693 }
1694}
1695
1696#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1697#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1698#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1699pub struct Join {
1700 pub relation: TableFactor,
1701 pub global: bool,
1704 pub join_operator: JoinOperator,
1705}
1706
1707impl fmt::Display for Join {
1708 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1709 fn prefix(constraint: &JoinConstraint) -> &'static str {
1710 match constraint {
1711 JoinConstraint::Natural => "NATURAL ",
1712 _ => "",
1713 }
1714 }
1715 fn suffix(constraint: &'_ JoinConstraint) -> impl fmt::Display + '_ {
1716 struct Suffix<'a>(&'a JoinConstraint);
1717 impl fmt::Display for Suffix<'_> {
1718 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1719 match self.0 {
1720 JoinConstraint::On(expr) => write!(f, " ON {expr}"),
1721 JoinConstraint::Using(attrs) => {
1722 write!(f, " USING({})", display_comma_separated(attrs))
1723 }
1724 _ => Ok(()),
1725 }
1726 }
1727 }
1728 Suffix(constraint)
1729 }
1730 if self.global {
1731 write!(f, " GLOBAL")?;
1732 }
1733
1734 match &self.join_operator {
1735 JoinOperator::Inner(constraint) => write!(
1736 f,
1737 " {}JOIN {}{}",
1738 prefix(constraint),
1739 self.relation,
1740 suffix(constraint)
1741 ),
1742 JoinOperator::LeftOuter(constraint) => write!(
1743 f,
1744 " {}LEFT JOIN {}{}",
1745 prefix(constraint),
1746 self.relation,
1747 suffix(constraint)
1748 ),
1749 JoinOperator::RightOuter(constraint) => write!(
1750 f,
1751 " {}RIGHT JOIN {}{}",
1752 prefix(constraint),
1753 self.relation,
1754 suffix(constraint)
1755 ),
1756 JoinOperator::FullOuter(constraint) => write!(
1757 f,
1758 " {}FULL JOIN {}{}",
1759 prefix(constraint),
1760 self.relation,
1761 suffix(constraint)
1762 ),
1763 JoinOperator::CrossJoin => write!(f, " CROSS JOIN {}", self.relation),
1764 JoinOperator::Semi(constraint) => write!(
1765 f,
1766 " {}SEMI JOIN {}{}",
1767 prefix(constraint),
1768 self.relation,
1769 suffix(constraint)
1770 ),
1771 JoinOperator::LeftSemi(constraint) => write!(
1772 f,
1773 " {}LEFT SEMI JOIN {}{}",
1774 prefix(constraint),
1775 self.relation,
1776 suffix(constraint)
1777 ),
1778 JoinOperator::RightSemi(constraint) => write!(
1779 f,
1780 " {}RIGHT SEMI JOIN {}{}",
1781 prefix(constraint),
1782 self.relation,
1783 suffix(constraint)
1784 ),
1785 JoinOperator::Anti(constraint) => write!(
1786 f,
1787 " {}ANTI JOIN {}{}",
1788 prefix(constraint),
1789 self.relation,
1790 suffix(constraint)
1791 ),
1792 JoinOperator::LeftAnti(constraint) => write!(
1793 f,
1794 " {}LEFT ANTI JOIN {}{}",
1795 prefix(constraint),
1796 self.relation,
1797 suffix(constraint)
1798 ),
1799 JoinOperator::RightAnti(constraint) => write!(
1800 f,
1801 " {}RIGHT ANTI JOIN {}{}",
1802 prefix(constraint),
1803 self.relation,
1804 suffix(constraint)
1805 ),
1806 JoinOperator::CrossApply => write!(f, " CROSS APPLY {}", self.relation),
1807 JoinOperator::OuterApply => write!(f, " OUTER APPLY {}", self.relation),
1808 JoinOperator::AsOf {
1809 match_condition,
1810 constraint,
1811 } => write!(
1812 f,
1813 " ASOF JOIN {} MATCH_CONDITION ({match_condition}){}",
1814 self.relation,
1815 suffix(constraint)
1816 ),
1817 }
1818 }
1819}
1820
1821#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1822#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1823#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1824pub enum JoinOperator {
1825 Inner(JoinConstraint),
1826 LeftOuter(JoinConstraint),
1827 RightOuter(JoinConstraint),
1828 FullOuter(JoinConstraint),
1829 CrossJoin,
1830 Semi(JoinConstraint),
1832 LeftSemi(JoinConstraint),
1834 RightSemi(JoinConstraint),
1836 Anti(JoinConstraint),
1838 LeftAnti(JoinConstraint),
1840 RightAnti(JoinConstraint),
1842 CrossApply,
1844 OuterApply,
1846 AsOf {
1851 match_condition: Expr,
1852 constraint: JoinConstraint,
1853 },
1854}
1855
1856#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1857#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1858#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1859pub enum JoinConstraint {
1860 On(Expr),
1861 Using(Vec<Ident>),
1862 Natural,
1863 None,
1864}
1865
1866#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1867#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1868#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1869pub struct OrderBy {
1870 pub exprs: Vec<OrderByExpr>,
1871 pub interpolate: Option<Interpolate>,
1876}
1877
1878impl fmt::Display for OrderBy {
1879 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1880 write!(f, "ORDER BY")?;
1881 if !self.exprs.is_empty() {
1882 write!(f, " {}", display_comma_separated(&self.exprs))?;
1883 }
1884 if let Some(ref interpolate) = self.interpolate {
1885 match &interpolate.exprs {
1886 Some(exprs) => write!(f, " INTERPOLATE ({})", display_comma_separated(exprs))?,
1887 None => write!(f, " INTERPOLATE")?,
1888 }
1889 }
1890 Ok(())
1891 }
1892}
1893
1894#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1896#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1897#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1898pub struct OrderByExpr {
1899 pub expr: Expr,
1900 pub asc: Option<bool>,
1902 pub nulls_first: Option<bool>,
1904 pub with_fill: Option<WithFill>,
1907}
1908
1909impl fmt::Display for OrderByExpr {
1910 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1911 write!(f, "{}", self.expr)?;
1912 match self.asc {
1913 Some(true) => write!(f, " ASC")?,
1914 Some(false) => write!(f, " DESC")?,
1915 None => (),
1916 }
1917 match self.nulls_first {
1918 Some(true) => write!(f, " NULLS FIRST")?,
1919 Some(false) => write!(f, " NULLS LAST")?,
1920 None => (),
1921 }
1922 if let Some(ref with_fill) = self.with_fill {
1923 write!(f, " {}", with_fill)?
1924 }
1925 Ok(())
1926 }
1927}
1928
1929#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1934#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1935#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1936pub struct WithFill {
1937 pub from: Option<Expr>,
1938 pub to: Option<Expr>,
1939 pub step: Option<Expr>,
1940}
1941
1942impl fmt::Display for WithFill {
1943 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1944 write!(f, "WITH FILL")?;
1945 if let Some(ref from) = self.from {
1946 write!(f, " FROM {}", from)?;
1947 }
1948 if let Some(ref to) = self.to {
1949 write!(f, " TO {}", to)?;
1950 }
1951 if let Some(ref step) = self.step {
1952 write!(f, " STEP {}", step)?;
1953 }
1954 Ok(())
1955 }
1956}
1957
1958#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1963#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1964#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1965pub struct InterpolateExpr {
1966 pub column: Ident,
1967 pub expr: Option<Expr>,
1968}
1969
1970#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1971#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1972#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1973pub struct Interpolate {
1974 pub exprs: Option<Vec<InterpolateExpr>>,
1975}
1976
1977impl fmt::Display for InterpolateExpr {
1978 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1979 write!(f, "{}", self.column)?;
1980 if let Some(ref expr) = self.expr {
1981 write!(f, " AS {}", expr)?;
1982 }
1983 Ok(())
1984 }
1985}
1986
1987#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1988#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1989#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1990pub struct Offset {
1991 pub value: Expr,
1992 pub rows: OffsetRows,
1993}
1994
1995impl fmt::Display for Offset {
1996 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1997 write!(f, "OFFSET {}{}", self.value, self.rows)
1998 }
1999}
2000
2001#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2003#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2004#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2005pub enum OffsetRows {
2006 None,
2008 Row,
2009 Rows,
2010}
2011
2012impl fmt::Display for OffsetRows {
2013 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2014 match self {
2015 OffsetRows::None => Ok(()),
2016 OffsetRows::Row => write!(f, " ROW"),
2017 OffsetRows::Rows => write!(f, " ROWS"),
2018 }
2019 }
2020}
2021
2022#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2023#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2024#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2025pub struct Fetch {
2026 pub with_ties: bool,
2027 pub percent: bool,
2028 pub quantity: Option<Expr>,
2029}
2030
2031impl fmt::Display for Fetch {
2032 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2033 let extension = if self.with_ties { "WITH TIES" } else { "ONLY" };
2034 if let Some(ref quantity) = self.quantity {
2035 let percent = if self.percent { " PERCENT" } else { "" };
2036 write!(f, "FETCH FIRST {quantity}{percent} ROWS {extension}")
2037 } else {
2038 write!(f, "FETCH FIRST ROWS {extension}")
2039 }
2040 }
2041}
2042
2043#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2044#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2045#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2046pub struct LockClause {
2047 pub lock_type: LockType,
2048 pub of: Option<ObjectName>,
2049 pub nonblock: Option<NonBlock>,
2050}
2051
2052impl fmt::Display for LockClause {
2053 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2054 write!(f, "FOR {}", &self.lock_type)?;
2055 if let Some(ref of) = self.of {
2056 write!(f, " OF {of}")?;
2057 }
2058 if let Some(ref nb) = self.nonblock {
2059 write!(f, " {nb}")?;
2060 }
2061 Ok(())
2062 }
2063}
2064
2065#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2066#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2067#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2068pub enum LockType {
2069 Share,
2070 Update,
2071}
2072
2073impl fmt::Display for LockType {
2074 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2075 let select_lock = match self {
2076 LockType::Share => "SHARE",
2077 LockType::Update => "UPDATE",
2078 };
2079 write!(f, "{select_lock}")
2080 }
2081}
2082
2083#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2084#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2085#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2086pub enum NonBlock {
2087 Nowait,
2088 SkipLocked,
2089}
2090
2091impl fmt::Display for NonBlock {
2092 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2093 let nonblock = match self {
2094 NonBlock::Nowait => "NOWAIT",
2095 NonBlock::SkipLocked => "SKIP LOCKED",
2096 };
2097 write!(f, "{nonblock}")
2098 }
2099}
2100
2101#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2102#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2103#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2104pub enum Distinct {
2105 Distinct,
2107
2108 On(Vec<Expr>),
2110}
2111
2112impl fmt::Display for Distinct {
2113 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2114 match self {
2115 Distinct::Distinct => write!(f, "DISTINCT"),
2116 Distinct::On(col_names) => {
2117 let col_names = display_comma_separated(col_names);
2118 write!(f, "DISTINCT ON ({col_names})")
2119 }
2120 }
2121 }
2122}
2123
2124#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2125#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2126#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2127pub struct Top {
2128 pub with_ties: bool,
2131 pub percent: bool,
2133 pub quantity: Option<TopQuantity>,
2134}
2135
2136#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2137#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2138#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2139pub enum TopQuantity {
2140 Expr(Expr),
2142 Constant(u64),
2144}
2145
2146impl fmt::Display for Top {
2147 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2148 let extension = if self.with_ties { " WITH TIES" } else { "" };
2149 if let Some(ref quantity) = self.quantity {
2150 let percent = if self.percent { " PERCENT" } else { "" };
2151 match quantity {
2152 TopQuantity::Expr(quantity) => write!(f, "TOP ({quantity}){percent}{extension}"),
2153 TopQuantity::Constant(quantity) => {
2154 write!(f, "TOP {quantity}{percent}{extension}")
2155 }
2156 }
2157 } else {
2158 write!(f, "TOP{extension}")
2159 }
2160 }
2161}
2162
2163#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2164#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2165#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2166pub struct Values {
2167 pub explicit_row: bool,
2170 pub rows: Vec<Vec<Expr>>,
2171}
2172
2173impl fmt::Display for Values {
2174 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2175 write!(f, "VALUES ")?;
2176 let prefix = if self.explicit_row { "ROW" } else { "" };
2177 let mut delim = "";
2178 for row in &self.rows {
2179 write!(f, "{delim}")?;
2180 delim = ", ";
2181 write!(f, "{prefix}({})", display_comma_separated(row))?;
2182 }
2183 Ok(())
2184 }
2185}
2186
2187#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2188#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2189#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2190pub struct SelectInto {
2191 pub temporary: bool,
2192 pub unlogged: bool,
2193 pub table: bool,
2194 pub name: ObjectName,
2195}
2196
2197impl fmt::Display for SelectInto {
2198 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2199 let temporary = if self.temporary { " TEMPORARY" } else { "" };
2200 let unlogged = if self.unlogged { " UNLOGGED" } else { "" };
2201 let table = if self.table { " TABLE" } else { "" };
2202
2203 write!(f, "INTO{}{}{} {}", temporary, unlogged, table, self.name)
2204 }
2205}
2206
2207#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2212#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2213#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2214pub enum GroupByWithModifier {
2215 Rollup,
2216 Cube,
2217 Totals,
2218}
2219
2220impl fmt::Display for GroupByWithModifier {
2221 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2222 match self {
2223 GroupByWithModifier::Rollup => write!(f, "WITH ROLLUP"),
2224 GroupByWithModifier::Cube => write!(f, "WITH CUBE"),
2225 GroupByWithModifier::Totals => write!(f, "WITH TOTALS"),
2226 }
2227 }
2228}
2229
2230#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2231#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2232#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2233pub enum GroupByExpr {
2234 All(Vec<GroupByWithModifier>),
2244
2245 Expressions(Vec<Expr>, Vec<GroupByWithModifier>),
2247}
2248
2249impl fmt::Display for GroupByExpr {
2250 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2251 match self {
2252 GroupByExpr::All(modifiers) => {
2253 write!(f, "GROUP BY ALL")?;
2254 if !modifiers.is_empty() {
2255 write!(f, " {}", display_separated(modifiers, " "))?;
2256 }
2257 Ok(())
2258 }
2259 GroupByExpr::Expressions(col_names, modifiers) => {
2260 let col_names = display_comma_separated(col_names);
2261 write!(f, "GROUP BY {col_names}")?;
2262 if !modifiers.is_empty() {
2263 write!(f, " {}", display_separated(modifiers, " "))?;
2264 }
2265 Ok(())
2266 }
2267 }
2268 }
2269}
2270
2271#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2275#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2276#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2277pub enum FormatClause {
2278 Identifier(Ident),
2279 Null,
2280}
2281
2282impl fmt::Display for FormatClause {
2283 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2284 match self {
2285 FormatClause::Identifier(ident) => write!(f, "FORMAT {}", ident),
2286 FormatClause::Null => write!(f, "FORMAT NULL"),
2287 }
2288 }
2289}
2290
2291#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2294#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2295#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2296pub enum ForClause {
2297 Browse,
2298 Json {
2299 for_json: ForJson,
2300 root: Option<String>,
2301 include_null_values: bool,
2302 without_array_wrapper: bool,
2303 },
2304 Xml {
2305 for_xml: ForXml,
2306 elements: bool,
2307 binary_base64: bool,
2308 root: Option<String>,
2309 r#type: bool,
2310 },
2311}
2312
2313impl fmt::Display for ForClause {
2314 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2315 match self {
2316 ForClause::Browse => write!(f, "FOR BROWSE"),
2317 ForClause::Json {
2318 for_json,
2319 root,
2320 include_null_values,
2321 without_array_wrapper,
2322 } => {
2323 write!(f, "FOR JSON ")?;
2324 write!(f, "{}", for_json)?;
2325 if let Some(root) = root {
2326 write!(f, ", ROOT('{}')", root)?;
2327 }
2328 if *include_null_values {
2329 write!(f, ", INCLUDE_NULL_VALUES")?;
2330 }
2331 if *without_array_wrapper {
2332 write!(f, ", WITHOUT_ARRAY_WRAPPER")?;
2333 }
2334 Ok(())
2335 }
2336 ForClause::Xml {
2337 for_xml,
2338 elements,
2339 binary_base64,
2340 root,
2341 r#type,
2342 } => {
2343 write!(f, "FOR XML ")?;
2344 write!(f, "{}", for_xml)?;
2345 if *binary_base64 {
2346 write!(f, ", BINARY BASE64")?;
2347 }
2348 if *r#type {
2349 write!(f, ", TYPE")?;
2350 }
2351 if let Some(root) = root {
2352 write!(f, ", ROOT('{}')", root)?;
2353 }
2354 if *elements {
2355 write!(f, ", ELEMENTS")?;
2356 }
2357 Ok(())
2358 }
2359 }
2360 }
2361}
2362
2363#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2364#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2365#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2366pub enum ForXml {
2367 Raw(Option<String>),
2368 Auto,
2369 Explicit,
2370 Path(Option<String>),
2371}
2372
2373impl fmt::Display for ForXml {
2374 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2375 match self {
2376 ForXml::Raw(root) => {
2377 write!(f, "RAW")?;
2378 if let Some(root) = root {
2379 write!(f, "('{}')", root)?;
2380 }
2381 Ok(())
2382 }
2383 ForXml::Auto => write!(f, "AUTO"),
2384 ForXml::Explicit => write!(f, "EXPLICIT"),
2385 ForXml::Path(root) => {
2386 write!(f, "PATH")?;
2387 if let Some(root) = root {
2388 write!(f, "('{}')", root)?;
2389 }
2390 Ok(())
2391 }
2392 }
2393 }
2394}
2395
2396#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2397#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2398#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2399pub enum ForJson {
2400 Auto,
2401 Path,
2402}
2403
2404impl fmt::Display for ForJson {
2405 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2406 match self {
2407 ForJson::Auto => write!(f, "AUTO"),
2408 ForJson::Path => write!(f, "PATH"),
2409 }
2410 }
2411}
2412
2413#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2434#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2435#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2436pub enum JsonTableColumn {
2437 Named(JsonTableNamedColumn),
2439 ForOrdinality(Ident),
2441 Nested(JsonTableNestedColumn),
2443}
2444
2445impl fmt::Display for JsonTableColumn {
2446 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2447 match self {
2448 JsonTableColumn::Named(json_table_named_column) => {
2449 write!(f, "{json_table_named_column}")
2450 }
2451 JsonTableColumn::ForOrdinality(ident) => write!(f, "{} FOR ORDINALITY", ident),
2452 JsonTableColumn::Nested(json_table_nested_column) => {
2453 write!(f, "{json_table_nested_column}")
2454 }
2455 }
2456 }
2457}
2458
2459#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2463#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2464#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2465pub struct JsonTableNestedColumn {
2466 pub path: Value,
2467 pub columns: Vec<JsonTableColumn>,
2468}
2469
2470impl fmt::Display for JsonTableNestedColumn {
2471 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2472 write!(
2473 f,
2474 "NESTED PATH {} COLUMNS ({})",
2475 self.path,
2476 display_comma_separated(&self.columns)
2477 )
2478 }
2479}
2480
2481#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2489#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2490#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2491pub struct JsonTableNamedColumn {
2492 pub name: Ident,
2494 pub r#type: DataType,
2496 pub path: Value,
2498 pub exists: bool,
2500 pub on_empty: Option<JsonTableColumnErrorHandling>,
2502 pub on_error: Option<JsonTableColumnErrorHandling>,
2504}
2505
2506impl fmt::Display for JsonTableNamedColumn {
2507 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2508 write!(
2509 f,
2510 "{} {}{} PATH {}",
2511 self.name,
2512 self.r#type,
2513 if self.exists { " EXISTS" } else { "" },
2514 self.path
2515 )?;
2516 if let Some(on_empty) = &self.on_empty {
2517 write!(f, " {} ON EMPTY", on_empty)?;
2518 }
2519 if let Some(on_error) = &self.on_error {
2520 write!(f, " {} ON ERROR", on_error)?;
2521 }
2522 Ok(())
2523 }
2524}
2525
2526#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2529#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2530#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2531pub enum JsonTableColumnErrorHandling {
2532 Null,
2533 Default(Value),
2534 Error,
2535}
2536
2537impl fmt::Display for JsonTableColumnErrorHandling {
2538 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2539 match self {
2540 JsonTableColumnErrorHandling::Null => write!(f, "NULL"),
2541 JsonTableColumnErrorHandling::Default(json_string) => {
2542 write!(f, "DEFAULT {}", json_string)
2543 }
2544 JsonTableColumnErrorHandling::Error => write!(f, "ERROR"),
2545 }
2546 }
2547}
2548
2549#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2557#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2558#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2559pub struct OpenJsonTableColumn {
2560 pub name: Ident,
2562 pub r#type: DataType,
2564 pub path: Option<String>,
2566 pub as_json: bool,
2568}
2569
2570impl fmt::Display for OpenJsonTableColumn {
2571 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2572 write!(f, "{} {}", self.name, self.r#type)?;
2573 if let Some(path) = &self.path {
2574 write!(f, " '{}'", value::escape_single_quote_string(path))?;
2575 }
2576 if self.as_json {
2577 write!(f, " AS JSON")?;
2578 }
2579 Ok(())
2580 }
2581}
2582
2583#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2588#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2589#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2590pub enum ValueTableMode {
2591 AsStruct,
2592 AsValue,
2593}
2594
2595impl fmt::Display for ValueTableMode {
2596 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2597 match self {
2598 ValueTableMode::AsStruct => write!(f, "AS STRUCT"),
2599 ValueTableMode::AsValue => write!(f, "AS VALUE"),
2600 }
2601 }
2602}