Skip to content

Commit

Permalink
new parser, building query rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
luigidellaquila committed Feb 6, 2015
1 parent f5f033b commit 46119a3
Show file tree
Hide file tree
Showing 19 changed files with 1,381 additions and 1,009 deletions.
211 changes: 126 additions & 85 deletions core/src/main/grammar/OrientSQL.jjt
Expand Up @@ -64,7 +64,6 @@ SKIP :
}



/* reserved words */
TOKEN:
{
Expand Down Expand Up @@ -972,11 +971,14 @@ void ArraySingleValuesSelector():
OArrayRangeSelector ArrayRangeSelector():
{}
{
<INTEGER_RANGE>
|
(
ArrayNumberSelector() <RANGE> ArrayNumberSelector()
<INTEGER_RANGE>
|
(
ArrayNumberSelector() <RANGE> ArrayNumberSelector()
)
)
{ return jjtThis; }
}


Expand Down Expand Up @@ -1078,6 +1080,7 @@ OModifier Modifier():
<RBRACKET>
)
|
LOOKAHEAD( MethodCall() )
MethodCall()
|
<DOT> SuffixIdentifier()
Expand All @@ -1097,9 +1100,9 @@ OExpression Expression():
<NULL> {jjtThis.value = null;}
|
LOOKAHEAD(2)
token = <STRING_LITERAL> {jjtThis.value = token.image;}
token = <STRING_LITERAL> {jjtThis.value = token.image.substring(1, token.image.length() - 1);}
|
token = <CHARACTER_LITERAL> {jjtThis.value = token.image;}
token = <CHARACTER_LITERAL> {jjtThis.value = token.image.substring(1, token.image.length() - 1);}
|
LOOKAHEAD( Rid() )
jjtThis.value = Rid()
Expand Down Expand Up @@ -1207,7 +1210,7 @@ OBaseExpression BaseExpression():
OFromClause FromClause():
{}
{
FromItem()
jjtThis.item = FromItem()
{ return jjtThis; }
}

Expand All @@ -1229,49 +1232,64 @@ OLetItem LetItem():


OFromItem FromItem():
{}
{
jjtThis.rids = new java.util.ArrayList();
ORid lastRid;
}
{
(
Rid()
lastRid = Rid() { jjtThis.rids.add(lastRid); }
|
<LBRACKET> Rid() ( <COMMA> Rid())* <RBRACKET>
(
<LBRACKET> lastRid = Rid() { jjtThis.rids.add(lastRid); }
(
<COMMA> lastRid = Rid() { jjtThis.rids.add(lastRid); }
)* <RBRACKET>
)
|
Cluster()
jjtThis.cluster = Cluster()
|
IndexIdentifier()
jjtThis.index = IndexIdentifier()
|
MetadataIdentifier()
jjtThis.metadata = MetadataIdentifier()
|
jjtThis.className = Identifier()
|
<LPAREN> ( SelectStatement() | TraverseStatement() ) <RPAREN>
<LPAREN> ( jjtThis.statement = SelectStatement() | jjtThis.statement = TraverseStatement() ) <RPAREN>
) { return jjtThis; }
}

OCluster Cluster():
{}
{ Token cName; }
{
<CLUSTER_IDENTIFIER>
(
cName = <CLUSTER_IDENTIFIER> {jjtThis.clusterName = cName.image.split(":")[1];}
)
{ return jjtThis; }
}

void MetadataIdentifier():
{}
OMetadataIdentifier MetadataIdentifier():
{ Token mdName; }
{
<METADATA_IDENTIFIER>

(
mdName = <METADATA_IDENTIFIER> {jjtThis.name = mdName.image.split(":")[1];}
)
{ return jjtThis; }
}

void IndexIdentifier():
OIndexIdentifier IndexIdentifier():
{}
{
<INDEX_IDENTIFIER>
|
<INDEXVALUES_IDENTIFIER>
|
<INDEXVALUESASC_IDENTIFIER>
|
<INDEXVALUESDESC_IDENTIFIER>
(
<INDEX_IDENTIFIER>
|
<INDEXVALUES_IDENTIFIER>
|
<INDEXVALUESASC_IDENTIFIER>
|
<INDEXVALUESDESC_IDENTIFIER>
)
{return jjtThis;}
}

OWhereClause WhereClause():
Expand Down Expand Up @@ -1576,17 +1594,17 @@ OBooleanExpression IsNotDefinedCondition():
OBooleanExpression ContainsCondition():
{}
{
(
jjtThis.left = Expression() <CONTAINS>

(
LOOKAHEAD( 3 )
( <LPAREN> jjtThis.condition = OrBlock() <RPAREN> )
|
LOOKAHEAD( Expression() )
jjtThis.right = Expression()
)
){return jjtThis;}
(
jjtThis.left = Expression() <CONTAINS>
(
LOOKAHEAD( 3 )
( <LPAREN> jjtThis.condition = OrBlock() <RPAREN> )
|
LOOKAHEAD( Expression() )
jjtThis.right = Expression()
)
)
{return jjtThis;}
}

OInOperator InOperator():
Expand Down Expand Up @@ -1626,50 +1644,54 @@ OBooleanExpression InCondition():
OBooleanExpression NotInCondition():
{}
{
(
Expression() <NOT> InOperator()
(
( <LBRACKET> Expression() (<COMMA> Expression())* <RBRACKET> )
|
( <LPAREN> SelectStatement() <RPAREN> )
)
){return jjtThis;}
(
Expression() <NOT> InOperator()
(
( <LBRACKET> Expression() (<COMMA> Expression())* <RBRACKET> )
|
( <LPAREN> SelectStatement() <RPAREN> )
)
)
{return jjtThis;}
}

