Skip to content

bigquery: OPTIONS on ColumnDef #34

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 26, 2024
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
10 changes: 9 additions & 1 deletion src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use core::fmt;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use sqlparser::ast::{Value, WithSpan};
use sqlparser::ast::{SqlOption, Value, WithSpan};
#[cfg(feature = "visitor")]
use sqlparser_derive::{Visit, VisitMut};

Expand Down Expand Up @@ -551,6 +551,7 @@ pub struct ColumnDef {
pub collation: Option<ObjectName>,
pub codec: Option<Vec<Expr>>,
pub options: Vec<ColumnOptionDef>,
pub column_options: Vec<SqlOption>,
}

impl fmt::Display for ColumnDef {
Expand All @@ -565,6 +566,13 @@ impl fmt::Display for ColumnDef {
for option in &self.options {
write!(f, " {option}")?;
}
if !self.column_options.is_empty() {
write!(
f,
" OPTIONS({})",
display_comma_separated(&self.column_options)
)?;
}
Ok(())
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4303,12 +4303,16 @@ impl<'a> Parser<'a> {
break;
};
}

let column_options = self.parse_options(Keyword::OPTIONS)?;

Ok(ColumnDef {
name,
data_type,
collation,
codec,
options,
column_options,
})
}

Expand Down
9 changes: 9 additions & 0 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ fn parse_nested_data_types() {
collation: None,
codec: None,
options: vec![],
column_options: vec![]
},
ColumnDef {
name: Ident::new("y").empty_span(),
Expand All @@ -125,6 +126,7 @@ fn parse_nested_data_types() {
collation: None,
codec: None,
options: vec![],
column_options: vec![]
},
]
);
Expand Down Expand Up @@ -1276,3 +1278,10 @@ fn test_create_external_table_with_options() {
r#"CREATE EXTERNAL TABLE mytable (id INT64, timestamp TIMESTAMP) OPTIONS (sheet_range = "synq", skip_leading_rows = 1, format = "GOOGLE_SHEETS", uris = ["https://docs.google.com/spreadsheets/d/1g3xwWi1r-Ln2VVwv4mswwmqyfMeoJglv-MS80ywASGI/edit#gid=0"])"#,
);
}

