Skip to content

Commit

Permalink
redshift: add support for CREATE VIEW … WITH NO SCHEMA BINDING
Browse files Browse the repository at this point in the history
  • Loading branch information
lustefaniak committed Sep 25, 2023
1 parent 7723ea5 commit f93a6bb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ pub enum Expr {
///
/// Syntax:
/// ```sql
/// MARCH (<col>, <col>, ...) AGAINST (<expr> [<search modifier>])
/// MATCH (<col>, <col>, ...) AGAINST (<expr> [<search modifier>])
///
/// <col> = CompoundIdentifier
/// <expr> = String literal
Expand Down Expand Up @@ -1312,6 +1312,7 @@ pub enum Statement {
query: Box<Query>,
with_options: Vec<SqlOption>,
cluster_by: Vec<Ident>,
late_binding: bool,
},
/// CREATE TABLE
CreateTable {
Expand Down Expand Up @@ -2241,6 +2242,7 @@ impl fmt::Display for Statement {
materialized,
with_options,
cluster_by,
late_binding,
} => {
write!(
f,
Expand All @@ -2258,7 +2260,11 @@ impl fmt::Display for Statement {
if !cluster_by.is_empty() {
write!(f, " CLUSTER BY ({})", display_comma_separated(cluster_by))?;
}
write!(f, " AS {query}")
write!(f, " AS {query}")?;
if *late_binding {
write!(f, " WITH NO SCHEMA BINDING")?;
}
Ok(())
}
Statement::CreateTable {
name,
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ define_keywords!(
BIGINT,
BIGNUMERIC,
BINARY,
BINDING,
BLOB,
BLOOMFILTER,
BOOL,
Expand Down
10 changes: 10 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2957,6 +2957,15 @@ impl<'a> Parser<'a> {
self.expect_keyword(Keyword::AS)?;
let query = Box::new(self.parse_query()?);
// Optional `WITH [ CASCADED | LOCAL ] CHECK OPTION` is widely supported here.

let late_binding = dialect_of!(self is RedshiftSqlDialect)
&& self.parse_keywords(&[
Keyword::WITH,
Keyword::NO,
Keyword::SCHEMA,
Keyword::BINDING,
]);

Ok(Statement::CreateView {
name,
columns,
Expand All @@ -2965,6 +2974,7 @@ impl<'a> Parser<'a> {
or_replace,
with_options,
cluster_by,
late_binding,
})
}

Expand Down
12 changes: 12 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5310,6 +5310,7 @@ fn parse_create_view() {
materialized,
with_options,
cluster_by,
late_binding,
} => {
assert_eq!("myschema.myview", name.to_string());
assert_eq!(Vec::<Ident>::new(), columns);
Expand All @@ -5318,6 +5319,7 @@ fn parse_create_view() {
assert!(!or_replace);
assert_eq!(with_options, vec![]);
assert_eq!(cluster_by, vec![]);
assert_eq!(late_binding, false);
}
_ => unreachable!(),
}
Expand Down Expand Up @@ -5358,6 +5360,7 @@ fn parse_create_view_with_columns() {
query,
materialized,
cluster_by,
late_binding,
} => {
assert_eq!("v", name.to_string());
assert_eq!(columns, vec![Ident::new("has"), Ident::new("cols")]);
Expand All @@ -5366,6 +5369,7 @@ fn parse_create_view_with_columns() {
assert!(!materialized);
assert!(!or_replace);
assert_eq!(cluster_by, vec![]);
assert_eq!(late_binding, false);
}
_ => unreachable!(),
}
Expand All @@ -5383,6 +5387,7 @@ fn parse_create_or_replace_view() {
query,
materialized,
cluster_by,
late_binding,
} => {
assert_eq!("v", name.to_string());
assert_eq!(columns, vec![]);
Expand All @@ -5391,6 +5396,7 @@ fn parse_create_or_replace_view() {
assert!(!materialized);
assert!(or_replace);
assert_eq!(cluster_by, vec![]);
assert_eq!(late_binding, false);
}
_ => unreachable!(),
}
Expand All @@ -5412,6 +5418,7 @@ fn parse_create_or_replace_materialized_view() {
query,
materialized,
cluster_by,
late_binding,
} => {
assert_eq!("v", name.to_string());
assert_eq!(columns, vec![]);
Expand All @@ -5420,6 +5427,7 @@ fn parse_create_or_replace_materialized_view() {
assert!(materialized);
assert!(or_replace);
assert_eq!(cluster_by, vec![]);
assert_eq!(late_binding, false);
}
_ => unreachable!(),
}
Expand All @@ -5437,6 +5445,7 @@ fn parse_create_materialized_view() {
materialized,
with_options,
cluster_by,
late_binding,
} => {
assert_eq!("myschema.myview", name.to_string());
assert_eq!(Vec::<Ident>::new(), columns);
Expand All @@ -5445,6 +5454,7 @@ fn parse_create_materialized_view() {
assert_eq!(with_options, vec![]);
assert!(!or_replace);
assert_eq!(cluster_by, vec![]);
assert_eq!(late_binding, false);
}
_ => unreachable!(),
}
Expand All @@ -5462,6 +5472,7 @@ fn parse_create_materialized_view_with_cluster_by() {
materialized,
with_options,
cluster_by,
late_binding,
} => {
assert_eq!("myschema.myview", name.to_string());
assert_eq!(Vec::<Ident>::new(), columns);
Expand All @@ -5470,6 +5481,7 @@ fn parse_create_materialized_view_with_cluster_by() {
assert_eq!(with_options, vec![]);
assert!(!or_replace);
assert_eq!(cluster_by, vec![Ident::new("foo")]);
assert_eq!(late_binding, false);
}
_ => unreachable!(),
}
Expand Down
6 changes: 6 additions & 0 deletions tests/sqlparser_redshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,9 @@ fn test_sharp() {
select.projection[0]
);
}

#[test]
fn test_create_view_late_binding() {
redshift()
.verified_stmt("CREATE VIEW myevent AS SELECT eventname FROM event WITH NO SCHEMA BINDING");
}

0 comments on commit f93a6bb

Please sign in to comment.