Skip to content

Commit

Permalink
feat(alter table): alter table .. add/drop PATH <string literal> (#49)
Browse files Browse the repository at this point in the history
Signed-off-by: aceforeverd <[email protected]>
  • Loading branch information
aceforeverd authored May 16, 2023
1 parent 8605f2f commit ca3cf22
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 10 deletions.
4 changes: 3 additions & 1 deletion zetasql/parser/ast_node_kind.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,9 @@ enum ASTNodeKind {
AST_WINDOW_ATTRIBUTE_LIST,
AST_LIKE_TABLE_CLAUSE,
AST_EXIT_STATEMENT,
kLastASTNodeKind = AST_LIKE_TABLE_CLAUSE,
AST_ADD_PATH_ACTION,
AST_DROP_PATH_ACTION,
kLastASTNodeKind = AST_DROP_PATH_ACTION,
};

} // namespace zetasql
Expand Down
12 changes: 12 additions & 0 deletions zetasql/parser/bison_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@ using zetasql::ASTDropStatement;
%token KW_OUTFILE "OUTFILE"
%token KW_PARQUET "PARQUET"
%token KW_HIVE "HIVE"
%token KW_PATH "PATH"
%token KW_PERCENT "PERCENT"
%token KW_PIVOT "PIVOT"
%token KW_POLICIES "POLICIES"
Expand Down Expand Up @@ -1672,6 +1673,16 @@ alter_action:
node->set_is_if_exists($3);
$$ = node;
}
| "ADD" "PATH" string_literal
{
auto* node = MAKE_NODE(ASTAddPathAction, @$, {$3});
$$ = node;
}
| "DROP" "PATH" string_literal
{
auto* node = MAKE_NODE(ASTDropPathAction, @$, {$3});
$$ = node;
}
| "ADD" "COLUMN" opt_if_not_exists table_column_definition
opt_column_position opt_fill_using_expression
{
Expand Down Expand Up @@ -7567,6 +7578,7 @@ keyword_as_identifier:
| "OUTFILE"
| "PARQUET"
| "HIVE"
| "PATH"
| "PERCENT"
| "PIVOT"
| "POLICIES"
Expand Down
3 changes: 2 additions & 1 deletion zetasql/parser/flex_tokenizer.l
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ groups { return BisonParserImpl::token::KW_GROUPS; }
hash { return BisonParserImpl::token::KW_HASH; }
having { return BisonParserImpl::token::KW_HAVING; }
hidden { return BisonParserImpl::token::KW_HIDDEN; }
hive { return BisonParserImpl::token::KW_HIVE; }
if { return BisonParserImpl::token::KW_IF; }
ignore { return BisonParserImpl::token::KW_IGNORE; }
immediate { return BisonParserImpl::token::KW_IMMEDIATE; }
Expand Down Expand Up @@ -571,8 +572,8 @@ 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; }
path { return BisonParserImpl::token::KW_PATH; }
percent { return BisonParserImpl::token::KW_PERCENT; }
policies { return BisonParserImpl::token::KW_POLICIES; }
policy { return BisonParserImpl::token::KW_POLICY; }
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},
{"path", KW_PATH},
{"hive", KW_HIVE},
{"partition", KW_PARTITION, KeywordInfo::kReserved},
{"percent", KW_PERCENT},
Expand Down
10 changes: 10 additions & 0 deletions zetasql/parser/parse_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ static absl::flat_hash_map<ASTNodeKind, std::string> CreateNodeNamesMap() {
map[AST_WINDOW_ATTRIBUTE_INST_NOT_IN_WINDOW] = "WindowAttributeInstNotInWindow";
map[AST_WINDOW_ATTRIBUTE_LIST] = "WindowAttributeList";
map[AST_LIKE_TABLE_CLAUSE] = "LikeTableClause";
map[AST_ADD_PATH_ACTION] = "AddPathAction";
map[AST_DROP_PATH_ACTION] = "DropPathAction";
for (int kind = kFirstASTNodeKind; kind <= kLastASTNodeKind;
++kind) {
ZETASQL_DCHECK(zetasql_base::ContainsKey(map, static_cast<ASTNodeKind>(kind)))
Expand Down Expand Up @@ -1543,6 +1545,14 @@ std::string ASTAddColumnAction::GetSQLForAlterAction() const {
return "ADD COLUMN";
}

std::string ASTAddPathAction::GetSQLForAlterAction() const {
return "ADD PATH";
}

std::string ASTDropPathAction::GetSQLForAlterAction() const {
return "DROP PATH";
}

std::string ASTColumnPosition::SingleNodeDebugString() const {
return absl::StrCat(ASTNode::SingleNodeDebugString(),
type() == PRECEDING ? "(PRECEDING)" : "(FOLLOWING)");
Expand Down
46 changes: 46 additions & 0 deletions zetasql/parser/parse_tree_manual.h
Original file line number Diff line number Diff line change
Expand Up @@ -7196,6 +7196,52 @@ class ASTAlterConstraintSetOptionsAction final : public ASTAlterAction {
bool is_if_exists_ = false;
};

// ALTER table action for "ADD PATH" clause
class ASTAddPathAction final : public ASTAlterAction {
public:
static constexpr ASTNodeKind kConcreteNodeKind = AST_ADD_PATH_ACTION;

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

std::string GetSQLForAlterAction() const override;

auto path() const { return path_; }

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

const ASTStringLiteral* path_ = nullptr;
};

// ALTER table action for "DROP PATH" clause
class ASTDropPathAction final : public ASTAlterAction {
public:
static constexpr ASTNodeKind kConcreteNodeKind = AST_DROP_PATH_ACTION;

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

std::string GetSQLForAlterAction() const override;

auto path() const { return path_; }

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

const ASTStringLiteral* path_ = nullptr;
};

// ALTER table action for "ADD COLUMN" clause
class ASTAddColumnAction final : public ASTAlterAction {
public:
Expand Down
10 changes: 5 additions & 5 deletions zetasql/parser/testdata/alter_set_options.test
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,31 @@ ALTER {{DATABASE|SCHEMA|TABLE|VIEW|MATERIALIZED VIEW}} foo drop;
--
ALTERNATION GROUP: DATABASE
--
ERROR: Syntax error: Expected keyword COLUMN or keyword CONSTRAINT but got ";" [at 1:24]
ERROR: Syntax error: Expected keyword COLUMN or keyword CONSTRAINT or keyword PATH but got ";" [at 1:24]
ALTER DATABASE foo drop;
^
--
ALTERNATION GROUP: SCHEMA
--
ERROR: Syntax error: Expected keyword COLUMN or keyword CONSTRAINT but got ";" [at 1:22]
ERROR: Syntax error: Expected keyword COLUMN or keyword CONSTRAINT or keyword PATH but got ";" [at 1:22]
ALTER SCHEMA foo drop;
^
--
ALTERNATION GROUP: TABLE
--
ERROR: Syntax error: Expected keyword COLUMN or keyword CONSTRAINT but got ";" [at 1:21]
ERROR: Syntax error: Expected keyword COLUMN or keyword CONSTRAINT or keyword PATH but got ";" [at 1:21]
ALTER TABLE foo drop;
^
--
ALTERNATION GROUP: VIEW
--
ERROR: Syntax error: Expected keyword COLUMN or keyword CONSTRAINT but got ";" [at 1:20]
ERROR: Syntax error: Expected keyword COLUMN or keyword CONSTRAINT or keyword PATH but got ";" [at 1:20]
ALTER VIEW foo drop;
^
--
ALTERNATION GROUP: MATERIALIZED VIEW
--
ERROR: Syntax error: Expected keyword COLUMN or keyword CONSTRAINT but got ";" [at 1:33]
ERROR: Syntax error: Expected keyword COLUMN or keyword CONSTRAINT or keyword PATH but got ";" [at 1:33]
ALTER MATERIALIZED VIEW foo drop;
^
==
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
alter table foo.bar add;
--
ERROR: Syntax error: Expected keyword CHECK or keyword COLUMN or keyword CONSTRAINT or keyword FOREIGN but got ";" [at 1:24]
ERROR: Syntax error: Unexpected ";" [at 1:24]
alter table foo.bar add;
^

Expand Down
2 changes: 1 addition & 1 deletion zetasql/parser/testdata/alter_table_drop_column.test
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
alter table foo.bar drop;
--
ERROR: Syntax error: Expected keyword COLUMN or keyword CONSTRAINT but got ";" [at 1:25]
ERROR: Syntax error: Expected keyword COLUMN or keyword CONSTRAINT or keyword PATH but got ";" [at 1:25]
alter table foo.bar drop;
^

Expand Down
2 changes: 1 addition & 1 deletion zetasql/parser/testdata/alter_table_drop_constraint.test
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
alter table foo.bar drop;
--
ERROR: Syntax error: Expected keyword COLUMN or keyword CONSTRAINT but got ";" [at 1:25]
ERROR: Syntax error: Expected keyword COLUMN or keyword CONSTRAINT or keyword PATH but got ";" [at 1:25]
alter table foo.bar drop;
^

Expand Down
25 changes: 25 additions & 0 deletions zetasql/parser/testdata/alter_table_path.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ALTER TABLE to add column.
ALTER TABLE foo ADD PATH 'this is a path';
--
AlterTableStatement [0-41]
PathExpression [12-15]
Identifier(foo) [12-15]
AlterActionList [16-41]
AddPathAction [16-41]
StringLiteral('this is a path') [25-41]
--
ALTER TABLE foo ADD PATH 'this is a path'
==

# ALTER TABLE to drop column.
ALTER TABLE foo drop PATH 'nothing';
--
AlterTableStatement [0-35]
PathExpression [12-15]
Identifier(foo) [12-15]
AlterActionList [16-35]
DropPathAction [16-35]
StringLiteral('nothing') [26-35]
--
ALTER TABLE foo DROP PATH 'nothing'
==
11 changes: 11 additions & 0 deletions zetasql/parser/unparser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3493,5 +3493,16 @@ void Unparser::visitASTRaiseStatement(const ASTRaiseStatement* node,
}
}

void Unparser::visitASTAddPathAction(const ASTAddPathAction *node, void *data) {
print("ADD PATH");
node->path()->Accept(this, data);
}

void Unparser::visitASTDropPathAction(const ASTDropPathAction *node,
void *data) {
print("DROP PATH");
node->path()->Accept(this, data);
}

} // namespace parser
} // namespace zetasql
4 changes: 4 additions & 0 deletions zetasql/parser/unparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,10 @@ class Unparser : public ParseTreeVisitor {
void visitASTRaiseStatement(const ASTRaiseStatement* node,
void* data) override;

void visitASTAddPathAction(const ASTAddPathAction *node, void *data) override;
void visitASTDropPathAction(const ASTDropPathAction *node,
void *data) override;

protected:
// Set break_line to true if you want to print each child on a separate line.
virtual void UnparseChildrenWithSeparator(const ASTNode* node, void* data,
Expand Down

0 comments on commit ca3cf22

Please sign in to comment.