Skip to content

Commit

Permalink
feat: grant/revoke statement with extensions (#57)
Browse files Browse the repository at this point in the history
* feat: add privilege name

* feat: support grant option

* test: add show grants test

* feat: add object type

* feat: support grant path

* fix: fix comment

* update enum

* fix: revert grant_path in grant statement class

* fix: fix revoke

* refact: rm grant path node

* refactor: redesign grant/revoke stmt

* correct location for ".*"

---------

Co-authored-by: dl239 <[email protected]>
  • Loading branch information
aceforeverd and dl239 authored Mar 11, 2024
1 parent 3fabac8 commit 18146d1
Show file tree
Hide file tree
Showing 7 changed files with 596 additions and 9 deletions.
110 changes: 103 additions & 7 deletions zetasql/parser/bison_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,7 @@ using zetasql::ASTDropStatement;
%token KW_CURRENT_ROW "CURRENT_ROW"
%token KW_DATA "DATA"
%token KW_DATABASE "DATABASE"
%token KW_DATABASES "DATABASES"
%token KW_DATE "DATE"
%token KW_DATETIME "DATETIME"
%token KW_DECIMAL "DECIMAL"
Expand Down Expand Up @@ -880,6 +881,7 @@ using zetasql::ASTDropStatement;
%token KW_NUMERIC "NUMERIC"
%token KW_OFFSET "OFFSET"
%token KW_ONLY "ONLY"
%token KW_OPTION "OPTION"
%token KW_OPTIONS "OPTIONS"
%token KW_OUT "OUT"
%token KW_OUTFILE "OUTFILE"
Expand Down Expand Up @@ -908,6 +910,7 @@ using zetasql::ASTDropStatement;
%token KW_RETURN "RETURN"
%token KW_RETURNS "RETURNS"
%token KW_REVOKE "REVOKE"
%token KW_ROLE "ROLE"
%token KW_ROLLBACK "ROLLBACK"
%token KW_ROW "ROW"
%token KW_RUN "RUN"
Expand Down Expand Up @@ -1255,6 +1258,7 @@ using zetasql::ASTDropStatement;
%type <node> partition_by_clause_prefix
%type <node> partition_by_clause_prefix_no_hint
%type <expression> path_expression
%type <expression> path_expression_with_asterisk
%type <dashed_identifier> dashed_identifier
%type <expression> dashed_path_expression
%type <expression> maybe_dashed_path_expression
Expand Down Expand Up @@ -1418,6 +1422,7 @@ using zetasql::ASTDropStatement;
%type <boolean> opt_filter
%type <boolean> opt_if_exists
%type <boolean> opt_if_not_exists
%type <boolean> opt_grant_option
%type <boolean> opt_natural
%type <boolean> opt_not_aggregate
%type <boolean> opt_or_replace
Expand Down Expand Up @@ -3375,22 +3380,59 @@ export_model_statement:
;

grant_statement:
"GRANT" privileges "ON" identifier path_expression "TO" grantee_list
"GRANT" privileges "ON" identifier path_expression_with_asterisk "TO" grantee_list opt_grant_option
{
$$ = MAKE_NODE(ASTGrantStatement, @$, {$2, $4, $5, $7});
auto* grant = MAKE_NODE(ASTGrantStatement, @$, {$2, $4, $5, $7});
grant->set_with_grant_option($8);
$$ = grant;
}
| "GRANT" privileges "ON" path_expression "TO" grantee_list
| "GRANT" privileges "ON" path_expression_with_asterisk "TO" grantee_list opt_grant_option
{
$$ = MAKE_NODE(ASTGrantStatement, @$, {$2, $4, $6});
auto* grant = MAKE_NODE(ASTGrantStatement, @$, {$2, $4, $6});
grant->set_with_grant_option($7);
$$ = grant;
}
;

path_expression_with_asterisk:
path_expression
{
$$ = $1;
}
| path_expression ".*"
{
auto* id = parser->MakeIdentifier(@2, "*");
auto location = parser->GetBisonLocation(id->GetParseLocationRange());
// extra space allowed between DOT and STAR
location.begin = location.end - 1;
id = WithStartLocation(id, location);
auto* extended_path = WithEndLocation(WithExtraChildren($1, {id}), @2);
$$ = extended_path;
}
| "*"
{
auto* id = parser->MakeIdentifier(@1, parser->GetInputText(@1));
$$ = MAKE_NODE(ASTPathExpression, @1, {id});
}
| "*" ".*"
{
auto* id1 = parser->MakeIdentifier(@1, parser->GetInputText(@1));
auto* id2 = parser->MakeIdentifier(@2, "*");
auto location = parser->GetBisonLocation(id2->GetParseLocationRange());
// extra space allowed between DOT and STAR
location.begin = location.end - 1;
id2 = WithStartLocation(id2, location);
$$ = MAKE_NODE(ASTPathExpression, @$, {id1, id2});
}
;


revoke_statement:
"REVOKE" privileges "ON" identifier path_expression "FROM" grantee_list
"REVOKE" privileges "ON" identifier path_expression_with_asterisk "FROM" grantee_list
{
$$ = MAKE_NODE(ASTRevokeStatement, @$, {$2, $4, $5, $7});
}
| "REVOKE" privileges "ON" path_expression "FROM" grantee_list
| "REVOKE" privileges "ON" path_expression_with_asterisk "FROM" grantee_list
{
$$ = MAKE_NODE(ASTRevokeStatement, @$, {$2, $4, $6});
}
Expand Down Expand Up @@ -3435,11 +3477,62 @@ privilege_name:
{
$$ = $1;
}
| KW_SELECT
| "SELECT"
{
// The SELECT keyword is allowed to be a privilege name.
$$ = parser->MakeIdentifier(@1, parser->GetInputText(@1));
}
| "CREATE"
{
$$ = parser->MakeIdentifier(@1, parser->GetInputText(@1));
}
| "INDEX"
{
$$ = parser->MakeIdentifier(@1, parser->GetInputText(@1));
}
| "ALTER" "USER"
{
$$ = parser->MakeIdentifier(@$, "ALTER USER");
}
| "CREATE" "USER"
{
$$ = parser->MakeIdentifier(@$, "CREATE USER");
}
| "CREATE" "ROLE"
{
$$ = parser->MakeIdentifier(@$, "CREATE ROLE");
}
| "DROP" "DEPLOYMENT"
{
$$ = parser->MakeIdentifier(@$, "DROP DEPLOYMENT");
}
| "DROP" "USER"
{
$$ = parser->MakeIdentifier(@$, "DROP USER");
}
| "DROP" "ROLE"
{
$$ = parser->MakeIdentifier(@$, "DROP ROLE");
}
| "SHOW" "DATABASES"
{
$$ = parser->MakeIdentifier(@$, "SHOW DATABASES");
}
| "GRANT" "OPTION"
{
$$ = parser->MakeIdentifier(@$, "GRANT OPTION");
}
;

opt_grant_option:
"WITH" "GRANT" "OPTION"
{
$$ = true;
}
| /* Nothing */
{
$$ = false;
}
;

rename_statement:
Expand Down Expand Up @@ -7557,6 +7650,7 @@ keyword_as_identifier:
| "CURRENT_ROW"
| "DATA"
| "DATABASE"
| "DATABASES"
| "DATE"
| "DATETIME"
| "DECIMAL"
Expand Down Expand Up @@ -7620,6 +7714,7 @@ keyword_as_identifier:
| "NUMERIC"
| "OFFSET"
| "ONLY"
| "OPTION"
| "OPTIONS"
| "OUT"
| "OUTFILE"
Expand Down Expand Up @@ -7648,6 +7743,7 @@ keyword_as_identifier:
| "RETURNS"
| "RETURN"
| "REVOKE"
| "ROLE"
| "ROLLBACK"
| "ROW"
| "RUN"
Expand Down
3 changes: 3 additions & 0 deletions zetasql/parser/flex_tokenizer.l
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ current_time { return BisonParserImpl::token::KW_CURRENT_TIME;}
current_row { return BisonParserImpl::token::KW_CURRENT_ROW; }
data { return BisonParserImpl::token::KW_DATA; }
database { return BisonParserImpl::token::KW_DATABASE; }
databases { return BisonParserImpl::token::KW_DATABASES; }
date { return BisonParserImpl::token::KW_DATE; }
datetime { return BisonParserImpl::token::KW_DATETIME; }
decimal { return BisonParserImpl::token::KW_DECIMAL; }
Expand Down Expand Up @@ -566,6 +567,7 @@ offset { return BisonParserImpl::token::KW_OFFSET; }
on { return BisonParserImpl::token::KW_ON; }
only { return BisonParserImpl::token::KW_ONLY; }
open { return BisonParserImpl::token::KW_OPEN;}
option { return BisonParserImpl::token::KW_OPTION; }
options { return BisonParserImpl::token::KW_OPTIONS; }
or { return BisonParserImpl::token::KW_OR; }
order { return BisonParserImpl::token::KW_ORDER; }
Expand Down Expand Up @@ -603,6 +605,7 @@ return { return BisonParserImpl::token::KW_RETURN; }
returns { return BisonParserImpl::token::KW_RETURNS; }
revoke { return BisonParserImpl::token::KW_REVOKE; }
right { return BisonParserImpl::token::KW_RIGHT; }
role { return BisonParserImpl::token::KW_ROLE; }
rollback { return BisonParserImpl::token::KW_ROLLBACK; }
rollup { return BisonParserImpl::token::KW_ROLLUP; }
row { return BisonParserImpl::token::KW_ROW; }
Expand Down
3 changes: 3 additions & 0 deletions zetasql/parser/keywords.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ constexpr KeywordInfoPOD kAllKeywords[] = {
{"current_row", KW_CURRENT_ROW},
{"data", KW_DATA},
{"database", KW_DATABASE},
{"databases", KW_DATABASES},
{"date", KW_DATE},
{"datetime", KW_DATETIME},
{"decimal", KW_DECIMAL},
Expand Down Expand Up @@ -221,6 +222,7 @@ constexpr KeywordInfoPOD kAllKeywords[] = {
{"on", KW_ON, KeywordInfo::kReserved},
{"only", KW_ONLY},
{"open", KW_OPEN, KeywordInfo::kReserved},
{"option", KW_OPTION},
{"options", KW_OPTIONS},
{"or", KW_OR, KeywordInfo::kReserved},
{"order", KW_ORDER, KeywordInfo::kReserved},
Expand Down Expand Up @@ -259,6 +261,7 @@ constexpr KeywordInfoPOD kAllKeywords[] = {
{"returns", KW_RETURNS},
{"revoke", KW_REVOKE},
{"right", KW_RIGHT, KeywordInfo::kReserved},
{"role", KW_ROLE},
{"rollback", KW_ROLLBACK},
{"rollup", KW_ROLLUP, KeywordInfo::kReserved},
{"row", KW_ROW},
Expand Down
4 changes: 4 additions & 0 deletions zetasql/parser/parse_tree_manual.h
Original file line number Diff line number Diff line change
Expand Up @@ -6843,6 +6843,9 @@ class ASTGrantStatement final : public ASTStatement {
const ASTPathExpression* target_path() const { return target_path_; }
const ASTGranteeList* grantee_list() const { return grantee_list_; }

bool with_grant_option() const { return with_grant_option_; }
void set_with_grant_option(bool value) { with_grant_option_ = value; }

private:
void InitFields() final {
FieldLoader fl(this);
Expand All @@ -6856,6 +6859,7 @@ class ASTGrantStatement final : public ASTStatement {
const ASTIdentifier* target_type_ = nullptr; // Optional
const ASTPathExpression* target_path_ = nullptr; // Required
const ASTGranteeList* grantee_list_ = nullptr; // Required
bool with_grant_option_ = false;
};

class ASTRevokeStatement final : public ASTStatement {
Expand Down
Loading

0 comments on commit 18146d1

Please sign in to comment.