Skip to content

Commit

Permalink
feat(parser): create table like hive (#47)
Browse files Browse the repository at this point in the history
* feat(parser): create table like hive

* fix: missing }

* fix table_like_type
  • Loading branch information
zhanghaohit authored Jan 12, 2023
1 parent 057e680 commit 88b6989
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 3 deletions.
21 changes: 19 additions & 2 deletions zetasql/parser/bison_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -1452,6 +1454,7 @@ using zetasql::ASTDropStatement;
%type <null_handling_modifier> opt_null_handling_modifier
%type <frame_unit> frame_unit
%type <templated_parameter_kind> templated_parameter_kind
%type <table_like_kind> table_like_kind

%type <foreign_key_match> opt_foreign_key_match
%type <foreign_key_match> foreign_key_match_mode
Expand Down Expand Up @@ -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; }
;
Expand Down Expand Up @@ -7546,6 +7562,7 @@ keyword_as_identifier:
| "OUT"
| "OUTFILE"
| "PARQUET"
| "HIVE"
| "PERCENT"
| "PIVOT"
| "POLICIES"
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 @@ -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; }
Expand Down
1 change: 1 addition & 0 deletions zetasql/parser/keywords.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
4 changes: 4 additions & 0 deletions zetasql/parser/parse_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion zetasql/parser/parse_tree_manual.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions zetasql/parser/testdata/create_table.test
Original file line number Diff line number Diff line change
Expand Up @@ -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';
--
Expand Down
4 changes: 4 additions & 0 deletions zetasql/parser/unparser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2248,6 +2248,10 @@ void Unparser::visitASTLikeTableClause(const ASTLikeTableClause *node,
print("PARQUET");
break;
}
case ASTLikeTableClause::HIVE: {
print("HIVE");
break;
}
}
node->path()->Accept(this, data);
}
Expand Down

0 comments on commit 88b6989

Please sign in to comment.