Skip to content

Commit 1421b87

Browse files
Add PostgreSQL Collation DDL Support (apache#2249)
1 parent 6fa3614 commit 1421b87

File tree

6 files changed

+422
-24
lines changed

6 files changed

+422
-24
lines changed

src/ast/ddl.rs

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4503,6 +4503,142 @@ impl Spanned for DropExtension {
45034503
}
45044504
}
45054505

4506+
/// CREATE COLLATION statement.
4507+
/// Note: this is a PostgreSQL-specific statement.
4508+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4509+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4510+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4511+
pub struct CreateCollation {
4512+
/// Whether `IF NOT EXISTS` was specified.
4513+
pub if_not_exists: bool,
4514+
/// Name of the collation being created.
4515+
pub name: ObjectName,
4516+
/// Source definition for the collation.
4517+
pub definition: CreateCollationDefinition,
4518+
}
4519+
4520+
/// Definition forms supported by `CREATE COLLATION`.
4521+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4522+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4523+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4524+
pub enum CreateCollationDefinition {
4525+
/// Create from an existing collation.
4526+
///
4527+
/// ```sql
4528+
/// CREATE COLLATION name FROM existing_collation
4529+
/// ```
4530+
From(ObjectName),
4531+
/// Create with an option list.
4532+
///
4533+
/// ```sql
4534+
/// CREATE COLLATION name (key = value, ...)
4535+
/// ```
4536+
Options(Vec<SqlOption>),
4537+
}
4538+
4539+
impl fmt::Display for CreateCollation {
4540+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4541+
write!(
4542+
f,
4543+
"CREATE COLLATION {if_not_exists}{name}",
4544+
if_not_exists = if self.if_not_exists {
4545+
"IF NOT EXISTS "
4546+
} else {
4547+
""
4548+
},
4549+
name = self.name
4550+
)?;
4551+
match &self.definition {
4552+
CreateCollationDefinition::From(existing_collation) => {
4553+
write!(f, " FROM {existing_collation}")
4554+
}
4555+
CreateCollationDefinition::Options(options) => {
4556+
write!(f, " ({})", display_comma_separated(options))
4557+
}
4558+
}
4559+
}
4560+
}
4561+
4562+
impl Spanned for CreateCollation {
4563+
fn span(&self) -> Span {
4564+
Span::empty()
4565+
}
4566+
}
4567+
4568+
/// ALTER COLLATION statement.
4569+
/// Note: this is a PostgreSQL-specific statement.
4570+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4571+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4572+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4573+
pub struct AlterCollation {
4574+
/// Name of the collation being altered.
4575+
pub name: ObjectName,
4576+
/// The operation to perform on the collation.
4577+
pub operation: AlterCollationOperation,
4578+
}
4579+
4580+
/// Operations supported by `ALTER COLLATION`.
4581+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4582+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4583+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4584+
pub enum AlterCollationOperation {
4585+
/// Rename the collation.
4586+
///
4587+
/// ```sql
4588+
/// ALTER COLLATION name RENAME TO new_name
4589+
/// ```
4590+
RenameTo {
4591+
/// New collation name.
4592+
new_name: Ident,
4593+
},
4594+
/// Change the collation owner.
4595+
///
4596+
/// ```sql
4597+
/// ALTER COLLATION name OWNER TO role_name
4598+
/// ```
4599+
OwnerTo(Owner),
4600+
/// Move the collation to another schema.
4601+
///
4602+
/// ```sql
4603+
/// ALTER COLLATION name SET SCHEMA new_schema
4604+
/// ```
4605+
SetSchema {
4606+
/// Target schema name.
4607+
schema_name: ObjectName,
4608+
},
4609+
/// Refresh collation version metadata.
4610+
///
4611+
/// ```sql
4612+
/// ALTER COLLATION name REFRESH VERSION
4613+
/// ```
4614+
RefreshVersion,
4615+
}
4616+
4617+
impl fmt::Display for AlterCollationOperation {
4618+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4619+
match self {
4620+
AlterCollationOperation::RenameTo { new_name } => write!(f, "RENAME TO {new_name}"),
4621+
AlterCollationOperation::OwnerTo(owner) => write!(f, "OWNER TO {owner}"),
4622+
AlterCollationOperation::SetSchema { schema_name } => {
4623+
write!(f, "SET SCHEMA {schema_name}")
4624+
}
4625+
AlterCollationOperation::RefreshVersion => write!(f, "REFRESH VERSION"),
4626+
}
4627+
}
4628+
}
4629+
4630+
impl fmt::Display for AlterCollation {
4631+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4632+
write!(f, "ALTER COLLATION {} {}", self.name, self.operation)
4633+
}
4634+
}
4635+
4636+
impl Spanned for AlterCollation {
4637+
fn span(&self) -> Span {
4638+
Span::empty()
4639+
}
4640+
}
4641+
45064642
/// Table type for ALTER TABLE statements.
45074643
/// Used to distinguish between regular tables, Iceberg tables, and Dynamic tables.
45084644
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]

