Skip to content

Commit

Permalink
feat(ddl): user related DDLs
Browse files Browse the repository at this point in the history
merge #52

CREATE | ALTER | DROP USER
  • Loading branch information
aceforeverd committed Dec 12, 2023
1 parent 1323d17 commit 3c9cf36
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 4 deletions.
1 change: 1 addition & 0 deletions zetasql/parser/ast_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ enum class SchemaObjectKind {
kTableFunction,
kView,
kDeployment,
kUser,
__SchemaObjectKind__switch_must_have_a_default__ = -1,
};

Expand Down
3 changes: 3 additions & 0 deletions zetasql/parser/ast_node_kind.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ enum ASTNodeKind {
AST_ALTER_SCHEMA_STATEMENT,
AST_ALTER_TABLE_STATEMENT,
AST_ALTER_VIEW_STATEMENT,
AST_ALTER_USER_STATEMENT,
AST_ANALYTIC_FUNCTION_CALL,
AST_ANALYZE_STATEMENT,
AST_AND_EXPR,
Expand Down Expand Up @@ -87,6 +88,7 @@ enum ASTNodeKind {
AST_CREATE_EXTERNAL_TABLE_STATEMENT,
AST_CREATE_FUNCTION_STATEMENT,
AST_CREATE_INDEX_STATEMENT,
AST_CREATE_USER_STATEMENT,
AST_CREATE_MODEL_STATEMENT,
AST_CREATE_PROCEDURE_STATEMENT,
AST_CREATE_ROW_ACCESS_POLICY_STATEMENT,
Expand Down Expand Up @@ -117,6 +119,7 @@ enum ASTNodeKind {
AST_DROP_STATEMENT,
AST_DROP_TABLE_FUNCTION_STATEMENT,
AST_DROP_MATERIALIZED_VIEW_STATEMENT,
AST_DROP_USER_STATEMENT,
AST_ELSEIF_CLAUSE,
AST_ELSEIF_CLAUSE_LIST,
AST_EXCEPTION_HANDLER,
Expand Down
30 changes: 30 additions & 0 deletions zetasql/parser/bison_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,7 @@ using zetasql::ASTDropStatement;
%token KW_UNTIL "UNTIL"
%token KW_UPDATE "UPDATE"
%token KW_USE "USE"
%token KW_USER "USER"
%token KW_VALUE "VALUE"
%token KW_VALUES "VALUES"
%token KW_VARIABLES "VARIABLES"
Expand Down Expand Up @@ -1016,6 +1017,7 @@ using zetasql::ASTDropStatement;
%type <node> create_table_statement
%type <node> create_view_statement
%type <node> create_entity_statement
%type <node> create_user_statement
%type <expression> date_or_time_literal
%type <node> define_table_statement
%type <node> delete_statement
Expand Down Expand Up @@ -1597,6 +1599,7 @@ sql_statement_body:
| create_table_statement
| create_view_statement
| create_entity_statement
| create_user_statement
| define_table_statement
| describe_statement
| execute_immediate
Expand Down Expand Up @@ -1810,6 +1813,8 @@ schema_object_kind:
{ $$ = zetasql::SchemaObjectKind::kView; }
| "DEPLOYMENT"
{ $$ = zetasql::SchemaObjectKind::kDeployment; }
| "USER"
{ $$ = zetasql::SchemaObjectKind::kUser; }
;

alter_statement:
Expand Down Expand Up @@ -1839,6 +1844,8 @@ alter_statement:
node = MAKE_NODE(ASTAlterViewStatement, @$);
} else if ($2 == zetasql::SchemaObjectKind::kMaterializedView) {
node = MAKE_NODE(ASTAlterMaterializedViewStatement, @$);
} else if ($2 == zetasql::SchemaObjectKind::kUser) {
node = MAKE_NODE(ASTAlterUserStatement, @$);
} else {
YYERROR_AND_ABORT_AT(@2, absl::StrCat("ALTER ", absl::AsciiStrToUpper(
parser->GetInputText(@2)), " is not supported"));
Expand Down Expand Up @@ -2093,6 +2100,15 @@ create_database_statement:
}
;

create_user_statement:
"CREATE" "USER" opt_if_not_exists path_expression opt_options_list
{
auto* create = MAKE_NODE(ASTCreateUserStatement, @$, {$4, $5});
create->set_is_if_not_exists($3);
$$ = create;
}
;

create_function_statement:
"CREATE" opt_or_replace opt_create_scope opt_aggregate
"FUNCTION" opt_if_not_exists function_declaration opt_function_returns
Expand Down Expand Up @@ -7642,6 +7658,7 @@ keyword_as_identifier:
| "UNTIL"
| "UPDATE"
| "USE"
| "USER"
| "VALUE"
| "VALUES"
| "VARIABLES"
Expand Down Expand Up @@ -8539,6 +8556,10 @@ drop_statement:
MAKE_NODE(ASTDropFunctionStatement, @$, {$4, $5});
drop_function->set_is_if_exists($3);
$$ = drop_function;
} else if ($2 == zetasql::SchemaObjectKind::kUser) {
auto* drop = MAKE_NODE(ASTDropUserStatement, @$, {$4});
drop->set_is_if_exists($3);
$$ = drop;
} else {
if ($5 != nullptr) {
YYERROR_AND_ABORT_AT(@5,
Expand Down Expand Up @@ -9014,6 +9035,9 @@ next_statement_kind_without_hint:
case zetasql::SchemaObjectKind::kMaterializedView:
$$ = zetasql::ASTDropMaterializedViewStatement::kConcreteNodeKind;
break;
case zetasql::SchemaObjectKind::kUser:
$$ = zetasql::ASTDropUserStatement::kConcreteNodeKind;
break;
default:
$$ = zetasql::ASTDropStatement::kConcreteNodeKind;
break;
Expand Down Expand Up @@ -9053,6 +9077,8 @@ next_statement_kind_without_hint:
{ $$ = zetasql::ASTAlterTableStatement::kConcreteNodeKind; }
| "ALTER" "ROW"
{ $$ = zetasql::ASTAlterRowAccessPolicyStatement::kConcreteNodeKind; }
| "ALTER" "USER"
{ $$ = zetasql::ASTAlterUserStatement::kConcreteNodeKind; }
| "ALTER" "ALL" "ROW" "ACCESS" "POLICIES"
{ $$ =
zetasql::ASTAlterAllRowAccessPoliciesStatement::kConcreteNodeKind; }
Expand Down Expand Up @@ -9103,6 +9129,10 @@ next_statement_kind_without_hint:
{
$$ = zetasql::ASTCreateTableFunctionStatement::kConcreteNodeKind;
}
| "CREATE" "USER" opt_if_not_exists
{
$$ = zetasql::ASTCreateUserStatement::kConcreteNodeKind;
}
| "CREATE" next_statement_kind_create_modifiers "EXTERNAL"
{
$$ = zetasql::ASTCreateExternalTableStatement::kConcreteNodeKind;
Expand Down
1 change: 1 addition & 0 deletions zetasql/parser/flex_tokenizer.l
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ model { return BisonParserImpl::token::KW_MODEL; }
module { return BisonParserImpl::token::KW_MODULE; }
natural { return BisonParserImpl::token::KW_NATURAL; }
new { return BisonParserImpl::token::KW_NEW; }
user { return BisonParserImpl::token::KW_USER; }
no { return BisonParserImpl::token::KW_NO; }
{not} { return BisonParserImpl::token::KW_NOT; }
/* This returns a different token because returning KW_NOT would confuse the
Expand Down
5 changes: 3 additions & 2 deletions zetasql/parser/keywords.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ constexpr KeywordInfoPOD kAllKeywords[] = {
{"hash", KW_HASH, KeywordInfo::kReserved},
{"having", KW_HAVING, KeywordInfo::kReserved},
{"hidden", KW_HIDDEN},
{"hive", KW_HIVE},
{"if", KW_IF, KeywordInfo::kReserved},
{"ignore", KW_IGNORE, KeywordInfo::kReserved},
{"immediate", KW_IMMEDIATE},
Expand Down Expand Up @@ -214,6 +215,7 @@ constexpr KeywordInfoPOD kAllKeywords[] = {
{"nulls", KW_NULLS, KeywordInfo::kReserved},
{"numeric", KW_NUMERIC},
{"of", KW_OF, KeywordInfo::kReserved},
{"offline_path", KW_OFFLINE_PATH},
{"offset", KW_OFFSET},
{"on", KW_ON, KeywordInfo::kReserved},
{"only", KW_ONLY},
Expand All @@ -226,8 +228,6 @@ constexpr KeywordInfoPOD kAllKeywords[] = {
{"outer", KW_OUTER, KeywordInfo::kReserved},
{"over", KW_OVER, KeywordInfo::kReserved},
{"parquet", KW_PARQUET},
{"offline_path", KW_OFFLINE_PATH},
{"hive", KW_HIVE},
{"partition", KW_PARTITION, KeywordInfo::kReserved},
{"percent", KW_PERCENT},
{"pivot", KW_PIVOT},
Expand Down Expand Up @@ -309,6 +309,7 @@ constexpr KeywordInfoPOD kAllKeywords[] = {
{"until", KW_UNTIL},
{"update", KW_UPDATE},
{"use", KW_USE},
{"user", KW_USER},
{"using", KW_USING, KeywordInfo::kReserved},
{"value", KW_VALUE},
{"values", KW_VALUES},
Expand Down
5 changes: 5 additions & 0 deletions zetasql/parser/parse_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ static absl::flat_hash_map<ASTNodeKind, std::string> CreateNodeNamesMap() {
map[AST_ALTER_TABLE_STATEMENT] = "AlterTableStatement";
map[AST_ALTER_SCHEMA_STATEMENT]= "AlterSchemaStatement";
map[AST_ALTER_VIEW_STATEMENT] = "AlterViewStatement";
map[AST_ALTER_USER_STATEMENT] = "AlterUserStatement";
map[AST_ANALYTIC_FUNCTION_CALL] = "AnalyticFunctionCall";
map[AST_ANALYZE_STATEMENT] = "AnalyzeStatement";
map[AST_AND_EXPR] = "AndExpr";
Expand Down Expand Up @@ -119,6 +120,7 @@ static absl::flat_hash_map<ASTNodeKind, std::string> CreateNodeNamesMap() {
map[AST_CREATE_EXTERNAL_TABLE_STATEMENT] = "CreateExternalTableStatement";
map[AST_CREATE_FUNCTION_STATEMENT] = "CreateFunctionStatement";
map[AST_CREATE_INDEX_STATEMENT] = "CreateIndexStatement";
map[AST_CREATE_USER_STATEMENT] = "CreateUserStatement";
map[AST_CREATE_PROCEDURE_STATEMENT] = "CreateProcedureStatement";
map[AST_CREATE_MODEL_STATEMENT] = "CreateModelStatement";
map[AST_CREATE_ROW_ACCESS_POLICY_STATEMENT] =
Expand Down Expand Up @@ -152,6 +154,7 @@ static absl::flat_hash_map<ASTNodeKind, std::string> CreateNodeNamesMap() {
map[AST_DROP_ROW_ACCESS_POLICY_STATEMENT] = "DropRowAccessPolicyStatement";
map[AST_DROP_STATEMENT] = "DropStatement";
map[AST_DROP_MATERIALIZED_VIEW_STATEMENT] = "DropMaterializedViewStatement";
map[AST_DROP_USER_STATEMENT] = "DropUserStatement";
map[AST_ELSEIF_CLAUSE] = "ElseIf";
map[AST_ELSEIF_CLAUSE_LIST] = "ElseIfList";
map[AST_EXCEPTION_HANDLER] = "ExceptionHandler";
Expand Down Expand Up @@ -1694,6 +1697,8 @@ absl::string_view SchemaObjectKindToName(SchemaObjectKind schema_object_kind) {
return "VIEW";
case SchemaObjectKind::kDeployment:
return "DEPLOYMENT";
case SchemaObjectKind::kUser:
return "USER";
default:
return "<INVALID SCHEMA OBJECT KIND>";
}
Expand Down
63 changes: 63 additions & 0 deletions zetasql/parser/parse_tree_manual.h
Original file line number Diff line number Diff line change
Expand Up @@ -4648,6 +4648,53 @@ class ASTCreateTableStmtBase : public ASTCreateStatement {
const ASTPathExpression* like_table_name_ = nullptr; // May be NULL.
};

class ASTDropUserStatement final : public ASTDdlStatement {
public:
static constexpr ASTNodeKind kConcreteNodeKind =
AST_DROP_USER_STATEMENT;
explicit ASTDropUserStatement() : ASTDdlStatement(kConcreteNodeKind) {}
void Accept(ParseTreeVisitor* visitor, void* data) const override;
zetasql_base::StatusOr<VisitResult> Accept(
NonRecursiveParseTreeVisitor* visitor) const override;
const ASTPathExpression* name() const { return name_; }
bool is_if_exists() const { return is_if_exists_; }
void set_is_if_exists(bool value) { is_if_exists_ = value; }
const ASTPathExpression* GetDdlTarget() const override { return name_; }

private:
void InitFields() final {
FieldLoader fl(this);
fl.AddRequired(&name_);
}

const ASTPathExpression* name_;
bool is_if_exists_ = false;
};

class ASTCreateUserStatement final : public ASTCreateStatement {
public:
static constexpr ASTNodeKind kConcreteNodeKind =
AST_CREATE_USER_STATEMENT;
explicit ASTCreateUserStatement() : ASTCreateStatement(kConcreteNodeKind) {}
void Accept(ParseTreeVisitor* visitor, void* data) const override;
zetasql_base::StatusOr<VisitResult> Accept(
NonRecursiveParseTreeVisitor* visitor) const override;
const ASTPathExpression* name() const { return name_; }
const ASTOptionsList* options_list() const { return options_list_; }
const ASTPathExpression* GetDdlTarget() const override { return name_; }

private:
void InitFields() final {
FieldLoader fl(this);
fl.AddRequired(&name_);
fl.AddOptional(&options_list_, AST_OPTIONS_LIST);
}

const ASTPathExpression* name_ = nullptr;
const ASTOptionsList* options_list_ = nullptr;

};

class ASTCreateTableStatement final : public ASTCreateTableStmtBase {
public:
static constexpr ASTNodeKind kConcreteNodeKind = AST_CREATE_TABLE_STATEMENT;
Expand Down Expand Up @@ -7578,6 +7625,22 @@ class ASTAlterDatabaseStatement final : public ASTAlterStatementBase {
}
};

class ASTAlterUserStatement final : public ASTAlterStatementBase {
public:
static constexpr ASTNodeKind kConcreteNodeKind =
AST_ALTER_USER_STATEMENT;
ASTAlterUserStatement() : ASTAlterStatementBase(kConcreteNodeKind) {}
void Accept(ParseTreeVisitor* visitor, void* data) const override;
zetasql_base::StatusOr<VisitResult> Accept(
NonRecursiveParseTreeVisitor* visitor) const override;

private:
void InitFields() final {
FieldLoader fl(this);
InitPathAndAlterActions(&fl);
}
};

class ASTAlterSchemaStatement final : public ASTAlterStatementBase {
public:
static constexpr ASTNodeKind kConcreteNodeKind = AST_ALTER_SCHEMA_STATEMENT;
Expand Down
4 changes: 2 additions & 2 deletions zetasql/parser/testdata/alter_row_access_policy.test
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ alter row access policy p1 on t1 grant to (null);
# Grant to: grantee strings must be quoted.
alter row access policy p1 on t1 grant to (user);
--
ERROR: Syntax error: Expected "@" or "@@" or string literal but got identifier "user" [at 1:44]
ERROR: Syntax error: Expected "@" or "@@" or string literal but got keyword USER [at 1:44]
alter row access policy p1 on t1 grant to (user);
^
==
Expand Down Expand Up @@ -929,7 +929,7 @@ alter row access policy p1 on t1 revoke from (null);
# Revoke from: invalid non-string-literal revokee.
alter row access policy p1 on t1 revoke from (user);
--
ERROR: Syntax error: Expected "@" or "@@" or string literal but got identifier "user" [at 1:47]
ERROR: Syntax error: Expected "@" or "@@" or string literal but got keyword USER [at 1:47]
alter row access policy p1 on t1 revoke from (user);
^
==
Expand Down
36 changes: 36 additions & 0 deletions zetasql/parser/testdata/alter_user.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
alter user root;
--
ERROR: Syntax error: Unexpected ";" [at 1:16]
alter user root;
^
==

alter user root set options (password='abc');
--
AlterUserStatement [0-44]
PathExpression [11-15]
Identifier(root) [11-15]
AlterActionList [16-44]
SetOptionsOperation [16-44]
OptionsList [28-44]
OptionsEntry [29-43]
Identifier(password) [29-37]
StringLiteral('abc') [38-43]
--
ALTER USER root SET OPTIONS(password = 'abc')
==

alter user if exists root set options (password='abc');
--
AlterUserStatement(is_if_exists) [0-54]
PathExpression [21-25]
Identifier(root) [21-25]
AlterActionList [26-54]
SetOptionsOperation [26-54]
OptionsList [38-54]
OptionsEntry [39-53]
Identifier(password) [39-47]
StringLiteral('abc') [48-53]
--
ALTER USER IF EXISTS root SET OPTIONS(password = 'abc')
==
30 changes: 30 additions & 0 deletions zetasql/parser/testdata/create_user.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
create user root;
--
CreateUserStatement [0-16]
PathExpression [12-16]
Identifier(root) [12-16]
--
CREATE USER root
==

create user if not exists root;
--
CreateUserStatement(is_if_not_exists) [0-30]
PathExpression [26-30]
Identifier(root) [26-30]
--
CREATE USER IF NOT EXISTS root
==

create user root options (password='abc');
--
CreateUserStatement [0-41]
PathExpression [12-16]
Identifier(root) [12-16]
OptionsList [25-41]
OptionsEntry [26-40]
Identifier(password) [26-34]
StringLiteral('abc') [35-40]
--
CREATE USER root OPTIONS(password = 'abc')
==
17 changes: 17 additions & 0 deletions zetasql/parser/testdata/drop_user.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
drop user root;
--
DropUserStatement [0-14]
PathExpression [10-14]
Identifier(root) [10-14]
--
DROP USER root
==

drop user if exists user1;
--
DropUserStatement [0-25]
PathExpression [20-25]
Identifier(user1) [20-25]
--
DROP USER IF EXISTS user1
==
Loading

0 comments on commit 3c9cf36

Please sign in to comment.