Skip to content

Support CREATE MATERIALIZED VIEW #1

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

Merged
merged 1 commit into from
Mar 8, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/dialect/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ keyword!(
LOCATION,
LOWER,
MATCH,
MATERIALIZED,
MAX,
MEMBER,
MERGE,
Expand Down Expand Up @@ -539,6 +540,7 @@ pub const ALL_KEYWORDS: &'static [&'static str] = &[
LOCATION,
LOWER,
MATCH,
MATERIALIZED,
MAX,
MEMBER,
MERGE,
Expand Down
6 changes: 4 additions & 2 deletions src/sqlast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ pub enum SQLStatement {
/// View name
name: SQLObjectName,
query: SQLQuery,
materialized: bool,
},
/// CREATE TABLE
SQLCreateTable {
Expand Down Expand Up @@ -347,8 +348,9 @@ impl ToString for SQLStatement {
}
s
}
SQLStatement::SQLCreateView { name, query } => {
format!("CREATE VIEW {} AS {}", name.to_string(), query.to_string())
SQLStatement::SQLCreateView { name, query, materialized } => {
let modifier = if *materialized { " MATERIALIZED" } else { "" };
format!("CREATE{} VIEW {} AS {}", modifier, name.to_string(), query.to_string())
}
SQLStatement::SQLCreateTable { name, columns } => format!(
"CREATE TABLE {} ({})",
Expand Down
7 changes: 5 additions & 2 deletions src/sqlparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,8 @@ impl Parser {
pub fn parse_create(&mut self) -> Result<SQLStatement, ParserError> {
if self.parse_keyword("TABLE") {
self.parse_create_table()
} else if self.parse_keyword("VIEW") {
} else if self.parse_keyword("MATERIALIZED") || self.parse_keyword("VIEW") {
self.prev_token();
self.parse_create_view()
} else {
parser_err!(format!(
Expand All @@ -628,6 +629,8 @@ impl Parser {
}

pub fn parse_create_view(&mut self) -> Result<SQLStatement, ParserError> {
let materialized = self.parse_keyword("MATERIALIZED");
self.expect_keyword("VIEW")?;
// Many dialects support `OR REPLACE` | `OR ALTER` right after `CREATE`, but we don't (yet).
// ANSI SQL and Postgres support RECURSIVE here, but we don't support it either.
let name = self.parse_object_name()?;
Expand All @@ -637,7 +640,7 @@ impl Parser {
self.expect_keyword("AS")?;
let query = self.parse_query()?;
// Optional `WITH [ CASCADED | LOCAL ] CHECK OPTION` is widely supported here.
Ok(SQLStatement::SQLCreateView { name, query })
Ok(SQLStatement::SQLCreateView { name, query, materialized })
}

pub fn parse_create_table(&mut self) -> Result<SQLStatement, ParserError> {
Expand Down
16 changes: 15 additions & 1 deletion tests/sqlparser_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -901,9 +901,23 @@ fn parse_scalar_subqueries() {
fn parse_create_view() {
let sql = "CREATE VIEW myschema.myview AS SELECT foo FROM bar";
match verified_stmt(sql) {
SQLStatement::SQLCreateView { name, query } => {
SQLStatement::SQLCreateView { name, query, materialized } => {
assert_eq!("myschema.myview", name.to_string());
assert_eq!("SELECT foo FROM bar", query.to_string());
assert!(!materialized);
}
_ => assert!(false),
}
}

#[test]
fn parse_create_materialized_view() {
let sql = "CREATE MATERIALIZED VIEW myschema.myview AS SELECT foo FROM bar";
match verified_stmt(sql) {
SQLStatement::SQLCreateView { name, query, materialized } => {
assert_eq!("myschema.myview", name.to_string());
assert_eq!("SELECT foo FROM bar", query.to_string());
assert!(materialized);
}
_ => assert!(false),
}
Expand Down