Skip to content

Commit 8348fbd

Browse files
chenkovskyayman-sigma
authored andcommitted
feat: support postgres alter schema (apache#2038)
1 parent 18ace7f commit 8348fbd

4 files changed

Lines changed: 87 additions & 1 deletion

File tree

src/ast/ddl.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3084,6 +3084,7 @@ impl fmt::Display for CreateConnector {
30843084
/// An `ALTER SCHEMA` (`Statement::AlterSchema`) operation.
30853085
///
30863086
/// See [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#alter_schema_collate_statement)
3087+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-alterschema.html)
30873088
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
30883089
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
30893090
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
@@ -3101,6 +3102,12 @@ pub enum AlterSchemaOperation {
31013102
SetOptionsParens {
31023103
options: Vec<SqlOption>,
31033104
},
3105+
Rename {
3106+
name: ObjectName,
3107+
},
3108+
OwnerTo {
3109+
owner: Owner,
3110+
},
31043111
}
31053112

31063113
impl fmt::Display for AlterSchemaOperation {
@@ -3120,6 +3127,8 @@ impl fmt::Display for AlterSchemaOperation {
31203127
AlterSchemaOperation::SetOptionsParens { options } => {
31213128
write!(f, "SET OPTIONS ({})", display_comma_separated(options))
31223129
}
3130+
AlterSchemaOperation::Rename { name } => write!(f, "RENAME TO {name}"),
3131+
AlterSchemaOperation::OwnerTo { owner } => write!(f, "OWNER TO {owner}"),
31233132
}
31243133
}
31253134
}

src/ast/spans.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
use crate::ast::{
1919
ddl::AlterSchema, query::SelectItemQualifiedWildcardKind, AlterSchemaOperation, ColumnOptions,
20-
ExportData, TypedString,
20+
ExportData, Owner, TypedString,
2121
};
2222
use core::iter;
2323

@@ -2433,6 +2433,14 @@ impl Spanned for AlterSchemaOperation {
24332433
AlterSchemaOperation::SetOptionsParens { options } => {
24342434
union_spans(options.iter().map(|i| i.span()))
24352435
}
2436+
AlterSchemaOperation::Rename { name } => name.span(),
2437+
AlterSchemaOperation::OwnerTo { owner } => {
2438+
if let Owner::Ident(ident) = owner {
2439+
ident.span
2440+
} else {
2441+
Span::empty()
2442+
}
2443+
}
24362444
}
24372445
}
24382446
}

src/parser/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9475,6 +9475,12 @@ impl<'a> Parser<'a> {
94759475
} else if self.parse_keywords(&[Keyword::DROP, Keyword::REPLICA]) {
94769476
let replica = self.parse_identifier()?;
94779477
AlterSchemaOperation::DropReplica { replica }
9478+
} else if self.parse_keywords(&[Keyword::RENAME, Keyword::TO]) {
9479+
let new_name = self.parse_object_name(false)?;
9480+
AlterSchemaOperation::Rename { name: new_name }
9481+
} else if self.parse_keywords(&[Keyword::OWNER, Keyword::TO]) {
9482+
let owner = self.parse_owner()?;
9483+
AlterSchemaOperation::OwnerTo { owner }
94789484
} else {
94799485
return self.expected_ref("ALTER SCHEMA operation", self.peek_token_ref());
94809486
};

tests/sqlparser_postgres.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6576,3 +6576,66 @@ fn parse_create_server() {
65766576
assert_eq!(stmt, expected);
65776577
}
65786578
}
6579+
6580+
#[test]
6581+
fn parse_alter_schema() {
6582+
match pg_and_generic().verified_stmt("ALTER SCHEMA foo RENAME TO bar") {
6583+
Statement::AlterSchema(AlterSchema { operations, .. }) => {
6584+
assert_eq!(
6585+
operations,
6586+
vec![AlterSchemaOperation::Rename {
6587+
name: ObjectName::from(vec!["bar".into()])
6588+
}]
6589+
);
6590+
}
6591+
_ => unreachable!(),
6592+
}
6593+
6594+
match pg_and_generic().verified_stmt("ALTER SCHEMA foo OWNER TO bar") {
6595+
Statement::AlterSchema(AlterSchema { operations, .. }) => {
6596+
assert_eq!(
6597+
operations,
6598+
vec![AlterSchemaOperation::OwnerTo {
6599+
owner: Owner::Ident("bar".into())
6600+
}]
6601+
);
6602+
}
6603+
_ => unreachable!(),
6604+
}
6605+
6606+
match pg_and_generic().verified_stmt("ALTER SCHEMA foo OWNER TO CURRENT_ROLE") {
6607+
Statement::AlterSchema(AlterSchema { operations, .. }) => {
6608+
assert_eq!(
6609+
operations,
6610+
vec![AlterSchemaOperation::OwnerTo {
6611+
owner: Owner::CurrentRole
6612+
}]
6613+
);
6614+
}
6615+
_ => unreachable!(),
6616+
}
6617+
6618+
match pg_and_generic().verified_stmt("ALTER SCHEMA foo OWNER TO CURRENT_USER") {
6619+
Statement::AlterSchema(AlterSchema { operations, .. }) => {
6620+
assert_eq!(
6621+
operations,
6622+
vec![AlterSchemaOperation::OwnerTo {
6623+
owner: Owner::CurrentUser
6624+
}]
6625+
);
6626+
}
6627+
_ => unreachable!(),
6628+
}
6629+
6630+
match pg_and_generic().verified_stmt("ALTER SCHEMA foo OWNER TO SESSION_USER") {
6631+
Statement::AlterSchema(AlterSchema { operations, .. }) => {
6632+
assert_eq!(
6633+
operations,
6634+
vec![AlterSchemaOperation::OwnerTo {
6635+
owner: Owner::SessionUser
6636+
}]
6637+
);
6638+
}
6639+
_ => unreachable!(),
6640+
}
6641+
}

0 commit comments

Comments
 (0)