Skip to content

Commit f81aed6

Browse files
authored
BigQuery: Add support for CREATE SCHEMA options (#1742)
1 parent 862e887 commit f81aed6

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

src/ast/mod.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3450,6 +3450,22 @@ pub enum Statement {
34503450
/// `<schema name> | AUTHORIZATION <schema authorization identifier> | <schema name> AUTHORIZATION <schema authorization identifier>`
34513451
schema_name: SchemaName,
34523452
if_not_exists: bool,
3453+
/// Schema options.
3454+
///
3455+
/// ```sql
3456+
/// CREATE SCHEMA myschema OPTIONS(key1='value1');
3457+
/// ```
3458+
///
3459+
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_schema_statement)
3460+
options: Option<Vec<SqlOption>>,
3461+
/// Default collation specification for the schema.
3462+
///
3463+
/// ```sql
3464+
/// CREATE SCHEMA myschema DEFAULT COLLATE 'und:ci';
3465+
/// ```
3466+
///
3467+
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_schema_statement)
3468+
default_collate_spec: Option<Expr>,
34533469
},
34543470
/// ```sql
34553471
/// CREATE DATABASE
@@ -5177,12 +5193,26 @@ impl fmt::Display for Statement {
51775193
Statement::CreateSchema {
51785194
schema_name,
51795195
if_not_exists,
5180-
} => write!(
5181-
f,
5182-
"CREATE SCHEMA {if_not_exists}{name}",
5183-
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
5184-
name = schema_name
5185-
),
5196+
options,
5197+
default_collate_spec,
5198+
} => {
5199+
write!(
5200+
f,
5201+
"CREATE SCHEMA {if_not_exists}{name}",
5202+
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
5203+
name = schema_name
5204+
)?;
5205+
5206+
if let Some(collate) = default_collate_spec {
5207+
write!(f, " DEFAULT COLLATE {collate}")?;
5208+
}
5209+
5210+
if let Some(options) = options {
5211+
write!(f, " OPTIONS({})", display_comma_separated(options))?;
5212+
}
5213+
5214+
Ok(())
5215+
}
51865216
Statement::Assert { condition, message } => {
51875217
write!(f, "ASSERT {condition}")?;
51885218
if let Some(m) = message {

src/parser/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4731,9 +4731,23 @@ impl<'a> Parser<'a> {
47314731

47324732
let schema_name = self.parse_schema_name()?;
47334733

4734+
let default_collate_spec = if self.parse_keywords(&[Keyword::DEFAULT, Keyword::COLLATE]) {
4735+
Some(self.parse_expr()?)
4736+
} else {
4737+
None
4738+
};
4739+
4740+
let options = if self.peek_keyword(Keyword::OPTIONS) {
4741+
Some(self.parse_options(Keyword::OPTIONS)?)
4742+
} else {
4743+
None
4744+
};
4745+
47344746
Ok(Statement::CreateSchema {
47354747
schema_name,
47364748
if_not_exists,
4749+
options,
4750+
default_collate_spec,
47374751
})
47384752
}
47394753

tests/sqlparser_common.rs

Lines changed: 5 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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)