Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  * fix typos and make all expressions have Expression suffix in the node name.
  * add getOperator/setOperator/isOperator for quickly finding out what the operator is
  * add names to operator tokens so they can be returned as GetOperator
  * mark get/is methods const in cpp
  * Add GetFunctionName
  * Add missing LambdaParam node
  * Add IsNegated for all negatable builtins
  • Loading branch information
Sreeni Viswanadha authored and mbasmanova committed Oct 13, 2022
1 parent dc9c276 commit 19f8399
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 60 deletions.
66 changes: 64 additions & 2 deletions parser/cpp/AstNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ namespace parser {
class AstNode;

class AstNode : public SimpleNode {
private:
int op;
bool negated;

public:
Token* beginToken;
Token* endToken;
virtual ~AstNode() {}
AstNode(int id) : SimpleNode(id) {}
AstNode(SqlParser* parser, int id) : SimpleNode(parser, id) {}
int NumChildren() { return jjtGetNumChildren(); }
AstNode* GetChild(int i) { return static_cast<AstNode*>(jjtGetChild(i)); }
int NumChildren() const { return jjtGetNumChildren(); }
AstNode* GetChild(int i) const { return static_cast<AstNode*>(jjtGetChild(i)); }
int Kind() const { return id; }
JJString GetImage() const { return NumChildren() == 0 ? beginToken->image : ""; }
JJString toString(const JJString& prefix) const {
return SimpleNode::toString(prefix) +
string(" (") +
Expand All @@ -35,6 +41,62 @@ class AstNode : public SimpleNode {
std::to_string(endToken->endColumn) +
string(")");
}

// Other utility functions.
bool IsNegatableOperator()
{
switch (Kind()) {
case JJTBETWEEN:
case JJTLIKE:
case JJTINPREDICATE:
case JJTISNULL:
return true;
default:
return false;
}
}

bool IsOperator() const
{
switch (Kind()) {
case JJTADDITIVEEXPRESSION:
case JJTMULTIPLICATIVEEXPRESSION:
case JJTOREXPRESSION:
case JJTANDEXPRESSION:
case JJTNOTEXPRESSION:
case JJTCOMPARISON:
return true;
default:
return false;
}
}

void SetOperator(int op)
{
this->op = op;
}

int GetOperator() const
{
assert(IsOperator());
return op;
}

JJString GetFunctionName() const
{
if (Kind() == JJTFUNCTIONCALL) {
return GetChild(0)->GetImage();
}

if (Kind() == JJTBUILTINFUNCTIONCALL) {
return beginToken->image;
}

return "";
}

void SetNegated(bool negated) { this->negated = negated; }
bool IsNegated() const { return negated; }
};

}
Expand Down
39 changes: 39 additions & 0 deletions parser/cpp/javacc-options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,45 @@ PARSER_BEGIN(SqlParser)
if (astNode->getId() == JJTUNSUPPORTED) {
cout << "Unsupported feature used at: " << t->beginLine << ":" << t->beginColumn << " " << t->image << "\n";
}


if (astNode->IsNegatableOperator()) {
Token* t1 = astNode->GetChild(0)->endToken;

if (astNode->Kind() == JJTISNULL) {
// IsNull -- see if the penultimate token is NOT
while (t1 != null && t1->kind != IS) {
t1 = t1->next;
}

if (t1->next->kind == NOT) {
astNode->SetNegated(true);
}
}
else if (astNode->NumChildren() > 1) {
Token* t2 = astNode->GetChild(1)->beginToken;
while (t1->next != null && t1->next != t2) {
if (t1->kind == NOT) {
astNode->SetNegated(true);
break;
}
t1 = t1->next;
}
}
}
else if (astNode->NumChildren() == 2 && astNode->IsOperator()) {
// Hack locate the token just before the first token of the second operator
Token* t1 = astNode->GetChild(0)->endToken;
Token* t2 = astNode->GetChild(1)->beginToken;
while (t1->next != nullptr && t1->next != t2) {
t1 = t1->next;
}
astNode->SetOperator(t1->kind);
}

if (astNode->NumChildren() == 1 && astNode->IsOperator()) {
astNode->SetOperator(astNode->beginToken->kind);
}
}

PARSER_END(SqlParser)
Expand Down
37 changes: 37 additions & 0 deletions parser/grammar/javacc-options-java.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,43 @@ public class SqlParser {
astNode.beginToken = t0;
}
}

