Skip to content

Add support for BigQuery table and view options #1061

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 5 commits into from
Jan 23, 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
48 changes: 48 additions & 0 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -634,6 +635,42 @@ impl fmt::Display for ColumnDef {
}
}

/// Column definition specified in a `CREATE VIEW` statement.
///
/// Syntax
/// ```markdown
/// <name> [OPTIONS(option, ...)]
///
/// option: <name> = <value>
/// ```
///
/// Examples:
/// ```sql
/// name
/// age OPTIONS(description = "age column", tag = "prod")
/// ```
#[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
Expand Down Expand Up @@ -710,6 +747,14 @@ pub enum ColumnOption {
/// false if 'GENERATED ALWAYS' is skipped (option starts with AS)
generated_keyword: bool,
},
/// 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
Options(Vec<SqlOption>),
}

impl fmt::Display for ColumnOption {
Expand Down Expand Up @@ -788,6 +833,9 @@ impl fmt::Display for ColumnOption {
Ok(())
}
}
Options(options) => {
write!(f, "OPTIONS({})", display_comma_separated(options))
}
}
}
}
Expand Down
42 changes: 40 additions & 2 deletions src/ast/helpers/stmt_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use serde::{Deserialize, Serialize};
use sqlparser_derive::{Visit, VisitMut};

use crate::ast::{
ColumnDef, FileFormat, HiveDistributionStyle, HiveFormat, Ident, ObjectName, OnCommit, Query,
SqlOption, Statement, TableConstraint,
ColumnDef, Expr, FileFormat, HiveDistributionStyle, HiveFormat, Ident, ObjectName, OnCommit,
Query, SqlOption, Statement, TableConstraint,
};
use crate::parser::ParserError;

Expand Down Expand Up @@ -72,6 +72,9 @@ pub struct CreateTableBuilder {
pub on_commit: Option<OnCommit>,
pub on_cluster: Option<String>,
pub order_by: Option<Vec<Ident>>,
pub partition_by: Option<Box<Expr>>,
pub cluster_by: Option<Vec<Ident>>,
pub options: Option<Vec<SqlOption>>,
pub strict: bool,
}

Expand Down Expand Up @@ -105,6 +108,9 @@ impl CreateTableBuilder {
on_commit: None,
on_cluster: None,
order_by: None,
partition_by: None,
cluster_by: None,
options: None,
strict: false,
}
}
Expand Down Expand Up @@ -236,6 +242,21 @@ impl CreateTableBuilder {
self
}

pub fn partition_by(mut self, partition_by: Option<Box<Expr>>) -> Self {
self.partition_by = partition_by;
self
}

pub fn cluster_by(mut self, cluster_by: Option<Vec<Ident>>) -> Self {
self.cluster_by = cluster_by;
self
}

pub fn options(mut self, options: Option<Vec<SqlOption>>) -> Self {
self.options = options;
self
}

pub fn strict(mut self, strict: bool) -> Self {
self.strict = strict;
self
Expand Down Expand Up @@ -270,6 +291,9 @@ impl CreateTableBuilder {
on_commit: self.on_commit,
on_cluster: self.on_cluster,
order_by: self.order_by,
partition_by: self.partition_by,
cluster_by: self.cluster_by,
options: self.options,
strict: self.strict,
}
}
Expand Down Expand Up @@ -310,6 +334,9 @@ impl TryFrom<Statement> for CreateTableBuilder {
on_commit,
on_cluster,
order_by,
partition_by,
cluster_by,
options,
strict,
} => Ok(Self {
or_replace,
Expand Down Expand Up @@ -339,6 +366,9 @@ impl TryFrom<Statement> for CreateTableBuilder {
on_commit,
on_cluster,
order_by,
partition_by,
cluster_by,
options,
strict,
}),
_ => Err(ParserError::ParserError(format!(
Expand All @@ -348,6 +378,14 @@ impl TryFrom<Statement> for CreateTableBuilder {
}
}

/// Helper return type when parsing configuration for a BigQuery `CREATE TABLE` statement.
#[derive(Default)]
pub(crate) struct BigQueryTableConfiguration {
pub partition_by: Option<Box<Expr>>,
pub cluster_by: Option<Vec<Ident>>,
pub options: Option<Vec<SqlOption>>,
}

#[cfg(test)]
mod tests {
use crate::ast::helpers::stmt_create_table::CreateTableBuilder;
Expand Down
78 changes: 71 additions & 7 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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::{
Expand Down Expand Up @@ -1367,6 +1367,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)]
Expand Down Expand Up @@ -1550,9 +1582,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,
Expand Down Expand Up @@ -1600,6 +1632,15 @@ 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>>,
/// BigQuery: A partition expression for the table.
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#partition_expression>
partition_by: Option<Box<Expr>>,
/// BigQuery: Table clustering column list.
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#table_option_list>
cluster_by: Option<Vec<Ident>>,
/// BigQuery: Table options list.
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#table_option_list>
options: Option<Vec<SqlOption>>,
/// 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.
Expand Down Expand Up @@ -2731,7 +2772,7 @@ impl fmt::Display for Statement {
columns,
query,
materialized,
with_options,
options,
cluster_by,
with_no_schema_binding,
if_not_exists,
Expand All @@ -2746,15 +2787,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")?;
Expand Down Expand Up @@ -2789,6 +2833,9 @@ impl fmt::Display for Statement {
on_commit,
on_cluster,
order_by,
partition_by,
cluster_by,
options,
strict,
} => {
// We want to allow the following options
Expand Down Expand Up @@ -2945,6 +2992,23 @@ impl fmt::Display for Statement {
if let Some(order_by) = order_by {
write!(f, " ORDER BY ({})", display_comma_separated(order_by))?;
}
if let Some(partition_by) = partition_by.as_ref() {
write!(f, " PARTITION BY {partition_by}")?;
}
if let Some(cluster_by) = cluster_by.as_ref() {
write!(
f,
" CLUSTER BY {}",
display_comma_separated(cluster_by.as_slice())
)?;
}
if let Some(options) = options.as_ref() {
write!(
f,
" OPTIONS({})",
display_comma_separated(options.as_slice())
)?;
}
if let Some(query) = query {
write!(f, " AS {query}")?;
}
Expand Down Expand Up @@ -4496,7 +4560,7 @@ pub struct HiveFormat {
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct SqlOption {
pub name: Ident,
pub value: Value,
pub value: Expr,
}

impl fmt::Display for SqlOption {
Expand Down
Loading