-
Notifications
You must be signed in to change notification settings - Fork 583
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
Add support for BigQuery table and view options #1061
Changes from 3 commits
b374f0e
3b2dec1
f7a1c56
a894a74
87642c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -26,6 +26,7 @@ use sqlparser_derive::{Visit, VisitMut}; | |||||
use crate::ast::value::escape_single_quote_string; | ||||||
use crate::ast::{ | ||||||
display_comma_separated, display_separated, DataType, Expr, Ident, ObjectName, SequenceOptions, | ||||||
SqlOption, | ||||||
}; | ||||||
use crate::tokenizer::Token; | ||||||
|
||||||
|
@@ -527,6 +528,29 @@ impl fmt::Display for ColumnDef { | |||||
} | ||||||
} | ||||||
|
||||||
/// Column definition for a view. | ||||||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] | ||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] | ||||||
pub struct ViewColumnDef { | ||||||
pub name: Ident, | ||||||
pub options: Option<Vec<SqlOption>>, | ||||||
} | ||||||
|
||||||
impl fmt::Display for ViewColumnDef { | ||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||||||
write!(f, "{}", self.name)?; | ||||||
if let Some(options) = self.options.as_ref() { | ||||||
write!( | ||||||
f, | ||||||
" OPTIONS({})", | ||||||
display_comma_separated(options.as_slice()) | ||||||
)?; | ||||||
} | ||||||
Ok(()) | ||||||
} | ||||||
} | ||||||
|
||||||
/// An optionally-named `ColumnOption`: `[ CONSTRAINT <name> ] <column-option>`. | ||||||
/// | ||||||
/// Note that implementations are substantially more permissive than the ANSI | ||||||
|
@@ -601,6 +625,14 @@ pub enum ColumnOption { | |||||
generation_expr: Option<Expr>, | ||||||
generation_expr_mode: Option<GeneratedExpressionMode>, | ||||||
}, | ||||||
/// BigQuery specific: Explicit column options in a view [1] or table [2] | ||||||
/// Syntax | ||||||
/// ```sql | ||||||
/// 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 | ||||||
SqlOptions(Vec<SqlOption>), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given the SQL is
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||||||
} | ||||||
|
||||||
impl fmt::Display for ColumnOption { | ||||||
|
@@ -674,6 +706,9 @@ impl fmt::Display for ColumnOption { | |||||
Ok(()) | ||||||
} | ||||||
} | ||||||
SqlOptions(options) => { | ||||||
write!(f, "OPTIONS({})", display_comma_separated(options)) | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ pub use self::ddl::{ | |
AlterColumnOperation, AlterIndexOperation, AlterTableOperation, ColumnDef, ColumnOption, | ||
ColumnOptionDef, GeneratedAs, GeneratedExpressionMode, IndexType, KeyOrIndexDisplay, Partition, | ||
ProcedureParam, ReferentialAction, TableConstraint, UserDefinedTypeCompositeAttributeDef, | ||
UserDefinedTypeRepresentation, | ||
UserDefinedTypeRepresentation, ViewColumnDef, | ||
}; | ||
pub use self::operator::{BinaryOperator, UnaryOperator}; | ||
pub use self::query::{ | ||
|
@@ -1364,6 +1364,38 @@ pub enum Password { | |
NullPassword, | ||
} | ||
|
||
/// Sql options of a `CREATE TABLE` statement. | ||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] | ||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] | ||
pub enum CreateTableOptions { | ||
None, | ||
/// Options specified using the `WITH` keyword. | ||
/// e.g. `WITH (description = "123")` | ||
/// | ||
/// <https://www.postgresql.org/docs/current/sql-createtable.html> | ||
With(Vec<SqlOption>), | ||
/// Options specified using the `OPTIONS` keyword. | ||
/// e.g. `OPTIONS(description = "123")` | ||
/// | ||
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#table_option_list> | ||
Options(Vec<SqlOption>), | ||
} | ||
|
||
impl fmt::Display for CreateTableOptions { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
CreateTableOptions::With(with_options) => { | ||
write!(f, "WITH ({})", display_comma_separated(with_options)) | ||
} | ||
CreateTableOptions::Options(options) => { | ||
write!(f, "OPTIONS({})", display_comma_separated(options)) | ||
} | ||
CreateTableOptions::None => Ok(()), | ||
} | ||
} | ||
} | ||
|
||
/// A top-level statement (SELECT, INSERT, CREATE, etc.) | ||
#[allow(clippy::large_enum_variant)] | ||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] | ||
|
@@ -1512,9 +1544,9 @@ pub enum Statement { | |
materialized: bool, | ||
/// View name | ||
name: ObjectName, | ||
columns: Vec<Ident>, | ||
columns: Vec<ViewColumnDef>, | ||
query: Box<Query>, | ||
with_options: Vec<SqlOption>, | ||
options: CreateTableOptions, | ||
cluster_by: Vec<Ident>, | ||
/// if true, has RedShift [`WITH NO SCHEMA BINDING`] clause <https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_VIEW.html> | ||
with_no_schema_binding: bool, | ||
|
@@ -1560,6 +1592,8 @@ pub enum Statement { | |
/// than empty (represented as ()), the latter meaning "no sorting". | ||
/// <https://clickhouse.com/docs/en/sql-reference/statements/create/table/> | ||
order_by: Option<Vec<Ident>>, | ||
/// Database specific configuration during table creation. | ||
table_config: Option<Box<CreateTableConfiguration>>, | ||
/// SQLite "STRICT" clause. | ||
/// if the "STRICT" table-option keyword is added to the end, after the closing ")", | ||
/// then strict typing rules apply to that table. | ||
|
@@ -2499,7 +2533,7 @@ impl fmt::Display for Statement { | |
columns, | ||
query, | ||
materialized, | ||
with_options, | ||
options, | ||
cluster_by, | ||
with_no_schema_binding, | ||
if_not_exists, | ||
|
@@ -2514,15 +2548,18 @@ impl fmt::Display for Statement { | |
temporary = if *temporary { "TEMPORARY " } else { "" }, | ||
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" } | ||
)?; | ||
if !with_options.is_empty() { | ||
write!(f, " WITH ({})", display_comma_separated(with_options))?; | ||
if matches!(options, CreateTableOptions::With(_)) { | ||
write!(f, " {options}")?; | ||
} | ||
if !columns.is_empty() { | ||
write!(f, " ({})", display_comma_separated(columns))?; | ||
} | ||
if !cluster_by.is_empty() { | ||
write!(f, " CLUSTER BY ({})", display_comma_separated(cluster_by))?; | ||
} | ||
if matches!(options, CreateTableOptions::Options(_)) { | ||
write!(f, " {options}")?; | ||
} | ||
write!(f, " AS {query}")?; | ||
if *with_no_schema_binding { | ||
write!(f, " WITH NO SCHEMA BINDING")?; | ||
|
@@ -2557,6 +2594,7 @@ impl fmt::Display for Statement { | |
on_commit, | ||
on_cluster, | ||
order_by, | ||
table_config, | ||
strict, | ||
} => { | ||
// We want to allow the following options | ||
|
@@ -2713,6 +2751,27 @@ impl fmt::Display for Statement { | |
if let Some(order_by) = order_by { | ||
write!(f, " ORDER BY ({})", display_comma_separated(order_by))?; | ||
} | ||
if let Some(CreateTableConfiguration::BigQuery(big_query_config)) = | ||
table_config.as_ref().map(|c| c.as_ref()) | ||
{ | ||
if let Some(partition_by) = big_query_config.partition_by.as_ref() { | ||
write!(f, " PARTITION BY {partition_by}")?; | ||
} | ||
if let Some(cluster_by) = big_query_config.cluster_by.as_ref() { | ||
write!( | ||
f, | ||
" CLUSTER BY {}", | ||
display_comma_separated(cluster_by.as_slice()) | ||
)?; | ||
} | ||
if let Some(options) = big_query_config.options.as_ref() { | ||
write!( | ||
f, | ||
" OPTIONS({})", | ||
display_comma_separated(options.as_slice()) | ||
)?; | ||
} | ||
} | ||
if let Some(query) = query { | ||
write!(f, " AS {query}")?; | ||
} | ||
|
@@ -4220,12 +4279,39 @@ pub struct HiveFormat { | |
pub location: Option<String>, | ||
} | ||
|
||
/// Represents BigQuery specific configuration like partitioning, clustering | ||
/// information during table creation. | ||
/// | ||
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_statement> | ||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] | ||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] | ||
pub struct BigQueryCreateTableConfiguration { | ||
/// A partition expression for the table. | ||
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#partition_expression> | ||
pub partition_by: Option<Expr>, | ||
/// Table clustering column list. | ||
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#table_option_list> | ||
pub cluster_by: Option<Vec<Ident>>, | ||
/// Table options list. | ||
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#table_option_list> | ||
pub options: Option<Vec<SqlOption>>, | ||
} | ||
|
||
/// Represents database specific configuration during table creation. | ||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] | ||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] | ||
pub enum CreateTableConfiguration { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if you considered inlining these into Like
I think that might be more consistent with other dialect specific fields as well as Having an extra struct here also may be confusing as it doesn't seem to mirror the structure of the SQL -- for example, it seems to imply to me that there is some sort of "CONFIG" keyword / clause that appears in the SQL when https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_statement seems to imply that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes I figured to have them inline initially, agree that makes it less confusing. The only issue was with serializing back to SQL text - since the fields in bigquery's case have a defined order (e.g There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think an inlined version would be better to maintain consistency. If someone needs a different order when serializing for another dialect we can add that as a follow on PR perhaps. If for some reason we need to keep the separate struct I think that would be acceptable if there were some doc comments explaining the rational (as that is not easy to understand from the code). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That sounds good! Will make updates to this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inlined! |
||
BigQuery(BigQueryCreateTableConfiguration), | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] | ||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] | ||
pub struct SqlOption { | ||
pub name: Ident, | ||
pub value: Value, | ||
pub value: Expr, | ||
} | ||
|
||
impl fmt::Display for SqlOption { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add some examples to this doc comment showing what is allowed (and not)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the description