Skip to content

Commit 426b151

Browse files
committed
Add support for CREATE SCHEMA options
```sql CREATE SCHEMA DEFAULT COLLATE 'foo' myschema OPTIONS(key='value'); ``` [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_schema_statement)
1 parent aab12ad commit 426b151

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

src/ast/mod.rs

+36-6
Original file line numberDiff line numberDiff line change
@@ -3144,6 +3144,22 @@ pub enum Statement {
31443144
/// `<schema name> | AUTHORIZATION <schema authorization identifier> | <schema name> AUTHORIZATION <schema authorization identifier>`
31453145
schema_name: SchemaName,
31463146
if_not_exists: bool,
3147+
/// Schema options.
3148+
///
3149+
/// ```sql
3150+
/// CREATE SCHEMA myschema OPTIONS(key1='value1');
3151+
/// ```
3152+
///
3153+
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_schema_statement)
3154+
options: Option<Vec<SqlOption>>,
3155+
/// Default collation specification for the schema.
3156+
///
3157+
/// ```sql
3158+
/// CREATE SCHEMA myschema DEFAULT COLLATE 'und:ci';
3159+
/// ```
3160+
///
3161+
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_schema_statement)
3162+
default_collate_spec: Option<Expr>,
31473163
},
31483164
/// ```sql
31493165
/// CREATE DATABASE
@@ -4928,12 +4944,26 @@ impl fmt::Display for Statement {
49284944
Statement::CreateSchema {
49294945
schema_name,
49304946
if_not_exists,
4931-
} => write!(
4932-
f,
4933-
"CREATE SCHEMA {if_not_exists}{name}",
4934-
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
4935-
name = schema_name
4936-
),
4947+
options,
4948+
default_collate_spec,
4949+
} => {
4950+
write!(
4951+
f,
4952+
"CREATE SCHEMA {if_not_exists}{name}",
4953+
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
4954+
name = schema_name
4955+
)?;
4956+
4957+
if let Some(collate) = default_collate_spec {
4958+
write!(f, " DEFAULT COLLATE {collate}")?;
4959+
}
4960+
4961+
if let Some(options) = options {
4962+
write!(f, " OPTIONS({})", display_comma_separated(options))?;
4963+
}
4964+
4965+
Ok(())
4966+
}
49374967
Statement::Assert { condition, message } => {
49384968
write!(f, "ASSERT {condition}")?;
49394969
if let Some(m) = message {

src/parser/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -4614,9 +4614,23 @@ impl<'a> Parser<'a> {
46144614

46154615
let schema_name = self.parse_schema_name()?;
46164616

4617+
let default_collate_spec = if self.parse_keywords(&[Keyword::DEFAULT, Keyword::COLLATE]) {
4618+
Some(self.parse_expr()?)
4619+
} else {
4620+
None
4621+
};
4622+
4623+
let options = if self.peek_keyword(Keyword::OPTIONS) {
4624+
Some(self.parse_options(Keyword::OPTIONS)?)
4625+
} else {
4626+
None
4627+
};
4628+
46174629
Ok(Statement::CreateSchema {
46184630
schema_name,
46194631
if_not_exists,
4632+
options,
4633+
default_collate_spec,
46204634
})
46214635
}
46224636

tests/sqlparser_common.rs

+5
Original file line numberDiff line numberDiff line change
@@ -4124,6 +4124,11 @@ fn parse_create_schema() {
41244124
}
41254125
_ => unreachable!(),
41264126
}
4127+
4128+
verified_stmt(r#"CREATE SCHEMA a.b.c OPTIONS(key1 = 'value1', key2 = 'value2')"#);
4129+
verified_stmt(r#"CREATE SCHEMA IF NOT EXISTS a OPTIONS(key1 = 'value1')"#);
4130+
verified_stmt(r#"CREATE SCHEMA IF NOT EXISTS a OPTIONS()"#);
4131+
verified_stmt(r#"CREATE SCHEMA IF NOT EXISTS a DEFAULT COLLATE 'und:ci' OPTIONS()"#);
41274132
}
41284133

41294134
#[test]

tests/sqlparser_postgres.rs

+1
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,7 @@ fn parse_create_schema_if_not_exists() {
985985
Statement::CreateSchema {
986986
if_not_exists: true,
987987
schema_name,
988+
..
988989
} => assert_eq!("schema_name", schema_name.to_string()),
989990
_ => unreachable!(),
990991
}

0 commit comments

Comments
 (0)