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

redshift: add support for CREATE VIEW … WITH NO SCHEMA BINDING #979

Merged
merged 1 commit into from
Oct 2, 2023
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
11 changes: 9 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>])
lustefaniak marked this conversation as resolved.
Show resolved Hide resolved
///
/// <col> = CompoundIdentifier
/// <expr> = String literal
Expand Down Expand Up @@ -1316,6 +1316,8 @@ pub enum Statement {
query: Box<Query>,
with_options: Vec<SqlOption>,
cluster_by: Vec<Ident>,
/// if true, has RedShift [`WITH NO SCHEMA BINDING`] clause <https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_VIEW.html>
with_no_schema_binding: bool,
},
/// CREATE TABLE
CreateTable {
Expand Down Expand Up @@ -2271,6 +2273,7 @@ impl fmt::Display for Statement {
materialized,
with_options,
cluster_by,
with_no_schema_binding,
} => {
write!(
f,
Expand All @@ -2288,7 +2291,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 *with_no_schema_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 @@ -110,6 +110,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 @@ -2974,6 +2974,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 with_no_schema_binding = dialect_of!(self is RedshiftSqlDialect | GenericDialect)
&& self.parse_keywords(&[
Keyword::WITH,
Keyword::NO,
Keyword::SCHEMA,
Keyword::BINDING,
]);

Ok(Statement::CreateView {
name,
columns,
Expand All @@ -2982,6 +2991,7 @@ impl<'a> Parser<'a> {
or_replace,
with_options,
cluster_by,
with_no_schema_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 @@ -5316,6 +5316,7 @@ fn parse_create_view() {
materialized,
with_options,
cluster_by,
with_no_schema_binding: late_binding,
} => {
assert_eq!("myschema.myview", name.to_string());
assert_eq!(Vec::<Ident>::new(), columns);
Expand All @@ -5324,6 +5325,7 @@ fn parse_create_view() {
assert!(!or_replace);
assert_eq!(with_options, vec![]);
assert_eq!(cluster_by, vec![]);
assert!(!late_binding);
}
_ => unreachable!(),
}
Expand Down Expand Up @@ -5364,6 +5366,7 @@ fn parse_create_view_with_columns() {
query,
materialized,
cluster_by,
with_no_schema_binding: late_binding,
} => {
assert_eq!("v", name.to_string());
assert_eq!(columns, vec![Ident::new("has"), Ident::new("cols")]);
Expand All @@ -5372,6 +5375,7 @@ fn parse_create_view_with_columns() {
assert!(!materialized);
assert!(!or_replace);
assert_eq!(cluster_by, vec![]);
assert!(!late_binding);
}
_ => unreachable!(),
}
Expand All @@ -5389,6 +5393,7 @@ fn parse_create_or_replace_view() {
query,
materialized,
cluster_by,
with_no_schema_binding: late_binding,
} => {
assert_eq!("v", name.to_string());
assert_eq!(columns, vec![]);
Expand All @@ -5397,6 +5402,7 @@ fn parse_create_or_replace_view() {
assert!(!materialized);
assert!(or_replace);
assert_eq!(cluster_by, vec![]);
assert!(!late_binding);
}
_ => unreachable!(),
}
Expand All @@ -5418,6 +5424,7 @@ fn parse_create_or_replace_materialized_view() {
query,
materialized,
cluster_by,
with_no_schema_binding: late_binding,
} => {
assert_eq!("v", name.to_string());
assert_eq!(columns, vec![]);
Expand All @@ -5426,6 +5433,7 @@ fn parse_create_or_replace_materialized_view() {
assert!(materialized);
assert!(or_replace);
assert_eq!(cluster_by, vec![]);
assert!(!late_binding);
}
_ => unreachable!(),
}
Expand All @@ -5443,6 +5451,7 @@ fn parse_create_materialized_view() {
materialized,
with_options,
cluster_by,
with_no_schema_binding: late_binding,
} => {
assert_eq!("myschema.myview", name.to_string());
assert_eq!(Vec::<Ident>::new(), columns);
Expand All @@ -5451,6 +5460,7 @@ fn parse_create_materialized_view() {
assert_eq!(with_options, vec![]);
assert!(!or_replace);
assert_eq!(cluster_by, vec![]);
assert!(!late_binding);
}
_ => unreachable!(),
}
Expand All @@ -5468,6 +5478,7 @@ fn parse_create_materialized_view_with_cluster_by() {
materialized,
with_options,
cluster_by,
with_no_schema_binding: late_binding,
} => {
assert_eq!("myschema.myview", name.to_string());
assert_eq!(Vec::<Ident>::new(), columns);
Expand All @@ -5476,6 +5487,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!(!late_binding);
}
_ => unreachable!(),
}
Expand Down
14 changes: 14 additions & 0 deletions tests/sqlparser_redshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod test_utils;
use test_utils::*;

use sqlparser::ast::*;
use sqlparser::dialect::GenericDialect;
use sqlparser::dialect::RedshiftSqlDialect;

#[test]
Expand Down Expand Up @@ -272,6 +273,13 @@ fn redshift() -> TestedDialects {
}
}

fn redshift_and_generic() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(RedshiftSqlDialect {}), Box::new(GenericDialect {})],
options: None,
}
}

#[test]
fn test_sharp() {
let sql = "SELECT #_of_values";
Expand All @@ -281,3 +289,9 @@ fn test_sharp() {
select.projection[0]
);
}

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