Skip to content

Commit 3cd2a47

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 fb578bb commit 3cd2a47

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

src/ast/mod.rs

+36-6
Original file line numberDiff line numberDiff line change
@@ -3270,6 +3270,22 @@ pub enum Statement {
32703270
/// `<schema name> | AUTHORIZATION <schema authorization identifier> | <schema name> AUTHORIZATION <schema authorization identifier>`
32713271
schema_name: SchemaName,
32723272
if_not_exists: bool,
3273+
/// Schema options.
3274+
///
3275+
/// ```sql
3276+
/// CREATE SCHEMA myschema OPTIONS(key1='value1');
3277+
/// ```
3278+
///
3279+
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_schema_statement)
3280+
options: Option<Vec<SqlOption>>,
3281+
/// Default collation specification for the schema.
3282+
///
3283+
/// ```sql
3284+
/// CREATE SCHEMA myschema DEFAULT COLLATE 'und:ci';
3285+
/// ```
3286+
///
3287+
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_schema_statement)
3288+
default_collate_spec: Option<Expr>,
32733289
},
32743290
/// ```sql
32753291
/// CREATE DATABASE
@@ -4995,12 +5011,26 @@ impl fmt::Display for Statement {
49955011
Statement::CreateSchema {
49965012
schema_name,
49975013
if_not_exists,
4998-
} => write!(
4999-
f,
5000-
"CREATE SCHEMA {if_not_exists}{name}",
5001-
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
5002-
name = schema_name
5003-
),
5014+
options,
5015+
default_collate_spec,
5016+
} => {
5017+
write!(
5018+
f,
5019+
"CREATE SCHEMA {if_not_exists}{name}",
5020+
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
5021+
name = schema_name
5022+
)?;
5023+
5024+
if let Some(collate) = default_collate_spec {
5025+
write!(f, " DEFAULT COLLATE {collate}")?;
5026+
}
5027+
5028+
if let Some(options) = options {
5029+
write!(f, " OPTIONS({})", display_comma_separated(options))?;
5030+
}
5031+
5032+
Ok(())
5033+
}
50045034
Statement::Assert { condition, message } => {
50055035
write!(f, "ASSERT {condition}")?;
50065036
if let Some(m) = message {

src/parser/mod.rs

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

46284628
let schema_name = self.parse_schema_name()?;
46294629

4630+
let default_collate_spec = if self.parse_keywords(&[Keyword::DEFAULT, Keyword::COLLATE]) {
4631+
Some(self.parse_expr()?)
4632+
} else {
4633+
None
4634+
};
4635+
4636+
let options = if self.peek_keyword(Keyword::OPTIONS) {
4637+
Some(self.parse_options(Keyword::OPTIONS)?)
4638+
} else {
4639+
None
4640+
};
4641+
46304642
Ok(Statement::CreateSchema {
46314643
schema_name,
46324644
if_not_exists,
4645+
options,
4646+
default_collate_spec,
46334647
})
46344648
}
46354649

tests/sqlparser_common.rs

+5
Original file line numberDiff line numberDiff line change
@@ -4208,6 +4208,11 @@ fn parse_create_schema() {
42084208
}
42094209
_ => unreachable!(),
42104210
}
4211+
4212+
verified_stmt(r#"CREATE SCHEMA a.b.c OPTIONS(key1 = 'value1', key2 = 'value2')"#);
4213+
verified_stmt(r#"CREATE SCHEMA IF NOT EXISTS a OPTIONS(key1 = 'value1')"#);
4214+
verified_stmt(r#"CREATE SCHEMA IF NOT EXISTS a OPTIONS()"#);
4215+
verified_stmt(r#"CREATE SCHEMA IF NOT EXISTS a DEFAULT COLLATE 'und:ci' OPTIONS()"#);
42114216
}
42124217

42134218
#[test]

tests/sqlparser_postgres.rs

+2
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,8 @@ fn parse_create_schema_if_not_exists() {
988988
Statement::CreateSchema {
989989
if_not_exists: true,
990990
schema_name,
991+
options: _,
992+
default_collate_spec: _,
991993
} => assert_eq!("schema_name", schema_name.to_string()),
992994
_ => unreachable!(),
993995
}

0 commit comments

Comments
 (0)