Skip to content

Commit ae675e8

Browse files
authored
Merge pull request #13 from Embucket/issues/1036_cluster_by_expr
Add support for cluster by expressions
2 parents 5de341b + f0ece38 commit ae675e8

File tree

8 files changed

+50
-20
lines changed

8 files changed

+50
-20
lines changed

src/ast/dml.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ pub struct CreateTable {
156156
pub partition_by: Option<Box<Expr>>,
157157
/// BigQuery: Table clustering column list.
158158
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#table_option_list>
159-
pub cluster_by: Option<WrappedCollection<Vec<Ident>>>,
159+
/// Snowflake: Table clustering list which contains base column, expressions on base columns.
160+
/// <https://docs.snowflake.com/en/user-guide/tables-clustering-keys#defining-a-clustering-key-for-a-table>
161+
pub cluster_by: Option<WrappedCollection<Vec<Expr>>>,
160162
/// Hive: Table clustering column list.
161163
/// <https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-CreateTable>
162164
pub clustered_by: Option<ClusteredBy>,

src/ast/helpers/stmt_create_table.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub struct CreateTableBuilder {
9494
pub primary_key: Option<Box<Expr>>,
9595
pub order_by: Option<OneOrManyWithParens<Expr>>,
9696
pub partition_by: Option<Box<Expr>>,
97-
pub cluster_by: Option<WrappedCollection<Vec<Ident>>>,
97+
pub cluster_by: Option<WrappedCollection<Vec<Expr>>>,
9898
pub clustered_by: Option<ClusteredBy>,
9999
pub options: Option<Vec<SqlOption>>,
100100
pub strict: bool,
@@ -316,7 +316,7 @@ impl CreateTableBuilder {
316316
self
317317
}
318318

319-
pub fn cluster_by(mut self, cluster_by: Option<WrappedCollection<Vec<Ident>>>) -> Self {
319+
pub fn cluster_by(mut self, cluster_by: Option<WrappedCollection<Vec<Expr>>>) -> Self {
320320
self.cluster_by = cluster_by;
321321
self
322322
}
@@ -589,7 +589,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
589589
#[derive(Default)]
590590
pub(crate) struct CreateTableConfiguration {
591591
pub partition_by: Option<Box<Expr>>,
592-
pub cluster_by: Option<WrappedCollection<Vec<Ident>>>,
592+
pub cluster_by: Option<WrappedCollection<Vec<Expr>>>,
593593
pub options: Option<Vec<SqlOption>>,
594594
}
595595

src/dialect/snowflake.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ pub fn parse_create_table(
443443
parser.expect_keyword_is(Keyword::BY)?;
444444
parser.expect_token(&Token::LParen)?;
445445
let cluster_by = Some(WrappedCollection::Parentheses(
446-
parser.parse_comma_separated(|p| p.parse_identifier())?,
446+
parser.parse_comma_separated(|p| p.parse_expr())?,
447447
));
448448
parser.expect_token(&Token::RParen)?;
449449

src/parser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6904,7 +6904,7 @@ impl<'a> Parser<'a> {
69046904
if dialect_of!(self is BigQueryDialect | GenericDialect) {
69056905
if self.parse_keywords(&[Keyword::CLUSTER, Keyword::BY]) {
69066906
cluster_by = Some(WrappedCollection::NoWrapping(
6907-
self.parse_comma_separated(|p| p.parse_identifier())?,
6907+
self.parse_comma_separated(|p| p.parse_expr())?,
69086908
));
69096909
};
69106910

tests/sqlparser_bigquery.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,8 @@ fn parse_create_table_with_options() {
536536
(
537537
Some(Box::new(Expr::Identifier(Ident::new("_PARTITIONDATE")))),
538538
Some(WrappedCollection::NoWrapping(vec![
539-
Ident::new("userid"),
540-
Ident::new("age"),
539+
Expr::Identifier(Ident::new("userid")),
540+
Expr::Identifier(Ident::new("age")),
541541
])),
542542
Some(vec![
543543
SqlOption::KeyValue {

tests/sqlparser_mysql.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,11 +2158,13 @@ fn parse_alter_table_add_column() {
21582158
if_exists,
21592159
only,
21602160
operations,
2161+
iceberg,
21612162
location: _,
21622163
on_cluster: _,
21632164
} => {
21642165
assert_eq!(name.to_string(), "tab");
21652166
assert!(!if_exists);
2167+
assert!(!iceberg);
21662168
assert!(!only);
21672169
assert_eq!(
21682170
operations,
@@ -2187,8 +2189,7 @@ fn parse_alter_table_add_column() {
21872189
if_exists,
21882190
only,
21892191
operations,
2190-
location: _,
2191-
on_cluster: _,
2192+
..
21922193
} => {
21932194
assert_eq!(name.to_string(), "tab");
21942195
assert!(!if_exists);
@@ -2225,8 +2226,7 @@ fn parse_alter_table_add_columns() {
22252226
if_exists,
22262227
only,
22272228
operations,
2228-
location: _,
2229-
on_cluster: _,
2229+
..
22302230
} => {
22312231
assert_eq!(name.to_string(), "tab");
22322232
assert!(!if_exists);

tests/sqlparser_postgres.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -828,8 +828,7 @@ fn parse_alter_table_add_columns() {
828828
if_exists,
829829
only,
830830
operations,
831-
location: _,
832-
on_cluster: _,
831+
..
833832
} => {
834833
assert_eq!(name.to_string(), "tab");
835834
assert!(if_exists);
@@ -909,8 +908,7 @@ fn parse_alter_table_owner_to() {
909908
if_exists: _,
910909
only: _,
911910
operations,
912-
location: _,
913-
on_cluster: _,
911+
..
914912
} => {
915913
assert_eq!(name.to_string(), "tab");
916914
assert_eq!(

tests/sqlparser_snowflake.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,38 @@ fn test_snowflake_create_table_cluster_by() {
457457
assert_eq!("my_table", name.to_string());
458458
assert_eq!(
459459
Some(WrappedCollection::Parentheses(vec![
460-
Ident::new("a"),
461-
Ident::new("b"),
460+
Expr::Identifier(Ident::new("a")),
461+
Expr::Identifier(Ident::new("b")),
462+
])),
463+
cluster_by
464+
)
465+
}
466+
_ => unreachable!(),
467+
}
468+
match snowflake().verified_stmt("CREATE TABLE my_table (ts DATE, a TEXT) CLUSTER BY (to_date(ts), a)") {
469+
Statement::CreateTable(CreateTable {
470+
name, cluster_by, ..
471+
}) => {
472+
assert_eq!("my_table", name.to_string());
473+
assert_eq!(
474+
Some(WrappedCollection::Parentheses(vec![
475+
Expr::Function(Function {
476+
name: ObjectName::from(vec![Ident::new("to_date")]),
477+
uses_odbc_syntax: false,
478+
parameters: FunctionArguments::None,
479+
args: FunctionArguments::List(FunctionArgumentList {
480+
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
481+
Expr::Identifier(Ident::new("ts"))
482+
))],
483+
duplicate_treatment: None,
484+
clauses: vec![],
485+
}),
486+
filter: None,
487+
null_treatment: None,
488+
over: None,
489+
within_group: vec![],
490+
}),
491+
Expr::Identifier(Ident::new("a")),
462492
])),
463493
cluster_by
464494
)
@@ -869,8 +899,8 @@ fn test_snowflake_create_iceberg_table_all_options() {
869899
assert_eq!("my_table", name.to_string());
870900
assert_eq!(
871901
Some(WrappedCollection::Parentheses(vec![
872-
Ident::new("a"),
873-
Ident::new("b"),
902+
Expr::Identifier(Ident::new("a")),
903+
Expr::Identifier(Ident::new("b")),
874904
])),
875905
cluster_by
876906
);

0 commit comments

Comments
 (0)