use of com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression in project cobar by alibaba.
the class PartitionKeyVisitor method visit.
@Override
public void visit(LogicalOrExpression node) {
for (int i = 0, len = node.getArity(); i < len; ++i) {
Expression oprand = node.getOperand(i);
visitChild(2, verdictColumn && isVerdictPassthroughWhere(oprand), false, oprand);
}
}
use of com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression in project cobar by alibaba.
the class MySQLExprParserTest method testAssignment.
public void testAssignment() throws Exception {
String sql = "a /*dd*/:=b:=c";
MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("a := b := c", output);
Assert.assertEquals(AssignmentExpression.class, expr.getClass());
AssignmentExpression ass = (AssignmentExpression) expr;
Assert.assertEquals(AssignmentExpression.class, ass.getRightOprand().getClass());
ass = (AssignmentExpression) ass.getRightOprand();
Assert.assertEquals("b", ((Identifier) ass.getLeftOprand()).getIdText());
sql = "c=@var:=1";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("c = (@var := 1)", output);
Assert.assertEquals(ComparisionEqualsExpression.class, expr.getClass());
ass = (AssignmentExpression) ((BinaryOperatorExpression) expr).getRightOprand();
UsrDefVarPrimary usr = (UsrDefVarPrimary) ass.getLeftOprand();
Assert.assertEquals("@var", usr.getVarText());
sql = "a:=b or c &&d :=0b1101 or b'01'&0xabc";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a := b OR c AND d := b'1101' OR b'01' & x'abc'", output);
Assert.assertEquals(AssignmentExpression.class, expr.getClass());
ass = (AssignmentExpression) expr;
Assert.assertEquals(AssignmentExpression.class, ass.getRightOprand().getClass());
ass = (AssignmentExpression) ass.getRightOprand();
Assert.assertEquals(LogicalOrExpression.class, ass.getLeftOprand().getClass());
Assert.assertEquals(LogicalOrExpression.class, ass.getRightOprand().getClass());
LogicalOrExpression lor = (LogicalOrExpression) ass.getLeftOprand();
Assert.assertEquals(LogicalAndExpression.class, lor.getOperand(1).getClass());
lor = (LogicalOrExpression) ass.getRightOprand();
Assert.assertEquals(LiteralBitField.class, lor.getOperand(0).getClass());
Assert.assertEquals(BitAndExpression.class, lor.getOperand(1).getClass());
sql = "a:=((b or (c &&d)) :=((0b1101 or (b'01'&0xabc))))";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a := b OR c AND d := b'1101' OR b'01' & x'abc'", output);
Assert.assertEquals(AssignmentExpression.class, expr.getClass());
ass = (AssignmentExpression) expr;
Assert.assertEquals(AssignmentExpression.class, ass.getRightOprand().getClass());
ass = (AssignmentExpression) ass.getRightOprand();
Assert.assertEquals(LogicalOrExpression.class, ass.getLeftOprand().getClass());
Assert.assertEquals(LogicalOrExpression.class, ass.getRightOprand().getClass());
lor = (LogicalOrExpression) ass.getLeftOprand();
Assert.assertEquals(LogicalAndExpression.class, lor.getOperand(1).getClass());
lor = (LogicalOrExpression) ass.getRightOprand();
Assert.assertEquals(LiteralBitField.class, lor.getOperand(0).getClass());
Assert.assertEquals(BitAndExpression.class, lor.getOperand(1).getClass());
sql = "(a:=b) or c &&(d :=0b1101 or b'01')&0xabc ^null";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("(a := b) OR c AND (d := b'1101' OR b'01') & x'abc' ^ NULL", output);
Assert.assertEquals(LogicalOrExpression.class, expr.getClass());
lor = (LogicalOrExpression) expr;
Assert.assertEquals(AssignmentExpression.class, lor.getOperand(0).getClass());
Assert.assertEquals(LogicalAndExpression.class, lor.getOperand(1).getClass());
LogicalAndExpression land = (LogicalAndExpression) lor.getOperand(1);
Assert.assertEquals(Identifier.class, land.getOperand(0).getClass());
Assert.assertEquals(BitAndExpression.class, land.getOperand(1).getClass());
BitAndExpression band = (BitAndExpression) land.getOperand(1);
Assert.assertEquals(AssignmentExpression.class, band.getLeftOprand().getClass());
Assert.assertEquals(BitXORExpression.class, band.getRightOprand().getClass());
ass = (AssignmentExpression) band.getLeftOprand();
Assert.assertEquals(LogicalOrExpression.class, ass.getRightOprand().getClass());
BitXORExpression bxor = (BitXORExpression) band.getRightOprand();
Assert.assertEquals(LiteralHexadecimal.class, bxor.getLeftOprand().getClass());
Assert.assertEquals(LiteralNull.class, bxor.getRightOprand().getClass());
}
use of com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression in project cobar by alibaba.
the class MySQLExprParserTest method testExpr1.
public void testExpr1() throws Exception {
String sql = "\"abc\" /* */ '\\'s' + id2/ id3, 123-456*(ii moD d)";
MySQLLexer lexer = new MySQLLexer(sql);
MySQLExprParser parser = new MySQLExprParser(lexer);
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("'abc\\'s' + id2 / id3", output);
Assert.assertEquals(ArithmeticAddExpression.class, expr.getClass());
BinaryOperatorExpression bex = (BinaryOperatorExpression) ((ArithmeticAddExpression) expr).getRightOprand();
Assert.assertEquals(ArithmeticDivideExpression.class, bex.getClass());
Assert.assertEquals(Identifier.class, bex.getRightOprand().getClass());
lexer.nextToken();
parser = new MySQLExprParser(lexer);
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("123 - 456 * (ii % d)", output);
Assert.assertEquals(ArithmeticSubtractExpression.class, expr.getClass());
sql = "(n'\"abc\"' \"abc\" /* */ '\\'s' + 1.123e1/ id3)*(.1e3-a||b)mod x'abc'&&(select 0b1001^b'0000')";
lexer = new MySQLLexer(sql);
parser = new MySQLExprParser(lexer);
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("(N'\"abc\"abc\\'s' + 11.23 / id3) * (1E+2 - a OR b) % x'abc' AND (SELECT b'1001' ^ b'0000')", output);
Assert.assertEquals(LogicalAndExpression.class, expr.getClass());
bex = (BinaryOperatorExpression) ((LogicalAndExpression) expr).getOperand(0);
Assert.assertEquals(ArithmeticModExpression.class, bex.getClass());
bex = (BinaryOperatorExpression) ((ArithmeticModExpression) bex).getLeftOprand();
Assert.assertEquals(ArithmeticMultiplyExpression.class, bex.getClass());
bex = (BinaryOperatorExpression) ((ArithmeticMultiplyExpression) bex).getLeftOprand();
Assert.assertEquals(ArithmeticAddExpression.class, bex.getClass());
Assert.assertEquals(LiteralString.class, ((ArithmeticAddExpression) bex).getLeftOprand().getClass());
bex = (BinaryOperatorExpression) ((ArithmeticAddExpression) bex).getRightOprand();
Assert.assertEquals(ArithmeticDivideExpression.class, bex.getClass());
Assert.assertEquals(DMLSelectStatement.class, ((LogicalAndExpression) expr).getOperand(1).getClass());
sql = "not! ~`select` in (1,current_date,`current_date`)like `all` div a between (c&&d) and (d|e)";
lexer = new MySQLLexer(sql);
parser = new MySQLExprParser(lexer);
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("NOT ! ~ `select` IN (1, CURDATE(), `current_date`) LIKE `all` DIV a BETWEEN (c AND d) AND d | e", output);
Assert.assertEquals(LogicalNotExpression.class, expr.getClass());
TernaryOperatorExpression tex = (TernaryOperatorExpression) ((LogicalNotExpression) expr).getOperand();
Assert.assertEquals(BetweenAndExpression.class, tex.getClass());
Assert.assertEquals(LikeExpression.class, tex.getFirst().getClass());
Assert.assertEquals(LogicalAndExpression.class, tex.getSecond().getClass());
Assert.assertEquals(BitOrExpression.class, tex.getThird().getClass());
tex = (TernaryOperatorExpression) ((BetweenAndExpression) tex).getFirst();
Assert.assertEquals(InExpression.class, tex.getFirst().getClass());
Assert.assertEquals(ArithmeticIntegerDivideExpression.class, tex.getSecond().getClass());
bex = (BinaryOperatorExpression) (InExpression) tex.getFirst();
Assert.assertEquals(NegativeValueExpression.class, bex.getLeftOprand().getClass());
Assert.assertEquals(InExpressionList.class, bex.getRightOprand().getClass());
UnaryOperatorExpression uex = (UnaryOperatorExpression) ((NegativeValueExpression) bex.getLeftOprand());
Assert.assertEquals(BitInvertExpression.class, uex.getOperand().getClass());
sql = " binary case ~a||b&&c^d xor e when 2>any(select a ) then 3 else 4 end is not null =a";
lexer = new MySQLLexer(sql);
parser = new MySQLExprParser(lexer);
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("BINARY CASE ~ a OR b AND c ^ d XOR e WHEN 2 > ANY (SELECT a) THEN 3 ELSE 4 END IS NOT NULL = a", output);
Assert.assertEquals(ComparisionEqualsExpression.class, expr.getClass());
bex = (BinaryOperatorExpression) ((ComparisionEqualsExpression) expr);
Assert.assertEquals(ComparisionIsExpression.class, bex.getLeftOprand().getClass());
Assert.assertEquals(Identifier.class, bex.getRightOprand().getClass());
ComparisionIsExpression cex = (ComparisionIsExpression) bex.getLeftOprand();
Assert.assertEquals(CastBinaryExpression.class, cex.getOperand().getClass());
uex = (UnaryOperatorExpression) cex.getOperand();
Assert.assertEquals(CaseWhenOperatorExpression.class, uex.getOperand().getClass());
CaseWhenOperatorExpression cwex = (CaseWhenOperatorExpression) uex.getOperand();
Assert.assertEquals(LogicalOrExpression.class, cwex.getComparee().getClass());
PolyadicOperatorExpression pex = (LogicalOrExpression) cwex.getComparee();
Assert.assertEquals(BitInvertExpression.class, pex.getOperand(0).getClass());
Assert.assertEquals(LogicalXORExpression.class, pex.getOperand(1).getClass());
bex = (LogicalXORExpression) pex.getOperand(1);
Assert.assertEquals(LogicalAndExpression.class, bex.getLeftOprand().getClass());
Assert.assertEquals(Identifier.class, bex.getRightOprand().getClass());
pex = (LogicalAndExpression) bex.getLeftOprand();
Assert.assertEquals(Identifier.class, pex.getOperand(0).getClass());
Assert.assertEquals(BitXORExpression.class, pex.getOperand(1).getClass());
sql = " !interval(a,b)<=>a>>b collate x /?+a!=@@1 or @var sounds like -(a-b) mod -(d or e)";
lexer = new MySQLLexer(sql);
parser = new MySQLExprParser(lexer);
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("! INTERVAL(a, b) <=> a >> b COLLATE x / ? + a != @@1 OR @var SOUNDS LIKE - (a - b) % - (d OR e)", output);
Assert.assertEquals(LogicalOrExpression.class, expr.getClass());
pex = (LogicalOrExpression) expr;
Assert.assertEquals(ComparisionNotEqualsExpression.class, pex.getOperand(0).getClass());
Assert.assertEquals(SoundsLikeExpression.class, pex.getOperand(1).getClass());
bex = (BinaryOperatorExpression) pex.getOperand(0);
Assert.assertEquals(ComparisionNullSafeEqualsExpression.class, bex.getLeftOprand().getClass());
Assert.assertEquals(SysVarPrimary.class, bex.getRightOprand().getClass());
bex = (BinaryOperatorExpression) bex.getLeftOprand();
Assert.assertEquals(NegativeValueExpression.class, bex.getLeftOprand().getClass());
Assert.assertEquals(BitShiftExpression.class, bex.getRightOprand().getClass());
bex = (BinaryOperatorExpression) bex.getRightOprand();
Assert.assertEquals(Identifier.class, bex.getLeftOprand().getClass());
Assert.assertEquals(ArithmeticAddExpression.class, bex.getRightOprand().getClass());
bex = (BinaryOperatorExpression) bex.getRightOprand();
Assert.assertEquals(ArithmeticDivideExpression.class, bex.getLeftOprand().getClass());
Assert.assertEquals(Identifier.class, bex.getRightOprand().getClass());
bex = (BinaryOperatorExpression) bex.getLeftOprand();
Assert.assertEquals(CollateExpression.class, bex.getLeftOprand().getClass());
Assert.assertEquals(ParamMarker.class, bex.getRightOprand().getClass());
bex = (BinaryOperatorExpression) ((LogicalOrExpression) expr).getOperand(1);
Assert.assertEquals(UsrDefVarPrimary.class, bex.getLeftOprand().getClass());
Assert.assertEquals(ArithmeticModExpression.class, bex.getRightOprand().getClass());
bex = (BinaryOperatorExpression) bex.getRightOprand();
Assert.assertEquals(MinusExpression.class, bex.getLeftOprand().getClass());
Assert.assertEquals(MinusExpression.class, bex.getRightOprand().getClass());
uex = (UnaryOperatorExpression) bex.getLeftOprand();
Assert.assertEquals(ArithmeticSubtractExpression.class, uex.getOperand().getClass());
uex = (UnaryOperatorExpression) bex.getRightOprand();
Assert.assertEquals(LogicalOrExpression.class, uex.getOperand().getClass());
}
use of com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression in project cobar by alibaba.
the class MySQLExprParserTest method testLogical.
public void testLogical() throws Exception {
String sql = "a || b Or c";
MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("a OR b OR c", output);
Assert.assertEquals(LogicalOrExpression.class, expr.getClass());
LogicalOrExpression or = (LogicalOrExpression) expr;
Assert.assertEquals(3, or.getArity());
Assert.assertEquals("b", ((Identifier) or.getOperand(1)).getIdText());
sql = "a XOR b xOr c";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a XOR b XOR c", output);
Assert.assertEquals(LogicalXORExpression.class, expr.getClass());
LogicalXORExpression xor = (LogicalXORExpression) expr;
Assert.assertEquals(LogicalXORExpression.class, xor.getLeftOprand().getClass());
xor = (LogicalXORExpression) xor.getLeftOprand();
Assert.assertEquals("b", ((Identifier) xor.getRightOprand()).getIdText());
sql = "a XOR( b xOr c)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a XOR (b XOR c)", output);
xor = (LogicalXORExpression) expr;
LogicalXORExpression xor2 = (LogicalXORExpression) xor.getRightOprand();
Assert.assertEquals("a", ((Identifier) xor.getLeftOprand()).getIdText());
Assert.assertEquals("b", ((Identifier) xor2.getLeftOprand()).getIdText());
Assert.assertEquals("c", ((Identifier) xor2.getRightOprand()).getIdText());
sql = "a and b && c";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a AND b AND c", output);
Assert.assertEquals(LogicalAndExpression.class, expr.getClass());
LogicalAndExpression and = (LogicalAndExpression) expr;
Assert.assertEquals(3, or.getArity());
Assert.assertEquals("b", ((Identifier) and.getOperand(1)).getIdText());
sql = "not NOT Not a";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("NOT NOT NOT a", output);
Assert.assertEquals(LogicalNotExpression.class, expr.getClass());
LogicalNotExpression not = (LogicalNotExpression) ((LogicalNotExpression) expr).getOperand();
Assert.assertEquals(LogicalNotExpression.class, not.getClass());
not = (LogicalNotExpression) not.getOperand();
Assert.assertEquals(LogicalNotExpression.class, not.getClass());
Assert.assertEquals("a", ((Identifier) not.getOperand()).getIdText());
}
use of com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression in project cobar by alibaba.
the class MySQLExprParser method logicalOrExpression.
/**
* <code>higherPRJExpr ( ( '||' | 'OR' ) higherPRJExpr )*</code>
*
* @throws SQLSyntaxErrorException
*/
private Expression logicalOrExpression() throws SQLSyntaxErrorException {
LogicalOrExpression or = null;
for (Expression expr = logicalXORExpression(); ; ) {
switch(lexer.token()) {
case OP_LOGICAL_OR:
case KW_OR:
lexer.nextToken();
if (or == null) {
or = new LogicalOrExpression();
or.setCacheEvalRst(cacheEvalRst);
or.appendOperand(expr);
expr = or;
}
Expression newExpr = logicalXORExpression();
or.appendOperand(newExpr);
break;
default:
return expr;
}
}
}
Aggregations