if (astNode.IsNegatableOperator()) {
Token t1 = astNode.GetChild(0).endToken;

if (astNode.Kind() == JJTISNULL) {
// IsNull -- see if the penultimate token is NOT
while (t1 != null && t1.kind != IS) {
t1 = t1.next;
}

if (t1.next.kind == NOT) {
astNode.SetNegated(true);
}
}
else if (astNode.NumChildren() > 1) {
Token t2 = astNode.GetChild(1).beginToken;
while (t1.next != null && t1.next != t2) {
if (t1.kind == NOT) {
astNode.SetNegated(true);
break;
}
t1 = t1.next;
}
}
}
else if (astNode.NumChildren() == 2 && astNode.IsOperator()) {
// Hack locate the token just before the first token of the second operator
Token t1 = astNode.GetChild(0).endToken;
Token t2 = astNode.GetChild(1).beginToken;
while (t1.next != null && t1.next != t2) {
t1 = t1.next;
}
astNode.SetOperator(t1.kind);
}
else if (astNode.NumChildren() == 1 && astNode.IsOperator()) {
astNode.SetOperator(astNode.beginToken.kind);
}
}

public AstNode getResult()
Expand Down
6 changes: 3 additions & 3 deletions parser/grammar/presto-extensions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ void lambda_body() #LambdaBody:
void lambda_params() #LambdaParams:
{}
{
actual_identifier()
| "(" [ actual_identifier() ( "," actual_identifier() )* ] ")"
( actual_identifier() )#LambdaParam(0)
| "(" [ ( actual_identifier() )#LambdaParam(0) ( "," ( actual_identifier() #LambdaParam(0) ) )* ] ")"
}

void if_not_exists():
Expand Down Expand Up @@ -66,7 +66,7 @@ void presto_map_type() #MapType():
void percent_operator():
{}
{
"%"
<PERCENT: "%">
}

void distinct():
Expand Down
38 changes: 19 additions & 19 deletions parser/grammar/sql-spec.txt
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ void when_operand_list():
void when_operand() #WhenOperand:
{}
{
// We push a dummy operand so the binary expressions are buil properly
// We push a dummy operand so the binary expressions are built properly
( { } )#SearchedCaseOperand(true)
(
row_value_predicand()
Expand Down Expand Up @@ -924,7 +924,7 @@ void result():
| ( "NULL" )#NullLiteral
}

void cast_specification() #CastEpression:
void cast_specification() #CastExpression:
{}
{
"CAST" "(" cast_operand() "AS" cast_target() ")"
Expand Down Expand Up @@ -1093,8 +1093,8 @@ void numeric_value_expression():
{
term()
(
{ PushNode(PopNode()); } "+" term() #Add(2)
| { PushNode(PopNode()); } "-" term() #Subtract(2)
{ PushNode(PopNode()); } <PLUS: "+"> term() #AdditiveExpression(2)
| { PushNode(PopNode()); } <MINUS: "-"> term() #AdditiveExpression(2)
)*
}

Expand All @@ -1103,9 +1103,9 @@ void term():
{
factor()
(
{ PushNode(PopNode()); } "*" factor() #Multiply(2)
| { PushNode(PopNode()); } "/" factor() #Divide(2)
| { PushNode(PopNode()); } percent_operator() factor() #Mod(2)
{ PushNode(PopNode()); } <STAR: "*"> factor() #MultiplicativeExpression(2)
| { PushNode(PopNode()); } <DIV: "/"> factor() #MultiplicativeExpression(2)
| { PushNode(PopNode()); } percent_operator() factor() #MultiplicativeExpression(2)
)*
}

Expand Down Expand Up @@ -1565,8 +1565,8 @@ void binary_overlay_function() #Unsupported:
void datetime_value_expression():
{}
{
( datetime_term() ) // [ ( "+" | "-" ) interval_term() ] )#AdditiveEpression(>1)
| ( interval_value_expression() ) //[ "+" datetime_term() ] )#AdditiveEpression(>1)
( datetime_term() ) // [ ( "+" | "-" ) interval_term() ] )#AdditiveExpression(>1)
| ( interval_value_expression() ) //[ "+" datetime_term() ] )#AdditiveExpression(>1)
}

void datetime_term():
Expand Down Expand Up @@ -1646,9 +1646,9 @@ void current_local_timestamp_value_function() #Unsupported:
void interval_value_expression():
{}
{
( interval_term() ) //[ ( "+" | "-" ) interval_term() ] ) #AdditiveEpression(>1)
( interval_term() ) //[ ( "+" | "-" ) interval_term() ] ) #AdditiveExpression(>1)
| LOOKAHEAD("(" datetime_value_expression() "-" datetime_term() ")" )
( "(" datetime_value_expression() "-" datetime_term() ")" )#AdditiveEpression
( "(" datetime_value_expression() "-" datetime_term() ")" )#AdditiveExpression
interval_qualifier()
}

Expand Down Expand Up @@ -1691,7 +1691,7 @@ void interval_absolute_value_function():
void boolean_value_expression() #OrExpression(>1):
{}
{
boolean_term() ( "OR" boolean_term() )*
boolean_term() ( "OR" boolean_term() )*
}

void boolean_term() #AndExpression(>1):
Expand Down Expand Up @@ -2772,13 +2772,13 @@ void comparison_predicate_part_2() #Comparison(2):
void comp_op():
{}
{
"="
| "<>"
| "<"
| ">"
| "<="
| ">="
| "!=" // Non-standard
<EQUAL: "=">
| <NOT_EQUAL: "<>">
| <LESS_THAN: "<">
| <GREATER_THAN: ">">
| <LESS_THAN_OR_EQUAL: "<=">
| <GREATER_THAN_OR_EQUAL: ">=">
| <NOT_EQUAL_2: "!="> // Non-standard
}

void between_predicate():
Expand Down
83 changes: 83 additions & 0 deletions parser/src/main/java/com/facebook/coresql/parser/AstNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,26 @@
*/
package com.facebook.coresql.parser;

import static com.facebook.coresql.parser.SqlParserTreeConstants.JJTADDITIVEEXPRESSION;
import static com.facebook.coresql.parser.SqlParserTreeConstants.JJTANDEXPRESSION;
import static com.facebook.coresql.parser.SqlParserTreeConstants.JJTBETWEEN;
import static com.facebook.coresql.parser.SqlParserTreeConstants.JJTBUILTINFUNCTIONCALL;
import static com.facebook.coresql.parser.SqlParserTreeConstants.JJTCOMPARISON;
import static com.facebook.coresql.parser.SqlParserTreeConstants.JJTFUNCTIONCALL;
import static com.facebook.coresql.parser.SqlParserTreeConstants.JJTINPREDICATE;
import static com.facebook.coresql.parser.SqlParserTreeConstants.JJTISNULL;
import static com.facebook.coresql.parser.SqlParserTreeConstants.JJTLIKE;
import static com.facebook.coresql.parser.SqlParserTreeConstants.JJTMULTIPLICATIVEEXPRESSION;
import static com.facebook.coresql.parser.SqlParserTreeConstants.JJTNOTEXPRESSION;
import static com.facebook.coresql.parser.SqlParserTreeConstants.JJTOREXPRESSION;

public class AstNode
extends com.facebook.coresql.parser.SimpleNode
{
public Token beginToken;
public Token endToken;
private int operator;
private boolean negated;

protected AstNode(int id)
{
Expand Down Expand Up @@ -91,4 +106,72 @@ public String toString(String prefix)
return super.toString(prefix) + " (" + getLocation().toString() + ")" +
(NumChildren() == 0 ? " (" + beginToken.image + ")" : "");
}

// Other utility methods.

public boolean IsNegatableOperator()
{
switch (Kind()) {
case JJTBETWEEN:
case JJTLIKE:
case JJTINPREDICATE:
case JJTISNULL:
return true;
default:
return false;
}
}

public boolean IsOperator()
{
switch (Kind()) {
case JJTADDITIVEEXPRESSION:
case JJTMULTIPLICATIVEEXPRESSION:
case JJTOREXPRESSION:
case JJTANDEXPRESSION:
case JJTNOTEXPRESSION:
case JJTCOMPARISON:
// Other binary negatable operators
return true;
default:
return false;
}
}

public void SetOperator(int operator)
{
this.operator = operator;
}

public int GetOperator()
{
if (!IsOperator()) {
throw new IllegalArgumentException("GetOperator cannot be called on: " + this);
}

return operator;
}

public String GetFunctionName()
{
if (Kind() == JJTFUNCTIONCALL) {
return GetChild(0).GetImage();
}

if (Kind() == JJTBUILTINFUNCTIONCALL) {
return beginToken.image;
}

return null;
}

public void SetNegated(boolean negated)
{
this.negated = negated;
}

public boolean IsNegated()
{
return negated;
}
}
Loading

0 comments on commit 19f8399

Please sign in to comment.