Skip to content

Add support for DENY statements #1836

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

aharpervc
Copy link
Contributor

@aharpervc aharpervc commented May 1, 2025

This is another statement for SQL Server: https://learn.microsoft.com/en-us/sql/t-sql/statements/deny-transact-sql, but implemented in a common way. Similar to GRANT & REVOKE, so using a lot of those patterns with a similar test.

Supplementary improvements:

  1. While I was working on that, I also noticed that EXEC was missing from the privileges list, so that's added now too (with a test example).
  2. Additionally, SQL Server supports identifiers quoted [like this]. That dialect function hadn't been implemented yet, which caused a test failure on example that use that quoting style. So I've added that now too with a couple example cases for grant/deny.
  3. Finally, "public" is a built in role on SQL Server, so doing something like grant select on mytable to public should parse public normally. However, the former logic was reading it as a keyword. This has been adjusted to be not special on SQL Server and tests cases have been added as well.
  4. Actually finally, GRANT ... AS role parsing is now supported (formerly only worked for REVOKE/DENY) and a new test case example

@aharpervc aharpervc marked this pull request as draft May 1, 2025 22:11
@aharpervc aharpervc marked this pull request as ready for review May 1, 2025 22:30
Comment on lines 13003 to 13004
self.parse_keywords(&[Keyword::AS])
.then(|| self.parse_identifier().unwrap())
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I realise this unwrap wasn't introduced by this PR, but can we switch it to return an error instead? it looks otherwise like an scenario where the parser will panic on invalid sql which would be undesirable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've refactored these three related methods to avoid unwrap 👍


let granted_by = if self.peek_keywords(&[Keyword::GRANTED, Keyword::BY]) {
self.parse_keywords(&[Keyword::GRANTED, Keyword::BY])
.then(|| self.parse_identifier().unwrap())
Copy link
Contributor

Choose a reason for hiding this comment

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

similar here, we would want to return an error vs unwrap

let cascade = self.parse_cascade_option();
let granted_by = self
.parse_keywords(&[Keyword::AS])
.then(|| self.parse_identifier().unwrap());
Copy link
Contributor

Choose a reason for hiding this comment

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

same comment here re unwrap

Comment on lines +13045 to +13049
if dialect_of!(self is MsSqlDialect) {
grantee_type
} else {
GranteesType::Public
}
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm not sure I followed the intention here, public is represented differently for mssql? (we would want to describe that behavior without the dialect_of macro I think)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm not sure I followed the intention here, public is represented differently for mssql? (we would want to describe that behavior without the dialect_of macro I think)

My interpretation of the existing code is that "public" is being interpreted as a keyword. With SQL Server, "public" is a built in role (ref)

From a parsing perspective, there wouldn't be any difference between a built-in role & any other role. The consideration here is "keyword" vs "not keyword". The additional test case examples here cover this behavior.

Put simply, this (and GRANT) should be able to parse normally:

DENY SELECT ON my_table TO public

With regards to the code, what is the preferred way to implement this behavior? Brainstorming...

  1. Use dialect_of
  2. Copy/paste this entire method into the dialect so it's fully duplicated except for this keyword
  3. Implement a dialect function to describe "tokens that are listed as keywords but should be treated as identifiers", or similar, and then it's blank for most dialects but returns "public" for SQL Server
  4. Something else...?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants