1#[cfg(not(feature = "std"))]
22use alloc::{
23 boxed::Box,
24 format,
25 string::{String, ToString},
26 vec,
27 vec::Vec,
28};
29use core::fmt::{self, Display, Write};
30
31#[cfg(feature = "serde")]
32use serde::{Deserialize, Serialize};
33
34#[cfg(feature = "visitor")]
35use sqlparser_derive::{Visit, VisitMut};
36
37use crate::ast::value::escape_single_quote_string;
38use crate::ast::{
39 display_comma_separated, display_separated,
40 table_constraints::{
41 CheckConstraint, ForeignKeyConstraint, PrimaryKeyConstraint, TableConstraint,
42 UniqueConstraint,
43 },
44 ArgMode, AttachedToken, CommentDef, ConditionalStatements, CreateFunctionBody,
45 CreateFunctionUsing, CreateServerOption, CreateTableLikeKind, CreateTableOptions,
46 CreateViewParams, DataType, Expr,
47 FileFormat, FunctionBehavior, FunctionCalledOnNull, FunctionDefinitionSetParam, FunctionDesc,
48 FunctionDeterminismSpecifier, FunctionParallel, FunctionSecurity, HiveDistributionStyle,
49 HiveFormat, HiveIOFormat, HiveRowFormat, HiveSetLocation, Ident, InitializeKind,
50 MySQLColumnPosition, ObjectName, OnCommit, OneOrManyWithParens, OperateFunctionArg,
51 OrderByExpr, ProjectionSelect, Query, RefreshModeKind, ResetConfig, RowAccessPolicy,
52 SequenceOptions, Spanned, SqlOption, StorageLifecyclePolicy, StorageSerializationPolicy,
53 TableVersion, Tag, TriggerEvent, TriggerExecBody, TriggerObject, TriggerPeriod,
54 TriggerReferencing, Value, ValueWithSpan, WrappedCollection,
55};
56use crate::display_utils::{DisplayCommaSeparated, Indent, NewLine, SpaceOrNewline};
57use crate::keywords::Keyword;
58use crate::tokenizer::{Span, Token};
59
60#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
62#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
63#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
64pub struct IndexColumn {
65 pub column: OrderByExpr,
67 pub operator_class: Option<ObjectName>,
69}
70
71impl From<Ident> for IndexColumn {
72 fn from(c: Ident) -> Self {
73 Self {
74 column: OrderByExpr::from(c),
75 operator_class: None,
76 }
77 }
78}
79
80impl<'a> From<&'a str> for IndexColumn {
81 fn from(c: &'a str) -> Self {
82 let ident = Ident::new(c);
83 ident.into()
84 }
85}
86
87impl fmt::Display for IndexColumn {
88 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
89 write!(f, "{}", self.column)?;
90 if let Some(operator_class) = &self.operator_class {
91 write!(f, " {operator_class}")?;
92 }
93 Ok(())
94 }
95}
96
97#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
100#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
101#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
102pub enum ReplicaIdentity {
103 Nothing,
105 Full,
107 Default,
109 Index(Ident),
111}
112
113impl fmt::Display for ReplicaIdentity {
114 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
115 match self {
116 ReplicaIdentity::Nothing => f.write_str("NOTHING"),
117 ReplicaIdentity::Full => f.write_str("FULL"),
118 ReplicaIdentity::Default => f.write_str("DEFAULT"),
119 ReplicaIdentity::Index(idx) => write!(f, "USING INDEX {idx}"),
120 }
121 }
122}
123
124#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
126#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
127#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
128pub enum AlterTableOperation {
129 AddConstraint {
131 constraint: TableConstraint,
133 not_valid: bool,
135 },
136 AddColumn {
138 column_keyword: bool,
140 if_not_exists: bool,
142 column_def: ColumnDef,
144 column_position: Option<MySQLColumnPosition>,
146 },
147 AddProjection {
152 if_not_exists: bool,
154 name: Ident,
156 select: ProjectionSelect,
158 },
159 DropProjection {
164 if_exists: bool,
166 name: Ident,
168 },
169 MaterializeProjection {
174 if_exists: bool,
176 name: Ident,
178 partition: Option<Ident>,
180 },
181 ClearProjection {
186 if_exists: bool,
188 name: Ident,
190 partition: Option<Ident>,
192 },
193 DisableRowLevelSecurity,
198 DisableRule {
202 name: Ident,
204 },
205 DisableTrigger {
209 name: Ident,
211 },
212 DropConstraint {
214 if_exists: bool,
216 name: Ident,
218 drop_behavior: Option<DropBehavior>,
220 },
221 DropColumn {
223 has_column_keyword: bool,
225 column_names: Vec<Ident>,
227 if_exists: bool,
229 drop_behavior: Option<DropBehavior>,
231 },
232 AttachPartition {
236 partition: Partition,
240 },
241 DetachPartition {
245 partition: Partition,
248 },
249 FreezePartition {
253 partition: Partition,
255 with_name: Option<Ident>,
257 },
258 UnfreezePartition {
262 partition: Partition,
264 with_name: Option<Ident>,
266 },
267 DropPrimaryKey {
272 drop_behavior: Option<DropBehavior>,
274 },
275 DropForeignKey {
280 name: Ident,
282 drop_behavior: Option<DropBehavior>,
284 },
285 DropIndex {
289 name: Ident,
291 },
292 EnableAlwaysRule {
296 name: Ident,
298 },
299 EnableAlwaysTrigger {
303 name: Ident,
305 },
306 EnableReplicaRule {
310 name: Ident,
312 },
313 EnableReplicaTrigger {
317 name: Ident,
319 },
320 EnableRowLevelSecurity,
325 ForceRowLevelSecurity,
330 NoForceRowLevelSecurity,
335 EnableRule {
339 name: Ident,
341 },
342 EnableTrigger {
346 name: Ident,
348 },
349 RenamePartitions {
351 old_partitions: Vec<Expr>,
353 new_partitions: Vec<Expr>,
355 },
356 ReplicaIdentity {
361 identity: ReplicaIdentity,
363 },
364 AddPartitions {
366 if_not_exists: bool,
368 new_partitions: Vec<Partition>,
370 },
371 DropPartitions {
373 partitions: Vec<Expr>,
375 if_exists: bool,
377 },
378 RenameColumn {
380 old_column_name: Ident,
382 new_column_name: Ident,
384 },
385 RenameTable {
387 table_name: RenameTableNameKind,
389 },
390 ChangeColumn {
393 old_name: Ident,
395 new_name: Ident,
397 data_type: DataType,
399 options: Vec<ColumnOption>,
401 column_position: Option<MySQLColumnPosition>,
403 },
404 ModifyColumn {
407 col_name: Ident,
409 data_type: DataType,
411 options: Vec<ColumnOption>,
413 column_position: Option<MySQLColumnPosition>,
415 },
416 RenameConstraint {
421 old_name: Ident,
423 new_name: Ident,
425 },
426 AlterColumn {
429 column_name: Ident,
431 op: AlterColumnOperation,
433 },
434 SwapWith {
438 table_name: ObjectName,
440 },
441 SetTblProperties {
443 table_properties: Vec<SqlOption>,
445 },
446 OwnerTo {
450 new_owner: Owner,
452 },
453 ClusterBy {
456 exprs: Vec<Expr>,
458 },
459 DropClusteringKey,
461 AlterSortKey {
464 columns: Vec<Expr>,
466 },
467 SuspendRecluster,
469 ResumeRecluster,
471 Refresh {
477 subpath: Option<String>,
479 },
480 Suspend,
484 Resume,
488 Algorithm {
494 equals: bool,
496 algorithm: AlterTableAlgorithm,
498 },
499
500 Lock {
506 equals: bool,
508 lock: AlterTableLock,
510 },
511 AutoIncrement {
517 equals: bool,
519 value: ValueWithSpan,
521 },
522 ValidateConstraint {
524 name: Ident,
526 },
527 SetOptionsParens {
535 options: Vec<SqlOption>,
537 },
538 SetTablespace {
543 tablespace_name: Ident,
545 },
546}
547
548#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
552#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
553#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
554pub enum AlterPolicyOperation {
555 Rename {
557 new_name: Ident,
559 },
560 Apply {
562 to: Option<Vec<Owner>>,
564 using: Option<Expr>,
566 with_check: Option<Expr>,
568 },
569}
570
571impl fmt::Display for AlterPolicyOperation {
572 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
573 match self {
574 AlterPolicyOperation::Rename { new_name } => {
575 write!(f, " RENAME TO {new_name}")
576 }
577 AlterPolicyOperation::Apply {
578 to,
579 using,
580 with_check,
581 } => {
582 if let Some(to) = to {
583 write!(f, " TO {}", display_comma_separated(to))?;
584 }
585 if let Some(using) = using {
586 write!(f, " USING ({using})")?;
587 }
588 if let Some(with_check) = with_check {
589 write!(f, " WITH CHECK ({with_check})")?;
590 }
591 Ok(())
592 }
593 }
594 }
595}
596
597#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
601#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
602#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
603pub enum AlterTableAlgorithm {
605 Default,
607 Instant,
609 Inplace,
611 Copy,
613}
614
615impl fmt::Display for AlterTableAlgorithm {
616 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
617 f.write_str(match self {
618 Self::Default => "DEFAULT",
619 Self::Instant => "INSTANT",
620 Self::Inplace => "INPLACE",
621 Self::Copy => "COPY",
622 })
623 }
624}
625
626#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
630#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
631#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
632pub enum AlterTableLock {
634 Default,
636 None,
638 Shared,
640 Exclusive,
642}
643
644impl fmt::Display for AlterTableLock {
645 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
646 f.write_str(match self {
647 Self::Default => "DEFAULT",
648 Self::None => "NONE",
649 Self::Shared => "SHARED",
650 Self::Exclusive => "EXCLUSIVE",
651 })
652 }
653}
654
655#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
656#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
657#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
658pub enum Owner {
660 Ident(Ident),
662 CurrentRole,
664 CurrentUser,
666 SessionUser,
668}
669
670impl fmt::Display for Owner {
671 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
672 match self {
673 Owner::Ident(ident) => write!(f, "{ident}"),
674 Owner::CurrentRole => write!(f, "CURRENT_ROLE"),
675 Owner::CurrentUser => write!(f, "CURRENT_USER"),
676 Owner::SessionUser => write!(f, "SESSION_USER"),
677 }
678 }
679}
680
681#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
682#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
683#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
684pub enum AlterConnectorOwner {
686 User(Ident),
688 Role(Ident),
690}
691
692impl fmt::Display for AlterConnectorOwner {
693 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
694 match self {
695 AlterConnectorOwner::User(ident) => write!(f, "USER {ident}"),
696 AlterConnectorOwner::Role(ident) => write!(f, "ROLE {ident}"),
697 }
698 }
699}
700
701#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
702#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
703#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
704pub enum AlterIndexOperation {
706 RenameIndex {
708 index_name: ObjectName,
710 },
711 SetTablespace {
716 tablespace_name: Ident,
718 },
719}
720
721impl fmt::Display for AlterTableOperation {
722 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
723 match self {
724 AlterTableOperation::AddPartitions {
725 if_not_exists,
726 new_partitions,
727 } => write!(
728 f,
729 "ADD{ine} {}",
730 display_separated(new_partitions, " "),
731 ine = if *if_not_exists { " IF NOT EXISTS" } else { "" }
732 ),
733 AlterTableOperation::AddConstraint {
734 not_valid,
735 constraint,
736 } => {
737 write!(f, "ADD {constraint}")?;
738 if *not_valid {
739 write!(f, " NOT VALID")?;
740 }
741 Ok(())
742 }
743 AlterTableOperation::AddColumn {
744 column_keyword,
745 if_not_exists,
746 column_def,
747 column_position,
748 } => {
749 write!(f, "ADD")?;
750 if *column_keyword {
751 write!(f, " COLUMN")?;
752 }
753 if *if_not_exists {
754 write!(f, " IF NOT EXISTS")?;
755 }
756 write!(f, " {column_def}")?;
757
758 if let Some(position) = column_position {
759 write!(f, " {position}")?;
760 }
761
762 Ok(())
763 }
764 AlterTableOperation::AddProjection {
765 if_not_exists,
766 name,
767 select: query,
768 } => {
769 write!(f, "ADD PROJECTION")?;
770 if *if_not_exists {
771 write!(f, " IF NOT EXISTS")?;
772 }
773 write!(f, " {name} ({query})")
774 }
775 AlterTableOperation::Algorithm { equals, algorithm } => {
776 write!(
777 f,
778 "ALGORITHM {}{}",
779 if *equals { "= " } else { "" },
780 algorithm
781 )
782 }
783 AlterTableOperation::DropProjection { if_exists, name } => {
784 write!(f, "DROP PROJECTION")?;
785 if *if_exists {
786 write!(f, " IF EXISTS")?;
787 }
788 write!(f, " {name}")
789 }
790 AlterTableOperation::MaterializeProjection {
791 if_exists,
792 name,
793 partition,
794 } => {
795 write!(f, "MATERIALIZE PROJECTION")?;
796 if *if_exists {
797 write!(f, " IF EXISTS")?;
798 }
799 write!(f, " {name}")?;
800 if let Some(partition) = partition {
801 write!(f, " IN PARTITION {partition}")?;
802 }
803 Ok(())
804 }
805 AlterTableOperation::ClearProjection {
806 if_exists,
807 name,
808 partition,
809 } => {
810 write!(f, "CLEAR PROJECTION")?;
811 if *if_exists {
812 write!(f, " IF EXISTS")?;
813 }
814 write!(f, " {name}")?;
815 if let Some(partition) = partition {
816 write!(f, " IN PARTITION {partition}")?;
817 }
818 Ok(())
819 }
820 AlterTableOperation::AlterColumn { column_name, op } => {
821 write!(f, "ALTER COLUMN {column_name} {op}")
822 }
823 AlterTableOperation::DisableRowLevelSecurity => {
824 write!(f, "DISABLE ROW LEVEL SECURITY")
825 }
826 AlterTableOperation::DisableRule { name } => {
827 write!(f, "DISABLE RULE {name}")
828 }
829 AlterTableOperation::DisableTrigger { name } => {
830 write!(f, "DISABLE TRIGGER {name}")
831 }
832 AlterTableOperation::DropPartitions {
833 partitions,
834 if_exists,
835 } => write!(
836 f,
837 "DROP{ie} PARTITION ({})",
838 display_comma_separated(partitions),
839 ie = if *if_exists { " IF EXISTS" } else { "" }
840 ),
841 AlterTableOperation::DropConstraint {
842 if_exists,
843 name,
844 drop_behavior,
845 } => {
846 write!(
847 f,
848 "DROP CONSTRAINT {}{}",
849 if *if_exists { "IF EXISTS " } else { "" },
850 name
851 )?;
852 if let Some(drop_behavior) = drop_behavior {
853 write!(f, " {drop_behavior}")?;
854 }
855 Ok(())
856 }
857 AlterTableOperation::DropPrimaryKey { drop_behavior } => {
858 write!(f, "DROP PRIMARY KEY")?;
859 if let Some(drop_behavior) = drop_behavior {
860 write!(f, " {drop_behavior}")?;
861 }
862 Ok(())
863 }
864 AlterTableOperation::DropForeignKey {
865 name,
866 drop_behavior,
867 } => {
868 write!(f, "DROP FOREIGN KEY {name}")?;
869 if let Some(drop_behavior) = drop_behavior {
870 write!(f, " {drop_behavior}")?;
871 }
872 Ok(())
873 }
874 AlterTableOperation::DropIndex { name } => write!(f, "DROP INDEX {name}"),
875 AlterTableOperation::DropColumn {
876 has_column_keyword,
877 column_names: column_name,
878 if_exists,
879 drop_behavior,
880 } => {
881 write!(
882 f,
883 "DROP {}{}{}",
884 if *has_column_keyword { "COLUMN " } else { "" },
885 if *if_exists { "IF EXISTS " } else { "" },
886 display_comma_separated(column_name),
887 )?;
888 if let Some(drop_behavior) = drop_behavior {
889 write!(f, " {drop_behavior}")?;
890 }
891 Ok(())
892 }
893 AlterTableOperation::AttachPartition { partition } => {
894 write!(f, "ATTACH {partition}")
895 }
896 AlterTableOperation::DetachPartition { partition } => {
897 write!(f, "DETACH {partition}")
898 }
899 AlterTableOperation::EnableAlwaysRule { name } => {
900 write!(f, "ENABLE ALWAYS RULE {name}")
901 }
902 AlterTableOperation::EnableAlwaysTrigger { name } => {
903 write!(f, "ENABLE ALWAYS TRIGGER {name}")
904 }
905 AlterTableOperation::EnableReplicaRule { name } => {
906 write!(f, "ENABLE REPLICA RULE {name}")
907 }
908 AlterTableOperation::EnableReplicaTrigger { name } => {
909 write!(f, "ENABLE REPLICA TRIGGER {name}")
910 }
911 AlterTableOperation::EnableRowLevelSecurity => {
912 write!(f, "ENABLE ROW LEVEL SECURITY")
913 }
914 AlterTableOperation::ForceRowLevelSecurity => {
915 write!(f, "FORCE ROW LEVEL SECURITY")
916 }
917 AlterTableOperation::NoForceRowLevelSecurity => {
918 write!(f, "NO FORCE ROW LEVEL SECURITY")
919 }
920 AlterTableOperation::EnableRule { name } => {
921 write!(f, "ENABLE RULE {name}")
922 }
923 AlterTableOperation::EnableTrigger { name } => {
924 write!(f, "ENABLE TRIGGER {name}")
925 }
926 AlterTableOperation::RenamePartitions {
927 old_partitions,
928 new_partitions,
929 } => write!(
930 f,
931 "PARTITION ({}) RENAME TO PARTITION ({})",
932 display_comma_separated(old_partitions),
933 display_comma_separated(new_partitions)
934 ),
935 AlterTableOperation::RenameColumn {
936 old_column_name,
937 new_column_name,
938 } => write!(f, "RENAME COLUMN {old_column_name} TO {new_column_name}"),
939 AlterTableOperation::RenameTable { table_name } => {
940 write!(f, "RENAME {table_name}")
941 }
942 AlterTableOperation::ChangeColumn {
943 old_name,
944 new_name,
945 data_type,
946 options,
947 column_position,
948 } => {
949 write!(f, "CHANGE COLUMN {old_name} {new_name} {data_type}")?;
950 if !options.is_empty() {
951 write!(f, " {}", display_separated(options, " "))?;
952 }
953 if let Some(position) = column_position {
954 write!(f, " {position}")?;
955 }
956
957 Ok(())
958 }
959 AlterTableOperation::ModifyColumn {
960 col_name,
961 data_type,
962 options,
963 column_position,
964 } => {
965 write!(f, "MODIFY COLUMN {col_name} {data_type}")?;
966 if !options.is_empty() {
967 write!(f, " {}", display_separated(options, " "))?;
968 }
969 if let Some(position) = column_position {
970 write!(f, " {position}")?;
971 }
972
973 Ok(())
974 }
975 AlterTableOperation::RenameConstraint { old_name, new_name } => {
976 write!(f, "RENAME CONSTRAINT {old_name} TO {new_name}")
977 }
978 AlterTableOperation::SwapWith { table_name } => {
979 write!(f, "SWAP WITH {table_name}")
980 }
981 AlterTableOperation::OwnerTo { new_owner } => {
982 write!(f, "OWNER TO {new_owner}")
983 }
984 AlterTableOperation::SetTblProperties { table_properties } => {
985 write!(
986 f,
987 "SET TBLPROPERTIES({})",
988 display_comma_separated(table_properties)
989 )
990 }
991 AlterTableOperation::FreezePartition {
992 partition,
993 with_name,
994 } => {
995 write!(f, "FREEZE {partition}")?;
996 if let Some(name) = with_name {
997 write!(f, " WITH NAME {name}")?;
998 }
999 Ok(())
1000 }
1001 AlterTableOperation::UnfreezePartition {
1002 partition,
1003 with_name,
1004 } => {
1005 write!(f, "UNFREEZE {partition}")?;
1006 if let Some(name) = with_name {
1007 write!(f, " WITH NAME {name}")?;
1008 }
1009 Ok(())
1010 }
1011 AlterTableOperation::ClusterBy { exprs } => {
1012 write!(f, "CLUSTER BY ({})", display_comma_separated(exprs))?;
1013 Ok(())
1014 }
1015 AlterTableOperation::DropClusteringKey => {
1016 write!(f, "DROP CLUSTERING KEY")?;
1017 Ok(())
1018 }
1019 AlterTableOperation::AlterSortKey { columns } => {
1020 write!(f, "ALTER SORTKEY({})", display_comma_separated(columns))?;
1021 Ok(())
1022 }
1023 AlterTableOperation::SuspendRecluster => {
1024 write!(f, "SUSPEND RECLUSTER")?;
1025 Ok(())
1026 }
1027 AlterTableOperation::ResumeRecluster => {
1028 write!(f, "RESUME RECLUSTER")?;
1029 Ok(())
1030 }
1031 AlterTableOperation::Refresh { subpath } => {
1032 write!(f, "REFRESH")?;
1033 if let Some(path) = subpath {
1034 write!(f, " '{path}'")?;
1035 }
1036 Ok(())
1037 }
1038 AlterTableOperation::Suspend => {
1039 write!(f, "SUSPEND")
1040 }
1041 AlterTableOperation::Resume => {
1042 write!(f, "RESUME")
1043 }
1044 AlterTableOperation::AutoIncrement { equals, value } => {
1045 write!(
1046 f,
1047 "AUTO_INCREMENT {}{}",
1048 if *equals { "= " } else { "" },
1049 value
1050 )
1051 }
1052 AlterTableOperation::Lock { equals, lock } => {
1053 write!(f, "LOCK {}{}", if *equals { "= " } else { "" }, lock)
1054 }
1055 AlterTableOperation::ReplicaIdentity { identity } => {
1056 write!(f, "REPLICA IDENTITY {identity}")
1057 }
1058 AlterTableOperation::ValidateConstraint { name } => {
1059 write!(f, "VALIDATE CONSTRAINT {name}")
1060 }
1061 AlterTableOperation::SetOptionsParens { options } => {
1062 write!(f, "SET ({})", display_comma_separated(options))
1063 }
1064 AlterTableOperation::SetTablespace { tablespace_name } => {
1065 write!(f, "SET TABLESPACE {tablespace_name}")
1066 }
1067 }
1068 }
1069}
1070
1071impl fmt::Display for AlterIndexOperation {
1072 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1073 match self {
1074 AlterIndexOperation::RenameIndex { index_name } => {
1075 write!(f, "RENAME TO {index_name}")
1076 }
1077 AlterIndexOperation::SetTablespace { tablespace_name } => {
1078 write!(f, "SET TABLESPACE {tablespace_name}")
1079 }
1080 }
1081 }
1082}
1083
1084#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1086#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1087#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1088pub struct AlterType {
1089 pub name: ObjectName,
1091 pub operation: AlterTypeOperation,
1093}
1094
1095#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1097#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1098#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1099pub enum AlterTypeOperation {
1100 Rename(AlterTypeRename),
1102 AddValue(AlterTypeAddValue),
1104 RenameValue(AlterTypeRenameValue),
1106}
1107
1108#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1110#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1111#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1112pub struct AlterTypeRename {
1113 pub new_name: Ident,
1115}
1116
1117#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1119#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1120#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1121pub struct AlterTypeAddValue {
1122 pub if_not_exists: bool,
1124 pub value: Ident,
1126 pub position: Option<AlterTypeAddValuePosition>,
1128}
1129
1130#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1132#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1133#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1134pub enum AlterTypeAddValuePosition {
1135 Before(Ident),
1137 After(Ident),
1139}
1140
1141#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1143#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1144#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1145pub struct AlterTypeRenameValue {
1146 pub from: Ident,
1148 pub to: Ident,
1150}
1151
1152impl fmt::Display for AlterTypeOperation {
1153 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1154 match self {
1155 Self::Rename(AlterTypeRename { new_name }) => {
1156 write!(f, "RENAME TO {new_name}")
1157 }
1158 Self::AddValue(AlterTypeAddValue {
1159 if_not_exists,
1160 value,
1161 position,
1162 }) => {
1163 write!(f, "ADD VALUE")?;
1164 if *if_not_exists {
1165 write!(f, " IF NOT EXISTS")?;
1166 }
1167 write!(f, " {value}")?;
1168 match position {
1169 Some(AlterTypeAddValuePosition::Before(neighbor_value)) => {
1170 write!(f, " BEFORE {neighbor_value}")?;
1171 }
1172 Some(AlterTypeAddValuePosition::After(neighbor_value)) => {
1173 write!(f, " AFTER {neighbor_value}")?;
1174 }
1175 None => {}
1176 };
1177 Ok(())
1178 }
1179 Self::RenameValue(AlterTypeRenameValue { from, to }) => {
1180 write!(f, "RENAME VALUE {from} TO {to}")
1181 }
1182 }
1183 }
1184}
1185
1186#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1189#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1190#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1191pub struct AlterOperator {
1192 pub name: ObjectName,
1194 pub left_type: Option<DataType>,
1196 pub right_type: DataType,
1198 pub operation: AlterOperatorOperation,
1200}
1201
1202#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1204#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1205#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1206pub enum AlterOperatorOperation {
1207 OwnerTo(Owner),
1209 SetSchema {
1212 schema_name: ObjectName,
1214 },
1215 Set {
1217 options: Vec<OperatorOption>,
1219 },
1220}
1221
1222#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1224#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1225#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1226pub enum OperatorOption {
1227 Restrict(Option<ObjectName>),
1229 Join(Option<ObjectName>),
1231 Commutator(ObjectName),
1233 Negator(ObjectName),
1235 Hashes,
1237 Merges,
1239}
1240
1241impl fmt::Display for AlterOperator {
1242 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1243 write!(f, "ALTER OPERATOR {} (", self.name)?;
1244 if let Some(left_type) = &self.left_type {
1245 write!(f, "{}", left_type)?;
1246 } else {
1247 write!(f, "NONE")?;
1248 }
1249 write!(f, ", {}) {}", self.right_type, self.operation)
1250 }
1251}
1252
1253impl fmt::Display for AlterOperatorOperation {
1254 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1255 match self {
1256 Self::OwnerTo(owner) => write!(f, "OWNER TO {}", owner),
1257 Self::SetSchema { schema_name } => write!(f, "SET SCHEMA {}", schema_name),
1258 Self::Set { options } => {
1259 write!(f, "SET (")?;
1260 for (i, option) in options.iter().enumerate() {
1261 if i > 0 {
1262 write!(f, ", ")?;
1263 }
1264 write!(f, "{}", option)?;
1265 }
1266 write!(f, ")")
1267 }
1268 }
1269 }
1270}
1271
1272impl fmt::Display for OperatorOption {
1273 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1274 match self {
1275 Self::Restrict(Some(proc_name)) => write!(f, "RESTRICT = {}", proc_name),
1276 Self::Restrict(None) => write!(f, "RESTRICT = NONE"),
1277 Self::Join(Some(proc_name)) => write!(f, "JOIN = {}", proc_name),
1278 Self::Join(None) => write!(f, "JOIN = NONE"),
1279 Self::Commutator(op_name) => write!(f, "COMMUTATOR = {}", op_name),
1280 Self::Negator(op_name) => write!(f, "NEGATOR = {}", op_name),
1281 Self::Hashes => write!(f, "HASHES"),
1282 Self::Merges => write!(f, "MERGES"),
1283 }
1284 }
1285}
1286
1287#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1289#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1290#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1291pub enum AlterColumnOperation {
1292 SetNotNull,
1294 DropNotNull,
1296 SetDefault {
1299 value: Expr,
1301 },
1302 DropDefault,
1304 SetDataType {
1306 data_type: DataType,
1308 using: Option<Expr>,
1310 had_set: bool,
1312 },
1313
1314 AddGenerated {
1318 generated_as: Option<GeneratedAs>,
1320 sequence_options: Option<Vec<SequenceOptions>>,
1322 },
1323}
1324
1325impl fmt::Display for AlterColumnOperation {
1326 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1327 match self {
1328 AlterColumnOperation::SetNotNull => write!(f, "SET NOT NULL",),
1329 AlterColumnOperation::DropNotNull => write!(f, "DROP NOT NULL",),
1330 AlterColumnOperation::SetDefault { value } => {
1331 write!(f, "SET DEFAULT {value}")
1332 }
1333 AlterColumnOperation::DropDefault => {
1334 write!(f, "DROP DEFAULT")
1335 }
1336 AlterColumnOperation::SetDataType {
1337 data_type,
1338 using,
1339 had_set,
1340 } => {
1341 if *had_set {
1342 write!(f, "SET DATA ")?;
1343 }
1344 write!(f, "TYPE {data_type}")?;
1345 if let Some(expr) = using {
1346 write!(f, " USING {expr}")?;
1347 }
1348 Ok(())
1349 }
1350 AlterColumnOperation::AddGenerated {
1351 generated_as,
1352 sequence_options,
1353 } => {
1354 let generated_as = match generated_as {
1355 Some(GeneratedAs::Always) => " ALWAYS",
1356 Some(GeneratedAs::ByDefault) => " BY DEFAULT",
1357 _ => "",
1358 };
1359
1360 write!(f, "ADD GENERATED{generated_as} AS IDENTITY",)?;
1361 if let Some(options) = sequence_options {
1362 write!(f, " (")?;
1363
1364 for sequence_option in options {
1365 write!(f, "{sequence_option}")?;
1366 }
1367
1368 write!(f, " )")?;
1369 }
1370 Ok(())
1371 }
1372 }
1373 }
1374}
1375
1376#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1384#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1385#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1386pub enum KeyOrIndexDisplay {
1387 None,
1389 Key,
1391 Index,
1393}
1394
1395impl KeyOrIndexDisplay {
1396 pub fn is_none(self) -> bool {
1398 matches!(self, Self::None)
1399 }
1400}
1401
1402impl fmt::Display for KeyOrIndexDisplay {
1403 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1404 let left_space = matches!(f.align(), Some(fmt::Alignment::Right));
1405
1406 if left_space && !self.is_none() {
1407 f.write_char(' ')?
1408 }
1409
1410 match self {
1411 KeyOrIndexDisplay::None => {
1412 write!(f, "")
1413 }
1414 KeyOrIndexDisplay::Key => {
1415 write!(f, "KEY")
1416 }
1417 KeyOrIndexDisplay::Index => {
1418 write!(f, "INDEX")
1419 }
1420 }
1421 }
1422}
1423
1424#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1433#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1434#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1435pub enum IndexType {
1436 BTree,
1438 Hash,
1440 GIN,
1442 GiST,
1444 SPGiST,
1446 BRIN,
1448 Bloom,
1450 Custom(Ident),
1453}
1454
1455impl fmt::Display for IndexType {
1456 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1457 match self {
1458 Self::BTree => write!(f, "BTREE"),
1459 Self::Hash => write!(f, "HASH"),
1460 Self::GIN => write!(f, "GIN"),
1461 Self::GiST => write!(f, "GIST"),
1462 Self::SPGiST => write!(f, "SPGIST"),
1463 Self::BRIN => write!(f, "BRIN"),
1464 Self::Bloom => write!(f, "BLOOM"),
1465 Self::Custom(name) => write!(f, "{name}"),
1466 }
1467 }
1468}
1469
1470#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1476#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1477#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1478pub enum IndexOption {
1479 Using(IndexType),
1483 Comment(String),
1485}
1486
1487impl fmt::Display for IndexOption {
1488 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1489 match self {
1490 Self::Using(index_type) => write!(f, "USING {index_type}"),
1491 Self::Comment(s) => write!(f, "COMMENT '{s}'"),
1492 }
1493 }
1494}
1495
1496#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1500#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1501#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1502pub enum NullsDistinctOption {
1503 None,
1505 Distinct,
1507 NotDistinct,
1509}
1510
1511impl fmt::Display for NullsDistinctOption {
1512 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1513 match self {
1514 Self::None => Ok(()),
1515 Self::Distinct => write!(f, " NULLS DISTINCT"),
1516 Self::NotDistinct => write!(f, " NULLS NOT DISTINCT"),
1517 }
1518 }
1519}
1520
1521#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1522#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1523#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1524pub struct ProcedureParam {
1526 pub name: Ident,
1528 pub data_type: DataType,
1530 pub mode: Option<ArgMode>,
1532 pub default: Option<Expr>,
1534}
1535
1536impl fmt::Display for ProcedureParam {
1537 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1538 if let Some(mode) = &self.mode {
1539 if let Some(default) = &self.default {
1540 write!(f, "{mode} {} {} = {}", self.name, self.data_type, default)
1541 } else {
1542 write!(f, "{mode} {} {}", self.name, self.data_type)
1543 }
1544 } else if let Some(default) = &self.default {
1545 write!(f, "{} {} = {}", self.name, self.data_type, default)
1546 } else {
1547 write!(f, "{} {}", self.name, self.data_type)
1548 }
1549 }
1550}
1551
1552#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1554#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1555#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1556pub struct ColumnDef {
1557 pub name: Ident,
1559 pub data_type: DataType,
1561 pub options: Vec<ColumnOptionDef>,
1563}
1564
1565impl fmt::Display for ColumnDef {
1566 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1567 if self.data_type == DataType::Unspecified {
1568 write!(f, "{}", self.name)?;
1569 } else {
1570 write!(f, "{} {}", self.name, self.data_type)?;
1571 }
1572 for option in &self.options {
1573 write!(f, " {option}")?;
1574 }
1575 Ok(())
1576 }
1577}
1578
1579#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1596#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1597#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1598pub struct ViewColumnDef {
1599 pub name: Ident,
1601 pub data_type: Option<DataType>,
1603 pub options: Option<ColumnOptions>,
1605}
1606
1607#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1608#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1609#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1610pub enum ColumnOptions {
1612 CommaSeparated(Vec<ColumnOption>),
1614 SpaceSeparated(Vec<ColumnOption>),
1616}
1617
1618impl ColumnOptions {
1619 pub fn as_slice(&self) -> &[ColumnOption] {
1621 match self {
1622 ColumnOptions::CommaSeparated(options) => options.as_slice(),
1623 ColumnOptions::SpaceSeparated(options) => options.as_slice(),
1624 }
1625 }
1626}
1627
1628impl fmt::Display for ViewColumnDef {
1629 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1630 write!(f, "{}", self.name)?;
1631 if let Some(data_type) = self.data_type.as_ref() {
1632 write!(f, " {data_type}")?;
1633 }
1634 if let Some(options) = self.options.as_ref() {
1635 match options {
1636 ColumnOptions::CommaSeparated(column_options) => {
1637 write!(f, " {}", display_comma_separated(column_options.as_slice()))?;
1638 }
1639 ColumnOptions::SpaceSeparated(column_options) => {
1640 write!(f, " {}", display_separated(column_options.as_slice(), " "))?
1641 }
1642 }
1643 }
1644 Ok(())
1645 }
1646}
1647
1648#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1665#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1666#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1667pub struct ColumnOptionDef {
1668 pub name: Option<Ident>,
1670 pub option: ColumnOption,
1672}
1673
1674impl fmt::Display for ColumnOptionDef {
1675 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1676 write!(f, "{}{}", display_constraint_name(&self.name), self.option)
1677 }
1678}
1679
1680#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1688#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1689#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1690pub enum IdentityPropertyKind {
1691 Autoincrement(IdentityProperty),
1699 Identity(IdentityProperty),
1712}
1713
1714impl fmt::Display for IdentityPropertyKind {
1715 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1716 let (command, property) = match self {
1717 IdentityPropertyKind::Identity(property) => ("IDENTITY", property),
1718 IdentityPropertyKind::Autoincrement(property) => ("AUTOINCREMENT", property),
1719 };
1720 write!(f, "{command}")?;
1721 if let Some(parameters) = &property.parameters {
1722 write!(f, "{parameters}")?;
1723 }
1724 if let Some(order) = &property.order {
1725 write!(f, "{order}")?;
1726 }
1727 Ok(())
1728 }
1729}
1730
1731#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1733#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1734#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1735pub struct IdentityProperty {
1736 pub parameters: Option<IdentityPropertyFormatKind>,
1738 pub order: Option<IdentityPropertyOrder>,
1740}
1741
1742#[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 IdentityPropertyFormatKind {
1760 FunctionCall(IdentityParameters),
1768 StartAndIncrement(IdentityParameters),
1775}
1776
1777impl fmt::Display for IdentityPropertyFormatKind {
1778 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1779 match self {
1780 IdentityPropertyFormatKind::FunctionCall(parameters) => {
1781 write!(f, "({}, {})", parameters.seed, parameters.increment)
1782 }
1783 IdentityPropertyFormatKind::StartAndIncrement(parameters) => {
1784 write!(
1785 f,
1786 " START {} INCREMENT {}",
1787 parameters.seed, parameters.increment
1788 )
1789 }
1790 }
1791 }
1792}
1793#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1795#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1796#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1797pub struct IdentityParameters {
1798 pub seed: Expr,
1800 pub increment: Expr,
1802}
1803
1804#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1811#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1812#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1813pub enum IdentityPropertyOrder {
1814 Order,
1816 NoOrder,
1818}
1819
1820impl fmt::Display for IdentityPropertyOrder {
1821 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1822 match self {
1823 IdentityPropertyOrder::Order => write!(f, " ORDER"),
1824 IdentityPropertyOrder::NoOrder => write!(f, " NOORDER"),
1825 }
1826 }
1827}
1828
1829#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1837#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1838#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1839pub enum ColumnPolicy {
1840 MaskingPolicy(ColumnPolicyProperty),
1842 ProjectionPolicy(ColumnPolicyProperty),
1844}
1845
1846impl fmt::Display for ColumnPolicy {
1847 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1848 let (command, property) = match self {
1849 ColumnPolicy::MaskingPolicy(property) => ("MASKING POLICY", property),
1850 ColumnPolicy::ProjectionPolicy(property) => ("PROJECTION POLICY", property),
1851 };
1852 if property.with {
1853 write!(f, "WITH ")?;
1854 }
1855 write!(f, "{command} {}", property.policy_name)?;
1856 if let Some(using_columns) = &property.using_columns {
1857 write!(f, " USING ({})", display_comma_separated(using_columns))?;
1858 }
1859 Ok(())
1860 }
1861}
1862
1863#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1864#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1865#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1866pub struct ColumnPolicyProperty {
1868 pub with: bool,
1875 pub policy_name: ObjectName,
1877 pub using_columns: Option<Vec<Ident>>,
1879}
1880
1881#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1888#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1889#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1890pub struct TagsColumnOption {
1891 pub with: bool,
1898 pub tags: Vec<Tag>,
1900}
1901
1902impl fmt::Display for TagsColumnOption {
1903 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1904 if self.with {
1905 write!(f, "WITH ")?;
1906 }
1907 write!(f, "TAG ({})", display_comma_separated(&self.tags))?;
1908 Ok(())
1909 }
1910}
1911
1912#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1915#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1916#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1917pub enum ColumnOption {
1918 Null,
1920 NotNull,
1922 Default(Expr),
1924
1925 Materialized(Expr),
1930 Ephemeral(Option<Expr>),
1934 Alias(Expr),
1938
1939 PrimaryKey(PrimaryKeyConstraint),
1941 Unique(UniqueConstraint),
1943 ForeignKey(ForeignKeyConstraint),
1951 Check(CheckConstraint),
1953 DialectSpecific(Vec<Token>),
1957 CharacterSet(ObjectName),
1959 Collation(ObjectName),
1961 Comment(String),
1963 OnUpdate(Expr),
1965 Generated {
1968 generated_as: GeneratedAs,
1970 sequence_options: Option<Vec<SequenceOptions>>,
1972 generation_expr: Option<Expr>,
1974 generation_expr_mode: Option<GeneratedExpressionMode>,
1976 generated_keyword: bool,
1978 },
1979 Options(Vec<SqlOption>),
1987 Identity(IdentityPropertyKind),
1995 OnConflict(Keyword),
1998 Policy(ColumnPolicy),
2006 Tags(TagsColumnOption),
2013 Srid(Box<Expr>),
2020 Invisible,
2027}
2028
2029impl From<UniqueConstraint> for ColumnOption {
2030 fn from(c: UniqueConstraint) -> Self {
2031 ColumnOption::Unique(c)
2032 }
2033}
2034
2035impl From<PrimaryKeyConstraint> for ColumnOption {
2036 fn from(c: PrimaryKeyConstraint) -> Self {
2037 ColumnOption::PrimaryKey(c)
2038 }
2039}
2040
2041impl From<CheckConstraint> for ColumnOption {
2042 fn from(c: CheckConstraint) -> Self {
2043 ColumnOption::Check(c)
2044 }
2045}
2046impl From<ForeignKeyConstraint> for ColumnOption {
2047 fn from(fk: ForeignKeyConstraint) -> Self {
2048 ColumnOption::ForeignKey(fk)
2049 }
2050}
2051
2052impl fmt::Display for ColumnOption {
2053 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2054 use ColumnOption::*;
2055 match self {
2056 Null => write!(f, "NULL"),
2057 NotNull => write!(f, "NOT NULL"),
2058 Default(expr) => write!(f, "DEFAULT {expr}"),
2059 Materialized(expr) => write!(f, "MATERIALIZED {expr}"),
2060 Ephemeral(expr) => {
2061 if let Some(e) = expr {
2062 write!(f, "EPHEMERAL {e}")
2063 } else {
2064 write!(f, "EPHEMERAL")
2065 }
2066 }
2067 Alias(expr) => write!(f, "ALIAS {expr}"),
2068 PrimaryKey(constraint) => {
2069 write!(f, "PRIMARY KEY")?;
2070 if let Some(characteristics) = &constraint.characteristics {
2071 write!(f, " {characteristics}")?;
2072 }
2073 Ok(())
2074 }
2075 Unique(constraint) => {
2076 write!(f, "UNIQUE{:>}", constraint.index_type_display)?;
2077 if let Some(characteristics) = &constraint.characteristics {
2078 write!(f, " {characteristics}")?;
2079 }
2080 Ok(())
2081 }
2082 ForeignKey(constraint) => {
2083 write!(f, "REFERENCES {}", constraint.foreign_table)?;
2084 if !constraint.referred_columns.is_empty() {
2085 write!(
2086 f,
2087 " ({})",
2088 display_comma_separated(&constraint.referred_columns)
2089 )?;
2090 }
2091 if let Some(match_kind) = &constraint.match_kind {
2092 write!(f, " {match_kind}")?;
2093 }
2094 if let Some(action) = &constraint.on_delete {
2095 write!(f, " ON DELETE {action}")?;
2096 }
2097 if let Some(action) = &constraint.on_update {
2098 write!(f, " ON UPDATE {action}")?;
2099 }
2100 if let Some(characteristics) = &constraint.characteristics {
2101 write!(f, " {characteristics}")?;
2102 }
2103 Ok(())
2104 }
2105 Check(constraint) => write!(f, "{constraint}"),
2106 DialectSpecific(val) => write!(f, "{}", display_separated(val, " ")),
2107 CharacterSet(n) => write!(f, "CHARACTER SET {n}"),
2108 Collation(n) => write!(f, "COLLATE {n}"),
2109 Comment(v) => write!(f, "COMMENT '{}'", escape_single_quote_string(v)),
2110 OnUpdate(expr) => write!(f, "ON UPDATE {expr}"),
2111 Generated {
2112 generated_as,
2113 sequence_options,
2114 generation_expr,
2115 generation_expr_mode,
2116 generated_keyword,
2117 } => {
2118 if let Some(expr) = generation_expr {
2119 let modifier = match generation_expr_mode {
2120 None => "",
2121 Some(GeneratedExpressionMode::Virtual) => " VIRTUAL",
2122 Some(GeneratedExpressionMode::Stored) => " STORED",
2123 };
2124 if *generated_keyword {
2125 write!(f, "GENERATED ALWAYS AS ({expr}){modifier}")?;
2126 } else {
2127 write!(f, "AS ({expr}){modifier}")?;
2128 }
2129 Ok(())
2130 } else {
2131 let when = match generated_as {
2133 GeneratedAs::Always => "ALWAYS",
2134 GeneratedAs::ByDefault => "BY DEFAULT",
2135 GeneratedAs::ExpStored => "",
2137 };
2138 write!(f, "GENERATED {when} AS IDENTITY")?;
2139 if sequence_options.is_some() {
2140 let so = sequence_options.as_ref().unwrap();
2141 if !so.is_empty() {
2142 write!(f, " (")?;
2143 }
2144 for sequence_option in so {
2145 write!(f, "{sequence_option}")?;
2146 }
2147 if !so.is_empty() {
2148 write!(f, " )")?;
2149 }
2150 }
2151 Ok(())
2152 }
2153 }
2154 Options(options) => {
2155 write!(f, "OPTIONS({})", display_comma_separated(options))
2156 }
2157 Identity(parameters) => {
2158 write!(f, "{parameters}")
2159 }
2160 OnConflict(keyword) => {
2161 write!(f, "ON CONFLICT {keyword:?}")?;
2162 Ok(())
2163 }
2164 Policy(parameters) => {
2165 write!(f, "{parameters}")
2166 }
2167 Tags(tags) => {
2168 write!(f, "{tags}")
2169 }
2170 Srid(srid) => {
2171 write!(f, "SRID {srid}")
2172 }
2173 Invisible => {
2174 write!(f, "INVISIBLE")
2175 }
2176 }
2177 }
2178}
2179
2180#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
2183#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2184#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2185pub enum GeneratedAs {
2186 Always,
2188 ByDefault,
2190 ExpStored,
2192}
2193
2194#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
2197#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2198#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2199pub enum GeneratedExpressionMode {
2200 Virtual,
2202 Stored,
2204}
2205
2206#[must_use]
2207pub(crate) fn display_constraint_name(name: &'_ Option<Ident>) -> impl fmt::Display + '_ {
2208 struct ConstraintName<'a>(&'a Option<Ident>);
2209 impl fmt::Display for ConstraintName<'_> {
2210 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2211 if let Some(name) = self.0 {
2212 write!(f, "CONSTRAINT {name} ")?;
2213 }
2214 Ok(())
2215 }
2216 }
2217 ConstraintName(name)
2218}
2219
2220#[must_use]
2224pub(crate) fn display_option<'a, T: fmt::Display>(
2225 prefix: &'a str,
2226 postfix: &'a str,
2227 option: &'a Option<T>,
2228) -> impl fmt::Display + 'a {
2229 struct OptionDisplay<'a, T>(&'a str, &'a str, &'a Option<T>);
2230 impl<T: fmt::Display> fmt::Display for OptionDisplay<'_, T> {
2231 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2232 if let Some(inner) = self.2 {
2233 let (prefix, postfix) = (self.0, self.1);
2234 write!(f, "{prefix}{inner}{postfix}")?;
2235 }
2236 Ok(())
2237 }
2238 }
2239 OptionDisplay(prefix, postfix, option)
2240}
2241
2242#[must_use]
2246pub(crate) fn display_option_spaced<T: fmt::Display>(option: &Option<T>) -> impl fmt::Display + '_ {
2247 display_option(" ", "", option)
2248}
2249
2250#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Default, Eq, Ord, Hash)]
2254#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2255#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2256pub struct ConstraintCharacteristics {
2257 pub deferrable: Option<bool>,
2259 pub initially: Option<DeferrableInitial>,
2261 pub enforced: Option<bool>,
2263}
2264
2265#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2267#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2268#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2269pub enum DeferrableInitial {
2270 Immediate,
2272 Deferred,
2274}
2275
2276impl ConstraintCharacteristics {
2277 fn deferrable_text(&self) -> Option<&'static str> {
2278 self.deferrable.map(|deferrable| {
2279 if deferrable {
2280 "DEFERRABLE"
2281 } else {
2282 "NOT DEFERRABLE"
2283 }
2284 })
2285 }
2286
2287 fn initially_immediate_text(&self) -> Option<&'static str> {
2288 self.initially
2289 .map(|initially_immediate| match initially_immediate {
2290 DeferrableInitial::Immediate => "INITIALLY IMMEDIATE",
2291 DeferrableInitial::Deferred => "INITIALLY DEFERRED",
2292 })
2293 }
2294
2295 fn enforced_text(&self) -> Option<&'static str> {
2296 self.enforced.map(
2297 |enforced| {
2298 if enforced {
2299 "ENFORCED"
2300 } else {
2301 "NOT ENFORCED"
2302 }
2303 },
2304 )
2305 }
2306}
2307
2308impl fmt::Display for ConstraintCharacteristics {
2309 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2310 let deferrable = self.deferrable_text();
2311 let initially_immediate = self.initially_immediate_text();
2312 let enforced = self.enforced_text();
2313
2314 match (deferrable, initially_immediate, enforced) {
2315 (None, None, None) => Ok(()),
2316 (None, None, Some(enforced)) => write!(f, "{enforced}"),
2317 (None, Some(initial), None) => write!(f, "{initial}"),
2318 (None, Some(initial), Some(enforced)) => write!(f, "{initial} {enforced}"),
2319 (Some(deferrable), None, None) => write!(f, "{deferrable}"),
2320 (Some(deferrable), None, Some(enforced)) => write!(f, "{deferrable} {enforced}"),
2321 (Some(deferrable), Some(initial), None) => write!(f, "{deferrable} {initial}"),
2322 (Some(deferrable), Some(initial), Some(enforced)) => {
2323 write!(f, "{deferrable} {initial} {enforced}")
2324 }
2325 }
2326 }
2327}
2328
2329#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2334#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2335#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2336pub enum ReferentialAction {
2337 Restrict,
2339 Cascade,
2341 SetNull,
2343 NoAction,
2345 SetDefault,
2347}
2348
2349impl fmt::Display for ReferentialAction {
2350 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2351 f.write_str(match self {
2352 ReferentialAction::Restrict => "RESTRICT",
2353 ReferentialAction::Cascade => "CASCADE",
2354 ReferentialAction::SetNull => "SET NULL",
2355 ReferentialAction::NoAction => "NO ACTION",
2356 ReferentialAction::SetDefault => "SET DEFAULT",
2357 })
2358 }
2359}
2360
2361#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2365#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2366#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2367pub enum DropBehavior {
2368 Restrict,
2370 Cascade,
2372}
2373
2374impl fmt::Display for DropBehavior {
2375 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2376 f.write_str(match self {
2377 DropBehavior::Restrict => "RESTRICT",
2378 DropBehavior::Cascade => "CASCADE",
2379 })
2380 }
2381}
2382
2383#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2385#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2386#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2387pub enum UserDefinedTypeRepresentation {
2388 Composite {
2390 attributes: Vec<UserDefinedTypeCompositeAttributeDef>,
2392 },
2393 Enum {
2398 labels: Vec<Ident>,
2400 },
2401 Range {
2405 options: Vec<UserDefinedTypeRangeOption>,
2407 },
2408 SqlDefinition {
2414 options: Vec<UserDefinedTypeSqlDefinitionOption>,
2416 },
2417}
2418
2419impl fmt::Display for UserDefinedTypeRepresentation {
2420 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2421 match self {
2422 Self::Composite { attributes } => {
2423 write!(f, "AS ({})", display_comma_separated(attributes))
2424 }
2425 Self::Enum { labels } => {
2426 write!(f, "AS ENUM ({})", display_comma_separated(labels))
2427 }
2428 Self::Range { options } => {
2429 write!(f, "AS RANGE ({})", display_comma_separated(options))
2430 }
2431 Self::SqlDefinition { options } => {
2432 write!(f, "({})", display_comma_separated(options))
2433 }
2434 }
2435 }
2436}
2437
2438#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2440#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2441#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2442pub struct UserDefinedTypeCompositeAttributeDef {
2443 pub name: Ident,
2445 pub data_type: DataType,
2447 pub collation: Option<ObjectName>,
2449}
2450
2451impl fmt::Display for UserDefinedTypeCompositeAttributeDef {
2452 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2453 write!(f, "{} {}", self.name, self.data_type)?;
2454 if let Some(collation) = &self.collation {
2455 write!(f, " COLLATE {collation}")?;
2456 }
2457 Ok(())
2458 }
2459}
2460
2461#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2484#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2485#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2486pub enum UserDefinedTypeInternalLength {
2487 Fixed(u64),
2489 Variable,
2491}
2492
2493impl fmt::Display for UserDefinedTypeInternalLength {
2494 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2495 match self {
2496 UserDefinedTypeInternalLength::Fixed(n) => write!(f, "{}", n),
2497 UserDefinedTypeInternalLength::Variable => write!(f, "VARIABLE"),
2498 }
2499 }
2500}
2501
2502#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2521#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2522#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2523pub enum Alignment {
2524 Char,
2526 Int2,
2528 Int4,
2530 Double,
2532}
2533
2534impl fmt::Display for Alignment {
2535 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2536 match self {
2537 Alignment::Char => write!(f, "char"),
2538 Alignment::Int2 => write!(f, "int2"),
2539 Alignment::Int4 => write!(f, "int4"),
2540 Alignment::Double => write!(f, "double"),
2541 }
2542 }
2543}
2544
2545#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2565#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2566#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2567pub enum UserDefinedTypeStorage {
2568 Plain,
2570 External,
2572 Extended,
2574 Main,
2576}
2577
2578impl fmt::Display for UserDefinedTypeStorage {
2579 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2580 match self {
2581 UserDefinedTypeStorage::Plain => write!(f, "plain"),
2582 UserDefinedTypeStorage::External => write!(f, "external"),
2583 UserDefinedTypeStorage::Extended => write!(f, "extended"),
2584 UserDefinedTypeStorage::Main => write!(f, "main"),
2585 }
2586 }
2587}
2588
2589#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2607#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2608#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2609pub enum UserDefinedTypeRangeOption {
2610 Subtype(DataType),
2612 SubtypeOpClass(ObjectName),
2614 Collation(ObjectName),
2616 Canonical(ObjectName),
2618 SubtypeDiff(ObjectName),
2620 MultirangeTypeName(ObjectName),
2622}
2623
2624impl fmt::Display for UserDefinedTypeRangeOption {
2625 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2626 match self {
2627 UserDefinedTypeRangeOption::Subtype(dt) => write!(f, "SUBTYPE = {}", dt),
2628 UserDefinedTypeRangeOption::SubtypeOpClass(name) => {
2629 write!(f, "SUBTYPE_OPCLASS = {}", name)
2630 }
2631 UserDefinedTypeRangeOption::Collation(name) => write!(f, "COLLATION = {}", name),
2632 UserDefinedTypeRangeOption::Canonical(name) => write!(f, "CANONICAL = {}", name),
2633 UserDefinedTypeRangeOption::SubtypeDiff(name) => write!(f, "SUBTYPE_DIFF = {}", name),
2634 UserDefinedTypeRangeOption::MultirangeTypeName(name) => {
2635 write!(f, "MULTIRANGE_TYPE_NAME = {}", name)
2636 }
2637 }
2638 }
2639}
2640
2641#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2662#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2663#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2664pub enum UserDefinedTypeSqlDefinitionOption {
2665 Input(ObjectName),
2667 Output(ObjectName),
2669 Receive(ObjectName),
2671 Send(ObjectName),
2673 TypmodIn(ObjectName),
2675 TypmodOut(ObjectName),
2677 Analyze(ObjectName),
2679 Subscript(ObjectName),
2681 InternalLength(UserDefinedTypeInternalLength),
2683 PassedByValue,
2685 Alignment(Alignment),
2687 Storage(UserDefinedTypeStorage),
2689 Like(ObjectName),
2691 Category(char),
2693 Preferred(bool),
2695 Default(Expr),
2697 Element(DataType),
2699 Delimiter(String),
2701 Collatable(bool),
2703}
2704
2705impl fmt::Display for UserDefinedTypeSqlDefinitionOption {
2706 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2707 match self {
2708 UserDefinedTypeSqlDefinitionOption::Input(name) => write!(f, "INPUT = {}", name),
2709 UserDefinedTypeSqlDefinitionOption::Output(name) => write!(f, "OUTPUT = {}", name),
2710 UserDefinedTypeSqlDefinitionOption::Receive(name) => write!(f, "RECEIVE = {}", name),
2711 UserDefinedTypeSqlDefinitionOption::Send(name) => write!(f, "SEND = {}", name),
2712 UserDefinedTypeSqlDefinitionOption::TypmodIn(name) => write!(f, "TYPMOD_IN = {}", name),
2713 UserDefinedTypeSqlDefinitionOption::TypmodOut(name) => {
2714 write!(f, "TYPMOD_OUT = {}", name)
2715 }
2716 UserDefinedTypeSqlDefinitionOption::Analyze(name) => write!(f, "ANALYZE = {}", name),
2717 UserDefinedTypeSqlDefinitionOption::Subscript(name) => {
2718 write!(f, "SUBSCRIPT = {}", name)
2719 }
2720 UserDefinedTypeSqlDefinitionOption::InternalLength(len) => {
2721 write!(f, "INTERNALLENGTH = {}", len)
2722 }
2723 UserDefinedTypeSqlDefinitionOption::PassedByValue => write!(f, "PASSEDBYVALUE"),
2724 UserDefinedTypeSqlDefinitionOption::Alignment(align) => {
2725 write!(f, "ALIGNMENT = {}", align)
2726 }
2727 UserDefinedTypeSqlDefinitionOption::Storage(storage) => {
2728 write!(f, "STORAGE = {}", storage)
2729 }
2730 UserDefinedTypeSqlDefinitionOption::Like(name) => write!(f, "LIKE = {}", name),
2731 UserDefinedTypeSqlDefinitionOption::Category(c) => write!(f, "CATEGORY = '{}'", c),
2732 UserDefinedTypeSqlDefinitionOption::Preferred(b) => write!(f, "PREFERRED = {}", b),
2733 UserDefinedTypeSqlDefinitionOption::Default(expr) => write!(f, "DEFAULT = {}", expr),
2734 UserDefinedTypeSqlDefinitionOption::Element(dt) => write!(f, "ELEMENT = {}", dt),
2735 UserDefinedTypeSqlDefinitionOption::Delimiter(s) => {
2736 write!(f, "DELIMITER = '{}'", escape_single_quote_string(s))
2737 }
2738 UserDefinedTypeSqlDefinitionOption::Collatable(b) => write!(f, "COLLATABLE = {}", b),
2739 }
2740 }
2741}
2742
2743#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
2747#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2748#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2749pub enum Partition {
2750 Identifier(Ident),
2752 Expr(Expr),
2754 Part(Expr),
2757 Partitions(Vec<Expr>),
2759}
2760
2761impl fmt::Display for Partition {
2762 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2763 match self {
2764 Partition::Identifier(id) => write!(f, "PARTITION ID {id}"),
2765 Partition::Expr(expr) => write!(f, "PARTITION {expr}"),
2766 Partition::Part(expr) => write!(f, "PART {expr}"),
2767 Partition::Partitions(partitions) => {
2768 write!(f, "PARTITION ({})", display_comma_separated(partitions))
2769 }
2770 }
2771 }
2772}
2773
2774#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2777#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2778#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2779pub enum Deduplicate {
2780 All,
2782 ByExpression(Expr),
2784}
2785
2786impl fmt::Display for Deduplicate {
2787 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2788 match self {
2789 Deduplicate::All => write!(f, "DEDUPLICATE"),
2790 Deduplicate::ByExpression(expr) => write!(f, "DEDUPLICATE BY {expr}"),
2791 }
2792 }
2793}
2794
2795#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2800#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2801#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2802pub struct ClusteredBy {
2803 pub columns: Vec<Ident>,
2805 pub sorted_by: Option<Vec<OrderByExpr>>,
2807 pub num_buckets: Value,
2809}
2810
2811impl fmt::Display for ClusteredBy {
2812 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2813 write!(
2814 f,
2815 "CLUSTERED BY ({})",
2816 display_comma_separated(&self.columns)
2817 )?;
2818 if let Some(ref sorted_by) = self.sorted_by {
2819 write!(f, " SORTED BY ({})", display_comma_separated(sorted_by))?;
2820 }
2821 write!(f, " INTO {} BUCKETS", self.num_buckets)
2822 }
2823}
2824
2825#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2827#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2828#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2829pub struct CreateIndex {
2830 pub name: Option<ObjectName>,
2832 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
2833 pub table_name: ObjectName,
2835 pub using: Option<IndexType>,
2838 pub columns: Vec<IndexColumn>,
2840 pub unique: bool,
2842 pub concurrently: bool,
2844 pub if_not_exists: bool,
2846 pub include: Vec<Ident>,
2848 pub nulls_distinct: Option<bool>,
2850 pub with: Vec<Expr>,
2852 pub predicate: Option<Expr>,
2854 pub index_options: Vec<IndexOption>,
2856 pub alter_options: Vec<AlterTableOperation>,
2863}
2864
2865impl fmt::Display for CreateIndex {
2866 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2867 write!(
2868 f,
2869 "CREATE {unique}INDEX {concurrently}{if_not_exists}",
2870 unique = if self.unique { "UNIQUE " } else { "" },
2871 concurrently = if self.concurrently {
2872 "CONCURRENTLY "
2873 } else {
2874 ""
2875 },
2876 if_not_exists = if self.if_not_exists {
2877 "IF NOT EXISTS "
2878 } else {
2879 ""
2880 },
2881 )?;
2882 if let Some(value) = &self.name {
2883 write!(f, "{value} ")?;
2884 }
2885 write!(f, "ON {}", self.table_name)?;
2886 if let Some(value) = &self.using {
2887 write!(f, " USING {value} ")?;
2888 }
2889 write!(f, "({})", display_comma_separated(&self.columns))?;
2890 if !self.include.is_empty() {
2891 write!(f, " INCLUDE ({})", display_comma_separated(&self.include))?;
2892 }
2893 if let Some(value) = self.nulls_distinct {
2894 if value {
2895 write!(f, " NULLS DISTINCT")?;
2896 } else {
2897 write!(f, " NULLS NOT DISTINCT")?;
2898 }
2899 }
2900 if !self.with.is_empty() {
2901 write!(f, " WITH ({})", display_comma_separated(&self.with))?;
2902 }
2903 if let Some(predicate) = &self.predicate {
2904 write!(f, " WHERE {predicate}")?;
2905 }
2906 if !self.index_options.is_empty() {
2907 write!(f, " {}", display_separated(&self.index_options, " "))?;
2908 }
2909 if !self.alter_options.is_empty() {
2910 write!(f, " {}", display_separated(&self.alter_options, " "))?;
2911 }
2912 Ok(())
2913 }
2914}
2915
2916#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2918#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2919#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2920pub struct CreateTable {
2921 pub or_replace: bool,
2923 pub temporary: bool,
2925 pub external: bool,
2927 pub dynamic: bool,
2929 pub global: Option<bool>,
2931 pub if_not_exists: bool,
2933 pub transient: bool,
2935 pub volatile: bool,
2937 pub iceberg: bool,
2939 pub snapshot: bool,
2942 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
2944 pub name: ObjectName,
2945 pub columns: Vec<ColumnDef>,
2947 pub constraints: Vec<TableConstraint>,
2949 pub hive_distribution: HiveDistributionStyle,
2951 pub hive_formats: Option<HiveFormat>,
2953 pub table_options: CreateTableOptions,
2955 pub file_format: Option<FileFormat>,
2957 pub location: Option<String>,
2959 pub query: Option<Box<Query>>,
2961 pub without_rowid: bool,
2963 pub like: Option<CreateTableLikeKind>,
2965 pub clone: Option<ObjectName>,
2967 pub version: Option<TableVersion>,
2969 pub comment: Option<CommentDef>,
2973 pub on_commit: Option<OnCommit>,
2976 pub on_cluster: Option<Ident>,
2979 pub primary_key: Option<Box<Expr>>,
2982 pub order_by: Option<OneOrManyWithParens<Expr>>,
2986 pub partition_by: Option<Box<Expr>>,
2989 pub cluster_by: Option<WrappedCollection<Vec<Expr>>>,
2994 pub clustered_by: Option<ClusteredBy>,
2997 pub inherits: Option<Vec<ObjectName>>,
3002 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
3006 pub partition_of: Option<ObjectName>,
3007 pub for_values: Option<ForValues>,
3010 pub strict: bool,
3014 pub copy_grants: bool,
3017 pub enable_schema_evolution: Option<bool>,
3020 pub change_tracking: Option<bool>,
3023 pub data_retention_time_in_days: Option<u64>,
3026 pub max_data_extension_time_in_days: Option<u64>,
3029 pub default_ddl_collation: Option<String>,
3032 pub with_aggregation_policy: Option<ObjectName>,
3035 pub with_row_access_policy: Option<RowAccessPolicy>,
3038 pub with_storage_lifecycle_policy: Option<StorageLifecyclePolicy>,
3041 pub with_tags: Option<Vec<Tag>>,
3044 pub external_volume: Option<String>,
3047 pub base_location: Option<String>,
3050 pub catalog: Option<String>,
3053 pub catalog_sync: Option<String>,
3056 pub storage_serialization_policy: Option<StorageSerializationPolicy>,
3059 pub target_lag: Option<String>,
3062 pub warehouse: Option<Ident>,
3065 pub refresh_mode: Option<RefreshModeKind>,
3068 pub initialize: Option<InitializeKind>,
3071 pub require_user: bool,
3074 pub diststyle: Option<DistStyle>,
3077 pub distkey: Option<Expr>,
3080 pub sortkey: Option<Vec<Expr>>,
3083 pub backup: Option<bool>,
3086}
3087
3088impl fmt::Display for CreateTable {
3089 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3090 write!(
3098 f,
3099 "CREATE {or_replace}{external}{global}{temporary}{transient}{volatile}{dynamic}{iceberg}{snapshot}TABLE {if_not_exists}{name}",
3100 or_replace = if self.or_replace { "OR REPLACE " } else { "" },
3101 external = if self.external { "EXTERNAL " } else { "" },
3102 snapshot = if self.snapshot { "SNAPSHOT " } else { "" },
3103 global = self.global
3104 .map(|global| {
3105 if global {
3106 "GLOBAL "
3107 } else {
3108 "LOCAL "
3109 }
3110 })
3111 .unwrap_or(""),
3112 if_not_exists = if self.if_not_exists { "IF NOT EXISTS " } else { "" },
3113 temporary = if self.temporary { "TEMPORARY " } else { "" },
3114 transient = if self.transient { "TRANSIENT " } else { "" },
3115 volatile = if self.volatile { "VOLATILE " } else { "" },
3116 iceberg = if self.iceberg { "ICEBERG " } else { "" },
3118 dynamic = if self.dynamic { "DYNAMIC " } else { "" },
3119 name = self.name,
3120 )?;
3121 if let Some(partition_of) = &self.partition_of {
3122 write!(f, " PARTITION OF {partition_of}")?;
3123 }
3124 if let Some(on_cluster) = &self.on_cluster {
3125 write!(f, " ON CLUSTER {on_cluster}")?;
3126 }
3127 if !self.columns.is_empty() || !self.constraints.is_empty() {
3128 f.write_str(" (")?;
3129 NewLine.fmt(f)?;
3130 Indent(DisplayCommaSeparated(&self.columns)).fmt(f)?;
3131 if !self.columns.is_empty() && !self.constraints.is_empty() {
3132 f.write_str(",")?;
3133 SpaceOrNewline.fmt(f)?;
3134 }
3135 Indent(DisplayCommaSeparated(&self.constraints)).fmt(f)?;
3136 NewLine.fmt(f)?;
3137 f.write_str(")")?;
3138 } else if self.query.is_none()
3139 && self.like.is_none()
3140 && self.clone.is_none()
3141 && self.partition_of.is_none()
3142 {
3143 f.write_str(" ()")?;
3145 } else if let Some(CreateTableLikeKind::Parenthesized(like_in_columns_list)) = &self.like {
3146 write!(f, " ({like_in_columns_list})")?;
3147 }
3148 if let Some(for_values) = &self.for_values {
3149 write!(f, " {for_values}")?;
3150 }
3151
3152 if let Some(comment) = &self.comment {
3155 write!(f, " COMMENT '{comment}'")?;
3156 }
3157
3158 if self.without_rowid {
3160 write!(f, " WITHOUT ROWID")?;
3161 }
3162
3163 if let Some(CreateTableLikeKind::Plain(like)) = &self.like {
3164 write!(f, " {like}")?;
3165 }
3166
3167 if let Some(c) = &self.clone {
3168 write!(f, " CLONE {c}")?;
3169 }
3170
3171 if let Some(version) = &self.version {
3172 write!(f, " {version}")?;
3173 }
3174
3175 match &self.hive_distribution {
3176 HiveDistributionStyle::PARTITIONED { columns } => {
3177 write!(f, " PARTITIONED BY ({})", display_comma_separated(columns))?;
3178 }
3179 HiveDistributionStyle::SKEWED {
3180 columns,
3181 on,
3182 stored_as_directories,
3183 } => {
3184 write!(
3185 f,
3186 " SKEWED BY ({})) ON ({})",
3187 display_comma_separated(columns),
3188 display_comma_separated(on)
3189 )?;
3190 if *stored_as_directories {
3191 write!(f, " STORED AS DIRECTORIES")?;
3192 }
3193 }
3194 _ => (),
3195 }
3196
3197 if let Some(clustered_by) = &self.clustered_by {
3198 write!(f, " {clustered_by}")?;
3199 }
3200
3201 if let Some(HiveFormat {
3202 row_format,
3203 serde_properties,
3204 storage,
3205 location,
3206 }) = &self.hive_formats
3207 {
3208 match row_format {
3209 Some(HiveRowFormat::SERDE { class }) => write!(f, " ROW FORMAT SERDE '{class}'")?,
3210 Some(HiveRowFormat::DELIMITED { delimiters }) => {
3211 write!(f, " ROW FORMAT DELIMITED")?;
3212 if !delimiters.is_empty() {
3213 write!(f, " {}", display_separated(delimiters, " "))?;
3214 }
3215 }
3216 None => (),
3217 }
3218 match storage {
3219 Some(HiveIOFormat::IOF {
3220 input_format,
3221 output_format,
3222 }) => write!(
3223 f,
3224 " STORED AS INPUTFORMAT {input_format} OUTPUTFORMAT {output_format}"
3225 )?,
3226 Some(HiveIOFormat::FileFormat { format }) if !self.external => {
3227 write!(f, " STORED AS {format}")?
3228 }
3229 _ => (),
3230 }
3231 if let Some(serde_properties) = serde_properties.as_ref() {
3232 write!(
3233 f,
3234 " WITH SERDEPROPERTIES ({})",
3235 display_comma_separated(serde_properties)
3236 )?;
3237 }
3238 if !self.external {
3239 if let Some(loc) = location {
3240 write!(f, " LOCATION '{loc}'")?;
3241 }
3242 }
3243 }
3244 if self.external {
3245 if let Some(file_format) = self.file_format {
3246 write!(f, " STORED AS {file_format}")?;
3247 }
3248 if let Some(location) = &self.location {
3249 write!(f, " LOCATION '{location}'")?;
3250 }
3251 }
3252
3253 match &self.table_options {
3254 options @ CreateTableOptions::With(_)
3255 | options @ CreateTableOptions::Plain(_)
3256 | options @ CreateTableOptions::TableProperties(_) => write!(f, " {options}")?,
3257 _ => (),
3258 }
3259
3260 if let Some(primary_key) = &self.primary_key {
3261 write!(f, " PRIMARY KEY {primary_key}")?;
3262 }
3263 if let Some(order_by) = &self.order_by {
3264 write!(f, " ORDER BY {order_by}")?;
3265 }
3266 if let Some(inherits) = &self.inherits {
3267 write!(f, " INHERITS ({})", display_comma_separated(inherits))?;
3268 }
3269 if let Some(partition_by) = self.partition_by.as_ref() {
3270 write!(f, " PARTITION BY {partition_by}")?;
3271 }
3272 if let Some(cluster_by) = self.cluster_by.as_ref() {
3273 write!(f, " CLUSTER BY {cluster_by}")?;
3274 }
3275 if let options @ CreateTableOptions::Options(_) = &self.table_options {
3276 write!(f, " {options}")?;
3277 }
3278 if let Some(external_volume) = self.external_volume.as_ref() {
3279 write!(f, " EXTERNAL_VOLUME='{external_volume}'")?;
3280 }
3281
3282 if let Some(catalog) = self.catalog.as_ref() {
3283 write!(f, " CATALOG='{catalog}'")?;
3284 }
3285
3286 if self.iceberg {
3287 if let Some(base_location) = self.base_location.as_ref() {
3288 write!(f, " BASE_LOCATION='{base_location}'")?;
3289 }
3290 }
3291
3292 if let Some(catalog_sync) = self.catalog_sync.as_ref() {
3293 write!(f, " CATALOG_SYNC='{catalog_sync}'")?;
3294 }
3295
3296 if let Some(storage_serialization_policy) = self.storage_serialization_policy.as_ref() {
3297 write!(
3298 f,
3299 " STORAGE_SERIALIZATION_POLICY={storage_serialization_policy}"
3300 )?;
3301 }
3302
3303 if self.copy_grants {
3304 write!(f, " COPY GRANTS")?;
3305 }
3306
3307 if let Some(is_enabled) = self.enable_schema_evolution {
3308 write!(
3309 f,
3310 " ENABLE_SCHEMA_EVOLUTION={}",
3311 if is_enabled { "TRUE" } else { "FALSE" }
3312 )?;
3313 }
3314
3315 if let Some(is_enabled) = self.change_tracking {
3316 write!(
3317 f,
3318 " CHANGE_TRACKING={}",
3319 if is_enabled { "TRUE" } else { "FALSE" }
3320 )?;
3321 }
3322
3323 if let Some(data_retention_time_in_days) = self.data_retention_time_in_days {
3324 write!(
3325 f,
3326 " DATA_RETENTION_TIME_IN_DAYS={data_retention_time_in_days}",
3327 )?;
3328 }
3329
3330 if let Some(max_data_extension_time_in_days) = self.max_data_extension_time_in_days {
3331 write!(
3332 f,
3333 " MAX_DATA_EXTENSION_TIME_IN_DAYS={max_data_extension_time_in_days}",
3334 )?;
3335 }
3336
3337 if let Some(default_ddl_collation) = &self.default_ddl_collation {
3338 write!(f, " DEFAULT_DDL_COLLATION='{default_ddl_collation}'",)?;
3339 }
3340
3341 if let Some(with_aggregation_policy) = &self.with_aggregation_policy {
3342 write!(f, " WITH AGGREGATION POLICY {with_aggregation_policy}",)?;
3343 }
3344
3345 if let Some(row_access_policy) = &self.with_row_access_policy {
3346 write!(f, " {row_access_policy}",)?;
3347 }
3348
3349 if let Some(storage_lifecycle_policy) = &self.with_storage_lifecycle_policy {
3350 write!(f, " {storage_lifecycle_policy}",)?;
3351 }
3352
3353 if let Some(tag) = &self.with_tags {
3354 write!(f, " WITH TAG ({})", display_comma_separated(tag.as_slice()))?;
3355 }
3356
3357 if let Some(target_lag) = &self.target_lag {
3358 write!(f, " TARGET_LAG='{target_lag}'")?;
3359 }
3360
3361 if let Some(warehouse) = &self.warehouse {
3362 write!(f, " WAREHOUSE={warehouse}")?;
3363 }
3364
3365 if let Some(refresh_mode) = &self.refresh_mode {
3366 write!(f, " REFRESH_MODE={refresh_mode}")?;
3367 }
3368
3369 if let Some(initialize) = &self.initialize {
3370 write!(f, " INITIALIZE={initialize}")?;
3371 }
3372
3373 if self.require_user {
3374 write!(f, " REQUIRE USER")?;
3375 }
3376
3377 if self.on_commit.is_some() {
3378 let on_commit = match self.on_commit {
3379 Some(OnCommit::DeleteRows) => "ON COMMIT DELETE ROWS",
3380 Some(OnCommit::PreserveRows) => "ON COMMIT PRESERVE ROWS",
3381 Some(OnCommit::Drop) => "ON COMMIT DROP",
3382 None => "",
3383 };
3384 write!(f, " {on_commit}")?;
3385 }
3386 if self.strict {
3387 write!(f, " STRICT")?;
3388 }
3389 if let Some(backup) = self.backup {
3390 write!(f, " BACKUP {}", if backup { "YES" } else { "NO" })?;
3391 }
3392 if let Some(diststyle) = &self.diststyle {
3393 write!(f, " DISTSTYLE {diststyle}")?;
3394 }
3395 if let Some(distkey) = &self.distkey {
3396 write!(f, " DISTKEY({distkey})")?;
3397 }
3398 if let Some(sortkey) = &self.sortkey {
3399 write!(f, " SORTKEY({})", display_comma_separated(sortkey))?;
3400 }
3401 if let Some(query) = &self.query {
3402 write!(f, " AS {query}")?;
3403 }
3404 Ok(())
3405 }
3406}
3407
3408#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3414#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3415#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3416pub enum ForValues {
3417 In(Vec<Expr>),
3419 From {
3421 from: Vec<PartitionBoundValue>,
3423 to: Vec<PartitionBoundValue>,
3425 },
3426 With {
3428 modulus: u64,
3430 remainder: u64,
3432 },
3433 Default,
3435}
3436
3437impl fmt::Display for ForValues {
3438 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3439 match self {
3440 ForValues::In(values) => {
3441 write!(f, "FOR VALUES IN ({})", display_comma_separated(values))
3442 }
3443 ForValues::From { from, to } => {
3444 write!(
3445 f,
3446 "FOR VALUES FROM ({}) TO ({})",
3447 display_comma_separated(from),
3448 display_comma_separated(to)
3449 )
3450 }
3451 ForValues::With { modulus, remainder } => {
3452 write!(
3453 f,
3454 "FOR VALUES WITH (MODULUS {modulus}, REMAINDER {remainder})"
3455 )
3456 }
3457 ForValues::Default => write!(f, "DEFAULT"),
3458 }
3459 }
3460}
3461
3462#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3467#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3468#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3469pub enum PartitionBoundValue {
3470 Expr(Expr),
3472 MinValue,
3474 MaxValue,
3476}
3477
3478impl fmt::Display for PartitionBoundValue {
3479 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3480 match self {
3481 PartitionBoundValue::Expr(expr) => write!(f, "{expr}"),
3482 PartitionBoundValue::MinValue => write!(f, "MINVALUE"),
3483 PartitionBoundValue::MaxValue => write!(f, "MAXVALUE"),
3484 }
3485 }
3486}
3487
3488#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3492#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3493#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3494pub enum DistStyle {
3495 Auto,
3497 Even,
3499 Key,
3501 All,
3503}
3504
3505impl fmt::Display for DistStyle {
3506 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3507 match self {
3508 DistStyle::Auto => write!(f, "AUTO"),
3509 DistStyle::Even => write!(f, "EVEN"),
3510 DistStyle::Key => write!(f, "KEY"),
3511 DistStyle::All => write!(f, "ALL"),
3512 }
3513 }
3514}
3515
3516
3517#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3518#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3519#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3520pub struct CreateDomain {
3533 pub name: ObjectName,
3535 pub data_type: DataType,
3537 pub collation: Option<Ident>,
3539 pub default: Option<Expr>,
3541 pub constraints: Vec<TableConstraint>,
3543}
3544
3545impl fmt::Display for CreateDomain {
3546 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3547 write!(
3548 f,
3549 "CREATE DOMAIN {name} AS {data_type}",
3550 name = self.name,
3551 data_type = self.data_type
3552 )?;
3553 if let Some(collation) = &self.collation {
3554 write!(f, " COLLATE {collation}")?;
3555 }
3556 if let Some(default) = &self.default {
3557 write!(f, " DEFAULT {default}")?;
3558 }
3559 if !self.constraints.is_empty() {
3560 write!(f, " {}", display_separated(&self.constraints, " "))?;
3561 }
3562 Ok(())
3563 }
3564}
3565
3566#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3568#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3569#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3570pub enum FunctionReturnType {
3571 DataType(DataType),
3573 SetOf(DataType),
3577}
3578
3579impl fmt::Display for FunctionReturnType {
3580 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3581 match self {
3582 FunctionReturnType::DataType(data_type) => write!(f, "{data_type}"),
3583 FunctionReturnType::SetOf(data_type) => write!(f, "SETOF {data_type}"),
3584 }
3585 }
3586}
3587
3588#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3589#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3590#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3591pub struct CreateFunction {
3593 pub or_alter: bool,
3597 pub or_replace: bool,
3599 pub temporary: bool,
3601 pub if_not_exists: bool,
3603 pub name: ObjectName,
3605 pub args: Option<Vec<OperateFunctionArg>>,
3607 pub return_type: Option<FunctionReturnType>,
3609 pub function_body: Option<CreateFunctionBody>,
3617 pub behavior: Option<FunctionBehavior>,
3623 pub called_on_null: Option<FunctionCalledOnNull>,
3627 pub parallel: Option<FunctionParallel>,
3631 pub security: Option<FunctionSecurity>,
3635 pub set_params: Vec<FunctionDefinitionSetParam>,
3639 pub using: Option<CreateFunctionUsing>,
3641 pub language: Option<Ident>,
3649 pub determinism_specifier: Option<FunctionDeterminismSpecifier>,
3653 pub options: Option<Vec<SqlOption>>,
3657 pub remote_connection: Option<ObjectName>,
3667}
3668
3669impl fmt::Display for CreateFunction {
3670 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3671 write!(
3672 f,
3673 "CREATE {or_alter}{or_replace}{temp}FUNCTION {if_not_exists}{name}",
3674 name = self.name,
3675 temp = if self.temporary { "TEMPORARY " } else { "" },
3676 or_alter = if self.or_alter { "OR ALTER " } else { "" },
3677 or_replace = if self.or_replace { "OR REPLACE " } else { "" },
3678 if_not_exists = if self.if_not_exists {
3679 "IF NOT EXISTS "
3680 } else {
3681 ""
3682 },
3683 )?;
3684 if let Some(args) = &self.args {
3685 write!(f, "({})", display_comma_separated(args))?;
3686 }
3687 if let Some(return_type) = &self.return_type {
3688 write!(f, " RETURNS {return_type}")?;
3689 }
3690 if let Some(determinism_specifier) = &self.determinism_specifier {
3691 write!(f, " {determinism_specifier}")?;
3692 }
3693 if let Some(language) = &self.language {
3694 write!(f, " LANGUAGE {language}")?;
3695 }
3696 if let Some(behavior) = &self.behavior {
3697 write!(f, " {behavior}")?;
3698 }
3699 if let Some(called_on_null) = &self.called_on_null {
3700 write!(f, " {called_on_null}")?;
3701 }
3702 if let Some(parallel) = &self.parallel {
3703 write!(f, " {parallel}")?;
3704 }
3705 if let Some(security) = &self.security {
3706 write!(f, " {security}")?;
3707 }
3708 for set_param in &self.set_params {
3709 write!(f, " {set_param}")?;
3710 }
3711 if let Some(remote_connection) = &self.remote_connection {
3712 write!(f, " REMOTE WITH CONNECTION {remote_connection}")?;
3713 }
3714 if let Some(CreateFunctionBody::AsBeforeOptions { body, link_symbol }) = &self.function_body
3715 {
3716 write!(f, " AS {body}")?;
3717 if let Some(link_symbol) = link_symbol {
3718 write!(f, ", {link_symbol}")?;
3719 }
3720 }
3721 if let Some(CreateFunctionBody::Return(function_body)) = &self.function_body {
3722 write!(f, " RETURN {function_body}")?;
3723 }
3724 if let Some(CreateFunctionBody::AsReturnExpr(function_body)) = &self.function_body {
3725 write!(f, " AS RETURN {function_body}")?;
3726 }
3727 if let Some(CreateFunctionBody::AsReturnSelect(function_body)) = &self.function_body {
3728 write!(f, " AS RETURN {function_body}")?;
3729 }
3730 if let Some(using) = &self.using {
3731 write!(f, " {using}")?;
3732 }
3733 if let Some(options) = &self.options {
3734 write!(
3735 f,
3736 " OPTIONS({})",
3737 display_comma_separated(options.as_slice())
3738 )?;
3739 }
3740 if let Some(CreateFunctionBody::AsAfterOptions(function_body)) = &self.function_body {
3741 write!(f, " AS {function_body}")?;
3742 }
3743 if let Some(CreateFunctionBody::AsBeginEnd(bes)) = &self.function_body {
3744 write!(f, " AS {bes}")?;
3745 }
3746 Ok(())
3747 }
3748}
3749
3750#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3760#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3761#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3762pub struct CreateConnector {
3763 pub name: Ident,
3765 pub if_not_exists: bool,
3767 pub connector_type: Option<String>,
3769 pub url: Option<String>,
3771 pub comment: Option<CommentDef>,
3773 pub with_dcproperties: Option<Vec<SqlOption>>,
3775}
3776
3777impl fmt::Display for CreateConnector {
3778 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3779 write!(
3780 f,
3781 "CREATE CONNECTOR {if_not_exists}{name}",
3782 if_not_exists = if self.if_not_exists {
3783 "IF NOT EXISTS "
3784 } else {
3785 ""
3786 },
3787 name = self.name,
3788 )?;
3789
3790 if let Some(connector_type) = &self.connector_type {
3791 write!(f, " TYPE '{connector_type}'")?;
3792 }
3793
3794 if let Some(url) = &self.url {
3795 write!(f, " URL '{url}'")?;
3796 }
3797
3798 if let Some(comment) = &self.comment {
3799 write!(f, " COMMENT = '{comment}'")?;
3800 }
3801
3802 if let Some(with_dcproperties) = &self.with_dcproperties {
3803 write!(
3804 f,
3805 " WITH DCPROPERTIES({})",
3806 display_comma_separated(with_dcproperties)
3807 )?;
3808 }
3809
3810 Ok(())
3811 }
3812}
3813
3814#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3819#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3820#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3821pub enum AlterSchemaOperation {
3822 SetDefaultCollate {
3824 collate: Expr,
3826 },
3827 AddReplica {
3829 replica: Ident,
3831 options: Option<Vec<SqlOption>>,
3833 },
3834 DropReplica {
3836 replica: Ident,
3838 },
3839 SetOptionsParens {
3841 options: Vec<SqlOption>,
3843 },
3844 Rename {
3846 name: ObjectName,
3848 },
3849 OwnerTo {
3851 owner: Owner,
3853 },
3854}
3855
3856impl fmt::Display for AlterSchemaOperation {
3857 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3858 match self {
3859 AlterSchemaOperation::SetDefaultCollate { collate } => {
3860 write!(f, "SET DEFAULT COLLATE {collate}")
3861 }
3862 AlterSchemaOperation::AddReplica { replica, options } => {
3863 write!(f, "ADD REPLICA {replica}")?;
3864 if let Some(options) = options {
3865 write!(f, " OPTIONS ({})", display_comma_separated(options))?;
3866 }
3867 Ok(())
3868 }
3869 AlterSchemaOperation::DropReplica { replica } => write!(f, "DROP REPLICA {replica}"),
3870 AlterSchemaOperation::SetOptionsParens { options } => {
3871 write!(f, "SET OPTIONS ({})", display_comma_separated(options))
3872 }
3873 AlterSchemaOperation::Rename { name } => write!(f, "RENAME TO {name}"),
3874 AlterSchemaOperation::OwnerTo { owner } => write!(f, "OWNER TO {owner}"),
3875 }
3876 }
3877}
3878#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3884#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3885#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3886pub enum RenameTableNameKind {
3887 As(ObjectName),
3889 To(ObjectName),
3891}
3892
3893impl fmt::Display for RenameTableNameKind {
3894 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3895 match self {
3896 RenameTableNameKind::As(name) => write!(f, "AS {name}"),
3897 RenameTableNameKind::To(name) => write!(f, "TO {name}"),
3898 }
3899 }
3900}
3901
3902#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3903#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3904#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3905pub struct AlterSchema {
3907 pub name: ObjectName,
3909 pub if_exists: bool,
3911 pub operations: Vec<AlterSchemaOperation>,
3913}
3914
3915impl fmt::Display for AlterSchema {
3916 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3917 write!(f, "ALTER SCHEMA ")?;
3918 if self.if_exists {
3919 write!(f, "IF EXISTS ")?;
3920 }
3921 write!(f, "{}", self.name)?;
3922 for operation in &self.operations {
3923 write!(f, " {operation}")?;
3924 }
3925
3926 Ok(())
3927 }
3928}
3929
3930impl Spanned for RenameTableNameKind {
3931 fn span(&self) -> Span {
3932 match self {
3933 RenameTableNameKind::As(name) => name.span(),
3934 RenameTableNameKind::To(name) => name.span(),
3935 }
3936 }
3937}
3938
3939#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
3940#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3941#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3942pub enum TriggerObjectKind {
3944 For(TriggerObject),
3946 ForEach(TriggerObject),
3948}
3949
3950impl Display for TriggerObjectKind {
3951 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3952 match self {
3953 TriggerObjectKind::For(obj) => write!(f, "FOR {obj}"),
3954 TriggerObjectKind::ForEach(obj) => write!(f, "FOR EACH {obj}"),
3955 }
3956 }
3957}
3958
3959#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3960#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3961#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3962pub struct CreateTrigger {
3976 pub or_alter: bool,
3980 pub temporary: bool,
3997 pub or_replace: bool,
4007 pub is_constraint: bool,
4009 pub name: ObjectName,
4011 pub period: Option<TriggerPeriod>,
4040 pub period_before_table: bool,
4051 pub events: Vec<TriggerEvent>,
4053 pub table_name: ObjectName,
4055 pub referenced_table_name: Option<ObjectName>,
4058 pub referencing: Vec<TriggerReferencing>,
4060 pub trigger_object: Option<TriggerObjectKind>,
4065 pub condition: Option<Expr>,
4067 pub exec_body: Option<TriggerExecBody>,
4069 pub statements_as: bool,
4071 pub statements: Option<ConditionalStatements>,
4073 pub characteristics: Option<ConstraintCharacteristics>,
4075}
4076
4077impl Display for CreateTrigger {
4078 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4079 let CreateTrigger {
4080 or_alter,
4081 temporary,
4082 or_replace,
4083 is_constraint,
4084 name,
4085 period_before_table,
4086 period,
4087 events,
4088 table_name,
4089 referenced_table_name,
4090 referencing,
4091 trigger_object,
4092 condition,
4093 exec_body,
4094 statements_as,
4095 statements,
4096 characteristics,
4097 } = self;
4098 write!(
4099 f,
4100 "CREATE {temporary}{or_alter}{or_replace}{is_constraint}TRIGGER {name} ",
4101 temporary = if *temporary { "TEMPORARY " } else { "" },
4102 or_alter = if *or_alter { "OR ALTER " } else { "" },
4103 or_replace = if *or_replace { "OR REPLACE " } else { "" },
4104 is_constraint = if *is_constraint { "CONSTRAINT " } else { "" },
4105 )?;
4106
4107 if *period_before_table {
4108 if let Some(p) = period {
4109 write!(f, "{p} ")?;
4110 }
4111 if !events.is_empty() {
4112 write!(f, "{} ", display_separated(events, " OR "))?;
4113 }
4114 write!(f, "ON {table_name}")?;
4115 } else {
4116 write!(f, "ON {table_name} ")?;
4117 if let Some(p) = period {
4118 write!(f, "{p}")?;
4119 }
4120 if !events.is_empty() {
4121 write!(f, " {}", display_separated(events, ", "))?;
4122 }
4123 }
4124
4125 if let Some(referenced_table_name) = referenced_table_name {
4126 write!(f, " FROM {referenced_table_name}")?;
4127 }
4128
4129 if let Some(characteristics) = characteristics {
4130 write!(f, " {characteristics}")?;
4131 }
4132
4133 if !referencing.is_empty() {
4134 write!(f, " REFERENCING {}", display_separated(referencing, " "))?;
4135 }
4136
4137 if let Some(trigger_object) = trigger_object {
4138 write!(f, " {trigger_object}")?;
4139 }
4140 if let Some(condition) = condition {
4141 write!(f, " WHEN {condition}")?;
4142 }
4143 if let Some(exec_body) = exec_body {
4144 write!(f, " EXECUTE {exec_body}")?;
4145 }
4146 if let Some(statements) = statements {
4147 if *statements_as {
4148 write!(f, " AS")?;
4149 }
4150 write!(f, " {statements}")?;
4151 }
4152 Ok(())
4153 }
4154}
4155
4156#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4157#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4158#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4159pub struct DropTrigger {
4166 pub if_exists: bool,
4168 pub trigger_name: ObjectName,
4170 pub table_name: Option<ObjectName>,
4172 pub option: Option<ReferentialAction>,
4174}
4175
4176impl fmt::Display for DropTrigger {
4177 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4178 let DropTrigger {
4179 if_exists,
4180 trigger_name,
4181 table_name,
4182 option,
4183 } = self;
4184 write!(f, "DROP TRIGGER")?;
4185 if *if_exists {
4186 write!(f, " IF EXISTS")?;
4187 }
4188 match &table_name {
4189 Some(table_name) => write!(f, " {trigger_name} ON {table_name}")?,
4190 None => write!(f, " {trigger_name}")?,
4191 };
4192 if let Some(option) = option {
4193 write!(f, " {option}")?;
4194 }
4195 Ok(())
4196 }
4197}
4198
4199#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4205#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4206#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4207pub struct Truncate {
4208 pub table_names: Vec<super::TruncateTableTarget>,
4210 pub partitions: Option<Vec<Expr>>,
4212 pub table: bool,
4214 pub if_exists: bool,
4216 pub identity: Option<super::TruncateIdentityOption>,
4218 pub cascade: Option<super::CascadeOption>,
4220 pub on_cluster: Option<Ident>,
4223}
4224
4225impl fmt::Display for Truncate {
4226 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4227 let table = if self.table { "TABLE " } else { "" };
4228 let if_exists = if self.if_exists { "IF EXISTS " } else { "" };
4229
4230 write!(
4231 f,
4232 "TRUNCATE {table}{if_exists}{table_names}",
4233 table_names = display_comma_separated(&self.table_names)
4234 )?;
4235
4236 if let Some(identity) = &self.identity {
4237 match identity {
4238 super::TruncateIdentityOption::Restart => write!(f, " RESTART IDENTITY")?,
4239 super::TruncateIdentityOption::Continue => write!(f, " CONTINUE IDENTITY")?,
4240 }
4241 }
4242 if let Some(cascade) = &self.cascade {
4243 match cascade {
4244 super::CascadeOption::Cascade => write!(f, " CASCADE")?,
4245 super::CascadeOption::Restrict => write!(f, " RESTRICT")?,
4246 }
4247 }
4248
4249 if let Some(ref parts) = &self.partitions {
4250 if !parts.is_empty() {
4251 write!(f, " PARTITION ({})", display_comma_separated(parts))?;
4252 }
4253 }
4254 if let Some(on_cluster) = &self.on_cluster {
4255 write!(f, " ON CLUSTER {on_cluster}")?;
4256 }
4257 Ok(())
4258 }
4259}
4260
4261impl Spanned for Truncate {
4262 fn span(&self) -> Span {
4263 Span::union_iter(
4264 self.table_names.iter().map(|i| i.name.span()).chain(
4265 self.partitions
4266 .iter()
4267 .flat_map(|i| i.iter().map(|k| k.span())),
4268 ),
4269 )
4270 }
4271}
4272
4273#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4280#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4281#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4282pub struct Msck {
4283 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
4285 pub table_name: ObjectName,
4286 pub repair: bool,
4288 pub partition_action: Option<super::AddDropSync>,
4290}
4291
4292impl fmt::Display for Msck {
4293 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4294 write!(
4295 f,
4296 "MSCK {repair}TABLE {table}",
4297 repair = if self.repair { "REPAIR " } else { "" },
4298 table = self.table_name
4299 )?;
4300 if let Some(pa) = &self.partition_action {
4301 write!(f, " {pa}")?;
4302 }
4303 Ok(())
4304 }
4305}
4306
4307impl Spanned for Msck {
4308 fn span(&self) -> Span {
4309 self.table_name.span()
4310 }
4311}
4312
4313#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4315#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4316#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4317pub struct CreateView {
4318 pub or_alter: bool,
4322 pub or_replace: bool,
4324 pub materialized: bool,
4326 pub secure: bool,
4329 pub name: ObjectName,
4331 pub name_before_not_exists: bool,
4342 pub columns: Vec<ViewColumnDef>,
4344 pub query: Box<Query>,
4346 pub options: CreateTableOptions,
4348 pub cluster_by: Vec<Ident>,
4350 pub comment: Option<String>,
4353 pub with_no_schema_binding: bool,
4355 pub if_not_exists: bool,
4357 pub temporary: bool,
4359 pub copy_grants: bool,
4362 pub to: Option<ObjectName>,
4365 pub params: Option<CreateViewParams>,
4367 pub with_data: Option<bool>,
4372}
4373
4374impl fmt::Display for CreateView {
4375 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4376 write!(
4377 f,
4378 "CREATE {or_alter}{or_replace}",
4379 or_alter = if self.or_alter { "OR ALTER " } else { "" },
4380 or_replace = if self.or_replace { "OR REPLACE " } else { "" },
4381 )?;
4382 if let Some(ref params) = self.params {
4383 params.fmt(f)?;
4384 }
4385 write!(
4386 f,
4387 "{secure}{materialized}{temporary}VIEW {if_not_and_name}{to}",
4388 if_not_and_name = if self.if_not_exists {
4389 if self.name_before_not_exists {
4390 format!("{} IF NOT EXISTS", self.name)
4391 } else {
4392 format!("IF NOT EXISTS {}", self.name)
4393 }
4394 } else {
4395 format!("{}", self.name)
4396 },
4397 secure = if self.secure { "SECURE " } else { "" },
4398 materialized = if self.materialized {
4399 "MATERIALIZED "
4400 } else {
4401 ""
4402 },
4403 temporary = if self.temporary { "TEMPORARY " } else { "" },
4404 to = self
4405 .to
4406 .as_ref()
4407 .map(|to| format!(" TO {to}"))
4408 .unwrap_or_default()
4409 )?;
4410 if self.copy_grants {
4411 write!(f, " COPY GRANTS")?;
4412 }
4413 if !self.columns.is_empty() {
4414 write!(f, " ({})", display_comma_separated(&self.columns))?;
4415 }
4416 if matches!(self.options, CreateTableOptions::With(_)) {
4417 write!(f, " {}", self.options)?;
4418 }
4419 if let Some(ref comment) = self.comment {
4420 write!(f, " COMMENT = '{}'", escape_single_quote_string(comment))?;
4421 }
4422 if !self.cluster_by.is_empty() {
4423 write!(
4424 f,
4425 " CLUSTER BY ({})",
4426 display_comma_separated(&self.cluster_by)
4427 )?;
4428 }
4429 if matches!(self.options, CreateTableOptions::Options(_)) {
4430 write!(f, " {}", self.options)?;
4431 }
4432 f.write_str(" AS")?;
4433 SpaceOrNewline.fmt(f)?;
4434 self.query.fmt(f)?;
4435 if self.with_no_schema_binding {
4436 write!(f, " WITH NO SCHEMA BINDING")?;
4437 }
4438 match self.with_data {
4439 Some(true) => write!(f, " WITH DATA")?,
4440 Some(false) => write!(f, " WITH NO DATA")?,
4441 None => {}
4442 }
4443 Ok(())
4444 }
4445}
4446
4447#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4450#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4451#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4452pub struct CreateExtension {
4453 pub name: Ident,
4455 pub if_not_exists: bool,
4457 pub cascade: bool,
4459 pub schema: Option<Ident>,
4461 pub version: Option<Ident>,
4463}
4464
4465impl fmt::Display for CreateExtension {
4466 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4467 write!(
4468 f,
4469 "CREATE EXTENSION {if_not_exists}{name}",
4470 if_not_exists = if self.if_not_exists {
4471 "IF NOT EXISTS "
4472 } else {
4473 ""
4474 },
4475 name = self.name
4476 )?;
4477 if self.cascade || self.schema.is_some() || self.version.is_some() {
4478 write!(f, " WITH")?;
4479
4480 if let Some(name) = &self.schema {
4481 write!(f, " SCHEMA {name}")?;
4482 }
4483 if let Some(version) = &self.version {
4484 write!(f, " VERSION {version}")?;
4485 }
4486 if self.cascade {
4487 write!(f, " CASCADE")?;
4488 }
4489 }
4490
4491 Ok(())
4492 }
4493}
4494
4495impl Spanned for CreateExtension {
4496 fn span(&self) -> Span {
4497 Span::empty()
4498 }
4499}
4500
4501#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4509#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4510#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4511pub struct DropExtension {
4512 pub names: Vec<Ident>,
4514 pub if_exists: bool,
4516 pub cascade_or_restrict: Option<ReferentialAction>,
4518}
4519
4520impl fmt::Display for DropExtension {
4521 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4522 write!(f, "DROP EXTENSION")?;
4523 if self.if_exists {
4524 write!(f, " IF EXISTS")?;
4525 }
4526 write!(f, " {}", display_comma_separated(&self.names))?;
4527 if let Some(cascade_or_restrict) = &self.cascade_or_restrict {
4528 write!(f, " {cascade_or_restrict}")?;
4529 }
4530 Ok(())
4531 }
4532}
4533
4534impl Spanned for DropExtension {
4535 fn span(&self) -> Span {
4536 Span::empty()
4537 }
4538}
4539
4540#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4543#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4544#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4545pub struct CreateCollation {
4546 pub if_not_exists: bool,
4548 pub name: ObjectName,
4550 pub definition: CreateCollationDefinition,
4552}
4553
4554#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4556#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4557#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4558pub enum CreateCollationDefinition {
4559 From(ObjectName),
4565 Options(Vec<SqlOption>),
4571}
4572
4573impl fmt::Display for CreateCollation {
4574 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4575 write!(
4576 f,
4577 "CREATE COLLATION {if_not_exists}{name}",
4578 if_not_exists = if self.if_not_exists {
4579 "IF NOT EXISTS "
4580 } else {
4581 ""
4582 },
4583 name = self.name
4584 )?;
4585 match &self.definition {
4586 CreateCollationDefinition::From(existing_collation) => {
4587 write!(f, " FROM {existing_collation}")
4588 }
4589 CreateCollationDefinition::Options(options) => {
4590 write!(f, " ({})", display_comma_separated(options))
4591 }
4592 }
4593 }
4594}
4595
4596impl Spanned for CreateCollation {
4597 fn span(&self) -> Span {
4598 Span::empty()
4599 }
4600}
4601
4602#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4605#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4606#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4607pub struct AlterCollation {
4608 pub name: ObjectName,
4610 pub operation: AlterCollationOperation,
4612}
4613
4614#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4616#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4617#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4618pub enum AlterCollationOperation {
4619 RenameTo {
4625 new_name: Ident,
4627 },
4628 OwnerTo(Owner),
4634 SetSchema {
4640 schema_name: ObjectName,
4642 },
4643 RefreshVersion,
4649}
4650
4651impl fmt::Display for AlterCollationOperation {
4652 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4653 match self {
4654 AlterCollationOperation::RenameTo { new_name } => write!(f, "RENAME TO {new_name}"),
4655 AlterCollationOperation::OwnerTo(owner) => write!(f, "OWNER TO {owner}"),
4656 AlterCollationOperation::SetSchema { schema_name } => {
4657 write!(f, "SET SCHEMA {schema_name}")
4658 }
4659 AlterCollationOperation::RefreshVersion => write!(f, "REFRESH VERSION"),
4660 }
4661 }
4662}
4663
4664impl fmt::Display for AlterCollation {
4665 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4666 write!(f, "ALTER COLLATION {} {}", self.name, self.operation)
4667 }
4668}
4669
4670impl Spanned for AlterCollation {
4671 fn span(&self) -> Span {
4672 Span::empty()
4673 }
4674}
4675
4676#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4679#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4680#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4681pub enum AlterTableType {
4682 Iceberg,
4685 Dynamic,
4688 External,
4691}
4692
4693#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4695#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4696#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4697pub struct AlterTable {
4698 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
4700 pub name: ObjectName,
4701 pub if_exists: bool,
4703 pub only: bool,
4705 pub operations: Vec<AlterTableOperation>,
4707 pub location: Option<HiveSetLocation>,
4709 pub on_cluster: Option<Ident>,
4713 pub table_type: Option<AlterTableType>,
4715 pub end_token: AttachedToken,
4717}
4718
4719impl fmt::Display for AlterTable {
4720 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4721 match &self.table_type {
4722 Some(AlterTableType::Iceberg) => write!(f, "ALTER ICEBERG TABLE ")?,
4723 Some(AlterTableType::Dynamic) => write!(f, "ALTER DYNAMIC TABLE ")?,
4724 Some(AlterTableType::External) => write!(f, "ALTER EXTERNAL TABLE ")?,
4725 None => write!(f, "ALTER TABLE ")?,
4726 }
4727
4728 if self.if_exists {
4729 write!(f, "IF EXISTS ")?;
4730 }
4731 if self.only {
4732 write!(f, "ONLY ")?;
4733 }
4734 write!(f, "{} ", &self.name)?;
4735 if let Some(cluster) = &self.on_cluster {
4736 write!(f, "ON CLUSTER {cluster} ")?;
4737 }
4738 write!(f, "{}", display_comma_separated(&self.operations))?;
4739 if let Some(loc) = &self.location {
4740 write!(f, " {loc}")?
4741 }
4742 Ok(())
4743 }
4744}
4745
4746#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4748#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4749#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4750pub struct DropFunction {
4751 pub if_exists: bool,
4753 pub func_desc: Vec<FunctionDesc>,
4755 pub drop_behavior: Option<DropBehavior>,
4757}
4758
4759impl fmt::Display for DropFunction {
4760 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4761 write!(
4762 f,
4763 "DROP FUNCTION{} {}",
4764 if self.if_exists { " IF EXISTS" } else { "" },
4765 display_comma_separated(&self.func_desc),
4766 )?;
4767 if let Some(op) = &self.drop_behavior {
4768 write!(f, " {op}")?;
4769 }
4770 Ok(())
4771 }
4772}
4773
4774impl Spanned for DropFunction {
4775 fn span(&self) -> Span {
4776 Span::empty()
4777 }
4778}
4779
4780#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4783#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4784#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4785pub struct CreateOperator {
4786 pub name: ObjectName,
4788 pub function: ObjectName,
4790 pub is_procedure: bool,
4792 pub left_arg: Option<DataType>,
4794 pub right_arg: Option<DataType>,
4796 pub options: Vec<OperatorOption>,
4798}
4799
4800#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4803#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4804#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4805pub struct CreateOperatorFamily {
4806 pub name: ObjectName,
4808 pub using: Ident,
4810}
4811
4812#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4815#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4816#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4817pub struct CreateOperatorClass {
4818 pub name: ObjectName,
4820 pub default: bool,
4822 pub for_type: DataType,
4824 pub using: Ident,
4826 pub family: Option<ObjectName>,
4828 pub items: Vec<OperatorClassItem>,
4830}
4831
4832impl fmt::Display for CreateOperator {
4833 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4834 write!(f, "CREATE OPERATOR {} (", self.name)?;
4835
4836 let function_keyword = if self.is_procedure {
4837 "PROCEDURE"
4838 } else {
4839 "FUNCTION"
4840 };
4841 let mut params = vec![format!("{} = {}", function_keyword, self.function)];
4842
4843 if let Some(left_arg) = &self.left_arg {
4844 params.push(format!("LEFTARG = {}", left_arg));
4845 }
4846 if let Some(right_arg) = &self.right_arg {
4847 params.push(format!("RIGHTARG = {}", right_arg));
4848 }
4849
4850 for option in &self.options {
4851 params.push(option.to_string());
4852 }
4853
4854 write!(f, "{}", params.join(", "))?;
4855 write!(f, ")")
4856 }
4857}
4858
4859impl fmt::Display for CreateOperatorFamily {
4860 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4861 write!(
4862 f,
4863 "CREATE OPERATOR FAMILY {} USING {}",
4864 self.name, self.using
4865 )
4866 }
4867}
4868
4869impl fmt::Display for CreateOperatorClass {
4870 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4871 write!(f, "CREATE OPERATOR CLASS {}", self.name)?;
4872 if self.default {
4873 write!(f, " DEFAULT")?;
4874 }
4875 write!(f, " FOR TYPE {} USING {}", self.for_type, self.using)?;
4876 if let Some(family) = &self.family {
4877 write!(f, " FAMILY {}", family)?;
4878 }
4879 write!(f, " AS {}", display_comma_separated(&self.items))
4880 }
4881}
4882
4883#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4885#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4886#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4887pub struct OperatorArgTypes {
4888 pub left: DataType,
4890 pub right: DataType,
4892}
4893
4894impl fmt::Display for OperatorArgTypes {
4895 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4896 write!(f, "{}, {}", self.left, self.right)
4897 }
4898}
4899
4900#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4902#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4903#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4904pub enum OperatorClassItem {
4905 Operator {
4907 strategy_number: u64,
4909 operator_name: ObjectName,
4911 op_types: Option<OperatorArgTypes>,
4913 purpose: Option<OperatorPurpose>,
4915 },
4916 Function {
4918 support_number: u64,
4920 op_types: Option<Vec<DataType>>,
4922 function_name: ObjectName,
4924 argument_types: Vec<DataType>,
4926 },
4927 Storage {
4929 storage_type: DataType,
4931 },
4932}
4933
4934#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4936#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4937#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4938pub enum OperatorPurpose {
4939 ForSearch,
4941 ForOrderBy {
4943 sort_family: ObjectName,
4945 },
4946}
4947
4948impl fmt::Display for OperatorClassItem {
4949 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4950 match self {
4951 OperatorClassItem::Operator {
4952 strategy_number,
4953 operator_name,
4954 op_types,
4955 purpose,
4956 } => {
4957 write!(f, "OPERATOR {strategy_number} {operator_name}")?;
4958 if let Some(types) = op_types {
4959 write!(f, " ({types})")?;
4960 }
4961 if let Some(purpose) = purpose {
4962 write!(f, " {purpose}")?;
4963 }
4964 Ok(())
4965 }
4966 OperatorClassItem::Function {
4967 support_number,
4968 op_types,
4969 function_name,
4970 argument_types,
4971 } => {
4972 write!(f, "FUNCTION {support_number}")?;
4973 if let Some(types) = op_types {
4974 write!(f, " ({})", display_comma_separated(types))?;
4975 }
4976 write!(f, " {function_name}")?;
4977 if !argument_types.is_empty() {
4978 write!(f, "({})", display_comma_separated(argument_types))?;
4979 }
4980 Ok(())
4981 }
4982 OperatorClassItem::Storage { storage_type } => {
4983 write!(f, "STORAGE {storage_type}")
4984 }
4985 }
4986 }
4987}
4988
4989impl fmt::Display for OperatorPurpose {
4990 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4991 match self {
4992 OperatorPurpose::ForSearch => write!(f, "FOR SEARCH"),
4993 OperatorPurpose::ForOrderBy { sort_family } => {
4994 write!(f, "FOR ORDER BY {sort_family}")
4995 }
4996 }
4997 }
4998}
4999
5000#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5003#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5004#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5005pub struct DropOperator {
5006 pub if_exists: bool,
5008 pub operators: Vec<DropOperatorSignature>,
5010 pub drop_behavior: Option<DropBehavior>,
5012}
5013
5014#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5016#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5017#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5018pub struct DropOperatorSignature {
5019 pub name: ObjectName,
5021 pub left_type: Option<DataType>,
5023 pub right_type: DataType,
5025}
5026
5027impl fmt::Display for DropOperatorSignature {
5028 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5029 write!(f, "{} (", self.name)?;
5030 if let Some(left_type) = &self.left_type {
5031 write!(f, "{}", left_type)?;
5032 } else {
5033 write!(f, "NONE")?;
5034 }
5035 write!(f, ", {})", self.right_type)
5036 }
5037}
5038
5039impl fmt::Display for DropOperator {
5040 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5041 write!(f, "DROP OPERATOR")?;
5042 if self.if_exists {
5043 write!(f, " IF EXISTS")?;
5044 }
5045 write!(f, " {}", display_comma_separated(&self.operators))?;
5046 if let Some(drop_behavior) = &self.drop_behavior {
5047 write!(f, " {}", drop_behavior)?;
5048 }
5049 Ok(())
5050 }
5051}
5052
5053impl Spanned for DropOperator {
5054 fn span(&self) -> Span {
5055 Span::empty()
5056 }
5057}
5058
5059#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5062#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5063#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5064pub struct DropOperatorFamily {
5065 pub if_exists: bool,
5067 pub names: Vec<ObjectName>,
5069 pub using: Ident,
5071 pub drop_behavior: Option<DropBehavior>,
5073}
5074
5075impl fmt::Display for DropOperatorFamily {
5076 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5077 write!(f, "DROP OPERATOR FAMILY")?;
5078 if self.if_exists {
5079 write!(f, " IF EXISTS")?;
5080 }
5081 write!(f, " {}", display_comma_separated(&self.names))?;
5082 write!(f, " USING {}", self.using)?;
5083 if let Some(drop_behavior) = &self.drop_behavior {
5084 write!(f, " {}", drop_behavior)?;
5085 }
5086 Ok(())
5087 }
5088}
5089
5090impl Spanned for DropOperatorFamily {
5091 fn span(&self) -> Span {
5092 Span::empty()
5093 }
5094}
5095
5096#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5099#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5100#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5101pub struct DropOperatorClass {
5102 pub if_exists: bool,
5104 pub names: Vec<ObjectName>,
5106 pub using: Ident,
5108 pub drop_behavior: Option<DropBehavior>,
5110}
5111
5112impl fmt::Display for DropOperatorClass {
5113 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5114 write!(f, "DROP OPERATOR CLASS")?;
5115 if self.if_exists {
5116 write!(f, " IF EXISTS")?;
5117 }
5118 write!(f, " {}", display_comma_separated(&self.names))?;
5119 write!(f, " USING {}", self.using)?;
5120 if let Some(drop_behavior) = &self.drop_behavior {
5121 write!(f, " {}", drop_behavior)?;
5122 }
5123 Ok(())
5124 }
5125}
5126
5127impl Spanned for DropOperatorClass {
5128 fn span(&self) -> Span {
5129 Span::empty()
5130 }
5131}
5132
5133#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5135#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5136#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5137pub enum OperatorFamilyItem {
5138 Operator {
5140 strategy_number: u64,
5142 operator_name: ObjectName,
5144 op_types: Vec<DataType>,
5146 purpose: Option<OperatorPurpose>,
5148 },
5149 Function {
5151 support_number: u64,
5153 op_types: Option<Vec<DataType>>,
5155 function_name: ObjectName,
5157 argument_types: Vec<DataType>,
5159 },
5160}
5161
5162#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5164#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5165#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5166pub enum OperatorFamilyDropItem {
5167 Operator {
5169 strategy_number: u64,
5171 op_types: Vec<DataType>,
5173 },
5174 Function {
5176 support_number: u64,
5178 op_types: Vec<DataType>,
5180 },
5181}
5182
5183impl fmt::Display for OperatorFamilyItem {
5184 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5185 match self {
5186 OperatorFamilyItem::Operator {
5187 strategy_number,
5188 operator_name,
5189 op_types,
5190 purpose,
5191 } => {
5192 write!(
5193 f,
5194 "OPERATOR {strategy_number} {operator_name} ({})",
5195 display_comma_separated(op_types)
5196 )?;
5197 if let Some(purpose) = purpose {
5198 write!(f, " {purpose}")?;
5199 }
5200 Ok(())
5201 }
5202 OperatorFamilyItem::Function {
5203 support_number,
5204 op_types,
5205 function_name,
5206 argument_types,
5207 } => {
5208 write!(f, "FUNCTION {support_number}")?;
5209 if let Some(types) = op_types {
5210 write!(f, " ({})", display_comma_separated(types))?;
5211 }
5212 write!(f, " {function_name}")?;
5213 if !argument_types.is_empty() {
5214 write!(f, "({})", display_comma_separated(argument_types))?;
5215 }
5216 Ok(())
5217 }
5218 }
5219 }
5220}
5221
5222impl fmt::Display for OperatorFamilyDropItem {
5223 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5224 match self {
5225 OperatorFamilyDropItem::Operator {
5226 strategy_number,
5227 op_types,
5228 } => {
5229 write!(
5230 f,
5231 "OPERATOR {strategy_number} ({})",
5232 display_comma_separated(op_types)
5233 )
5234 }
5235 OperatorFamilyDropItem::Function {
5236 support_number,
5237 op_types,
5238 } => {
5239 write!(
5240 f,
5241 "FUNCTION {support_number} ({})",
5242 display_comma_separated(op_types)
5243 )
5244 }
5245 }
5246 }
5247}
5248
5249#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5252#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5253#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5254pub struct AlterOperatorFamily {
5255 pub name: ObjectName,
5257 pub using: Ident,
5259 pub operation: AlterOperatorFamilyOperation,
5261}
5262
5263#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5265#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5266#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5267pub enum AlterOperatorFamilyOperation {
5268 Add {
5270 items: Vec<OperatorFamilyItem>,
5272 },
5273 Drop {
5275 items: Vec<OperatorFamilyDropItem>,
5277 },
5278 RenameTo {
5280 new_name: ObjectName,
5282 },
5283 OwnerTo(Owner),
5285 SetSchema {
5287 schema_name: ObjectName,
5289 },
5290}
5291
5292impl fmt::Display for AlterOperatorFamily {
5293 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5294 write!(
5295 f,
5296 "ALTER OPERATOR FAMILY {} USING {}",
5297 self.name, self.using
5298 )?;
5299 write!(f, " {}", self.operation)
5300 }
5301}
5302
5303impl fmt::Display for AlterOperatorFamilyOperation {
5304 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5305 match self {
5306 AlterOperatorFamilyOperation::Add { items } => {
5307 write!(f, "ADD {}", display_comma_separated(items))
5308 }
5309 AlterOperatorFamilyOperation::Drop { items } => {
5310 write!(f, "DROP {}", display_comma_separated(items))
5311 }
5312 AlterOperatorFamilyOperation::RenameTo { new_name } => {
5313 write!(f, "RENAME TO {new_name}")
5314 }
5315 AlterOperatorFamilyOperation::OwnerTo(owner) => {
5316 write!(f, "OWNER TO {owner}")
5317 }
5318 AlterOperatorFamilyOperation::SetSchema { schema_name } => {
5319 write!(f, "SET SCHEMA {schema_name}")
5320 }
5321 }
5322 }
5323}
5324
5325impl Spanned for AlterOperatorFamily {
5326 fn span(&self) -> Span {
5327 Span::empty()
5328 }
5329}
5330
5331#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5334#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5335#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5336pub struct AlterOperatorClass {
5337 pub name: ObjectName,
5339 pub using: Ident,
5341 pub operation: AlterOperatorClassOperation,
5343}
5344
5345#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5347#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5348#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5349pub enum AlterOperatorClassOperation {
5350 RenameTo {
5353 new_name: ObjectName,
5355 },
5356 OwnerTo(Owner),
5358 SetSchema {
5361 schema_name: ObjectName,
5363 },
5364}
5365
5366impl fmt::Display for AlterOperatorClass {
5367 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5368 write!(f, "ALTER OPERATOR CLASS {} USING {}", self.name, self.using)?;
5369 write!(f, " {}", self.operation)
5370 }
5371}
5372
5373impl fmt::Display for AlterOperatorClassOperation {
5374 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5375 match self {
5376 AlterOperatorClassOperation::RenameTo { new_name } => {
5377 write!(f, "RENAME TO {new_name}")
5378 }
5379 AlterOperatorClassOperation::OwnerTo(owner) => {
5380 write!(f, "OWNER TO {owner}")
5381 }
5382 AlterOperatorClassOperation::SetSchema { schema_name } => {
5383 write!(f, "SET SCHEMA {schema_name}")
5384 }
5385 }
5386 }
5387}
5388
5389impl Spanned for AlterOperatorClass {
5390 fn span(&self) -> Span {
5391 Span::empty()
5392 }
5393}
5394
5395#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5397#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5398#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5399pub struct AlterFunction {
5400 pub kind: AlterFunctionKind,
5402 pub function: FunctionDesc,
5404 pub aggregate_order_by: Option<Vec<OperateFunctionArg>>,
5408 pub aggregate_star: bool,
5412 pub operation: AlterFunctionOperation,
5414}
5415
5416#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5418#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5419#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5420pub enum AlterFunctionKind {
5421 Function,
5423 Aggregate,
5425 Procedure,
5427}
5428
5429impl fmt::Display for AlterFunctionKind {
5430 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5431 match self {
5432 Self::Function => write!(f, "FUNCTION"),
5433 Self::Aggregate => write!(f, "AGGREGATE"),
5434 Self::Procedure => write!(f, "PROCEDURE"),
5435 }
5436 }
5437}
5438
5439#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5441#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5442#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5443pub enum AlterFunctionOperation {
5444 RenameTo {
5446 new_name: Ident,
5448 },
5449 OwnerTo(Owner),
5451 SetSchema {
5453 schema_name: ObjectName,
5455 },
5456 DependsOnExtension {
5458 no: bool,
5460 extension_name: ObjectName,
5462 },
5463 Actions {
5465 actions: Vec<AlterFunctionAction>,
5467 restrict: bool,
5469 },
5470}
5471
5472#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5474#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5475#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5476pub enum AlterFunctionAction {
5477 CalledOnNull(FunctionCalledOnNull),
5479 Behavior(FunctionBehavior),
5481 Leakproof(bool),
5483 Security {
5485 external: bool,
5487 security: FunctionSecurity,
5489 },
5490 Parallel(FunctionParallel),
5492 Cost(Expr),
5494 Rows(Expr),
5496 Support(ObjectName),
5498 Set(FunctionDefinitionSetParam),
5501 Reset(ResetConfig),
5503}
5504
5505impl fmt::Display for AlterFunction {
5506 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5507 write!(f, "ALTER {} ", self.kind)?;
5508 match self.kind {
5509 AlterFunctionKind::Function | AlterFunctionKind::Procedure => {
5510 write!(f, "{} ", self.function)?;
5511 }
5512 AlterFunctionKind::Aggregate => {
5513 write!(f, "{}(", self.function.name)?;
5514 if self.aggregate_star {
5515 write!(f, "*")?;
5516 } else {
5517 if let Some(args) = &self.function.args {
5518 write!(f, "{}", display_comma_separated(args))?;
5519 }
5520 if let Some(order_by_args) = &self.aggregate_order_by {
5521 if self
5522 .function
5523 .args
5524 .as_ref()
5525 .is_some_and(|args| !args.is_empty())
5526 {
5527 write!(f, " ")?;
5528 }
5529 write!(f, "ORDER BY {}", display_comma_separated(order_by_args))?;
5530 }
5531 }
5532 write!(f, ") ")?;
5533 }
5534 }
5535 write!(f, "{}", self.operation)
5536 }
5537}
5538
5539impl fmt::Display for AlterFunctionOperation {
5540 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5541 match self {
5542 AlterFunctionOperation::RenameTo { new_name } => {
5543 write!(f, "RENAME TO {new_name}")
5544 }
5545 AlterFunctionOperation::OwnerTo(owner) => write!(f, "OWNER TO {owner}"),
5546 AlterFunctionOperation::SetSchema { schema_name } => {
5547 write!(f, "SET SCHEMA {schema_name}")
5548 }
5549 AlterFunctionOperation::DependsOnExtension { no, extension_name } => {
5550 if *no {
5551 write!(f, "NO DEPENDS ON EXTENSION {extension_name}")
5552 } else {
5553 write!(f, "DEPENDS ON EXTENSION {extension_name}")
5554 }
5555 }
5556 AlterFunctionOperation::Actions { actions, restrict } => {
5557 write!(f, "{}", display_separated(actions, " "))?;
5558 if *restrict {
5559 write!(f, " RESTRICT")?;
5560 }
5561 Ok(())
5562 }
5563 }
5564 }
5565}
5566
5567impl fmt::Display for AlterFunctionAction {
5568 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5569 match self {
5570 AlterFunctionAction::CalledOnNull(called_on_null) => write!(f, "{called_on_null}"),
5571 AlterFunctionAction::Behavior(behavior) => write!(f, "{behavior}"),
5572 AlterFunctionAction::Leakproof(leakproof) => {
5573 if *leakproof {
5574 write!(f, "LEAKPROOF")
5575 } else {
5576 write!(f, "NOT LEAKPROOF")
5577 }
5578 }
5579 AlterFunctionAction::Security { external, security } => {
5580 if *external {
5581 write!(f, "EXTERNAL ")?;
5582 }
5583 write!(f, "{security}")
5584 }
5585 AlterFunctionAction::Parallel(parallel) => write!(f, "{parallel}"),
5586 AlterFunctionAction::Cost(execution_cost) => write!(f, "COST {execution_cost}"),
5587 AlterFunctionAction::Rows(result_rows) => write!(f, "ROWS {result_rows}"),
5588 AlterFunctionAction::Support(support_function) => {
5589 write!(f, "SUPPORT {support_function}")
5590 }
5591 AlterFunctionAction::Set(set_param) => write!(f, "{set_param}"),
5592 AlterFunctionAction::Reset(reset_config) => match reset_config {
5593 ResetConfig::ALL => write!(f, "RESET ALL"),
5594 ResetConfig::ConfigName(name) => write!(f, "RESET {name}"),
5595 },
5596 }
5597 }
5598}
5599
5600impl Spanned for AlterFunction {
5601 fn span(&self) -> Span {
5602 Span::empty()
5603 }
5604}
5605
5606#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5610#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5611#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5612pub struct CreatePolicy {
5613 pub name: Ident,
5615 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
5617 pub table_name: ObjectName,
5618 pub policy_type: Option<CreatePolicyType>,
5620 pub command: Option<CreatePolicyCommand>,
5622 pub to: Option<Vec<Owner>>,
5624 pub using: Option<Expr>,
5626 pub with_check: Option<Expr>,
5628}
5629
5630impl fmt::Display for CreatePolicy {
5631 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5632 write!(
5633 f,
5634 "CREATE POLICY {name} ON {table_name}",
5635 name = self.name,
5636 table_name = self.table_name,
5637 )?;
5638 if let Some(ref policy_type) = self.policy_type {
5639 write!(f, " AS {policy_type}")?;
5640 }
5641 if let Some(ref command) = self.command {
5642 write!(f, " FOR {command}")?;
5643 }
5644 if let Some(ref to) = self.to {
5645 write!(f, " TO {}", display_comma_separated(to))?;
5646 }
5647 if let Some(ref using) = self.using {
5648 write!(f, " USING ({using})")?;
5649 }
5650 if let Some(ref with_check) = self.with_check {
5651 write!(f, " WITH CHECK ({with_check})")?;
5652 }
5653 Ok(())
5654 }
5655}
5656
5657#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
5663#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5664#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5665pub enum CreatePolicyType {
5666 Permissive,
5668 Restrictive,
5670}
5671
5672impl fmt::Display for CreatePolicyType {
5673 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5674 match self {
5675 CreatePolicyType::Permissive => write!(f, "PERMISSIVE"),
5676 CreatePolicyType::Restrictive => write!(f, "RESTRICTIVE"),
5677 }
5678 }
5679}
5680
5681#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
5687#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5688#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5689pub enum CreatePolicyCommand {
5690 All,
5692 Select,
5694 Insert,
5696 Update,
5698 Delete,
5700}
5701
5702impl fmt::Display for CreatePolicyCommand {
5703 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5704 match self {
5705 CreatePolicyCommand::All => write!(f, "ALL"),
5706 CreatePolicyCommand::Select => write!(f, "SELECT"),
5707 CreatePolicyCommand::Insert => write!(f, "INSERT"),
5708 CreatePolicyCommand::Update => write!(f, "UPDATE"),
5709 CreatePolicyCommand::Delete => write!(f, "DELETE"),
5710 }
5711 }
5712}
5713
5714#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5718#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5719#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5720pub struct DropPolicy {
5721 pub if_exists: bool,
5723 pub name: Ident,
5725 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
5727 pub table_name: ObjectName,
5728 pub drop_behavior: Option<DropBehavior>,
5730}
5731
5732impl fmt::Display for DropPolicy {
5733 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5734 write!(
5735 f,
5736 "DROP POLICY {if_exists}{name} ON {table_name}",
5737 if_exists = if self.if_exists { "IF EXISTS " } else { "" },
5738 name = self.name,
5739 table_name = self.table_name
5740 )?;
5741 if let Some(ref behavior) = self.drop_behavior {
5742 write!(f, " {behavior}")?;
5743 }
5744 Ok(())
5745 }
5746}
5747
5748impl From<CreatePolicy> for crate::ast::Statement {
5749 fn from(v: CreatePolicy) -> Self {
5750 crate::ast::Statement::CreatePolicy(v)
5751 }
5752}
5753
5754impl From<DropPolicy> for crate::ast::Statement {
5755 fn from(v: DropPolicy) -> Self {
5756 crate::ast::Statement::DropPolicy(v)
5757 }
5758}
5759
5760#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5767#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5768#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5769pub struct AlterPolicy {
5770 pub name: Ident,
5772 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
5774 pub table_name: ObjectName,
5775 pub operation: AlterPolicyOperation,
5777}
5778
5779impl fmt::Display for AlterPolicy {
5780 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5781 write!(
5782 f,
5783 "ALTER POLICY {name} ON {table_name}{operation}",
5784 name = self.name,
5785 table_name = self.table_name,
5786 operation = self.operation
5787 )
5788 }
5789}
5790
5791impl From<AlterPolicy> for crate::ast::Statement {
5792 fn from(v: AlterPolicy) -> Self {
5793 crate::ast::Statement::AlterPolicy(v)
5794 }
5795}
5796
5797#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5801#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5802#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5803pub enum FdwRoutineClause {
5804 Function(ObjectName),
5806 NoFunction,
5808}
5809
5810#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5814#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5815#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5816pub struct CreateForeignDataWrapper {
5817 pub name: Ident,
5819 pub handler: Option<FdwRoutineClause>,
5821 pub validator: Option<FdwRoutineClause>,
5823 pub options: Option<Vec<CreateServerOption>>,
5825}
5826
5827impl fmt::Display for CreateForeignDataWrapper {
5828 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5829 write!(f, "CREATE FOREIGN DATA WRAPPER {}", self.name)?;
5830 if let Some(handler) = &self.handler {
5831 match handler {
5832 FdwRoutineClause::Function(name) => write!(f, " HANDLER {name}")?,
5833 FdwRoutineClause::NoFunction => write!(f, " NO HANDLER")?,
5834 }
5835 }
5836 if let Some(validator) = &self.validator {
5837 match validator {
5838 FdwRoutineClause::Function(name) => write!(f, " VALIDATOR {name}")?,
5839 FdwRoutineClause::NoFunction => write!(f, " NO VALIDATOR")?,
5840 }
5841 }
5842 if let Some(options) = &self.options {
5843 write!(f, " OPTIONS ({})", display_comma_separated(options))?;
5844 }
5845 Ok(())
5846 }
5847}
5848
5849impl From<CreateForeignDataWrapper> for crate::ast::Statement {
5850 fn from(v: CreateForeignDataWrapper) -> Self {
5851 crate::ast::Statement::CreateForeignDataWrapper(v)
5852 }
5853}
5854
5855#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5859#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5860#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5861pub struct CreateForeignTable {
5862 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
5864 pub name: ObjectName,
5865 pub if_not_exists: bool,
5867 pub columns: Vec<ColumnDef>,
5869 pub server_name: Ident,
5871 pub options: Option<Vec<CreateServerOption>>,
5873}
5874
5875impl fmt::Display for CreateForeignTable {
5876 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5877 write!(
5878 f,
5879 "CREATE FOREIGN TABLE {if_not_exists}{name} ({columns}) SERVER {server_name}",
5880 if_not_exists = if self.if_not_exists { "IF NOT EXISTS " } else { "" },
5881 name = self.name,
5882 columns = display_comma_separated(&self.columns),
5883 server_name = self.server_name,
5884 )?;
5885 if let Some(options) = &self.options {
5886 write!(f, " OPTIONS ({})", display_comma_separated(options))?;
5887 }
5888 Ok(())
5889 }
5890}
5891
5892impl From<CreateForeignTable> for crate::ast::Statement {
5893 fn from(v: CreateForeignTable) -> Self {
5894 crate::ast::Statement::CreateForeignTable(v)
5895 }
5896}
5897
5898#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5901#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5902#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5903pub struct CreateAggregate {
5904 pub or_replace: bool,
5906 pub name: ObjectName,
5908 pub args: Vec<DataType>,
5910 pub options: Vec<CreateAggregateOption>,
5913}
5914
5915impl fmt::Display for CreateAggregate {
5916 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5917 write!(f, "CREATE")?;
5918 if self.or_replace {
5919 write!(f, " OR REPLACE")?;
5920 }
5921 write!(f, " AGGREGATE {}", self.name)?;
5922 write!(f, " ({})", display_comma_separated(&self.args))?;
5923 write!(f, " (")?;
5924 for (i, option) in self.options.iter().enumerate() {
5925 if i > 0 {
5926 write!(f, ", ")?;
5927 }
5928 write!(f, "{option}")?;
5929 }
5930 write!(f, ")")
5931 }
5932}
5933
5934impl From<CreateAggregate> for crate::ast::Statement {
5935 fn from(v: CreateAggregate) -> Self {
5936 crate::ast::Statement::CreateAggregate(v)
5937 }
5938}
5939
5940#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5944#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5945#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5946pub enum CreateAggregateOption {
5947 Sfunc(ObjectName),
5949 Stype(DataType),
5951 Sspace(u64),
5953 Finalfunc(ObjectName),
5955 FinalfuncExtra,
5957 FinalfuncModify(AggregateModifyKind),
5959 Combinefunc(ObjectName),
5961 Serialfunc(ObjectName),
5963 Deserialfunc(ObjectName),
5965 Initcond(Value),
5967 Msfunc(ObjectName),
5969 Minvfunc(ObjectName),
5971 Mstype(DataType),
5973 Msspace(u64),
5975 Mfinalfunc(ObjectName),
5977 MfinalfuncExtra,
5979 MfinalfuncModify(AggregateModifyKind),
5981 Minitcond(Value),
5983 Sortop(ObjectName),
5985 Parallel(FunctionParallel),
5987 Hypothetical,
5989}
5990
5991impl fmt::Display for CreateAggregateOption {
5992 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5993 match self {
5994 Self::Sfunc(name) => write!(f, "SFUNC = {name}"),
5995 Self::Stype(data_type) => write!(f, "STYPE = {data_type}"),
5996 Self::Sspace(size) => write!(f, "SSPACE = {size}"),
5997 Self::Finalfunc(name) => write!(f, "FINALFUNC = {name}"),
5998 Self::FinalfuncExtra => write!(f, "FINALFUNC_EXTRA"),
5999 Self::FinalfuncModify(kind) => write!(f, "FINALFUNC_MODIFY = {kind}"),
6000 Self::Combinefunc(name) => write!(f, "COMBINEFUNC = {name}"),
6001 Self::Serialfunc(name) => write!(f, "SERIALFUNC = {name}"),
6002 Self::Deserialfunc(name) => write!(f, "DESERIALFUNC = {name}"),
6003 Self::Initcond(cond) => write!(f, "INITCOND = {cond}"),
6004 Self::Msfunc(name) => write!(f, "MSFUNC = {name}"),
6005 Self::Minvfunc(name) => write!(f, "MINVFUNC = {name}"),
6006 Self::Mstype(data_type) => write!(f, "MSTYPE = {data_type}"),
6007 Self::Msspace(size) => write!(f, "MSSPACE = {size}"),
6008 Self::Mfinalfunc(name) => write!(f, "MFINALFUNC = {name}"),
6009 Self::MfinalfuncExtra => write!(f, "MFINALFUNC_EXTRA"),
6010 Self::MfinalfuncModify(kind) => write!(f, "MFINALFUNC_MODIFY = {kind}"),
6011 Self::Minitcond(cond) => write!(f, "MINITCOND = {cond}"),
6012 Self::Sortop(name) => write!(f, "SORTOP = {name}"),
6013 Self::Parallel(parallel) => {
6014 let kind = match parallel {
6015 FunctionParallel::Safe => "SAFE",
6016 FunctionParallel::Restricted => "RESTRICTED",
6017 FunctionParallel::Unsafe => "UNSAFE",
6018 };
6019 write!(f, "PARALLEL = {kind}")
6020 }
6021 Self::Hypothetical => write!(f, "HYPOTHETICAL"),
6022 }
6023 }
6024}
6025
6026#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6030#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6031#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6032pub enum AggregateModifyKind {
6033 ReadOnly,
6035 Shareable,
6037 ReadWrite,
6039}
6040
6041impl fmt::Display for AggregateModifyKind {
6042 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6043 match self {
6044 Self::ReadOnly => write!(f, "READ_ONLY"),
6045 Self::Shareable => write!(f, "SHAREABLE"),
6046 Self::ReadWrite => write!(f, "READ_WRITE"),
6047 }
6048 }
6049}
6050
6051#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6056#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6057#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6058pub struct CreateTextSearchConfiguration {
6059 pub name: ObjectName,
6061 pub options: Vec<SqlOption>,
6063}
6064
6065impl fmt::Display for CreateTextSearchConfiguration {
6066 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6067 write!(
6068 f,
6069 "CREATE TEXT SEARCH CONFIGURATION {name} ({options})",
6070 name = self.name,
6071 options = display_comma_separated(&self.options),
6072 )
6073 }
6074}
6075
6076impl From<CreateTextSearchConfiguration> for crate::ast::Statement {
6077 fn from(v: CreateTextSearchConfiguration) -> Self {
6078 crate::ast::Statement::CreateTextSearchConfiguration(v)
6079 }
6080}
6081
6082#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6087#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6088#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6089pub struct CreateTextSearchDictionary {
6090 pub name: ObjectName,
6092 pub options: Vec<SqlOption>,
6094}
6095
6096impl fmt::Display for CreateTextSearchDictionary {
6097 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6098 write!(
6099 f,
6100 "CREATE TEXT SEARCH DICTIONARY {name} ({options})",
6101 name = self.name,
6102 options = display_comma_separated(&self.options),
6103 )
6104 }
6105}
6106
6107impl From<CreateTextSearchDictionary> for crate::ast::Statement {
6108 fn from(v: CreateTextSearchDictionary) -> Self {
6109 crate::ast::Statement::CreateTextSearchDictionary(v)
6110 }
6111}
6112
6113#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6118#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6119#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6120pub struct CreateTextSearchParser {
6121 pub name: ObjectName,
6123 pub options: Vec<SqlOption>,
6125}
6126
6127impl fmt::Display for CreateTextSearchParser {
6128 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6129 write!(
6130 f,
6131 "CREATE TEXT SEARCH PARSER {name} ({options})",
6132 name = self.name,
6133 options = display_comma_separated(&self.options),
6134 )
6135 }
6136}
6137
6138impl From<CreateTextSearchParser> for crate::ast::Statement {
6139 fn from(v: CreateTextSearchParser) -> Self {
6140 crate::ast::Statement::CreateTextSearchParser(v)
6141 }
6142}
6143
6144#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6149#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6150#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6151pub struct CreateTextSearchTemplate {
6152 pub name: ObjectName,
6154 pub options: Vec<SqlOption>,
6156}
6157
6158impl fmt::Display for CreateTextSearchTemplate {
6159 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6160 write!(
6161 f,
6162 "CREATE TEXT SEARCH TEMPLATE {name} ({options})",
6163 name = self.name,
6164 options = display_comma_separated(&self.options),
6165 )
6166 }
6167}
6168
6169impl From<CreateTextSearchTemplate> for crate::ast::Statement {
6170 fn from(v: CreateTextSearchTemplate) -> Self {
6171 crate::ast::Statement::CreateTextSearchTemplate(v)
6172 }
6173}
6174
6175#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6179#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6180#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6181pub struct AlterDomain {
6182 pub name: ObjectName,
6184 pub operation: AlterDomainOperation,
6186}
6187
6188#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6190#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6191#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6192pub enum AlterDomainOperation {
6193 AddConstraint {
6195 constraint: TableConstraint,
6197 not_valid: bool,
6199 },
6200 DropConstraint {
6202 if_exists: bool,
6204 name: Ident,
6206 drop_behavior: Option<DropBehavior>,
6208 },
6209 RenameConstraint {
6211 old_name: Ident,
6213 new_name: Ident,
6215 },
6216 OwnerTo(Owner),
6218 RenameTo {
6220 new_name: Ident,
6222 },
6223 SetSchema {
6225 schema_name: ObjectName,
6227 },
6228 SetDefault {
6230 default: Expr,
6232 },
6233 DropDefault,
6235 ValidateConstraint {
6237 name: Ident,
6239 },
6240}
6241
6242impl fmt::Display for AlterDomain {
6243 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6244 write!(f, "ALTER DOMAIN {} {}", self.name, self.operation)
6245 }
6246}
6247
6248impl fmt::Display for AlterDomainOperation {
6249 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6250 match self {
6251 AlterDomainOperation::AddConstraint {
6252 constraint,
6253 not_valid,
6254 } => {
6255 write!(f, "ADD {constraint}")?;
6256 if *not_valid {
6257 write!(f, " NOT VALID")?;
6258 }
6259 Ok(())
6260 }
6261 AlterDomainOperation::DropConstraint {
6262 if_exists,
6263 name,
6264 drop_behavior,
6265 } => {
6266 write!(f, "DROP CONSTRAINT")?;
6267 if *if_exists {
6268 write!(f, " IF EXISTS")?;
6269 }
6270 write!(f, " {name}")?;
6271 if let Some(behavior) = drop_behavior {
6272 write!(f, " {behavior}")?;
6273 }
6274 Ok(())
6275 }
6276 AlterDomainOperation::RenameConstraint { old_name, new_name } => {
6277 write!(f, "RENAME CONSTRAINT {old_name} TO {new_name}")
6278 }
6279 AlterDomainOperation::OwnerTo(owner) => write!(f, "OWNER TO {owner}"),
6280 AlterDomainOperation::RenameTo { new_name } => write!(f, "RENAME TO {new_name}"),
6281 AlterDomainOperation::SetSchema { schema_name } => {
6282 write!(f, "SET SCHEMA {schema_name}")
6283 }
6284 AlterDomainOperation::SetDefault { default } => write!(f, "SET DEFAULT {default}"),
6285 AlterDomainOperation::DropDefault => write!(f, "DROP DEFAULT"),
6286 AlterDomainOperation::ValidateConstraint { name } => {
6287 write!(f, "VALIDATE CONSTRAINT {name}")
6288 }
6289 }
6290 }
6291}
6292
6293#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6297#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6298#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6299pub enum PublicationTarget {
6300 AllTables,
6302 Tables(Vec<ObjectName>),
6304 TablesInSchema(Vec<Ident>),
6306}
6307
6308impl fmt::Display for PublicationTarget {
6309 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6310 match self {
6311 PublicationTarget::AllTables => write!(f, "FOR ALL TABLES"),
6312 PublicationTarget::Tables(tables) => {
6313 write!(f, "FOR TABLE {}", display_comma_separated(tables))
6314 }
6315 PublicationTarget::TablesInSchema(schemas) => {
6316 write!(
6317 f,
6318 "FOR TABLES IN SCHEMA {}",
6319 display_comma_separated(schemas)
6320 )
6321 }
6322 }
6323 }
6324}
6325
6326impl From<AlterDomain> for crate::ast::Statement {
6327 fn from(a: AlterDomain) -> Self {
6328 crate::ast::Statement::AlterDomain(a)
6329 }
6330}
6331
6332#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6336#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6337#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6338pub struct AlterTrigger {
6339 pub name: Ident,
6341 pub table_name: ObjectName,
6343 pub operation: AlterTriggerOperation,
6345}
6346
6347#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6349#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6350#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6351pub enum AlterTriggerOperation {
6352 RenameTo {
6354 new_name: Ident,
6356 },
6357}
6358
6359impl fmt::Display for AlterTrigger {
6360 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6361 write!(
6362 f,
6363 "ALTER TRIGGER {} ON {} {}",
6364 self.name, self.table_name, self.operation
6365 )
6366 }
6367}
6368
6369impl fmt::Display for AlterTriggerOperation {
6370 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6371 match self {
6372 AlterTriggerOperation::RenameTo { new_name } => write!(f, "RENAME TO {new_name}"),
6373 }
6374 }
6375}
6376
6377impl From<AlterTrigger> for crate::ast::Statement {
6378 fn from(a: AlterTrigger) -> Self {
6379 crate::ast::Statement::AlterTrigger(a)
6380 }
6381}
6382
6383#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6387#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6388#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6389pub struct AlterExtension {
6390 pub name: Ident,
6392 pub operation: AlterExtensionOperation,
6394}
6395
6396#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6398#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6399#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6400pub enum AlterExtensionOperation {
6401 UpdateTo {
6403 version: Option<Ident>,
6405 },
6406 SetSchema {
6408 schema_name: ObjectName,
6410 },
6411 OwnerTo(Owner),
6413 RenameTo {
6415 new_name: Ident,
6417 },
6418}
6419
6420impl fmt::Display for AlterExtension {
6421 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6422 write!(f, "ALTER EXTENSION {} {}", self.name, self.operation)
6423 }
6424}
6425
6426impl fmt::Display for AlterExtensionOperation {
6427 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6428 match self {
6429 AlterExtensionOperation::UpdateTo { version } => {
6430 write!(f, "UPDATE")?;
6431 if let Some(v) = version {
6432 write!(f, " TO {v}")?;
6433 }
6434 Ok(())
6435 }
6436 AlterExtensionOperation::SetSchema { schema_name } => {
6437 write!(f, "SET SCHEMA {schema_name}")
6438 }
6439 AlterExtensionOperation::OwnerTo(owner) => write!(f, "OWNER TO {owner}"),
6440 AlterExtensionOperation::RenameTo { new_name } => write!(f, "RENAME TO {new_name}"),
6441 }
6442 }
6443}
6444
6445impl From<AlterExtension> for crate::ast::Statement {
6446 fn from(a: AlterExtension) -> Self {
6447 crate::ast::Statement::AlterExtension(a)
6448 }
6449}
6450
6451#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6456#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6457#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6458pub struct CreatePublication {
6459 pub name: Ident,
6461 pub target: Option<PublicationTarget>,
6463 pub with_options: Vec<SqlOption>,
6465}
6466
6467impl fmt::Display for CreatePublication {
6468 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6469 write!(f, "CREATE PUBLICATION {}", self.name)?;
6470 if let Some(target) = &self.target {
6471 write!(f, " {target}")?;
6472 }
6473 if !self.with_options.is_empty() {
6474 write!(f, " WITH ({})", display_comma_separated(&self.with_options))?;
6475 }
6476 Ok(())
6477 }
6478}
6479
6480impl From<CreatePublication> for crate::ast::Statement {
6481 fn from(v: CreatePublication) -> Self {
6482 crate::ast::Statement::CreatePublication(v)
6483 }
6484}
6485
6486#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6491#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6492#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6493pub struct CreateSubscription {
6494 pub name: Ident,
6496 pub connection: Value,
6498 pub publications: Vec<Ident>,
6500 pub with_options: Vec<SqlOption>,
6502}
6503
6504impl fmt::Display for CreateSubscription {
6505 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6506 write!(
6507 f,
6508 "CREATE SUBSCRIPTION {name} CONNECTION {connection} PUBLICATION {publications}",
6509 name = self.name,
6510 connection = self.connection,
6511 publications = display_comma_separated(&self.publications),
6512 )?;
6513 if !self.with_options.is_empty() {
6514 write!(f, " WITH ({})", display_comma_separated(&self.with_options))?;
6515 }
6516 Ok(())
6517 }
6518}
6519
6520impl From<CreateSubscription> for crate::ast::Statement {
6521 fn from(v: CreateSubscription) -> Self {
6522 crate::ast::Statement::CreateSubscription(v)
6523 }
6524}
6525
6526#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6530#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6531#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6532pub enum CastFunctionKind {
6533 WithFunction {
6535 function_name: ObjectName,
6537 argument_types: Vec<DataType>,
6540 },
6541 WithoutFunction,
6543 WithInout,
6545}
6546
6547impl fmt::Display for CastFunctionKind {
6548 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6549 match self {
6550 CastFunctionKind::WithFunction {
6551 function_name,
6552 argument_types,
6553 } => {
6554 write!(f, "WITH FUNCTION {function_name}")?;
6555 if !argument_types.is_empty() {
6556 write!(f, "({})", display_comma_separated(argument_types))?;
6557 }
6558 Ok(())
6559 }
6560 CastFunctionKind::WithoutFunction => write!(f, "WITHOUT FUNCTION"),
6561 CastFunctionKind::WithInout => write!(f, "WITH INOUT"),
6562 }
6563 }
6564}
6565
6566#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6571#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6572#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6573pub enum StatisticsKind {
6574 NDistinct,
6576 Dependencies,
6578 Mcv,
6580}
6581
6582impl fmt::Display for StatisticsKind {
6583 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6584 match self {
6585 StatisticsKind::NDistinct => write!(f, "ndistinct"),
6586 StatisticsKind::Dependencies => write!(f, "dependencies"),
6587 StatisticsKind::Mcv => write!(f, "mcv"),
6588 }
6589 }
6590}
6591
6592#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6596#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6597#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6598pub enum SecurityLabelObjectKind {
6599 Table,
6601 Column,
6603 Database,
6605 Domain,
6607 Function,
6609 Role,
6611 Schema,
6613 Sequence,
6615 Type,
6617 View,
6619 MaterializedView,
6621}
6622
6623impl fmt::Display for SecurityLabelObjectKind {
6624 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6625 match self {
6626 SecurityLabelObjectKind::Table => write!(f, "TABLE"),
6627 SecurityLabelObjectKind::Column => write!(f, "COLUMN"),
6628 SecurityLabelObjectKind::Database => write!(f, "DATABASE"),
6629 SecurityLabelObjectKind::Domain => write!(f, "DOMAIN"),
6630 SecurityLabelObjectKind::Function => write!(f, "FUNCTION"),
6631 SecurityLabelObjectKind::Role => write!(f, "ROLE"),
6632 SecurityLabelObjectKind::Schema => write!(f, "SCHEMA"),
6633 SecurityLabelObjectKind::Sequence => write!(f, "SEQUENCE"),
6634 SecurityLabelObjectKind::Type => write!(f, "TYPE"),
6635 SecurityLabelObjectKind::View => write!(f, "VIEW"),
6636 SecurityLabelObjectKind::MaterializedView => write!(f, "MATERIALIZED VIEW"),
6637 }
6638 }
6639}
6640
6641#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6645#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6646#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6647pub enum CastContext {
6648 Explicit,
6650 Assignment,
6652 Implicit,
6654}
6655
6656impl fmt::Display for CastContext {
6657 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6658 match self {
6659 CastContext::Explicit => Ok(()),
6660 CastContext::Assignment => write!(f, " AS ASSIGNMENT"),
6661 CastContext::Implicit => write!(f, " AS IMPLICIT"),
6662 }
6663 }
6664}
6665
6666#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6671#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6672#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6673pub struct CreateCast {
6674 pub source_type: DataType,
6676 pub target_type: DataType,
6678 pub function_kind: CastFunctionKind,
6680 pub cast_context: CastContext,
6682}
6683
6684impl fmt::Display for CreateCast {
6685 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6686 write!(
6687 f,
6688 "CREATE CAST ({source} AS {target}) {function_kind}{context}",
6689 source = self.source_type,
6690 target = self.target_type,
6691 function_kind = self.function_kind,
6692 context = self.cast_context,
6693 )
6694 }
6695}
6696
6697impl From<CreateCast> for crate::ast::Statement {
6698 fn from(v: CreateCast) -> Self {
6699 crate::ast::Statement::CreateCast(v)
6700 }
6701}
6702
6703#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6708#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6709#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6710pub struct CreateConversion {
6711 pub name: ObjectName,
6713 pub is_default: bool,
6715 pub source_encoding: String,
6717 pub destination_encoding: String,
6719 pub function_name: ObjectName,
6721}
6722
6723impl fmt::Display for CreateConversion {
6724 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6725 write!(f, "CREATE")?;
6726 if self.is_default {
6727 write!(f, " DEFAULT")?;
6728 }
6729 write!(
6730 f,
6731 " CONVERSION {name} FOR '{source}' TO '{destination}' FROM {function}",
6732 name = self.name,
6733 source = self.source_encoding,
6734 destination = self.destination_encoding,
6735 function = self.function_name,
6736 )
6737 }
6738}
6739
6740impl From<CreateConversion> for crate::ast::Statement {
6741 fn from(v: CreateConversion) -> Self {
6742 crate::ast::Statement::CreateConversion(v)
6743 }
6744}
6745
6746#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6751#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6752#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6753pub struct CreateLanguage {
6754 pub name: Ident,
6756 pub or_replace: bool,
6758 pub trusted: bool,
6760 pub procedural: bool,
6762 pub handler: Option<ObjectName>,
6764 pub inline_handler: Option<ObjectName>,
6766 pub validator: Option<ObjectName>,
6768}
6769
6770impl fmt::Display for CreateLanguage {
6771 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6772 write!(f, "CREATE")?;
6773 if self.or_replace {
6774 write!(f, " OR REPLACE")?;
6775 }
6776 if self.trusted {
6777 write!(f, " TRUSTED")?;
6778 }
6779 if self.procedural {
6780 write!(f, " PROCEDURAL")?;
6781 }
6782 write!(f, " LANGUAGE {}", self.name)?;
6783 if let Some(handler) = &self.handler {
6784 write!(f, " HANDLER {handler}")?;
6785 }
6786 if let Some(inline) = &self.inline_handler {
6787 write!(f, " INLINE {inline}")?;
6788 }
6789 if let Some(validator) = &self.validator {
6790 write!(f, " VALIDATOR {validator}")?;
6791 }
6792 Ok(())
6793 }
6794}
6795
6796#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6801#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6802#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6803pub struct CreateStatistics {
6804 pub if_not_exists: bool,
6806 pub name: ObjectName,
6808 pub kinds: Vec<StatisticsKind>,
6810 pub on: Vec<Expr>,
6812 pub from: ObjectName,
6814}
6815
6816impl fmt::Display for CreateStatistics {
6817 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6818 write!(f, "CREATE STATISTICS")?;
6819 if self.if_not_exists {
6820 write!(f, " IF NOT EXISTS")?;
6821 }
6822 write!(f, " {}", self.name)?;
6823 if !self.kinds.is_empty() {
6824 write!(f, " ({})", display_comma_separated(&self.kinds))?;
6825 }
6826 write!(f, " ON {}", display_comma_separated(&self.on))?;
6827 write!(f, " FROM {}", self.from)?;
6828 Ok(())
6829 }
6830}
6831
6832#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6837#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6838#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6839pub struct SecurityLabel {
6840 pub provider: Option<Ident>,
6842 pub object_kind: SecurityLabelObjectKind,
6844 pub object_name: ObjectName,
6846 pub label: Option<Value>,
6848}
6849
6850impl fmt::Display for SecurityLabel {
6851 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6852 write!(f, "SECURITY LABEL")?;
6853 if let Some(provider) = &self.provider {
6854 write!(f, " FOR {provider}")?;
6855 }
6856 write!(f, " ON {} {}", self.object_kind, self.object_name)?;
6857 write!(f, " IS ")?;
6858 match &self.label {
6859 Some(label) => write!(f, "{label}"),
6860 None => write!(f, "NULL"),
6861 }
6862 }
6863}
6864
6865impl From<SecurityLabel> for crate::ast::Statement {
6866 fn from(v: SecurityLabel) -> Self {
6867 crate::ast::Statement::SecurityLabel(v)
6868 }
6869}
6870
6871#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6875#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6876#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6877pub enum UserMappingUser {
6878 Ident(Ident),
6880 User,
6882 CurrentRole,
6884 CurrentUser,
6886 Public,
6888}
6889
6890impl fmt::Display for UserMappingUser {
6891 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6892 match self {
6893 UserMappingUser::Ident(ident) => write!(f, "{ident}"),
6894 UserMappingUser::User => write!(f, "USER"),
6895 UserMappingUser::CurrentRole => write!(f, "CURRENT_ROLE"),
6896 UserMappingUser::CurrentUser => write!(f, "CURRENT_USER"),
6897 UserMappingUser::Public => write!(f, "PUBLIC"),
6898 }
6899 }
6900}
6901
6902#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6907#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6908#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6909pub struct CreateUserMapping {
6910 pub if_not_exists: bool,
6912 pub user: UserMappingUser,
6914 pub server_name: Ident,
6916 pub options: Option<Vec<CreateServerOption>>,
6918}
6919
6920impl fmt::Display for CreateUserMapping {
6921 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6922 write!(f, "CREATE USER MAPPING")?;
6923 if self.if_not_exists {
6924 write!(f, " IF NOT EXISTS")?;
6925 }
6926 write!(f, " FOR {} SERVER {}", self.user, self.server_name)?;
6927 if let Some(options) = &self.options {
6928 write!(
6929 f,
6930 " OPTIONS ({})",
6931 display_comma_separated(options)
6932 )?;
6933 }
6934 Ok(())
6935 }
6936}
6937
6938impl From<CreateLanguage> for crate::ast::Statement {
6939 fn from(v: CreateLanguage) -> Self {
6940 crate::ast::Statement::CreateLanguage(v)
6941 }
6942}
6943
6944#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6948#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6949#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6950pub enum RuleEvent {
6951 Select,
6953 Insert,
6955 Update,
6957 Delete,
6959}
6960
6961impl fmt::Display for RuleEvent {
6962 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6963 match self {
6964 RuleEvent::Select => write!(f, "SELECT"),
6965 RuleEvent::Insert => write!(f, "INSERT"),
6966 RuleEvent::Update => write!(f, "UPDATE"),
6967 RuleEvent::Delete => write!(f, "DELETE"),
6968 }
6969 }
6970}
6971
6972impl From<CreateStatistics> for crate::ast::Statement {
6973 fn from(v: CreateStatistics) -> Self {
6974 crate::ast::Statement::CreateStatistics(v)
6975 }
6976}
6977
6978#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6983#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6984#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6985pub enum AccessMethodType {
6986 Index,
6988 Table,
6990}
6991
6992impl fmt::Display for AccessMethodType {
6993 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6994 match self {
6995 AccessMethodType::Index => write!(f, "INDEX"),
6996 AccessMethodType::Table => write!(f, "TABLE"),
6997 }
6998 }
6999}
7000
7001#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
7005#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7006#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
7007pub enum RuleAction {
7008 Nothing,
7010 Statements(Vec<crate::ast::Statement>),
7012}
7013
7014impl fmt::Display for RuleAction {
7015 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7016 match self {
7017 RuleAction::Nothing => write!(f, "NOTHING"),
7018 RuleAction::Statements(stmts) => {
7019 if stmts.len() == 1 {
7020 write!(f, "{}", stmts[0])
7021 } else {
7022 write!(f, "(")?;
7023 for (i, stmt) in stmts.iter().enumerate() {
7024 if i > 0 {
7025 write!(f, "; ")?;
7026 }
7027 write!(f, "{stmt}")?;
7028 }
7029 write!(f, ")")
7030 }
7031 }
7032 }
7033 }
7034}
7035
7036#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
7041#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7042#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
7043pub struct CreateRule {
7044 pub name: Ident,
7046 pub event: RuleEvent,
7048 pub table: ObjectName,
7050 pub condition: Option<Expr>,
7052 pub instead: bool,
7054 pub action: RuleAction,
7056}
7057
7058impl fmt::Display for CreateRule {
7059 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7060 write!(
7061 f,
7062 "CREATE RULE {name} AS ON {event} TO {table}",
7063 name = self.name,
7064 event = self.event,
7065 table = self.table,
7066 )?;
7067 if let Some(condition) = &self.condition {
7068 write!(f, " WHERE {condition}")?;
7069 }
7070 write!(f, " DO")?;
7071 if self.instead {
7072 write!(f, " INSTEAD")?;
7073 } else {
7074 write!(f, " ALSO")?;
7075 }
7076 write!(f, " {}", self.action)
7077 }
7078}
7079
7080impl From<CreateRule> for crate::ast::Statement {
7081 fn from(v: CreateRule) -> Self {
7082 crate::ast::Statement::CreateRule(v)
7083 }
7084}
7085
7086#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
7091#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7092#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
7093pub struct CreateAccessMethod {
7094 pub name: Ident,
7096 pub method_type: AccessMethodType,
7098 pub handler: ObjectName,
7100}
7101
7102impl fmt::Display for CreateAccessMethod {
7103 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7104 write!(
7105 f,
7106 "CREATE ACCESS METHOD {name} TYPE {method_type} HANDLER {handler}",
7107 name = self.name,
7108 method_type = self.method_type,
7109 handler = self.handler,
7110 )
7111 }
7112}
7113
7114impl From<CreateAccessMethod> for crate::ast::Statement {
7115 fn from(v: CreateAccessMethod) -> Self {
7116 crate::ast::Statement::CreateAccessMethod(v)
7117 }
7118}
7119
7120#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
7125#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7126#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
7127pub enum EventTriggerEvent {
7128 DdlCommandStart,
7130 DdlCommandEnd,
7132 TableRewrite,
7134 SqlDrop,
7136}
7137
7138impl fmt::Display for EventTriggerEvent {
7139 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7140 match self {
7141 EventTriggerEvent::DdlCommandStart => write!(f, "ddl_command_start"),
7142 EventTriggerEvent::DdlCommandEnd => write!(f, "ddl_command_end"),
7143 EventTriggerEvent::TableRewrite => write!(f, "table_rewrite"),
7144 EventTriggerEvent::SqlDrop => write!(f, "sql_drop"),
7145 }
7146 }
7147}
7148
7149#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
7154#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7155#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
7156pub struct CreateEventTrigger {
7157 pub name: Ident,
7159 pub event: EventTriggerEvent,
7161 pub when_tags: Option<Vec<Value>>,
7163 pub execute: ObjectName,
7165 pub is_procedure: bool,
7167}
7168
7169impl fmt::Display for CreateEventTrigger {
7170 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7171 write!(f, "CREATE EVENT TRIGGER {} ON {}", self.name, self.event)?;
7172 if let Some(tags) = &self.when_tags {
7173 write!(f, " WHEN TAG IN ({})", display_comma_separated(tags))?;
7174 }
7175 let func_kw = if self.is_procedure {
7176 "PROCEDURE"
7177 } else {
7178 "FUNCTION"
7179 };
7180 write!(f, " EXECUTE {func_kw} {}()", self.execute)?;
7181 Ok(())
7182 }
7183}
7184
7185impl From<CreateUserMapping> for crate::ast::Statement {
7186 fn from(v: CreateUserMapping) -> Self {
7187 crate::ast::Statement::CreateUserMapping(v)
7188 }
7189}
7190
7191#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
7196#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7197#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
7198pub struct CreateTablespace {
7199 pub name: Ident,
7201 pub owner: Option<Ident>,
7203 pub location: Value,
7205 pub with_options: Vec<SqlOption>,
7207}
7208
7209impl fmt::Display for CreateTablespace {
7210 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7211 write!(f, "CREATE TABLESPACE {}", self.name)?;
7212 if let Some(owner) = &self.owner {
7213 write!(f, " OWNER {owner}")?;
7214 }
7215 write!(f, " LOCATION {}", self.location)?;
7216 if !self.with_options.is_empty() {
7217 write!(f, " WITH ({})", display_comma_separated(&self.with_options))?;
7218 }
7219 Ok(())
7220 }
7221}
7222
7223impl From<CreateEventTrigger> for crate::ast::Statement {
7224 fn from(v: CreateEventTrigger) -> Self {
7225 crate::ast::Statement::CreateEventTrigger(v)
7226 }
7227}
7228
7229#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
7236#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7237#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
7238pub struct TransformElement {
7239 pub is_from: bool,
7241 pub function: ObjectName,
7243 pub arg_types: Vec<DataType>,
7245}
7246
7247impl fmt::Display for TransformElement {
7248 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7249 let direction = if self.is_from { "FROM" } else { "TO" };
7250 write!(
7251 f,
7252 "{direction} SQL WITH FUNCTION {}({})",
7253 self.function,
7254 display_comma_separated(&self.arg_types),
7255 )
7256 }
7257}
7258
7259#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
7264#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7265#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
7266pub struct CreateTransform {
7267 pub or_replace: bool,
7269 pub type_name: DataType,
7271 pub language: Ident,
7273 pub elements: Vec<TransformElement>,
7275}
7276
7277impl fmt::Display for CreateTransform {
7278 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7279 write!(f, "CREATE")?;
7280 if self.or_replace {
7281 write!(f, " OR REPLACE")?;
7282 }
7283 write!(
7284 f,
7285 " TRANSFORM FOR {} LANGUAGE {} ({})",
7286 self.type_name,
7287 self.language,
7288 display_comma_separated(&self.elements),
7289 )
7290 }
7291}
7292
7293impl From<CreateTransform> for crate::ast::Statement {
7294 fn from(v: CreateTransform) -> Self {
7295 crate::ast::Statement::CreateTransform(v)
7296 }
7297}
7298
7299impl From<CreateTablespace> for crate::ast::Statement {
7300 fn from(v: CreateTablespace) -> Self {
7301 crate::ast::Statement::CreateTablespace(v)
7302 }
7303}