#[test]
fn test_create_table_field_options() {
bigquery().verified_stmt(
"CREATE TABLE `pr`.`ts`.`salesforce_accounts` (account_name STRING OPTIONS(description = \"Account name\", label = \"dev\"))",
);
}
12 changes: 12 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2481,6 +2481,7 @@ fn parse_create_table() {
name: None,
option: ColumnOption::NotNull,
}],
column_options: vec![],
},
ColumnDef {
name: Ident::new("lat").empty_span(),
Expand All @@ -2491,13 +2492,15 @@ fn parse_create_table() {
name: None,
option: ColumnOption::Null,
}],
column_options: vec![],
},
ColumnDef {
name: Ident::new("lng").empty_span(),
data_type: DataType::Double,
collation: None,
codec: None,
options: vec![],
column_options: vec![],
},
ColumnDef {
name: Ident::new("constrained").empty_span(),
Expand Down Expand Up @@ -2526,6 +2529,7 @@ fn parse_create_table() {
option: ColumnOption::Check(verified_expr("constrained > 0")),
},
],
column_options: vec![],
},
ColumnDef {
name: Ident::new("ref").empty_span(),
Expand All @@ -2544,6 +2548,7 @@ fn parse_create_table() {
on_update: None,
},
}],
column_options: vec![],
},
ColumnDef {
name: Ident::new("ref2").empty_span(),
Expand All @@ -2559,6 +2564,7 @@ fn parse_create_table() {
on_update: Some(ReferentialAction::NoAction),
},
},],
column_options: vec![],
},
]
);
Expand Down Expand Up @@ -2676,13 +2682,15 @@ fn parse_create_table_hive_array() {
collation: None,
codec: None,
options: vec![],
column_options: vec![],
},
ColumnDef {
name: Ident::new("val").empty_span(),
data_type: DataType::Array(expected),
collation: None,
codec: None,
options: vec![],
column_options: vec![],
},
],
)
Expand Down Expand Up @@ -3046,6 +3054,7 @@ fn parse_create_external_table() {
name: None,
option: ColumnOption::NotNull,
}],
column_options: vec![],
},
ColumnDef {
name: Ident::new("lat").empty_span(),
Expand All @@ -3056,13 +3065,15 @@ fn parse_create_external_table() {
name: None,
option: ColumnOption::Null,
}],
column_options: vec![],
},
ColumnDef {
name: Ident::new("lng").empty_span(),
data_type: DataType::Double,
collation: None,
codec: None,
options: vec![],
column_options: vec![],
},
]
);
Expand Down Expand Up @@ -3120,6 +3131,7 @@ fn parse_create_or_replace_external_table() {
name: None,
option: ColumnOption::NotNull,
}],
column_options: vec![],
},]
);
assert!(constraints.is_empty());
Expand Down
20 changes: 20 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ fn parse_create_table_auto_increment() {
)]),
},
],
column_options: vec![],
}],
columns
);
Expand Down Expand Up @@ -338,6 +339,7 @@ fn parse_create_table_unique_key() {
)]),
},
],
column_options: vec![],
},
ColumnDef {
name: Ident::new("bar").empty_span(),
Expand All @@ -348,6 +350,7 @@ fn parse_create_table_unique_key() {
name: None,
option: ColumnOption::NotNull,
},],
column_options: vec![],
},
],
columns
Expand Down Expand Up @@ -412,13 +415,15 @@ fn parse_create_table_set_enum() {
collation: None,
codec: None,
options: vec![],
column_options: vec![],
},
ColumnDef {
name: Ident::new("baz").empty_span(),
data_type: DataType::Enum(vec!["a".to_string(), "b".to_string()]),
collation: None,
codec: None,
options: vec![],
column_options: vec![],
}
],
columns
Expand Down Expand Up @@ -447,6 +452,7 @@ fn parse_create_table_engine_default_charset() {
collation: None,
codec: None,
options: vec![],
column_options: vec![],
},],
columns
);
Expand Down Expand Up @@ -475,6 +481,7 @@ fn parse_create_table_collate() {
collation: None,
codec: None,
options: vec![],
column_options: vec![],
},],
columns
);
Expand Down Expand Up @@ -508,6 +515,7 @@ fn parse_create_table_comment_character_set() {
option: ColumnOption::Comment("comment".to_string())
}
],
column_options: vec![],
},],
columns
);
Expand All @@ -532,6 +540,7 @@ fn parse_quote_identifiers() {
name: None,
option: ColumnOption::Unique { is_primary: true },
}],
column_options: vec![],
}],
columns
);
Expand Down Expand Up @@ -885,34 +894,39 @@ fn parse_create_table_with_minimum_display_width() {
collation: None,
codec: None,
options: vec![],
column_options: vec![],
},
ColumnDef {
name: Ident::new("bar_smallint").empty_span(),
data_type: DataType::SmallInt(Some(5)),
collation: None,
codec: None,
options: vec![],
column_options: vec![],
},
ColumnDef {
name: Ident::new("bar_mediumint").empty_span(),
data_type: DataType::MediumInt(Some(6)),
collation: None,
codec: None,
options: vec![],
column_options: vec![],
},
ColumnDef {
name: Ident::new("bar_int").empty_span(),
data_type: DataType::Int(Some(11)),
collation: None,
codec: None,
options: vec![],
column_options: vec![],
},
ColumnDef {
name: Ident::new("bar_bigint").empty_span(),
data_type: DataType::BigInt(Some(20)),
collation: None,
codec: None,
options: vec![],
column_options: vec![],
}
],
columns
Expand All @@ -936,34 +950,39 @@ fn parse_create_table_unsigned() {
collation: None,
codec: None,
options: vec![],
column_options: vec![],
},
ColumnDef {
name: Ident::new("bar_smallint").empty_span(),
data_type: DataType::UnsignedSmallInt(Some(5)),
collation: None,
codec: None,
options: vec![],
column_options: vec![],
},
ColumnDef {
name: Ident::new("bar_mediumint").empty_span(),
data_type: DataType::UnsignedMediumInt(Some(13)),
collation: None,
codec: None,
options: vec![],
column_options: vec![],
},
ColumnDef {
name: Ident::new("bar_int").empty_span(),
data_type: DataType::UnsignedInt(Some(11)),
collation: None,
codec: None,
options: vec![],
column_options: vec![],
},
ColumnDef {
name: Ident::new("bar_bigint").empty_span(),
data_type: DataType::UnsignedBigInt(Some(20)),
collation: None,
codec: None,
options: vec![],
column_options: vec![],
},
],
columns
Expand Down Expand Up @@ -1570,6 +1589,7 @@ fn parse_table_colum_option_on_update() {
within_group: None,
})),
},],
column_options: vec![],
}],
columns
);
Expand Down
Loading