Skip to content

Commit

Permalink
format option labels without leading space
Browse files Browse the repository at this point in the history
also replace custom trimming with concat
  • Loading branch information
iffyio committed Dec 7, 2023
1 parent b374f0e commit 3b2dec1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 52 deletions.
6 changes: 3 additions & 3 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ impl fmt::Display for ViewColumnDef {
if let Some(options) = self.options.as_ref() {
write!(
f,
" OPTIONS ({})",
" OPTIONS({})",
display_comma_separated(options.as_slice())
)?;
}
Expand Down Expand Up @@ -628,7 +628,7 @@ pub enum ColumnOption {
/// BigQuery specific: Explicit column options in a view [1] or table [2]
/// Syntax
/// ```sql
/// OPTIONS (description="field desc")
/// OPTIONS(description="field desc")
/// ```
/// [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#view_column_option_list
/// [2]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#column_option_list
Expand Down Expand Up @@ -707,7 +707,7 @@ impl fmt::Display for ColumnOption {
}
}
SqlOptions(options) => {
write!(f, "OPTIONS ({})", display_comma_separated(options))
write!(f, "OPTIONS({})", display_comma_separated(options))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1376,7 +1376,7 @@ pub enum CreateTableOptions {
/// <https://www.postgresql.org/docs/current/sql-createtable.html>
With(Vec<SqlOption>),
/// Options specified using the `OPTIONS` keyword.
/// e.g. `OPTIONS (description = "123")`
/// e.g. `OPTIONS(description = "123")`
///
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#table_option_list>
Options(Vec<SqlOption>),
Expand All @@ -1389,7 +1389,7 @@ impl fmt::Display for CreateTableOptions {
write!(f, "WITH ({})", display_comma_separated(with_options))
}
CreateTableOptions::Options(options) => {
write!(f, "OPTIONS ({})", display_comma_separated(options))
write!(f, "OPTIONS({})", display_comma_separated(options))
}
CreateTableOptions::None => Ok(()),
}
Expand Down
71 changes: 24 additions & 47 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,6 @@ use sqlparser::dialect::{BigQueryDialect, GenericDialect};
use sqlparser::parser::ParserError;
use test_utils::*;

/// Strips out newlines and spaces from the `sql` so that it can
/// be comparable with the serialized result
fn trim_sql(sql: &str) -> String {
sql.split('\n')
.filter(|line| !line.trim().is_empty())
.map(|line| line.trim_start())
.collect::<Vec<_>>()
.join(" ")
.trim()
.to_string()
}

#[test]
fn parse_literal_string() {
let sql = r#"SELECT 'single', "double""#;
Expand Down Expand Up @@ -100,18 +88,14 @@ fn parse_raw_literal() {

#[test]
fn parse_create_view_with_options() {
let sql = trim_sql(
r#"
CREATE VIEW myproject.mydataset.newview
(name, age OPTIONS (description = "field age"))
OPTIONS
(expiration_timestamp = TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 48 HOUR),
friendly_name = "newview",
description = "a view that expires in 2 days",
labels = [("org_unit", "development")])
AS SELECT column_1, column_2, column_3 FROM myproject.mydataset.mytable"#,
let sql = concat!(
"CREATE VIEW myproject.mydataset.newview ",
r#"(name, age OPTIONS(description = "field age")) "#,
r#"OPTIONS(expiration_timestamp = TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 48 HOUR), "#,
r#"friendly_name = "newview", description = "a view that expires in 2 days", labels = [("org_unit", "development")]) "#,
"AS SELECT column_1, column_2, column_3 FROM myproject.mydataset.mytable",
);
match bigquery().verified_stmt(sql.as_str()) {
match bigquery().verified_stmt(sql) {
Statement::CreateView {
name,
query,
Expand Down Expand Up @@ -148,7 +132,7 @@ fn parse_create_view_with_options() {
query.to_string()
);
assert_eq!(
r#"OPTIONS (expiration_timestamp = TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 48 HOUR), friendly_name = "newview", description = "a view that expires in 2 days", labels = [("org_unit", "development")])"#,
r#"OPTIONS(expiration_timestamp = TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 48 HOUR), friendly_name = "newview", description = "a view that expires in 2 days", labels = [("org_unit", "development")])"#,
options.to_string()
);
let CreateTableOptions::Options(options) = options else {
Expand All @@ -170,19 +154,15 @@ fn parse_create_view_with_options() {

#[test]
fn parse_create_table_with_options() {
let sql = trim_sql(
r#"
CREATE TABLE mydataset.newtable
(x INT64 NOT NULL OPTIONS (description = "field x"),
y BOOL OPTIONS (description = "field y"))
PARTITION BY _PARTITIONDATE
CLUSTER BY userid, age
OPTIONS(partition_expiration_days = 1,
description = "table option description")
"#,
let sql = concat!(
"CREATE TABLE mydataset.newtable ",
r#"(x INT64 NOT NULL OPTIONS(description = "field x"), "#,
r#"y BOOL OPTIONS(description = "field y")) "#,
"PARTITION BY _PARTITIONDATE ",
"CLUSTER BY userid, age ",
r#"OPTIONS(partition_expiration_days = 1, description = "table option description")"#
);
match bigquery().verified_stmt(sql.as_str()) {
match bigquery().verified_stmt(sql) {
Statement::CreateTable {
name,
columns,
Expand Down Expand Up @@ -255,18 +235,15 @@ fn parse_create_table_with_options() {
_ => unreachable!(),
}

let sql = trim_sql(
r#"
CREATE TABLE mydataset.newtable
(x INT64 NOT NULL OPTIONS (description = "field x"),
y BOOL OPTIONS (description = "field y"))
CLUSTER BY userid
OPTIONS(partition_expiration_days = 1,
description = "table option description")
"#,
let sql = concat!(
"CREATE TABLE mydataset.newtable ",
r#"(x INT64 NOT NULL OPTIONS(description = "field x"), "#,
r#"y BOOL OPTIONS(description = "field y")) "#,
"CLUSTER BY userid ",
r#"OPTIONS(partition_expiration_days = 1, "#,
r#"description = "table option description")"#
);
bigquery().verified_stmt(sql.as_str());
bigquery().verified_stmt(sql);
}

#[test]
Expand Down

0 comments on commit 3b2dec1

Please sign in to comment.