Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3846,6 +3846,14 @@ pub enum Statement {
///
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_schema_statement)
default_collate_spec: Option<Expr>,
/// Clones a schema
///
/// ```sql
/// CREATE SCHEMA myschema CLONE otherschema
/// ```
///
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/sql/create-clone#databases-schemas)
clone: Option<ObjectName>,
},
/// ```sql
/// CREATE DATABASE
Expand All @@ -3855,6 +3863,14 @@ pub enum Statement {
if_not_exists: bool,
location: Option<String>,
managed_location: Option<String>,
/// Clones a database
///
/// ```sql
/// CREATE DATABASE mydb CLONE otherdb
/// ```
///
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/sql/create-clone#databases-schemas)
clone: Option<ObjectName>,
},
/// ```sql
/// CREATE FUNCTION
Expand Down Expand Up @@ -4797,6 +4813,7 @@ impl fmt::Display for Statement {
if_not_exists,
location,
managed_location,
clone,
} => {
write!(f, "CREATE DATABASE")?;
if *if_not_exists {
Expand All @@ -4809,6 +4826,9 @@ impl fmt::Display for Statement {
if let Some(ml) = managed_location {
write!(f, " MANAGEDLOCATION '{ml}'")?;
}
if let Some(clone) = clone {
write!(f, " CLONE {clone}")?;
}
Ok(())
}
Statement::CreateFunction(create_function) => create_function.fmt(f),
Expand Down Expand Up @@ -5730,6 +5750,7 @@ impl fmt::Display for Statement {
with,
options,
default_collate_spec,
clone,
} => {
write!(
f,
Expand All @@ -5750,6 +5771,9 @@ impl fmt::Display for Statement {
write!(f, " OPTIONS({})", display_comma_separated(options))?;
}

if let Some(clone) = clone {
write!(f, " CLONE {clone}")?;
}
Ok(())
}
Statement::Assert { condition, message } => {
Expand Down
14 changes: 14 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4900,12 +4900,19 @@ impl<'a> Parser<'a> {
None
};

let clone = if self.parse_keyword(Keyword::CLONE) {
Some(self.parse_object_name(false)?)
} else {
None
};

Ok(Statement::CreateSchema {
schema_name,
if_not_exists,
with,
options,
default_collate_spec,
clone,
})
}

Expand Down Expand Up @@ -4940,11 +4947,18 @@ impl<'a> Parser<'a> {
_ => break,
}
}
let clone = if self.parse_keyword(Keyword::CLONE) {
Some(self.parse_object_name(false)?)
} else {
None
};

Ok(Statement::CreateDatabase {
db_name,
if_not_exists: ine,
location,
managed_location,
clone,
})
}

Expand Down
25 changes: 25 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4296,6 +4296,7 @@ fn parse_create_schema() {
verified_stmt(r#"CREATE SCHEMA a.b.c WITH (key1 = 'value1', key2 = 'value2')"#);
verified_stmt(r#"CREATE SCHEMA IF NOT EXISTS a WITH (key1 = 'value1')"#);
verified_stmt(r#"CREATE SCHEMA IF NOT EXISTS a WITH ()"#);
verified_stmt(r#"CREATE SCHEMA a CLONE b"#);
}

#[test]
Expand Down Expand Up @@ -7900,11 +7901,33 @@ fn parse_create_database() {
if_not_exists,
location,
managed_location,
clone,
} => {
assert_eq!("mydb", db_name.to_string());
assert!(!if_not_exists);
assert_eq!(None, location);
assert_eq!(None, managed_location);
assert_eq!(None, clone);
}
_ => unreachable!(),
}
let sql = "CREATE DATABASE mydb CLONE otherdb";
match verified_stmt(sql) {
Statement::CreateDatabase {
db_name,
if_not_exists,
location,
managed_location,
clone,
} => {
assert_eq!("mydb", db_name.to_string());
assert!(!if_not_exists);
assert_eq!(None, location);
assert_eq!(None, managed_location);
assert_eq!(
Some(ObjectName::from(vec![Ident::new("otherdb".to_string())])),
clone
);
}
_ => unreachable!(),
}
Expand All @@ -7919,11 +7942,13 @@ fn parse_create_database_ine() {
if_not_exists,
location,
managed_location,
clone,
} => {
assert_eq!("mydb", db_name.to_string());
assert!(if_not_exists);
assert_eq!(None, location);
assert_eq!(None, managed_location);
assert_eq!(None, clone);
}
_ => unreachable!(),
}
Expand Down
Loading