Skip to content

Commit

Permalink
test: add drop user case
Browse files Browse the repository at this point in the history
  • Loading branch information
denglong committed Dec 7, 2023
1 parent aedbab0 commit 25f3284
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 6 deletions.
1 change: 1 addition & 0 deletions zetasql/parser/ast_node_kind.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ enum ASTNodeKind {
AST_START_BATCH_STATEMENT,
AST_STATEMENT_LIST,
AST_STRING_LITERAL,
AST_STRING_LITERAL_LIST,
AST_STRUCT_COLUMN_FIELD,
AST_STRUCT_COLUMN_SCHEMA,
AST_STRUCT_CONSTRUCTOR_ARG,
Expand Down
14 changes: 13 additions & 1 deletion zetasql/parser/bison_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,7 @@ using zetasql::ASTDropStatement;
%type <node> variable_declaration
%type <node> opt_default_expression
%type <node> identifier_list
%type <node> string_literal_list
%type <node> path_expression_list
%type <node> path_expression_list_with_opt_parens
%type <node> set_statement
Expand Down Expand Up @@ -8518,7 +8519,7 @@ drop_statement:
drop_row_access_policy->set_is_if_exists($5);
$$ = drop_row_access_policy;
}
| "DROP" "USER" opt_if_exists string_literal
| "DROP" "USER" opt_if_exists string_literal_list
{
auto* drop = MAKE_NODE(ASTDropUserStatement, @$, {$4});
drop->set_is_if_exists($3);
Expand Down Expand Up @@ -8871,6 +8872,17 @@ identifier_list:
}
;

string_literal_list:
string_literal
{
$$ = MAKE_NODE(ASTStringLiteralList, @$, {$1});
}
| string_literal_list "," string_literal
{
$$ = WithEndLocation(WithExtraChildren($1, {$3}), @$);
}
;

variable_declaration:
"DECLARE" identifier_list type opt_default_expression
{
Expand Down
1 change: 1 addition & 0 deletions zetasql/parser/parse_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ static absl::flat_hash_map<ASTNodeKind, std::string> CreateNodeNamesMap() {
map[AST_START_BATCH_STATEMENT] = "StartBatchStatement";
map[AST_STATEMENT_LIST] = "StatementList";
map[AST_STRING_LITERAL] = "StringLiteral";
map[AST_STRING_LITERAL_LIST] = "StringLiteralList";
map[AST_STRUCT_COLUMN_FIELD] = "StructColumnField";
map[AST_STRUCT_COLUMN_SCHEMA] = "StructColumnSchema";
map[AST_STRUCT_CONSTRUCTOR_ARG] = "StructConstructorArg";
Expand Down
28 changes: 25 additions & 3 deletions zetasql/parser/parse_tree_manual.h
Original file line number Diff line number Diff line change
Expand Up @@ -4658,15 +4658,15 @@ class ASTDropUserStatement final : public ASTStatement {
NonRecursiveParseTreeVisitor* visitor) const override;
bool is_if_exists() const { return is_if_exists_; }
void set_is_if_exists(bool value) { is_if_exists_ = value; }
const ASTStringLiteral* user_name() const { return user_name_; }
const ASTStringLiteralList* user_name_list() const { return user_name_list_; }

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

const ASTStringLiteral* user_name_ = nullptr;
const ASTStringLiteralList* user_name_list_ = nullptr;
bool is_if_exists_ = false;
};

Expand Down Expand Up @@ -8149,6 +8149,28 @@ class ASTIdentifierList final : public ASTNode {
absl::Span<const ASTIdentifier* const> identifier_list_;
};

class ASTStringLiteralList final : public ASTNode {
public:
static constexpr ASTNodeKind kConcreteNodeKind = AST_STRING_LITERAL_LIST;

ASTStringLiteralList() : ASTNode(kConcreteNodeKind) {}
void Accept(ParseTreeVisitor* visitor, void* data) const override;
zetasql_base::StatusOr<VisitResult> Accept(
NonRecursiveParseTreeVisitor* visitor) const override;

// Guaranteed by the parser to never be empty.
absl::Span<const ASTStringLiteral* const> string_literal_list() const {
return string_literal_list_;
}

private:
void InitFields() final {
FieldLoader fl(this);
fl.AddRestAsRepeated(&string_literal_list_);
}
absl::Span<const ASTStringLiteral* const> string_literal_list_;
};

class ASTVariableDeclaration final : public ASTScriptStatement {
public:
static constexpr ASTNodeKind kConcreteNodeKind = AST_VARIABLE_DECLARATION;
Expand Down
22 changes: 21 additions & 1 deletion zetasql/parser/testdata/drop_user.test
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
drop user 'root';
--
DropUserStatement [0-16]
StringLiteral('root') [10-16]
StringLiteralList [10-16]
StringLiteral('root') [10-16]
--
DROP USER 'root'
==

drop user 'user1', 'user2';
--
DropUserStatement [0-26]
StringLiteralList [10-26]
StringLiteral('user1') [10-17]
StringLiteral('user2') [19-26]
--
DROP USER 'user1', 'user2'
==

drop user if exists 'user1', 'user2';
--
DropUserStatement [0-36]
StringLiteralList [20-36]
StringLiteral('user1') [20-27]
StringLiteral('user2') [29-36]
--
DROP USER IF EXISTS 'user1', 'user2'
==
7 changes: 6 additions & 1 deletion zetasql/parser/unparser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3100,7 +3100,7 @@ void Unparser::visitASTDropUserStatement(const ASTDropUserStatement* node,
print("DROP");
print("USER");
if (node->is_if_exists()) print("IF EXISTS");
node->user_name()->Accept(this, data);
node->user_name_list()->Accept(this, data);
}

void Unparser::visitASTCreateUserStatement(const ASTCreateUserStatement* node,
Expand Down Expand Up @@ -3289,6 +3289,11 @@ void Unparser::visitASTIdentifierList(const ASTIdentifierList* node,
UnparseVectorWithSeparator(node->identifier_list(), data, ", ");
}

void Unparser::visitASTStringLiteralList(const ASTStringLiteralList* node,
void* data) {
UnparseVectorWithSeparator(node->string_literal_list(), data, ", ");
}

void Unparser::visitASTVariableDeclaration(const ASTVariableDeclaration* node,
void* data) {
print("DECLARE");
Expand Down
2 changes: 2 additions & 0 deletions zetasql/parser/unparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,8 @@ class Unparser : public ParseTreeVisitor {
void visitASTBeginEndBlock(const ASTBeginEndBlock* node, void* data) override;
void visitASTIdentifierList(const ASTIdentifierList* node,
void* data) override;
void visitASTStringLiteralList(const ASTStringLiteralList* node,
void* data) override;
void visitASTVariableDeclaration(const ASTVariableDeclaration* node,
void* data) override;
void visitASTParameterAssignment(const ASTParameterAssignment* node,
Expand Down

0 comments on commit 25f3284

Please sign in to comment.