Skip to content

Commit

Permalink
Add support for BigQuery table and view options
Browse files Browse the repository at this point in the history
Extends the parser with BigQuery support for
- Table/View level options
- Column level options
- Table creation configurations `CLUSTER BY`, `PARTITION BY`
  • Loading branch information
iffyio committed Dec 3, 2023
1 parent 8d97330 commit b374f0e
Show file tree
Hide file tree
Showing 8 changed files with 462 additions and 53 deletions.
35 changes: 35 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 @@ -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
Expand Down Expand Up @@ -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>),
}

impl fmt::Display for ColumnOption {
Expand Down Expand Up @@ -674,6 +706,9 @@ impl fmt::Display for ColumnOption {
Ok(())
}
}
SqlOptions(options) => {
write!(f, "OPTIONS ({})", display_comma_separated(options))
}
}
}
}
Expand Down
17 changes: 15 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,
BigQueryCreateTableConfiguration, ColumnDef, FileFormat, HiveDistributionStyle, HiveFormat,
Ident, ObjectName, OnCommit, Query, SqlOption, Statement, TableConstraint,
};
use crate::parser::ParserError;

Expand Down Expand Up @@ -72,6 +72,7 @@ pub struct CreateTableBuilder {
pub on_commit: Option<OnCommit>,
pub on_cluster: Option<String>,
pub order_by: Option<Vec<Ident>>,
pub big_query_config: Option<Box<BigQueryCreateTableConfiguration>>,
pub strict: bool,
}

Expand Down Expand Up @@ -105,6 +106,7 @@ impl CreateTableBuilder {
on_commit: None,
on_cluster: None,
order_by: None,
big_query_config: None,
strict: false,
}
}
Expand Down Expand Up @@ -236,6 +238,14 @@ impl CreateTableBuilder {
self
}

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

pub fn strict(mut self, strict: bool) -> Self {
self.strict = strict;
self
Expand Down Expand Up @@ -270,6 +280,7 @@ impl CreateTableBuilder {
on_commit: self.on_commit,
on_cluster: self.on_cluster,
order_by: self.order_by,
big_query_config: self.big_query_config,
strict: self.strict,
}
}
Expand Down Expand Up @@ -310,6 +321,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
on_commit,
on_cluster,
order_by,
big_query_config,
strict,
} => Ok(Self {
or_replace,
Expand Down Expand Up @@ -339,6 +351,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
on_commit,
on_cluster,
order_by,
big_query_config,
strict,
}),
_ => Err(ParserError::ParserError(format!(
Expand Down
91 changes: 84 additions & 7 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1560,6 +1592,9 @@ 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 specific configuration during table creation.
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_statement>
big_query_config: Option<Box<BigQueryCreateTableConfiguration>>,
/// 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 @@ -2499,7 +2534,7 @@ impl fmt::Display for Statement {
columns,
query,
materialized,
with_options,
options,
cluster_by,
with_no_schema_binding,
if_not_exists,
Expand All @@ -2514,15 +2549,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 @@ -2557,6 +2595,7 @@ impl fmt::Display for Statement {
on_commit,
on_cluster,
order_by,
big_query_config,
strict,
} => {
// We want to allow the following options
Expand Down Expand Up @@ -2713,6 +2752,25 @@ impl fmt::Display for Statement {
if let Some(order_by) = order_by {
write!(f, " ORDER BY ({})", display_comma_separated(order_by))?;
}
if let Some(bigquery_config) = big_query_config {
if let Some(partition_by) = bigquery_config.partition_by.as_ref() {
write!(f, " PARTITION BY {partition_by}")?;
}
if let Some(cluster_by) = bigquery_config.cluster_by.as_ref() {
write!(
f,
" CLUSTER BY {}",
display_comma_separated(cluster_by.as_slice())
)?;
}
if let Some(options) = bigquery_config.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 @@ -4220,12 +4278,31 @@ 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>>,
}

#[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 {
Expand Down
Loading

0 comments on commit b374f0e

Please sign in to comment.