src/ast/mod.rs

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,28 @@ pub use self::dcl::{
6060
SetConfigValue, Use,
6161
};
6262
pub use self::ddl::{
63-
Alignment, AlterColumnOperation, AlterConnectorOwner, AlterFunction, AlterFunctionAction,
64-
AlterFunctionKind, AlterFunctionOperation, AlterIndexOperation, AlterOperator,
65-
AlterOperatorClass, AlterOperatorClassOperation, AlterOperatorFamily,
66-
AlterOperatorFamilyOperation, AlterOperatorOperation, AlterPolicy, AlterPolicyOperation,
67-
AlterSchema, AlterSchemaOperation, AlterTable, AlterTableAlgorithm, AlterTableLock,
68-
AlterTableOperation, AlterTableType, AlterType, AlterTypeAddValue, AlterTypeAddValuePosition,
69-
AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue, ClusteredBy, ColumnDef,
70-
ColumnOption, ColumnOptionDef, ColumnOptions, ColumnPolicy, ColumnPolicyProperty,
71-
ConstraintCharacteristics, CreateConnector, CreateDomain, CreateExtension, CreateFunction,
72-
CreateIndex, CreateOperator, CreateOperatorClass, CreateOperatorFamily, CreatePolicy,
73-
CreatePolicyCommand, CreatePolicyType, CreateTable, CreateTrigger, CreateView, Deduplicate,
74-
DeferrableInitial, DistStyle, DropBehavior, DropExtension, DropFunction, DropOperator,
75-
DropOperatorClass, DropOperatorFamily, DropOperatorSignature, DropPolicy, DropTrigger,
76-
ForValues, FunctionReturnType, GeneratedAs, GeneratedExpressionMode, IdentityParameters,
77-
IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder,
78-
IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, Msck, NullsDistinctOption,
79-
OperatorArgTypes, OperatorClassItem, OperatorFamilyDropItem, OperatorFamilyItem,
80-
OperatorOption, OperatorPurpose, Owner, Partition, PartitionBoundValue, ProcedureParam,
81-
ReferentialAction, RenameTableNameKind, ReplicaIdentity, TagsColumnOption, TriggerObjectKind,
82-
Truncate, UserDefinedTypeCompositeAttributeDef, UserDefinedTypeInternalLength,
83-
UserDefinedTypeRangeOption, UserDefinedTypeRepresentation, UserDefinedTypeSqlDefinitionOption,
84-
UserDefinedTypeStorage, ViewColumnDef,
63+
Alignment, AlterCollation, AlterCollationOperation, AlterColumnOperation, AlterConnectorOwner,
64+
AlterFunction, AlterFunctionAction, AlterFunctionKind, AlterFunctionOperation,
65+
AlterIndexOperation, AlterOperator, AlterOperatorClass, AlterOperatorClassOperation,
66+
AlterOperatorFamily, AlterOperatorFamilyOperation, AlterOperatorOperation, AlterPolicy,
67+
AlterPolicyOperation, AlterSchema, AlterSchemaOperation, AlterTable, AlterTableAlgorithm,
68+
AlterTableLock, AlterTableOperation, AlterTableType, AlterType, AlterTypeAddValue,
69+
AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue,
70+
ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnOptions, ColumnPolicy,
71+
ColumnPolicyProperty, ConstraintCharacteristics, CreateCollation, CreateCollationDefinition,
72+
CreateConnector, CreateDomain, CreateExtension, CreateFunction, CreateIndex, CreateOperator,
73+
CreateOperatorClass, CreateOperatorFamily, CreatePolicy, CreatePolicyCommand, CreatePolicyType,
74+
CreateTable, CreateTrigger, CreateView, Deduplicate, DeferrableInitial, DistStyle,
75+
DropBehavior, DropExtension, DropFunction, DropOperator, DropOperatorClass, DropOperatorFamily,
76+
DropOperatorSignature, DropPolicy, DropTrigger, ForValues, FunctionReturnType, GeneratedAs,
77+
GeneratedExpressionMode, IdentityParameters, IdentityProperty, IdentityPropertyFormatKind,
78+
IdentityPropertyKind, IdentityPropertyOrder, IndexColumn, IndexOption, IndexType,
79+
KeyOrIndexDisplay, Msck, NullsDistinctOption, OperatorArgTypes, OperatorClassItem,
80+
OperatorFamilyDropItem, OperatorFamilyItem, OperatorOption, OperatorPurpose, Owner, Partition,
81+
PartitionBoundValue, ProcedureParam, ReferentialAction, RenameTableNameKind, ReplicaIdentity,
82+
TagsColumnOption, TriggerObjectKind, Truncate, UserDefinedTypeCompositeAttributeDef,
83+
UserDefinedTypeInternalLength, UserDefinedTypeRangeOption, UserDefinedTypeRepresentation,
84+
UserDefinedTypeSqlDefinitionOption, UserDefinedTypeStorage, ViewColumnDef,
8585
};
8686
pub use self::dml::{
8787
Delete, Insert, Merge, MergeAction, MergeClause, MergeClauseKind, MergeInsertExpr,
@@ -2450,6 +2450,8 @@ impl fmt::Display for ShowCreateObject {
24502450
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
24512451
/// Objects that can be targeted by a `COMMENT` statement.
24522452
pub enum CommentObject {
2453+
/// A collation.
2454+
Collation,
24532455
/// A table column.
24542456
Column,
24552457
/// A database.
@@ -2485,6 +2487,7 @@ pub enum CommentObject {
24852487
impl fmt::Display for CommentObject {
24862488
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24872489
match self {
2490+
CommentObject::Collation => f.write_str("COLLATION"),
24882491
CommentObject::Column => f.write_str("COLUMN"),
24892492
CommentObject::Database => f.write_str("DATABASE"),
24902493
CommentObject::Domain => f.write_str("DOMAIN"),
@@ -3764,6 +3767,11 @@ pub enum Statement {
37643767
/// ```
37653768
AlterType(AlterType),
37663769
/// ```sql
3770+
/// ALTER COLLATION
3771+
/// ```
3772+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-altercollation.html)
3773+
AlterCollation(AlterCollation),
3774+
/// ```sql
37673775
/// ALTER OPERATOR
37683776
/// ```
37693777
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-alteroperator.html)
@@ -3960,6 +3968,12 @@ pub enum Statement {
39603968
/// Note: this is a PostgreSQL-specific statement,
39613969
CreateExtension(CreateExtension),
39623970
/// ```sql
3971+
/// CREATE COLLATION
3972+
/// ```
3973+
/// Note: this is a PostgreSQL-specific statement.
3974+
/// <https://www.postgresql.org/docs/current/sql-createcollation.html>
3975+
CreateCollation(CreateCollation),
3976+
/// ```sql
39633977
/// DROP EXTENSION [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]
39643978
/// ```
39653979
/// Note: this is a PostgreSQL-specific statement.
@@ -5430,6 +5444,7 @@ impl fmt::Display for Statement {
54305444
}
54315445
Statement::CreateIndex(create_index) => create_index.fmt(f),
54325446
Statement::CreateExtension(create_extension) => write!(f, "{create_extension}"),
5447+
Statement::CreateCollation(create_collation) => write!(f, "{create_collation}"),
54335448
Statement::DropExtension(drop_extension) => write!(f, "{drop_extension}"),
54345449
Statement::DropOperator(drop_operator) => write!(f, "{drop_operator}"),
54355450
Statement::DropOperatorFamily(drop_operator_family) => {
@@ -5507,6 +5522,7 @@ impl fmt::Display for Statement {
55075522
Statement::AlterType(AlterType { name, operation }) => {
55085523
write!(f, "ALTER TYPE {name} {operation}")
55095524
}
5525+
Statement::AlterCollation(alter_collation) => write!(f, "{alter_collation}"),
55105526
Statement::AlterOperator(alter_operator) => write!(f, "{alter_operator}"),
55115527
Statement::AlterOperatorFamily(alter_operator_family) => {
55125528
write!(f, "{alter_operator_family}")
@@ -8380,6 +8396,8 @@ impl fmt::Display for HavingBoundKind {
83808396
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
83818397
/// Types of database objects referenced by DDL statements.
83828398
pub enum ObjectType {
8399+
/// A collation.
8400+
Collation,
83838401
/// A table.
83848402
Table,
83858403
/// A view.
@@ -8409,6 +8427,7 @@ pub enum ObjectType {
84098427
impl fmt::Display for ObjectType {
84108428
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
84118429
f.write_str(match self {
8430+
ObjectType::Collation => "COLLATION",
84128431
ObjectType::Table => "TABLE",
84138432
ObjectType::View => "VIEW",
84148433
ObjectType::MaterializedView => "MATERIALIZED VIEW",
@@ -12011,6 +12030,12 @@ impl From<CreateExtension> for Statement {
1201112030
}
1201212031
}
1201312032

12033+
impl From<CreateCollation> for Statement {
12034+
fn from(c: CreateCollation) -> Self {
12035+
Self::CreateCollation(c)
12036+
}
12037+
}
12038+
1201412039
impl From<DropExtension> for Statement {
1201512040
fn from(de: DropExtension) -> Self {
1201612041
Self::DropExtension(de)
@@ -12125,6 +12150,12 @@ impl From<AlterType> for Statement {
1212512150
}
1212612151
}
1212712152

12153+
impl From<AlterCollation> for Statement {
12154+
fn from(a: AlterCollation) -> Self {
12155+
Self::AlterCollation(a)
12156+
}
12157+
}
12158+
1212812159
impl From<AlterOperator> for Statement {
1212912160
fn from(a: AlterOperator) -> Self {
1213012161
Self::AlterOperator(a)

src/ast/spans.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ impl Spanned for Values {
264264
/// - [Statement::DropSecret]
265265
/// - [Statement::Declare]
266266
/// - [Statement::CreateExtension]
267+
/// - [Statement::CreateCollation]
268+
/// - [Statement::AlterCollation]
267269
/// - [Statement::Fetch]
268270
/// - [Statement::Flush]
269271
/// - [Statement::Discard]
@@ -377,6 +379,7 @@ impl Spanned for Statement {
377379
Statement::CreateIndex(create_index) => create_index.span(),
378380
Statement::CreateRole(create_role) => create_role.span(),
379381
Statement::CreateExtension(create_extension) => create_extension.span(),
382+
Statement::CreateCollation(create_collation) => create_collation.span(),
380383
Statement::DropExtension(drop_extension) => drop_extension.span(),
381384
Statement::DropOperator(drop_operator) => drop_operator.span(),
382385
Statement::DropOperatorFamily(drop_operator_family) => drop_operator_family.span(),
@@ -405,6 +408,7 @@ impl Spanned for Statement {
405408
// These statements need to be implemented
406409
Statement::AlterFunction { .. } => Span::empty(),
407410
Statement::AlterType { .. } => Span::empty(),
411+
Statement::AlterCollation { .. } => Span::empty(),
408412
Statement::AlterOperator { .. } => Span::empty(),
409413
Statement::AlterOperatorFamily { .. } => Span::empty(),
410414
Statement::AlterOperatorClass { .. } => Span::empty(),

0 commit comments

Comments
 (0)