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

Snowflake - Add support in CLUSTER BY when creating Materialised View #736

Merged
merged 1 commit into from Nov 30, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/ast/mod.rs
Expand Up @@ -1114,6 +1114,7 @@ pub enum Statement {
columns: Vec<Ident>,
query: Box<Query>,
with_options: Vec<SqlOption>,
cluster_by: Vec<Ident>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

https://docs.snowflake.com/en/sql-reference/sql/create-materialized-view.html says

  [ CLUSTER BY ( <expr1> [, <expr2> ... ] ) ]

But this PR has cluster by as only columns (not expressions).

I am fine with merging this as is but I wonder if you want to support the more general syntax?

},
/// CREATE TABLE
CreateTable {
Expand Down Expand Up @@ -1851,6 +1852,7 @@ impl fmt::Display for Statement {
query,
materialized,
with_options,
cluster_by,
} => {
write!(
f,
Expand All @@ -1865,6 +1867,9 @@ impl fmt::Display for Statement {
if !columns.is_empty() {
write!(f, " ({})", display_comma_separated(columns))?;
}
if !cluster_by.is_empty() {
write!(f, " CLUSTER BY ({})", display_comma_separated(cluster_by))?;
}
write!(f, " AS {}", query)
}
Statement::CreateTable {
Expand Down
9 changes: 9 additions & 0 deletions src/parser.rs
Expand Up @@ -2285,6 +2285,14 @@ impl<'a> Parser<'a> {
let name = self.parse_object_name()?;
let columns = self.parse_parenthesized_column_list(Optional)?;
let with_options = self.parse_options(Keyword::WITH)?;

let cluster_by = if self.parse_keyword(Keyword::CLUSTER) {
self.expect_keyword(Keyword::BY)?;
self.parse_parenthesized_column_list(Optional)?
} else {
vec![]
};

self.expect_keyword(Keyword::AS)?;
let query = Box::new(self.parse_query()?);
// Optional `WITH [ CASCADED | LOCAL ] CHECK OPTION` is widely supported here.
Expand All @@ -2295,6 +2303,7 @@ impl<'a> Parser<'a> {
materialized,
or_replace,
with_options,
cluster_by,
})
}

Expand Down
41 changes: 38 additions & 3 deletions tests/sqlparser_common.rs
Expand Up @@ -4397,13 +4397,15 @@ fn parse_create_view() {
or_replace,
materialized,
with_options,
cluster_by,
} => {
assert_eq!("myschema.myview", name.to_string());
assert_eq!(Vec::<Ident>::new(), columns);
assert_eq!("SELECT foo FROM bar", query.to_string());
assert!(!materialized);
assert!(!or_replace);
assert_eq!(with_options, vec![]);
assert_eq!(cluster_by, vec![]);
}
_ => unreachable!(),
}
Expand Down Expand Up @@ -4443,13 +4445,15 @@ fn parse_create_view_with_columns() {
with_options,
query,
materialized,
cluster_by,
} => {
assert_eq!("v", name.to_string());
assert_eq!(columns, vec![Ident::new("has"), Ident::new("cols")]);
assert_eq!(with_options, vec![]);
assert_eq!("SELECT 1, 2", query.to_string());
assert!(!materialized);
assert!(!or_replace)
assert!(!or_replace);
assert_eq!(cluster_by, vec![]);
}
_ => unreachable!(),
}
Expand All @@ -4466,13 +4470,15 @@ fn parse_create_or_replace_view() {
with_options,
query,
materialized,
cluster_by,
} => {
assert_eq!("v", name.to_string());
assert_eq!(columns, vec![]);
assert_eq!(with_options, vec![]);
assert_eq!("SELECT 1", query.to_string());
assert!(!materialized);
assert!(or_replace)
assert!(or_replace);
assert_eq!(cluster_by, vec![]);
}
_ => unreachable!(),
}
Expand All @@ -4493,13 +4499,15 @@ fn parse_create_or_replace_materialized_view() {
with_options,
query,
materialized,
cluster_by,
} => {
assert_eq!("v", name.to_string());
assert_eq!(columns, vec![]);
assert_eq!(with_options, vec![]);
assert_eq!("SELECT 1", query.to_string());
assert!(materialized);
assert!(or_replace)
assert!(or_replace);
assert_eq!(cluster_by, vec![]);
}
_ => unreachable!(),
}
Expand All @@ -4516,13 +4524,40 @@ fn parse_create_materialized_view() {
query,
materialized,
with_options,
cluster_by,
} => {
assert_eq!("myschema.myview", name.to_string());
assert_eq!(Vec::<Ident>::new(), columns);
assert_eq!("SELECT foo FROM bar", query.to_string());
assert!(materialized);
assert_eq!(with_options, vec![]);
assert!(!or_replace);
assert_eq!(cluster_by, vec![]);
}
_ => unreachable!(),
}
}

#[test]
fn parse_create_materialized_view_with_cluster_by() {
let sql = "CREATE MATERIALIZED VIEW myschema.myview CLUSTER BY (foo) AS SELECT foo FROM bar";
match verified_stmt(sql) {
Statement::CreateView {
name,
or_replace,
columns,
query,
materialized,
with_options,
cluster_by,
} => {
assert_eq!("myschema.myview", name.to_string());
assert_eq!(Vec::<Ident>::new(), columns);
assert_eq!("SELECT foo FROM bar", query.to_string());
assert!(materialized);
assert_eq!(with_options, vec![]);
assert!(!or_replace);
assert_eq!(cluster_by, vec![Ident::new("foo")]);
}
_ => unreachable!(),
}
Expand Down