Skip to content
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

BigQuery partition by in create table. #31

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 10 additions & 0 deletions src/ast/helpers/stmt_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub struct CreateTableBuilder {
pub default_charset: Option<String>,
pub collation: Option<String>,
pub on_commit: Option<OnCommit>,
pub partitioned_by: Option<Expr>,
pub on_cluster: Option<String>,
pub primary_key: Option<Vec<WithSpan<Ident>>>,
pub order_by: Option<Vec<WithSpan<Ident>>>,
Expand Down Expand Up @@ -112,6 +113,7 @@ impl CreateTableBuilder {
on_cluster: None,
primary_key: None,
order_by: None,
partitioned_by: None,
cluster_by: None,
strict: false,
table_ttl: None,
Expand Down Expand Up @@ -236,6 +238,11 @@ impl CreateTableBuilder {
self
}

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

pub fn on_cluster(mut self, on_cluster: Option<String>) -> Self {
self.on_cluster = on_cluster;
self
Expand Down Expand Up @@ -301,6 +308,7 @@ impl CreateTableBuilder {
on_cluster: self.on_cluster,
primary_key: self.primary_key,
order_by: self.order_by,
partitioned_by: self.partitioned_by,
cluster_by: self.cluster_by,
strict: self.strict,
table_ttl: self.table_ttl,
Expand Down Expand Up @@ -342,6 +350,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
default_charset,
collation,
on_commit,
partitioned_by,
on_cluster,
primary_key,
order_by,
Expand Down Expand Up @@ -375,6 +384,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
default_charset,
collation,
on_commit,
partitioned_by,
on_cluster,
primary_key,
order_by,
Expand Down
5 changes: 5 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,7 @@ pub enum Statement {
default_charset: Option<String>,
collation: Option<String>,
on_commit: Option<OnCommit>,
partitioned_by: Option<Expr>,
/// ClickHouse "ON CLUSTER" clause:
/// <https://clickhouse.com/docs/en/sql-reference/distributed-ddl/>
on_cluster: Option<String>,
Expand Down Expand Up @@ -2620,6 +2621,7 @@ impl fmt::Display for Statement {
auto_increment_offset,
collation,
on_commit,
partitioned_by,
on_cluster,
primary_key,
order_by,
Expand Down Expand Up @@ -2785,6 +2787,9 @@ impl fmt::Display for Statement {
if let Some(order_by) = order_by {
write!(f, " ORDER BY ({})", display_comma_separated(order_by))?;
}
if let Some(expr) = partitioned_by {
write!(f, " PARTITION BY {expr}")?;
}
if let Some(cluster_by) = cluster_by {
write!(f, " CLUSTER BY ({})", display_comma_separated(cluster_by))?;
}
Expand Down
10 changes: 9 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use IsOptional::*;
use crate::ast::helpers::stmt_create_table::CreateTableBuilder;
use crate::ast::*;
use crate::dialect::*;
use crate::keywords::{self, Keyword, ALL_KEYWORDS};
use crate::keywords::{self, Keyword};
use crate::tokenizer::*;

mod alter;
Expand Down Expand Up @@ -4083,6 +4083,13 @@ impl<'a> Parser<'a> {
None
};

// In BigQuery PARITION BY can be also defined after columns
let partitioned_by = if self.parse_keywords(&[Keyword::PARTITION, Keyword::BY]) {
Some(self.parse_expr()?)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went through code base and parsing PARTITION BY or PARTITION in parts like window specification are always parsing just expr. Keeping the same parsing.

Also not guarding it by dialect as whole parse_create_table is dialect independent.

} else {
None
};

// In Snowflake CLUSTER BY can be also defined after columns
if self.parse_keywords(&[Keyword::CLUSTER, Keyword::BY]) {
self.expect_token(&Token::LParen)?;
Expand Down Expand Up @@ -4181,6 +4188,7 @@ impl<'a> Parser<'a> {
.default_charset(default_charset)
.collation(collation)
.on_commit(on_commit)
.partitioned_by(partitioned_by)
.on_cluster(on_cluster)
.strict(strict)
.table_ttl(table_ttl)
Expand Down
7 changes: 7 additions & 0 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1262,3 +1262,10 @@ fn test_regexp_string_double_quote() {
bigquery_unescaped().verified_stmt(r#"SELECT "I\\\"m fine""#);
bigquery_unescaped().verified_stmt(r#"SELECT "[\"\\[\\]]""#);
}

#[test]
fn test_create_table_with_partition_by() {
bigquery().verified_stmt(
"CREATE TABLE mytable (id INT64, timestamp TIMESTAMP) PARTITION BY DATE(timestamp)",
);
}
Loading