Skip to content

Commit

Permalink
inital commit
Browse files Browse the repository at this point in the history
  • Loading branch information
yuval-illumex committed Nov 29, 2022
1 parent bae6822 commit bcc9e29
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
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,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 @@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit bcc9e29

Please sign in to comment.