From 25f328452bc6187de14afb8ec0b4f7504d67997b Mon Sep 17 00:00:00 2001 From: denglong Date: Thu, 7 Dec 2023 18:38:49 +0800 Subject: [PATCH] test: add drop user case --- zetasql/parser/ast_node_kind.h | 1 + zetasql/parser/bison_parser.y | 14 ++++++++++++- zetasql/parser/parse_tree.cc | 1 + zetasql/parser/parse_tree_manual.h | 28 +++++++++++++++++++++++--- zetasql/parser/testdata/drop_user.test | 22 +++++++++++++++++++- zetasql/parser/unparser.cc | 7 ++++++- zetasql/parser/unparser.h | 2 ++ 7 files changed, 69 insertions(+), 6 deletions(-) diff --git a/zetasql/parser/ast_node_kind.h b/zetasql/parser/ast_node_kind.h index fd82b58cd..6e5b71457 100755 --- a/zetasql/parser/ast_node_kind.h +++ b/zetasql/parser/ast_node_kind.h @@ -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, diff --git a/zetasql/parser/bison_parser.y b/zetasql/parser/bison_parser.y index 9eaed28ba..111acdf0f 100644 --- a/zetasql/parser/bison_parser.y +++ b/zetasql/parser/bison_parser.y @@ -1110,6 +1110,7 @@ using zetasql::ASTDropStatement; %type variable_declaration %type opt_default_expression %type identifier_list +%type string_literal_list %type path_expression_list %type path_expression_list_with_opt_parens %type set_statement @@ -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); @@ -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 { diff --git a/zetasql/parser/parse_tree.cc b/zetasql/parser/parse_tree.cc index 327a694fa..e79f47468 100644 --- a/zetasql/parser/parse_tree.cc +++ b/zetasql/parser/parse_tree.cc @@ -303,6 +303,7 @@ static absl::flat_hash_map 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"; diff --git a/zetasql/parser/parse_tree_manual.h b/zetasql/parser/parse_tree_manual.h index c69cb309f..b9bdf5023 100644 --- a/zetasql/parser/parse_tree_manual.h +++ b/zetasql/parser/parse_tree_manual.h @@ -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; }; @@ -8149,6 +8149,28 @@ class ASTIdentifierList final : public ASTNode { absl::Span 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 Accept( + NonRecursiveParseTreeVisitor* visitor) const override; + + // Guaranteed by the parser to never be empty. + absl::Span string_literal_list() const { + return string_literal_list_; + } + + private: + void InitFields() final { + FieldLoader fl(this); + fl.AddRestAsRepeated(&string_literal_list_); + } + absl::Span string_literal_list_; +}; + class ASTVariableDeclaration final : public ASTScriptStatement { public: static constexpr ASTNodeKind kConcreteNodeKind = AST_VARIABLE_DECLARATION; diff --git a/zetasql/parser/testdata/drop_user.test b/zetasql/parser/testdata/drop_user.test index 3d8727965..0911d5b5b 100644 --- a/zetasql/parser/testdata/drop_user.test +++ b/zetasql/parser/testdata/drop_user.test @@ -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' +== diff --git a/zetasql/parser/unparser.cc b/zetasql/parser/unparser.cc index 2ee459585..31715a607 100644 --- a/zetasql/parser/unparser.cc +++ b/zetasql/parser/unparser.cc @@ -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, @@ -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"); diff --git a/zetasql/parser/unparser.h b/zetasql/parser/unparser.h index b9126674a..f2992e363 100644 --- a/zetasql/parser/unparser.h +++ b/zetasql/parser/unparser.h @@ -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,