You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2025/09/19 08:39:18 UTC

(datafusion-sqlparser-rs) branch main updated: feat: support postgres alter schema (#2038)

This is an automated email from the ASF dual-hosted git repository.

github-bot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git


The following commit(s) were added to refs/heads/main by this push:
     new 0b723147 feat: support postgres alter schema (#2038)
0b723147 is described below

commit 0b723147b678a9e84ebb2c46245e80950245a314
Author: Chen Chongchen <ch...@qq.com>
AuthorDate: Fri Sep 19 16:37:27 2025 +0800

    feat: support postgres alter schema (#2038)
---
 src/ast/ddl.rs              |  9 +++++++
 src/ast/spans.rs            | 10 ++++++-
 src/parser/mod.rs           |  6 +++++
 tests/sqlparser_postgres.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 87 insertions(+), 1 deletion(-)

diff --git a/src/ast/ddl.rs b/src/ast/ddl.rs
index a1547475..f998143c 100644
--- a/src/ast/ddl.rs
+++ b/src/ast/ddl.rs
@@ -3084,6 +3084,7 @@ impl fmt::Display for CreateConnector {
 /// An `ALTER SCHEMA` (`Statement::AlterSchema`) operation.
 ///
 /// See [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#alter_schema_collate_statement)
+/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-alterschema.html)
 #[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
 #[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
@@ -3101,6 +3102,12 @@ pub enum AlterSchemaOperation {
     SetOptionsParens {
         options: Vec<SqlOption>,
     },
+    Rename {
+        name: ObjectName,
+    },
+    OwnerTo {
+        owner: Owner,
+    },
 }
 
 impl fmt::Display for AlterSchemaOperation {
@@ -3120,6 +3127,8 @@ impl fmt::Display for AlterSchemaOperation {
             AlterSchemaOperation::SetOptionsParens { options } => {
                 write!(f, "SET OPTIONS ({})", display_comma_separated(options))
             }
+            AlterSchemaOperation::Rename { name } => write!(f, "RENAME TO {name}"),
+            AlterSchemaOperation::OwnerTo { owner } => write!(f, "OWNER TO {owner}"),
         }
     }
 }
diff --git a/src/ast/spans.rs b/src/ast/spans.rs
index 4e15edd8..0c205b82 100644
--- a/src/ast/spans.rs
+++ b/src/ast/spans.rs
@@ -17,7 +17,7 @@
 
 use crate::ast::{
     ddl::AlterSchema, query::SelectItemQualifiedWildcardKind, AlterSchemaOperation, ColumnOptions,
-    ExportData, TypedString,
+    ExportData, Owner, TypedString,
 };
 use core::iter;
 
@@ -2424,6 +2424,14 @@ impl Spanned for AlterSchemaOperation {
             AlterSchemaOperation::SetOptionsParens { options } => {
                 union_spans(options.iter().map(|i| i.span()))
             }
+            AlterSchemaOperation::Rename { name } => name.span(),
+            AlterSchemaOperation::OwnerTo { owner } => {
+                if let Owner::Ident(ident) = owner {
+                    ident.span
+                } else {
+                    Span::empty()
+                }
+            }
         }
     }
 }
diff --git a/src/parser/mod.rs b/src/parser/mod.rs
index e121066f..d661efd4 100644
--- a/src/parser/mod.rs
+++ b/src/parser/mod.rs
@@ -9444,6 +9444,12 @@ impl<'a> Parser<'a> {
         } else if self.parse_keywords(&[Keyword::DROP, Keyword::REPLICA]) {
             let replica = self.parse_identifier()?;
             AlterSchemaOperation::DropReplica { replica }
+        } else if self.parse_keywords(&[Keyword::RENAME, Keyword::TO]) {
+            let new_name = self.parse_object_name(false)?;
+            AlterSchemaOperation::Rename { name: new_name }
+        } else if self.parse_keywords(&[Keyword::OWNER, Keyword::TO]) {
+            let owner = self.parse_owner()?;
+            AlterSchemaOperation::OwnerTo { owner }
         } else {
             return self.expected_ref("ALTER SCHEMA operation", self.peek_token_ref());
         };
diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs
index 5b95bb30..196a82f5 100644
--- a/tests/sqlparser_postgres.rs
+++ b/tests/sqlparser_postgres.rs
@@ -6576,3 +6576,66 @@ fn parse_create_server() {
         assert_eq!(stmt, expected);
     }
 }
+
+#[test]
+fn parse_alter_schema() {
+    match pg_and_generic().verified_stmt("ALTER SCHEMA foo RENAME TO bar") {
+        Statement::AlterSchema(AlterSchema { operations, .. }) => {
+            assert_eq!(
+                operations,
+                vec![AlterSchemaOperation::Rename {
+                    name: ObjectName::from(vec!["bar".into()])
+                }]
+            );
+        }
+        _ => unreachable!(),
+    }
+
+    match pg_and_generic().verified_stmt("ALTER SCHEMA foo OWNER TO bar") {
+        Statement::AlterSchema(AlterSchema { operations, .. }) => {
+            assert_eq!(
+                operations,
+                vec![AlterSchemaOperation::OwnerTo {
+                    owner: Owner::Ident("bar".into())
+                }]
+            );
+        }
+        _ => unreachable!(),
+    }
+
+    match pg_and_generic().verified_stmt("ALTER SCHEMA foo OWNER TO CURRENT_ROLE") {
+        Statement::AlterSchema(AlterSchema { operations, .. }) => {
+            assert_eq!(
+                operations,
+                vec![AlterSchemaOperation::OwnerTo {
+                    owner: Owner::CurrentRole
+                }]
+            );
+        }
+        _ => unreachable!(),
+    }
+
+    match pg_and_generic().verified_stmt("ALTER SCHEMA foo OWNER TO CURRENT_USER") {
+        Statement::AlterSchema(AlterSchema { operations, .. }) => {
+            assert_eq!(
+                operations,
+                vec![AlterSchemaOperation::OwnerTo {
+                    owner: Owner::CurrentUser
+                }]
+            );
+        }
+        _ => unreachable!(),
+    }
+
+    match pg_and_generic().verified_stmt("ALTER SCHEMA foo OWNER TO SESSION_USER") {
+        Statement::AlterSchema(AlterSchema { operations, .. }) => {
+            assert_eq!(
+                operations,
+                vec![AlterSchemaOperation::OwnerTo {
+                    owner: Owner::SessionUser
+                }]
+            );
+        }
+        _ => unreachable!(),
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org