Skip to content

BigQuery: Add support for CREATE SCHEMA options #1742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2025
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
42 changes: 36 additions & 6 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3270,6 +3270,22 @@ pub enum Statement {
/// `<schema name> | AUTHORIZATION <schema authorization identifier> | <schema name> AUTHORIZATION <schema authorization identifier>`
schema_name: SchemaName,
if_not_exists: bool,
/// Schema options.
///
/// ```sql
/// CREATE SCHEMA myschema OPTIONS(key1='value1');
/// ```
///
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_schema_statement)
options: Option<Vec<SqlOption>>,
/// Default collation specification for the schema.
///
/// ```sql
/// CREATE SCHEMA myschema DEFAULT COLLATE 'und:ci';
/// ```
///
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_schema_statement)
default_collate_spec: Option<Expr>,
},
/// ```sql
/// CREATE DATABASE
Expand Down Expand Up @@ -4995,12 +5011,26 @@ impl fmt::Display for Statement {
Statement::CreateSchema {
schema_name,
if_not_exists,
} => write!(
f,
"CREATE SCHEMA {if_not_exists}{name}",
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
name = schema_name
),
options,
default_collate_spec,
} => {
write!(
f,
"CREATE SCHEMA {if_not_exists}{name}",
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
name = schema_name
)?;

if let Some(collate) = default_collate_spec {
write!(f, " DEFAULT COLLATE {collate}")?;
}

if let Some(options) = options {
write!(f, " OPTIONS({})", display_comma_separated(options))?;
}

Ok(())
}
Statement::Assert { condition, message } => {
write!(f, "ASSERT {condition}")?;
if let Some(m) = 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 @@ -4627,9 +4627,23 @@ impl<'a> Parser<'a> {

let schema_name = self.parse_schema_name()?;

let default_collate_spec = if self.parse_keywords(&[Keyword::DEFAULT, Keyword::COLLATE]) {
Some(self.parse_expr()?)
} else {
None
};

let options = if self.peek_keyword(Keyword::OPTIONS) {
Some(self.parse_options(Keyword::OPTIONS)?)
} else {
None
};

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

Expand Down
5 changes: 5 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4208,6 +4208,11 @@ fn parse_create_schema() {
}
_ => unreachable!(),
}

verified_stmt(r#"CREATE SCHEMA a.b.c OPTIONS(key1 = 'value1', key2 = 'value2')"#);
verified_stmt(r#"CREATE SCHEMA IF NOT EXISTS a OPTIONS(key1 = 'value1')"#);
verified_stmt(r#"CREATE SCHEMA IF NOT EXISTS a OPTIONS()"#);
verified_stmt(r#"CREATE SCHEMA IF NOT EXISTS a DEFAULT COLLATE 'und:ci' OPTIONS()"#);
}

#[test]
Expand Down
2 changes: 2 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,8 @@ fn parse_create_schema_if_not_exists() {
Statement::CreateSchema {
if_not_exists: true,
schema_name,
options: _,
default_collate_spec: _,
} => assert_eq!("schema_name", schema_name.to_string()),
_ => unreachable!(),
}
Expand Down