From 88b698942609fdb1cf8ead05f16364761e9382a2 Mon Sep 17 00:00:00 2001 From: ZHANG Hao Date: Thu, 12 Jan 2023 16:51:57 +0800 Subject: [PATCH] feat(parser): create table like hive (#47) * feat(parser): create table like hive * fix: missing } * fix table_like_type --- zetasql/parser/bison_parser.y | 21 ++++++++++++++++-- zetasql/parser/flex_tokenizer.l | 1 + zetasql/parser/keywords.cc | 1 + zetasql/parser/parse_tree.cc | 4 ++++ zetasql/parser/parse_tree_manual.h | 2 +- zetasql/parser/testdata/create_table.test | 27 +++++++++++++++++++++++ zetasql/parser/unparser.cc | 4 ++++ 7 files changed, 57 insertions(+), 3 deletions(-) diff --git a/zetasql/parser/bison_parser.y b/zetasql/parser/bison_parser.y index 89238d8f6..ad0f0aa6b 100644 --- a/zetasql/parser/bison_parser.y +++ b/zetasql/parser/bison_parser.y @@ -475,6 +475,7 @@ class DashedIdentifierTmpNode final : public zetasql::ASTNode { zetasql::ASTExpression* default_expression; zetasql::ASTGeneratedColumnInfo* generated_column_info; } generated_or_default_column_info; + zetasql::ASTLikeTableClause::TableKind table_like_kind; } // YYEOF is a special token used to indicate the end of the input. It's alias // defaults to "end of file", but "end of input" is more appropriate for us. @@ -882,6 +883,7 @@ using zetasql::ASTDropStatement; %token KW_OUT "OUT" %token KW_OUTFILE "OUTFILE" %token KW_PARQUET "PARQUET" +%token KW_HIVE "HIVE" %token KW_PERCENT "PERCENT" %token KW_PIVOT "PIVOT" %token KW_POLICIES "POLICIES" @@ -1452,6 +1454,7 @@ using zetasql::ASTDropStatement; %type opt_null_handling_modifier %type frame_unit %type templated_parameter_kind +%type table_like_kind %type opt_foreign_key_match %type foreign_key_match_mode @@ -3654,14 +3657,27 @@ opt_like_path_expression: | /* Nothing */ { $$ = nullptr; } ; +table_like_kind: + "PARQUET" + { + $$ = zetasql::ASTLikeTableClause::PARQUET; + } + | "HIVE" + { + $$ = zetasql::ASTLikeTableClause::HIVE; + } + ; + opt_like_in_create_table: "LIKE" maybe_dashed_path_expression { $$ = $2; } - | "LIKE" "PARQUET" string_literal + | "LIKE" table_like_kind string_literal { - $$ = MAKE_NODE(ASTLikeTableClause, @$, {$3}); + auto* like_table_clause = MAKE_NODE(ASTLikeTableClause, @$, {$3}); + like_table_clause->set_kind($2); + $$ = like_table_clause; } | /* Nothing */ { $$ = nullptr; } ; @@ -7546,6 +7562,7 @@ keyword_as_identifier: | "OUT" | "OUTFILE" | "PARQUET" + | "HIVE" | "PERCENT" | "PIVOT" | "POLICIES" diff --git a/zetasql/parser/flex_tokenizer.l b/zetasql/parser/flex_tokenizer.l index f790fe4c3..a2fd61c8b 100644 --- a/zetasql/parser/flex_tokenizer.l +++ b/zetasql/parser/flex_tokenizer.l @@ -571,6 +571,7 @@ outfile { return BisonParserImpl::token::KW_OUTFILE; }; outer { return BisonParserImpl::token::KW_OUTER; } over { return BisonParserImpl::token::KW_OVER; } parquet { return BisonParserImpl::token::KW_PARQUET; } +hive { return BisonParserImpl::token::KW_HIVE; } partition { return BisonParserImpl::token::KW_PARTITION; } percent { return BisonParserImpl::token::KW_PERCENT; } policies { return BisonParserImpl::token::KW_POLICIES; } diff --git a/zetasql/parser/keywords.cc b/zetasql/parser/keywords.cc index 1350b844e..1d02e006d 100644 --- a/zetasql/parser/keywords.cc +++ b/zetasql/parser/keywords.cc @@ -226,6 +226,7 @@ constexpr KeywordInfoPOD kAllKeywords[] = { {"outer", KW_OUTER, KeywordInfo::kReserved}, {"over", KW_OVER, KeywordInfo::kReserved}, {"parquet", KW_PARQUET}, + {"hive", KW_HIVE}, {"partition", KW_PARTITION, KeywordInfo::kReserved}, {"percent", KW_PERCENT}, {"pivot", KW_PIVOT}, diff --git a/zetasql/parser/parse_tree.cc b/zetasql/parser/parse_tree.cc index 5f10d4b7d..384a75159 100644 --- a/zetasql/parser/parse_tree.cc +++ b/zetasql/parser/parse_tree.cc @@ -1693,6 +1693,10 @@ std::string ASTLikeTableClause::SingleNodeDebugString() const { switch (kind()) { case PARQUET: absl::StrAppend(&result, "(PARQUET)"); + break; + case HIVE: + absl::StrAppend(&result, "(HIVE)"); + break; } return result; } diff --git a/zetasql/parser/parse_tree_manual.h b/zetasql/parser/parse_tree_manual.h index 18c74287d..3d19135f8 100644 --- a/zetasql/parser/parse_tree_manual.h +++ b/zetasql/parser/parse_tree_manual.h @@ -4591,7 +4591,7 @@ class ASTCreateTableFunctionStatement final : public ASTCreateFunctionStmtBase { class ASTLikeTableClause final : public ASTNode { public: static constexpr ASTNodeKind kConcreteNodeKind = AST_LIKE_TABLE_CLAUSE; - enum TableKind { PARQUET }; + enum TableKind { PARQUET, HIVE }; ASTLikeTableClause() : ASTNode(kConcreteNodeKind) {} void Accept(ParseTreeVisitor* visitor, void* data) const override; diff --git a/zetasql/parser/testdata/create_table.test b/zetasql/parser/testdata/create_table.test index 219127d6c..11ee082e6 100644 --- a/zetasql/parser/testdata/create_table.test +++ b/zetasql/parser/testdata/create_table.test @@ -2803,6 +2803,33 @@ CREATE TABLE t LIKE PARQUET == +# CREATE TABLE LIKE HIVE '...' +create table t LIKE HIVE 'hdfs://path'; +-- +CreateTableStatement [0-38] + PathExpression [13-14] + Identifier(t) [13-14] + LikeTableClause(HIVE) [15-38] + StringLiteral('hdfs://path') [25-38] +-- +CREATE TABLE t LIKE +HIVE 'hdfs://path' +== + +# CREATE TABLE LIKE HIVE, 'HIVE' become like table name +create table t LIKE HIVE; +-- +CreateTableStatement [0-24] + PathExpression [13-14] + Identifier(t) [13-14] + PathExpression [20-24] + Identifier(HIVE) [20-24] +-- +CREATE TABLE t LIKE +HIVE +== + + # ERROR: CREATE TABLE LIKE PARXX '...' create table t LIKE PARXX 'hdfs://path'; -- diff --git a/zetasql/parser/unparser.cc b/zetasql/parser/unparser.cc index 1bd23bef5..c17543fc5 100644 --- a/zetasql/parser/unparser.cc +++ b/zetasql/parser/unparser.cc @@ -2248,6 +2248,10 @@ void Unparser::visitASTLikeTableClause(const ASTLikeTableClause *node, print("PARQUET"); break; } + case ASTLikeTableClause::HIVE: { + print("HIVE"); + break; + } } node->path()->Accept(this, data); }