Skip to content

Commit

Permalink
inital commit (#736)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuval-illumex committed Nov 30, 2022
1 parent 77eddfc commit a422116
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/ast/mod.rs
Expand Up @@ -1149,6 +1149,7 @@ pub enum Statement {
columns: Vec<Ident>,
query: Box<Query>,
with_options: Vec<SqlOption>,
cluster_by: Vec<Ident>,
},
/// CREATE TABLE
CreateTable {
Expand Down Expand Up @@ -1887,6 +1888,7 @@ impl fmt::Display for Statement {
query,
materialized,
with_options,
cluster_by,
} => {
write!(
f,
Expand All @@ -1901,6 +1903,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 @@ -2339,6 +2339,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 @@ -2349,6 +2357,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 @@ -4496,13 +4496,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 @@ -4542,13 +4544,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 @@ -4565,13 +4569,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 @@ -4592,13 +4598,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 @@ -4615,13 +4623,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

0 comments on commit a422116

Please sign in to comment.