Skip to content

Commit 3b2dec1

Browse files
committed
format option labels without leading space
also replace custom trimming with concat
1 parent b374f0e commit 3b2dec1

File tree

3 files changed

+29
-52
lines changed

3 files changed

+29
-52
lines changed

src/ast/ddl.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ impl fmt::Display for ViewColumnDef {
543543
if let Some(options) = self.options.as_ref() {
544544
write!(
545545
f,
546-
" OPTIONS ({})",
546+
" OPTIONS({})",
547547
display_comma_separated(options.as_slice())
548548
)?;
549549
}
@@ -628,7 +628,7 @@ pub enum ColumnOption {
628628
/// BigQuery specific: Explicit column options in a view [1] or table [2]
629629
/// Syntax
630630
/// ```sql
631-
/// OPTIONS (description="field desc")
631+
/// OPTIONS(description="field desc")
632632
/// ```
633633
/// [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#view_column_option_list
634634
/// [2]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#column_option_list
@@ -707,7 +707,7 @@ impl fmt::Display for ColumnOption {
707707
}
708708
}
709709
SqlOptions(options) => {
710-
write!(f, "OPTIONS ({})", display_comma_separated(options))
710+
write!(f, "OPTIONS({})", display_comma_separated(options))
711711
}
712712
}
713713
}

src/ast/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,7 @@ pub enum CreateTableOptions {
13761376
/// <https://www.postgresql.org/docs/current/sql-createtable.html>
13771377
With(Vec<SqlOption>),
13781378
/// Options specified using the `OPTIONS` keyword.
1379-
/// e.g. `OPTIONS (description = "123")`
1379+
/// e.g. `OPTIONS(description = "123")`
13801380
///
13811381
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#table_option_list>
13821382
Options(Vec<SqlOption>),
@@ -1389,7 +1389,7 @@ impl fmt::Display for CreateTableOptions {
13891389
write!(f, "WITH ({})", display_comma_separated(with_options))
13901390
}
13911391
CreateTableOptions::Options(options) => {
1392-
write!(f, "OPTIONS ({})", display_comma_separated(options))
1392+
write!(f, "OPTIONS({})", display_comma_separated(options))
13931393
}
13941394
CreateTableOptions::None => Ok(()),
13951395
}

tests/sqlparser_bigquery.rs

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,6 @@ use sqlparser::dialect::{BigQueryDialect, GenericDialect};
2121
use sqlparser::parser::ParserError;
2222
use test_utils::*;
2323

24-
/// Strips out newlines and spaces from the `sql` so that it can
25-
/// be comparable with the serialized result
26-
fn trim_sql(sql: &str) -> String {
27-
sql.split('\n')
28-
.filter(|line| !line.trim().is_empty())
29-
.map(|line| line.trim_start())
30-
.collect::<Vec<_>>()
31-
.join(" ")
32-
.trim()
33-
.to_string()
34-
}
35-
3624
#[test]
3725
fn parse_literal_string() {
3826
let sql = r#"SELECT 'single', "double""#;
@@ -100,18 +88,14 @@ fn parse_raw_literal() {
10088

10189
#[test]
10290
fn parse_create_view_with_options() {
103-
let sql = trim_sql(
104-
r#"
105-
CREATE VIEW myproject.mydataset.newview
106-
(name, age OPTIONS (description = "field age"))
107-
OPTIONS
108-
(expiration_timestamp = TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 48 HOUR),
109-
friendly_name = "newview",
110-
description = "a view that expires in 2 days",
111-
labels = [("org_unit", "development")])
112-
AS SELECT column_1, column_2, column_3 FROM myproject.mydataset.mytable"#,
91+
let sql = concat!(
92+
"CREATE VIEW myproject.mydataset.newview ",
93+
r#"(name, age OPTIONS(description = "field age")) "#,
94+
r#"OPTIONS(expiration_timestamp = TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 48 HOUR), "#,
95+
r#"friendly_name = "newview", description = "a view that expires in 2 days", labels = [("org_unit", "development")]) "#,
96+
"AS SELECT column_1, column_2, column_3 FROM myproject.mydataset.mytable",
11397
);
114-
match bigquery().verified_stmt(sql.as_str()) {
98+
match bigquery().verified_stmt(sql) {
11599
Statement::CreateView {
116100
name,
117101
query,
@@ -148,7 +132,7 @@ fn parse_create_view_with_options() {
148132
query.to_string()
149133
);
150134
assert_eq!(
151-
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")])"#,
135+
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")])"#,
152136
options.to_string()
153137
);
154138
let CreateTableOptions::Options(options) = options else {
@@ -170,19 +154,15 @@ fn parse_create_view_with_options() {
170154

171155
#[test]
172156
fn parse_create_table_with_options() {
173-
let sql = trim_sql(
174-
r#"
175-
CREATE TABLE mydataset.newtable
176-
(x INT64 NOT NULL OPTIONS (description = "field x"),
177-
y BOOL OPTIONS (description = "field y"))
178-
179-
PARTITION BY _PARTITIONDATE
180-
CLUSTER BY userid, age
181-
OPTIONS(partition_expiration_days = 1,
182-
description = "table option description")
183-
"#,
157+
let sql = concat!(
158+
"CREATE TABLE mydataset.newtable ",
159+
r#"(x INT64 NOT NULL OPTIONS(description = "field x"), "#,
160+
r#"y BOOL OPTIONS(description = "field y")) "#,
161+
"PARTITION BY _PARTITIONDATE ",
162+
"CLUSTER BY userid, age ",
163+
r#"OPTIONS(partition_expiration_days = 1, description = "table option description")"#
184164
);
185-
match bigquery().verified_stmt(sql.as_str()) {
165+
match bigquery().verified_stmt(sql) {
186166
Statement::CreateTable {
187167
name,
188168
columns,
@@ -255,18 +235,15 @@ fn parse_create_table_with_options() {
255235
_ => unreachable!(),
256236
}
257237

258-
let sql = trim_sql(
259-
r#"
260-
CREATE TABLE mydataset.newtable
261-
(x INT64 NOT NULL OPTIONS (description = "field x"),
262-
y BOOL OPTIONS (description = "field y"))
263-
264-
CLUSTER BY userid
265-
OPTIONS(partition_expiration_days = 1,
266-
description = "table option description")
267-
"#,
238+
let sql = concat!(
239+
"CREATE TABLE mydataset.newtable ",
240+
r#"(x INT64 NOT NULL OPTIONS(description = "field x"), "#,
241+
r#"y BOOL OPTIONS(description = "field y")) "#,
242+
"CLUSTER BY userid ",
243+
r#"OPTIONS(partition_expiration_days = 1, "#,
244+
r#"description = "table option description")"#
268245
);
269-
bigquery().verified_stmt(sql.as_str());
246+
bigquery().verified_stmt(sql);
270247
}
271248

272249
#[test]

0 commit comments

Comments
 (0)