Skip to content

Commit cdd8b3b

Browse files
iffyioayman-sigma
authored andcommitted
BigQuery: Add support for CREATE SCHEMA options (apache#1742)
1 parent c22e57a commit cdd8b3b

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
@@ -3468,6 +3468,22 @@ pub enum Statement {
34683468
/// `<schema name> | AUTHORIZATION <schema authorization identifier> | <schema name> AUTHORIZATION <schema authorization identifier>`
34693469
schema_name: SchemaName,
34703470
if_not_exists: bool,
3471+
/// Schema options.
3472+
///
3473+
/// ```sql
3474+
/// CREATE SCHEMA myschema OPTIONS(key1='value1');
3475+
/// ```
3476+
///
3477+
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_schema_statement)
3478+
options: Option<Vec<SqlOption>>,
3479+
/// Default collation specification for the schema.
3480+
///
3481+
/// ```sql
3482+
/// CREATE SCHEMA myschema DEFAULT COLLATE 'und:ci';
3483+
/// ```
3484+
///
3485+
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_schema_statement)
3486+
default_collate_spec: Option<Expr>,
34713487
},
34723488
/// ```sql
34733489
/// CREATE DATABASE
@@ -5195,12 +5211,26 @@ impl fmt::Display for Statement {
51955211
Statement::CreateSchema {
51965212
schema_name,
51975213
if_not_exists,
5198-
} => write!(
5199-
f,
5200-
"CREATE SCHEMA {if_not_exists}{name}",
5201-
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
5202-
name = schema_name
5203-
),
5214+
options,
5215+
default_collate_spec,
5216+
} => {
5217+
write!(
5218+
f,
5219+
"CREATE SCHEMA {if_not_exists}{name}",
5220+
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
5221+
name = schema_name
5222+
)?;
5223+
5224+
if let Some(collate) = default_collate_spec {
5225+
write!(f, " DEFAULT COLLATE {collate}")?;
5226+
}
5227+
5228+
if let Some(options) = options {
5229+
write!(f, " OPTIONS({})", display_comma_separated(options))?;
5230+
}
5231+
5232+
Ok(())
5233+
}
52045234
Statement::Assert { condition, message } => {
52055235
write!(f, "ASSERT {condition}")?;
52065236
if let Some(m) = message {

src/parser/mod.rs

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

47424742
let schema_name = self.parse_schema_name()?;
47434743

4744+
let default_collate_spec = if self.parse_keywords(&[Keyword::DEFAULT, Keyword::COLLATE]) {
4745+
Some(self.parse_expr()?)
4746+
} else {
4747+
None
4748+
};
4749+
4750+
let options = if self.peek_keyword(Keyword::OPTIONS) {
4751+
Some(self.parse_options(Keyword::OPTIONS)?)
4752+
} else {
4753+
None
4754+
};
4755+
47444756
Ok(Statement::CreateSchema {
47454757
schema_name,
47464758
if_not_exists,
4759+
options,
4760+
default_collate_spec,
47474761
})
47484762
}
47494763

tests/sqlparser_common.rs

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

42144219
#[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)