Skip to content

Commit 6f8a716

Browse files
sabir-akhadov-localstackhovaescoclaude
authored
Accept CREATE OR REPLACE SCHEMA (#2396)
Co-authored-by: Przemek Denkiewicz <67517453+hovaesco@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent 09827e1 commit 6f8a716

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

src/ast/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4346,6 +4346,8 @@ pub enum Statement {
43464346
CreateSchema {
43474347
/// `<schema name> | AUTHORIZATION <schema authorization identifier> | <schema name> AUTHORIZATION <schema authorization identifier>`
43484348
schema_name: SchemaName,
4349+
/// `true` when `OR REPLACE` was present.
4350+
or_replace: bool,
43494351
/// `true` when `IF NOT EXISTS` was present.
43504352
if_not_exists: bool,
43514353
/// Schema properties.
@@ -6016,6 +6018,7 @@ impl fmt::Display for Statement {
60166018
}
60176019
Statement::CreateSchema {
60186020
schema_name,
6021+
or_replace,
60196022
if_not_exists,
60206023
with,
60216024
options,
@@ -6024,7 +6027,8 @@ impl fmt::Display for Statement {
60246027
} => {
60256028
write!(
60266029
f,
6027-
"CREATE SCHEMA {if_not_exists}{name}",
6030+
"CREATE {or_replace}SCHEMA {if_not_exists}{name}",
6031+
or_replace = if *or_replace { "OR REPLACE " } else { "" },
60286032
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
60296033
name = schema_name
60306034
)?;

src/parser/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5278,11 +5278,13 @@ impl<'a> Parser<'a> {
52785278
self.parse_create_secret(or_replace, temporary, persistent)
52795279
} else if self.parse_keyword(Keyword::USER) {
52805280
self.parse_create_user(or_replace).map(Into::into)
5281+
} else if self.parse_keyword(Keyword::SCHEMA) {
5282+
self.parse_create_schema(or_replace)
52815283
} else if self.parse_keyword(Keyword::WAREHOUSE) {
52825284
self.parse_create_warehouse(or_replace).map(Into::into)
52835285
} else if or_replace {
52845286
self.expected_ref(
5285-
"[EXTERNAL] TABLE or [MATERIALIZED] VIEW or FUNCTION or WAREHOUSE after CREATE OR REPLACE",
5287+
"[EXTERNAL] TABLE or [MATERIALIZED] VIEW or FUNCTION or SCHEMA or WAREHOUSE after CREATE OR REPLACE",
52865288
self.peek_token_ref(),
52875289
)
52885290
} else if self.parse_keyword(Keyword::EXTENSION) {
@@ -5293,8 +5295,6 @@ impl<'a> Parser<'a> {
52935295
self.parse_create_index(true).map(Into::into)
52945296
} else if self.parse_keyword(Keyword::VIRTUAL) {
52955297
self.parse_create_virtual_table()
5296-
} else if self.parse_keyword(Keyword::SCHEMA) {
5297-
self.parse_create_schema()
52985298
} else if self.parse_keyword(Keyword::DATABASE) {
52995299
self.parse_create_database()
53005300
} else if self.parse_keyword(Keyword::ROLE) {
@@ -5624,7 +5624,7 @@ impl<'a> Parser<'a> {
56245624
}
56255625

56265626
/// Parse a `CREATE SCHEMA` statement.
5627-
pub fn parse_create_schema(&mut self) -> Result<Statement, ParserError> {
5627+
pub fn parse_create_schema(&mut self, or_replace: bool) -> Result<Statement, ParserError> {
56285628
let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
56295629

56305630
let schema_name = self.parse_schema_name()?;
@@ -5655,6 +5655,7 @@ impl<'a> Parser<'a> {
56555655

56565656
Ok(Statement::CreateSchema {
56575657
schema_name,
5658+
or_replace,
56585659
if_not_exists,
56595660
with,
56605661
options,

tests/sqlparser_common.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4534,6 +4534,25 @@ fn parse_create_schema() {
45344534
verified_stmt(r#"CREATE SCHEMA a CLONE b"#);
45354535
}
45364536

4537+
#[test]
4538+
fn parse_create_or_replace_schema() {
4539+
match verified_stmt("CREATE OR REPLACE SCHEMA X") {
4540+
Statement::CreateSchema {
4541+
schema_name,
4542+
or_replace,
4543+
if_not_exists,
4544+
..
4545+
} => {
4546+
assert_eq!(schema_name.to_string(), "X".to_owned());
4547+
assert!(or_replace);
4548+
assert!(!if_not_exists);
4549+
}
4550+
_ => unreachable!(),
4551+
}
4552+
4553+
verified_stmt("CREATE OR REPLACE SCHEMA IF NOT EXISTS X");
4554+
}
4555+
45374556
#[test]
45384557
fn parse_create_schema_with_authorization() {
45394558
let sql = "CREATE SCHEMA AUTHORIZATION Y";

0 commit comments

Comments
 (0)