OBooleanExpression ContainsAllCondition():
{}
{
(
Expression()
<CONTAINSALL>
(
LOOKAHEAD( 3 )
( <LPAREN> OrBlock() <RPAREN> )
|
LOOKAHEAD( Expression() )
(
Expression()
<CONTAINSALL>
(
LOOKAHEAD( 3 )
( <LPAREN> OrBlock() <RPAREN> )
|
LOOKAHEAD( Expression() )
Expression()
)
)
){return jjtThis;}
{return jjtThis;}
}

OBooleanExpression ContainsTextCondition():
{}
{
(
Expression() <CONTAINSTEXT> Expression()
){return jjtThis;}
(
Expression() <CONTAINSTEXT> Expression()
)
{return jjtThis;}
}

OBooleanExpression MatchesCondition():
{}
{
(
Expression() <MATCHES> <STRING_LITERAL>
){return jjtThis;}
(
Expression() <MATCHES> <STRING_LITERAL>
)
{return jjtThis;}
}

OOrderBy OrderBy():
{}
{ jjtThis.items = new java.util.ArrayList(); }
{
(
<ORDER> <BY> ( Identifier() | Rid() | <RECORD_ATTRIBUTE> ) [ <DESC> | <ASC> ]
Expand All @@ -1686,42 +1708,53 @@ OGroupBy GroupBy():
}


Integer Limit():
java.lang.Integer Limit():
{ OInteger value = null; }
{
(
<LIMIT> value = Integer()
){ return value.getValue(); }
(
<LIMIT> value = Integer()
)
{ return value.getValue(); }
}

Integer Skip():
java.lang.Integer Skip():
{ OInteger value = null;}
{
(
<SKIP2> value = Integer()
|
<OFFSET> value = Integer()
) {return value.getValue();}
(
<SKIP2> value = Integer()
|
<OFFSET> value = Integer()
) {return value.getValue();}
}

void Timeout():
{}

java.lang.Integer Timeout():
{ OInteger val; }
{
<TIMEOUT> Integer()
(
<TIMEOUT> val = Integer()
)
{ return val.getValue(); }
}


void Wait():
{}
java.lang.Integer Wait():
{ OInteger val; }
{
<WAIT> Integer()
(
<WAIT> val = Integer()
)
{ return val.getValue(); }
}


void Retry():
{}
java.lang.Integer Retry():
{ OInteger val; }
{
<RETRY> Integer()
(
<RETRY> val = Integer()
)
{ return val.getValue(); }
}


Expand Down Expand Up @@ -1770,10 +1803,18 @@ void TraverseProjectionItem():

}

void Array():
{}
OArray Array():
{
<LBRACKET> [ Expression() ( <COMMA> Expression() )* ] <RBRACKET>
jjtThis.expressions = new java.util.ArrayList();
OExpression currentExpr;
}
{
(
<LBRACKET> [ currentExpr = Expression() { jjtThis.expressions.add(currentExpr); }
( <COMMA> currentExpr = Expression() { jjtThis.expressions.add(currentExpr); } )* ] <RBRACKET>
)
{ return jjtThis; }

}


Expand Down
Expand Up @@ -19,6 +19,8 @@
*/
package com.orientechnologies.orient.core.sql;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -64,6 +66,9 @@
import com.orientechnologies.orient.core.sql.functions.coll.OSQLFunctionDistinct;
import com.orientechnologies.orient.core.sql.functions.misc.OSQLFunctionCount;
import com.orientechnologies.orient.core.sql.operator.*;
import com.orientechnologies.orient.core.sql.parser.OStatement;
import com.orientechnologies.orient.core.sql.parser.OrientSql;
import com.orientechnologies.orient.core.sql.parser.ParseException;
import com.orientechnologies.orient.core.sql.query.OResultSet;
import com.orientechnologies.orient.core.sql.query.OSQLQuery;
import com.orientechnologies.orient.core.storage.OStorage;
Expand Down Expand Up @@ -191,9 +196,12 @@ public OCommandExecutorSQLSelect parse(final OCommandRequest iRequest) {
final OCommandRequestText textRequest = (OCommandRequestText) iRequest;
String queryText = textRequest.getText();
queryText = preParse(queryText);
if (!queryText.toUpperCase().equals(textRequest.getText().toUpperCase())) {
throwParsingException(queryText + " \nDIFFERENT FROM\n" + textRequest.getText());
}
textRequest.setText(queryText);

testNewParser(iRequest);
// testNewParser(iRequest);
super.parse(iRequest);

initContext();
Expand Down Expand Up @@ -272,7 +280,17 @@ public OCommandExecutorSQLSelect parse(final OCommandRequest iRequest) {
}

private String preParse(String queryText) {
return queryText;// TODO
InputStream is = new ByteArrayInputStream(queryText.getBytes());
OrientSql osql = new OrientSql(is);
try {
OStatement result = osql.parse();
return result.toString();
} catch (ParseException e) {
System.out.println("NEW PARSER FAILED: " + queryText);
e.printStackTrace();
throwParsingException(e.getMessage());
}
return "ERROR!";
}

/**
Expand Down

0 comments on commit 46119a3

Please sign in to comment.