Skip to content

Commit

Permalink
feat: support XOR and C stype equal op
Browse files Browse the repository at this point in the history
  • Loading branch information
jingchen2222 committed Jun 9, 2021
1 parent 3676aae commit 86416f4
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 6 deletions.
6 changes: 6 additions & 0 deletions zetasql/parser/bison_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ class BisonParser {
*ast_nodes = std::move(*allocated_ast_nodes_);
}

void InitAllocatedASTNodes() {
for (const auto& ast_node : *allocated_ast_nodes_) {
ast_node->InitFields();
}
}

private:
// Identifiers and literal values are allocated from this arena. Not owned.
// Only valid during Parse().
Expand Down
22 changes: 21 additions & 1 deletion zetasql/parser/bison_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ class DashedIdentifierTmpNode final : public zetasql::ASTNode {
%token '=' "="
%token KW_NOT_EQUALS_C_STYLE "!="
%token KW_NOT_EQUALS_SQL_STYLE "<>"
%token KW_EQUALS_C_STYLE "=="
%token '<' "<"
%token KW_LESS_EQUALS "<="
%token '>' ">"
Expand Down Expand Up @@ -553,8 +554,9 @@ class DashedIdentifierTmpNode final : public zetasql::ASTNode {
%left ".*"
%left "OR"
%left "AND"
%left "XOR"
%left UNARY_NOT_PRECEDENCE
%nonassoc "=" "<>" ">" "<" ">=" "<=" "!=" "LIKE" "IN" "DISTINCT" "BETWEEN" "IS" "NOT_SPECIAL"
%nonassoc "=" "==" "<>" ">" "<" ">=" "<=" "!=" "LIKE" "IN" "DISTINCT" "BETWEEN" "IS" "NOT_SPECIAL"
%left "|"
%left "^"
%left "&"
Expand Down Expand Up @@ -750,6 +752,7 @@ using zetasql::ASTDropStatement;
%token KW_WINDOW "WINDOW"
%token KW_WITH "WITH"
%token KW_UNNEST "UNNEST"
%token KW_XOR "XOR"

// These keywords may not be used in the grammar currently but are reserved
// for future use.
Expand Down Expand Up @@ -5213,6 +5216,7 @@ unnest_expression_with_opt_alias_and_offset:
// This rule returns the JavaCC operator id for the operator.
comparative_operator:
"=" { $$ = zetasql::ASTBinaryExpression::EQ; }
| "==" { $$ = zetasql::ASTBinaryExpression::EQ;}
| "!=" { $$ = zetasql::ASTBinaryExpression::NE; }
| "<>" { $$ = zetasql::ASTBinaryExpression::NE2; }
| "<" { $$ = zetasql::ASTBinaryExpression::LT; }
Expand Down Expand Up @@ -5393,6 +5397,21 @@ expression:
$$ = MAKE_NODE(ASTDotIdentifier, @2, @3, {$1, $3});
}
}
| expression "XOR" expression %prec "XOR"
{
// NOT has lower precedence but can be parsed unparenthesized in the
// rhs because it is not ambiguous. This is not allowed. Other
// expressions with lower precedence wouldn't be parsed as children, so
// we don't have to check for those.
if (IsUnparenthesizedNotExpression($3)) {
YYERROR_UNEXPECTED_AND_ABORT_AT(@3);
}
auto* binary_expression =
MAKE_NODE(ASTBinaryExpression, @1, @3, {$1, $3});
binary_expression->set_op(
zetasql::ASTBinaryExpression::XOR);
$$ = binary_expression;
}
| expression "OR" expression %prec "OR"
{
if ($1->node_kind() == zetasql::AST_OR_EXPR &&
Expand Down Expand Up @@ -7166,6 +7185,7 @@ reserved_keyword_rule:
| "SOME"
| "TREAT"
| "WITHIN"
| "XOR"
// END_RESERVED_KEYWORD_RULE -- Do not remove this!
;

Expand Down
4 changes: 4 additions & 0 deletions zetasql/parser/flex_tokenizer.l
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ window { return BisonParserImpl::token::KW_WINDOW; }
with { return BisonParserImpl::token::KW_WITH; }
within { return BisonParserImpl::token::KW_WITHIN; }
write { return BisonParserImpl::token::KW_WRITE; }
xor { return BisonParserImpl::token::KW_XOR; }
zone { return BisonParserImpl::token::KW_ZONE; }
/* END_KEYWORDS -- Do not remove! */

Expand Down Expand Up @@ -833,6 +834,9 @@ zone { return BisonParserImpl::token::KW_ZONE; }
"!=" {
return BisonParserImpl::token::KW_NOT_EQUALS_C_STYLE;
}
"==" {
return BisonParserImpl::token::KW_EQUALS_C_STYLE;
}

"<=" { return BisonParserImpl::token::KW_LESS_EQUALS; }
/* Don't recognize these in ARRAY<> or STRUCT<> context. */
Expand Down
1 change: 1 addition & 0 deletions zetasql/parser/keywords.cc
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ constexpr KeywordInfoPOD kAllKeywords[] = {
{"with", KW_WITH, KeywordInfo::kReserved},
{"within", KW_WITHIN, KeywordInfo::kReserved},
{"write", KW_WRITE},
{"xor", KW_XOR, KeywordInfo::kReserved},
{"zone", KW_ZONE},
};

Expand Down
2 changes: 1 addition & 1 deletion zetasql/parser/keywords_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ TEST(ParserTest, DontAddNewReservedKeywords) {
// allows new queries to work that will not work on older code.
// Before changing this, co-ordinate with all engines to make sure the change
// is done safely.
EXPECT_EQ(102 /* CAUTION */, num_reserved);
EXPECT_EQ(103 /* CAUTION */, num_reserved);
}

} // namespace
Expand Down
2 changes: 2 additions & 0 deletions zetasql/parser/parse_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,8 @@ std::string ASTBinaryExpression::GetSQLForOperator() const {
return "%";
case CONCAT_OP:
return "||";
case XOR:
return "XOR";
case DISTINCT:
return is_not_ ? "IS NOT DISTINCT FROM" : "IS DISTINCT FROM";
}
Expand Down
1 change: 1 addition & 0 deletions zetasql/parser/parse_tree_manual.h
Original file line number Diff line number Diff line change
Expand Up @@ -1841,6 +1841,7 @@ class ASTBinaryExpression final : public ASTExpression {
IDIVIDE, // "div", integer division
MOD, // "%"
CONCAT_OP, // "||"
XOR, // "XOR"
DISTINCT, // "IS DISTINCT FROM"
};

Expand Down
Loading

0 comments on commit 86416f4

Please sign in to comment.