use of com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessOrGreaterThanExpression in project cobar by alibaba.
the class MySQLExprParser method comparisionExpression.
/**
* <code>BETWEEN ... AND</code> has lower precedence than other comparison
* operator
*/
private Expression comparisionExpression() throws SQLSyntaxErrorException {
Expression temp;
for (Expression fst = bitOrExpression(null, null); ; ) {
switch(lexer.token()) {
case KW_NOT:
lexer.nextToken();
switch(lexer.token()) {
case KW_BETWEEN:
lexer.nextToken();
Expression snd = comparisionExpression();
match(KW_AND);
Expression trd = comparisionExpression();
return new BetweenAndExpression(true, fst, snd, trd).setCacheEvalRst(cacheEvalRst);
case KW_RLIKE:
case KW_REGEXP:
lexer.nextToken();
temp = bitOrExpression(null, null);
fst = new RegexpExpression(true, fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case KW_LIKE:
lexer.nextToken();
temp = bitOrExpression(null, null);
Expression escape = null;
if (equalsIdentifier("ESCAPE") >= 0) {
lexer.nextToken();
escape = bitOrExpression(null, null);
}
fst = new LikeExpression(true, fst, temp, escape).setCacheEvalRst(cacheEvalRst);
continue;
case KW_IN:
if (lexer.nextToken() != PUNC_LEFT_PAREN) {
lexer.addCacheToke(KW_IN);
return fst;
}
Expression in = rightOprandOfIn();
fst = new InExpression(true, fst, in).setCacheEvalRst(cacheEvalRst);
continue;
default:
throw err("unexpect token after NOT: " + lexer.token());
}
case KW_BETWEEN:
lexer.nextToken();
Expression snd = comparisionExpression();
match(KW_AND);
Expression trd = comparisionExpression();
return new BetweenAndExpression(false, fst, snd, trd).setCacheEvalRst(cacheEvalRst);
case KW_RLIKE:
case KW_REGEXP:
lexer.nextToken();
temp = bitOrExpression(null, null);
fst = new RegexpExpression(false, fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case KW_LIKE:
lexer.nextToken();
temp = bitOrExpression(null, null);
Expression escape = null;
if (equalsIdentifier("ESCAPE") >= 0) {
lexer.nextToken();
escape = bitOrExpression(null, null);
}
fst = new LikeExpression(false, fst, temp, escape).setCacheEvalRst(cacheEvalRst);
continue;
case KW_IN:
if (lexer.nextToken() != PUNC_LEFT_PAREN) {
lexer.addCacheToke(KW_IN);
return fst;
}
temp = rightOprandOfIn();
fst = new InExpression(false, fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case KW_IS:
switch(lexer.nextToken()) {
case KW_NOT:
switch(lexer.nextToken()) {
case LITERAL_NULL:
lexer.nextToken();
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_NOT_NULL).setCacheEvalRst(cacheEvalRst);
continue;
case LITERAL_BOOL_FALSE:
lexer.nextToken();
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_NOT_FALSE).setCacheEvalRst(cacheEvalRst);
continue;
case LITERAL_BOOL_TRUE:
lexer.nextToken();
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_NOT_TRUE).setCacheEvalRst(cacheEvalRst);
continue;
default:
matchIdentifier("UNKNOWN");
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_NOT_UNKNOWN).setCacheEvalRst(cacheEvalRst);
continue;
}
case LITERAL_NULL:
lexer.nextToken();
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_NULL).setCacheEvalRst(cacheEvalRst);
continue;
case LITERAL_BOOL_FALSE:
lexer.nextToken();
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_FALSE).setCacheEvalRst(cacheEvalRst);
continue;
case LITERAL_BOOL_TRUE:
lexer.nextToken();
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_TRUE).setCacheEvalRst(cacheEvalRst);
continue;
default:
matchIdentifier("UNKNOWN");
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_UNKNOWN).setCacheEvalRst(cacheEvalRst);
continue;
}
case OP_EQUALS:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionEqualsExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_NULL_SAFE_EQUALS:
lexer.nextToken();
temp = bitOrExpression(null, null);
fst = new ComparisionNullSafeEqualsExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_GREATER_OR_EQUALS:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionGreaterThanOrEqualsExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_GREATER_THAN:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionGreaterThanExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_LESS_OR_EQUALS:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionLessThanOrEqualsExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_LESS_THAN:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionLessThanExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_LESS_OR_GREATER:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionLessOrGreaterThanExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_NOT_EQUALS:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionNotEqualsExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
default:
if (equalsIdentifier("SOUNDS") >= 0) {
lexer.nextToken();
match(KW_LIKE);
temp = bitOrExpression(null, null);
fst = new SoundsLikeExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
}
return fst;
}
}
}
use of com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessOrGreaterThanExpression in project cobar by alibaba.
the class MySQLExprParserTest method testComparision.
public void testComparision() throws Exception {
String sql = "a betwEen b and c Not between d and e";
MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("a BETWEEN b AND c NOT BETWEEN d AND e", output);
BetweenAndExpression ba = (BetweenAndExpression) expr;
Assert.assertEquals("a", ((Identifier) ba.getFirst()).getIdText());
Assert.assertEquals("b", ((Identifier) ba.getSecond()).getIdText());
Assert.assertEquals(false, ba.isNot());
ba = (BetweenAndExpression) ba.getThird();
Assert.assertEquals("c", ((Identifier) ba.getFirst()).getIdText());
Assert.assertEquals("d", ((Identifier) ba.getSecond()).getIdText());
Assert.assertEquals("e", ((Identifier) ba.getThird()).getIdText());
Assert.assertEquals(true, ba.isNot());
sql = "a between b between c and d and e between f and g";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a BETWEEN b BETWEEN c AND d AND e BETWEEN f AND g", output);
ba = (BetweenAndExpression) expr;
BetweenAndExpression ba2 = (BetweenAndExpression) ba.getSecond();
BetweenAndExpression ba3 = (BetweenAndExpression) ba.getThird();
Assert.assertEquals("a", ((Identifier) ba.getFirst()).getIdText());
Assert.assertEquals("b", ((Identifier) ba2.getFirst()).getIdText());
Assert.assertEquals("c", ((Identifier) ba2.getSecond()).getIdText());
Assert.assertEquals("d", ((Identifier) ba2.getThird()).getIdText());
Assert.assertEquals("e", ((Identifier) ba3.getFirst()).getIdText());
Assert.assertEquals("f", ((Identifier) ba3.getSecond()).getIdText());
Assert.assertEquals("g", ((Identifier) ba3.getThird()).getIdText());
sql = "((select a)) between (select b) and (select d) ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("(SELECT a) BETWEEN (SELECT b) AND (SELECT d)", output);
sql = "a rliKe b not REGEXP c";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a REGEXP b NOT REGEXP c", output);
RegexpExpression re = (RegexpExpression) expr;
RegexpExpression re2 = (RegexpExpression) re.getLeftOprand();
Assert.assertEquals("a", ((Identifier) re2.getLeftOprand()).getIdText());
Assert.assertEquals("b", ((Identifier) re2.getRightOprand()).getIdText());
Assert.assertEquals("c", ((Identifier) re.getRightOprand()).getIdText());
Assert.assertEquals(true, re.isNot());
Assert.assertEquals(false, re2.isNot());
sql = "((a)) like (((b)))escape (((d)))";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a LIKE b ESCAPE d", output);
sql = "((select a)) like (((select b)))escape (((select d)))";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("(SELECT a) LIKE (SELECT b) ESCAPE (SELECT d)", output);
sql = "a like b NOT LIKE c escape d";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a LIKE b NOT LIKE c ESCAPE d", output);
LikeExpression le = (LikeExpression) expr;
LikeExpression le2 = (LikeExpression) le.getFirst();
Assert.assertEquals("a", ((Identifier) le2.getFirst()).getIdText());
Assert.assertEquals("b", ((Identifier) le2.getSecond()).getIdText());
Assert.assertEquals("c", ((Identifier) le.getSecond()).getIdText());
Assert.assertEquals("d", ((Identifier) le.getThird()).getIdText());
Assert.assertEquals(true, le.isNot());
Assert.assertEquals(false, le2.isNot());
sql = "b NOT LIKE c ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("b NOT LIKE c", output);
sql = "a in (b) not in (select id from t1)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a IN (b) NOT IN (SELECT id FROM t1)", output);
InExpression in = (InExpression) expr;
InExpression in2 = (InExpression) in.getLeftOprand();
Assert.assertEquals("a", ((Identifier) in2.getLeftOprand()).getIdText());
Assert.assertTrue(QueryExpression.class.isAssignableFrom(in.getRightOprand().getClass()));
Assert.assertEquals(true, in.isNot());
Assert.assertEquals(false, in2.isNot());
sql = "(select a)is not null";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("(SELECT a) IS NOT NULL", output);
sql = "a is not null is not false is not true is not UNKNOWn is null is false is true is unknown";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a IS NOT NULL IS NOT FALSE IS NOT TRUE IS NOT UNKNOWN IS NULL IS FALSE IS TRUE IS UNKNOWN", output);
ComparisionIsExpression is = (ComparisionIsExpression) expr;
ComparisionIsExpression is2 = (ComparisionIsExpression) is.getOperand();
ComparisionIsExpression is3 = (ComparisionIsExpression) is2.getOperand();
ComparisionIsExpression is4 = (ComparisionIsExpression) is3.getOperand();
ComparisionIsExpression is5 = (ComparisionIsExpression) is4.getOperand();
ComparisionIsExpression is6 = (ComparisionIsExpression) is5.getOperand();
ComparisionIsExpression is7 = (ComparisionIsExpression) is6.getOperand();
ComparisionIsExpression is8 = (ComparisionIsExpression) is7.getOperand();
Assert.assertEquals(ComparisionIsExpression.IS_UNKNOWN, is.getMode());
Assert.assertEquals(ComparisionIsExpression.IS_TRUE, is2.getMode());
Assert.assertEquals(ComparisionIsExpression.IS_FALSE, is3.getMode());
Assert.assertEquals(ComparisionIsExpression.IS_NULL, is4.getMode());
Assert.assertEquals(ComparisionIsExpression.IS_NOT_UNKNOWN, is5.getMode());
Assert.assertEquals(ComparisionIsExpression.IS_NOT_TRUE, is6.getMode());
Assert.assertEquals(ComparisionIsExpression.IS_NOT_FALSE, is7.getMode());
Assert.assertEquals(ComparisionIsExpression.IS_NOT_NULL, is8.getMode());
Assert.assertEquals("a", ((Identifier) is8.getOperand()).getIdText());
sql = "a = b <=> c >= d > e <= f < g <> h != i";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a = b <=> c >= d > e <= f < g <> h != i", output);
ComparisionNotEqualsExpression neq = (ComparisionNotEqualsExpression) expr;
ComparisionLessOrGreaterThanExpression lg = (ComparisionLessOrGreaterThanExpression) neq.getLeftOprand();
ComparisionLessThanExpression l = (ComparisionLessThanExpression) lg.getLeftOprand();
ComparisionLessThanOrEqualsExpression leq = (ComparisionLessThanOrEqualsExpression) l.getLeftOprand();
ComparisionGreaterThanExpression g = (ComparisionGreaterThanExpression) leq.getLeftOprand();
ComparisionGreaterThanOrEqualsExpression geq = (ComparisionGreaterThanOrEqualsExpression) g.getLeftOprand();
ComparisionNullSafeEqualsExpression nseq = (ComparisionNullSafeEqualsExpression) geq.getLeftOprand();
ComparisionEqualsExpression eq = (ComparisionEqualsExpression) nseq.getLeftOprand();
Assert.assertEquals("i", ((Identifier) neq.getRightOprand()).getIdText());
Assert.assertEquals("h", ((Identifier) lg.getRightOprand()).getIdText());
Assert.assertEquals("g", ((Identifier) l.getRightOprand()).getIdText());
Assert.assertEquals("f", ((Identifier) leq.getRightOprand()).getIdText());
Assert.assertEquals("e", ((Identifier) g.getRightOprand()).getIdText());
Assert.assertEquals("d", ((Identifier) geq.getRightOprand()).getIdText());
Assert.assertEquals("c", ((Identifier) nseq.getRightOprand()).getIdText());
Assert.assertEquals("b", ((Identifier) eq.getRightOprand()).getIdText());
Assert.assertEquals("a", ((Identifier) eq.getLeftOprand()).getIdText());
sql = "a sounds like b sounds like c";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a SOUNDS LIKE b SOUNDS LIKE c", output);
SoundsLikeExpression sl = (SoundsLikeExpression) expr;
SoundsLikeExpression sl2 = (SoundsLikeExpression) sl.getLeftOprand();
Assert.assertEquals("a", ((Identifier) sl2.getLeftOprand()).getIdText());
Assert.assertEquals("b", ((Identifier) sl2.getRightOprand()).getIdText());
Assert.assertEquals("c", ((Identifier) sl.getRightOprand()).getIdText());
sql = "a like b escape c";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a LIKE b ESCAPE c", output);
sql = "(select a) collate z";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("(SELECT a) COLLATE z", output);
sql = "val1 IN (1,2,'a')";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("val1 IN (1, 2, 'a')", output);
}
Aggregations