Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6533,10 +6533,16 @@ impl<'a> Parser<'a> {
let name_before_not_exists = !if_not_exists_first
&& self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
let if_not_exists = if_not_exists_first || name_before_not_exists;
let copy_grants = self.parse_keywords(&[Keyword::COPY, Keyword::GRANTS]);
let mut copy_grants = self.parse_keywords(&[Keyword::COPY, Keyword::GRANTS]);
// Many dialects support `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 columns = self.parse_view_columns()?;
// Snowflake also documents `COPY GRANTS` *after* the column list; accept
// either position, but not both.
// <https://docs.snowflake.com/en/sql-reference/sql/create-view#syntax>
if !copy_grants {
copy_grants = self.parse_keywords(&[Keyword::COPY, Keyword::GRANTS]);
}
let mut options = CreateTableOptions::None;
let with_options = self.parse_options(Keyword::WITH)?;
if !with_options.is_empty() {
Expand Down
46 changes: 46 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4764,6 +4764,52 @@ fn test_snowflake_create_view_copy_grants() {
);
}

#[test]
fn test_snowflake_create_view_copy_grants_after_columns() {
// Snowflake's documented placement for `COPY GRANTS` on `CREATE VIEW` is
// *after* the column list. Display normalizes to the pre-columns form
// already supported, so use `one_statement_parses_to` to assert the
// post-columns input is accepted and the AST flag is set.
// <https://docs.snowflake.com/en/sql-reference/sql/create-view#syntax>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Snowflake's documented placement for `COPY GRANTS` on `CREATE VIEW` is
// *after* the column list. Display normalizes to the pre-columns form
// already supported, so use `one_statement_parses_to` to assert the
// post-columns input is accepted and the AST flag is set.
// <https://docs.snowflake.com/en/sql-reference/sql/create-view#syntax>

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

let cases = [
(
"CREATE OR REPLACE VIEW v (a, b) COPY GRANTS AS SELECT a, b FROM t",
"CREATE OR REPLACE VIEW v COPY GRANTS (a, b) AS SELECT a, b FROM t",
),
(
"CREATE OR REPLACE SECURE VIEW v (a, b) COPY GRANTS AS SELECT a, b FROM t",
"CREATE OR REPLACE SECURE VIEW v COPY GRANTS (a, b) AS SELECT a, b FROM t",
),
(
"CREATE MATERIALIZED VIEW v (a) COPY GRANTS AS SELECT a FROM t",
"CREATE MATERIALIZED VIEW v COPY GRANTS (a) AS SELECT a FROM t",
),
];
for (sql, parsed) in cases {
match snowflake().one_statement_parses_to(sql, parsed) {
Statement::CreateView(CreateView {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can drop the ast assertions in the tests

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

name,
copy_grants,
columns,
..
}) => {
assert_eq!("v", name.to_string());
assert!(copy_grants, "copy_grants should be true for {sql:?}");
assert!(!columns.is_empty(), "columns should be set for {sql:?}");
}
_ => unreachable!(),
}
}

// Baseline: the same query without COPY GRANTS must not flip the flag.
match snowflake().verified_stmt("CREATE OR REPLACE VIEW v (a) AS SELECT a FROM t") {
Statement::CreateView(CreateView { copy_grants, .. }) => {
assert!(!copy_grants);
}
_ => unreachable!(),
}
}

#[test]
fn test_snowflake_identifier_function() {
// Using IDENTIFIER to reference a column
Expand Down