Skip to content

Commit 8fa9821

Browse files
git-hulkayman-sigma
authored andcommitted
Enable PARTITION BY feature for PostgreSQL while parsing the create table statement (apache#1338)
1 parent b89e5ac commit 8fa9821

File tree

3 files changed

+77
-30
lines changed

3 files changed

+77
-30
lines changed

src/ast/helpers/stmt_create_table.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,9 @@ impl TryFrom<Statement> for CreateTableBuilder {
496496
}
497497
}
498498

499-
/// Helper return type when parsing configuration for a BigQuery `CREATE TABLE` statement.
499+
/// Helper return type when parsing configuration for a `CREATE TABLE` statement.
500500
#[derive(Default)]
501-
pub(crate) struct BigQueryTableConfiguration {
501+
pub(crate) struct CreateTableConfiguration {
502502
pub partition_by: Option<Box<Expr>>,
503503
pub cluster_by: Option<WrappedCollection<Vec<Ident>>>,
504504
pub options: Option<Vec<SqlOption>>,

src/parser/mod.rs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use recursion::RecursionCounter;
3131
use IsLateral::*;
3232
use IsOptional::*;
3333

34-
use crate::ast::helpers::stmt_create_table::{BigQueryTableConfiguration, CreateTableBuilder};
34+
use crate::ast::helpers::stmt_create_table::{CreateTableBuilder, CreateTableConfiguration};
3535
use crate::ast::*;
3636
use crate::dialect::*;
3737
use crate::keywords::{Keyword, ALL_KEYWORDS};
@@ -5430,11 +5430,7 @@ impl<'a> Parser<'a> {
54305430
None
54315431
};
54325432

5433-
let big_query_config = if dialect_of!(self is BigQueryDialect | GenericDialect) {
5434-
self.parse_optional_big_query_create_table_config()?
5435-
} else {
5436-
Default::default()
5437-
};
5433+
let create_table_config = self.parse_optional_create_table_config()?;
54385434

54395435
// Parse optional `AS ( query )`
54405436
let query = if self.parse_keyword(Keyword::AS) {
@@ -5519,39 +5515,46 @@ impl<'a> Parser<'a> {
55195515
.collation(collation)
55205516
.on_commit(on_commit)
55215517
.on_cluster(on_cluster)
5522-
.partition_by(big_query_config.partition_by)
5523-
.cluster_by(big_query_config.cluster_by)
5524-
.options(big_query_config.options)
5518+
.partition_by(create_table_config.partition_by)
5519+
.cluster_by(create_table_config.cluster_by)
5520+
.options(create_table_config.options)
55255521
.primary_key(primary_key)
55265522
.strict(strict)
55275523
.build())
55285524
}
55295525

5530-
/// Parse configuration like partitioning, clustering information during big-query table creation.
5531-
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#syntax_2>
5532-
fn parse_optional_big_query_create_table_config(
5526+
/// Parse configuration like partitioning, clustering information during the table creation.
5527+
///
5528+
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#syntax_2)
5529+
/// [PostgreSQL](https://www.postgresql.org/docs/current/ddl-partitioning.html)
5530+
fn parse_optional_create_table_config(
55335531
&mut self,
5534-
) -> Result<BigQueryTableConfiguration, ParserError> {
5535-
let mut partition_by = None;
5536-
if self.parse_keywords(&[Keyword::PARTITION, Keyword::BY]) {
5537-
partition_by = Some(Box::new(self.parse_expr()?));
5532+
) -> Result<CreateTableConfiguration, ParserError> {
5533+
let partition_by = if dialect_of!(self is BigQueryDialect | PostgreSqlDialect | GenericDialect)
5534+
&& self.parse_keywords(&[Keyword::PARTITION, Keyword::BY])
5535+
{
5536+
Some(Box::new(self.parse_expr()?))
5537+
} else {
5538+
None
55385539
};
55395540

55405541
let mut cluster_by = None;
5541-
if self.parse_keywords(&[Keyword::CLUSTER, Keyword::BY]) {
5542-
cluster_by = Some(WrappedCollection::NoWrapping(
5543-
self.parse_comma_separated(|p| p.parse_identifier(false))?,
5544-
));
5545-
};
5546-
55475542
let mut options = None;
5548-
if let Token::Word(word) = self.peek_token().token {
5549-
if word.keyword == Keyword::OPTIONS {
5550-
options = Some(self.parse_options(Keyword::OPTIONS)?);
5551-
}
5552-
};
5543+
if dialect_of!(self is BigQueryDialect | GenericDialect) {
5544+
if self.parse_keywords(&[Keyword::CLUSTER, Keyword::BY]) {
5545+
cluster_by = Some(WrappedCollection::NoWrapping(
5546+
self.parse_comma_separated(|p| p.parse_identifier(false))?,
5547+
));
5548+
};
5549+
5550+
if let Token::Word(word) = self.peek_token().token {
5551+
if word.keyword == Keyword::OPTIONS {
5552+
options = Some(self.parse_options(Keyword::OPTIONS)?);
5553+
}
5554+
};
5555+
}
55535556

5554-
Ok(BigQueryTableConfiguration {
5557+
Ok(CreateTableConfiguration {
55555558
partition_by,
55565559
cluster_by,
55575560
options,

tests/sqlparser_postgres.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4039,6 +4039,50 @@ fn parse_create_table_with_alias() {
40394039
}
40404040
}
40414041

4042+
#[test]
4043+
fn parse_create_table_with_partition_by() {
4044+
let sql = "CREATE TABLE t1 (a INT, b TEXT) PARTITION BY RANGE(a)";
4045+
match pg_and_generic().verified_stmt(sql) {
4046+
Statement::CreateTable(create_table) => {
4047+
assert_eq!("t1", create_table.name.to_string());
4048+
assert_eq!(
4049+
vec![
4050+
ColumnDef {
4051+
name: "a".into(),
4052+
data_type: DataType::Int(None),
4053+
collation: None,
4054+
options: vec![]
4055+
},
4056+
ColumnDef {
4057+
name: "b".into(),
4058+
data_type: DataType::Text,
4059+
collation: None,
4060+
options: vec![]
4061+
}
4062+
],
4063+
create_table.columns
4064+
);
4065+
match *create_table.partition_by.unwrap() {
4066+
Expr::Function(f) => {
4067+
assert_eq!("RANGE", f.name.to_string());
4068+
assert_eq!(
4069+
FunctionArguments::List(FunctionArgumentList {
4070+
duplicate_treatment: None,
4071+
clauses: vec![],
4072+
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
4073+
Expr::Identifier(Ident::new("a"))
4074+
))],
4075+
}),
4076+
f.args
4077+
);
4078+
}
4079+
_ => unreachable!(),
4080+
}
4081+
}
4082+
_ => unreachable!(),
4083+
}
4084+
}
4085+
40424086
#[test]
40434087
fn parse_join_constraint_unnest_alias() {
40444088
assert_eq!(

0 commit comments

Comments
 (0)