Search in sources :

Example 6 with MySQLLexer

use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer 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());
}
Also used : UsrDefVarPrimary(com.alibaba.cobar.parser.ast.expression.primary.UsrDefVarPrimary) MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) BinaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) BitXORExpression(com.alibaba.cobar.parser.ast.expression.bit.BitXORExpression) AssignmentExpression(com.alibaba.cobar.parser.ast.expression.misc.AssignmentExpression) BitAndExpression(com.alibaba.cobar.parser.ast.expression.bit.BitAndExpression) RegexpExpression(com.alibaba.cobar.parser.ast.expression.string.RegexpExpression) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) UnaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression) MinusExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.MinusExpression) ArithmeticMultiplyExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticMultiplyExpression) ComparisionLessThanOrEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessThanOrEqualsExpression) ComparisionNullSafeEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression) BetweenAndExpression(com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression) LikeExpression(com.alibaba.cobar.parser.ast.expression.string.LikeExpression) TernaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression) ArithmeticSubtractExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticSubtractExpression) BitAndExpression(com.alibaba.cobar.parser.ast.expression.bit.BitAndExpression) ComparisionEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression) NegativeValueExpression(com.alibaba.cobar.parser.ast.expression.logical.NegativeValueExpression) SoundsLikeExpression(com.alibaba.cobar.parser.ast.expression.string.SoundsLikeExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) CastBinaryExpression(com.alibaba.cobar.parser.ast.expression.type.CastBinaryExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) ArithmeticIntegerDivideExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticIntegerDivideExpression) CollateExpression(com.alibaba.cobar.parser.ast.expression.type.CollateExpression) BitInvertExpression(com.alibaba.cobar.parser.ast.expression.bit.BitInvertExpression) BitXORExpression(com.alibaba.cobar.parser.ast.expression.bit.BitXORExpression) LogicalNotExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalNotExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) BitShiftExpression(com.alibaba.cobar.parser.ast.expression.bit.BitShiftExpression) AssignmentExpression(com.alibaba.cobar.parser.ast.expression.misc.AssignmentExpression) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) BitOrExpression(com.alibaba.cobar.parser.ast.expression.bit.BitOrExpression) ComparisionGreaterThanOrEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionGreaterThanOrEqualsExpression) LogicalXORExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalXORExpression) UserExpression(com.alibaba.cobar.parser.ast.expression.misc.UserExpression) ArithmeticAddExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticAddExpression) PolyadicOperatorExpression(com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression) ComparisionNotEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNotEqualsExpression) BinaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) ComparisionLessThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessThanExpression) ArithmeticModExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticModExpression) ComparisionGreaterThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionGreaterThanExpression) ArithmeticDivideExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticDivideExpression) ComparisionLessOrGreaterThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessOrGreaterThanExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString)

Example 7 with MySQLLexer

use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.

the class MySQLExprParserTest method testArithmetic.

public void testArithmetic() throws Exception {
    String sql = "? + @usrVar1 * c/@@version- e % -f diV g";
    MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
    Expression expr = parser.expression();
    String output = output2MySQL(expr, sql);
    Assert.assertEquals("? + @usrVar1 * c / @@version - e % - f DIV g", output);
    ArithmeticSubtractExpression sub = (ArithmeticSubtractExpression) expr;
    ArithmeticAddExpression add = (ArithmeticAddExpression) sub.getLeftOprand();
    ArithmeticIntegerDivideExpression idiv = (ArithmeticIntegerDivideExpression) sub.getRightOprand();
    ArithmeticModExpression mod = (ArithmeticModExpression) idiv.getLeftOprand();
    ArithmeticDivideExpression div = (ArithmeticDivideExpression) add.getRightOprand();
    ArithmeticMultiplyExpression mt = (ArithmeticMultiplyExpression) div.getLeftOprand();
    MinusExpression mi = (MinusExpression) mod.getRightOprand();
    Assert.assertEquals("c", ((Identifier) mt.getRightOprand()).getIdText());
    Assert.assertEquals("f", ((Identifier) mi.getOperand()).getIdText());
    sql = "a+-b";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("a + - b", output);
    sql = "a+--b";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("a + - - b", output);
    sql = "a++b";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("a + b", output);
    sql = "a+++b";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("a + b", output);
    sql = "a++-b";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("a + - b", output);
    sql = "a + b mod (-((select id from t1 limit 1)- e) ) ";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("a + b % - ((SELECT id FROM t1 LIMIT 0, 1) - e)", output);
    add = (ArithmeticAddExpression) expr;
    mod = (ArithmeticModExpression) add.getRightOprand();
    mi = (MinusExpression) mod.getRightOprand();
    sub = (ArithmeticSubtractExpression) mi.getOperand();
    Assert.assertTrue(QueryExpression.class.isAssignableFrom(sub.getLeftOprand().getClass()));
    Assert.assertEquals("e", ((Identifier) sub.getRightOprand()).getIdText());
}
Also used : ArithmeticIntegerDivideExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticIntegerDivideExpression) MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) ArithmeticMultiplyExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticMultiplyExpression) ArithmeticSubtractExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticSubtractExpression) ArithmeticDivideExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticDivideExpression) RegexpExpression(com.alibaba.cobar.parser.ast.expression.string.RegexpExpression) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) UnaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression) MinusExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.MinusExpression) ArithmeticMultiplyExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticMultiplyExpression) ComparisionLessThanOrEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessThanOrEqualsExpression) ComparisionNullSafeEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression) BetweenAndExpression(com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression) LikeExpression(com.alibaba.cobar.parser.ast.expression.string.LikeExpression) TernaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression) ArithmeticSubtractExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticSubtractExpression) BitAndExpression(com.alibaba.cobar.parser.ast.expression.bit.BitAndExpression) ComparisionEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression) NegativeValueExpression(com.alibaba.cobar.parser.ast.expression.logical.NegativeValueExpression) SoundsLikeExpression(com.alibaba.cobar.parser.ast.expression.string.SoundsLikeExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) CastBinaryExpression(com.alibaba.cobar.parser.ast.expression.type.CastBinaryExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) ArithmeticIntegerDivideExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticIntegerDivideExpression) CollateExpression(com.alibaba.cobar.parser.ast.expression.type.CollateExpression) BitInvertExpression(com.alibaba.cobar.parser.ast.expression.bit.BitInvertExpression) BitXORExpression(com.alibaba.cobar.parser.ast.expression.bit.BitXORExpression) LogicalNotExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalNotExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) BitShiftExpression(com.alibaba.cobar.parser.ast.expression.bit.BitShiftExpression) AssignmentExpression(com.alibaba.cobar.parser.ast.expression.misc.AssignmentExpression) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) BitOrExpression(com.alibaba.cobar.parser.ast.expression.bit.BitOrExpression) ComparisionGreaterThanOrEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionGreaterThanOrEqualsExpression) LogicalXORExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalXORExpression) UserExpression(com.alibaba.cobar.parser.ast.expression.misc.UserExpression) ArithmeticAddExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticAddExpression) PolyadicOperatorExpression(com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression) ComparisionNotEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNotEqualsExpression) BinaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) ComparisionLessThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessThanExpression) ArithmeticModExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticModExpression) ComparisionGreaterThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionGreaterThanExpression) ArithmeticDivideExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticDivideExpression) ComparisionLessOrGreaterThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessOrGreaterThanExpression) MinusExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.MinusExpression) ArithmeticAddExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticAddExpression) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) ArithmeticModExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticModExpression)

Example 8 with MySQLLexer

use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.

the class MySQLExprParserTest method testStartedFromIdentifier.

public void testStartedFromIdentifier() throws Exception {
    // QS_TODO
    String sql = "cast(CAST(1-2 AS UNSIGNED) AS SIGNED)";
    MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
    Expression expr = parser.expression();
    String output = output2MySQL(expr, sql);
    Assert.assertEquals("CAST(CAST(1 - 2 AS UNSIGNED) AS SIGNED)", output);
    sql = "position('a' in \"abc\")";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("LOCATE('a', 'abc')", output);
    sql = "cast(CAST(1-2 AS UNSIGNED integer) AS SIGNED integer)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("CAST(CAST(1 - 2 AS UNSIGNED) AS SIGNED)", output);
    sql = "CAST(expr as char)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("CAST(expr AS CHAR)", output);
    sql = "CAST(6/4 AS DECIMAL(3,1))";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("CAST(6 / 4 AS DECIMAL(3, 1))", output);
    sql = "CAST(6/4 AS DECIMAL(3))";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("CAST(6 / 4 AS DECIMAL(3))", output);
    sql = "CAST(6/4 AS DECIMAL)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("CAST(6 / 4 AS DECIMAL)", output);
    sql = "CAST(now() as date)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("CAST(NOW() AS DATE)", output);
    sql = "CAST(expr as char(5))";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("CAST(expr AS CHAR(5))", output);
    sql = "SUBSTRING('abc',pos,len)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("SUBSTRING('abc', pos, len)", output);
    sql = "SUBSTRING('abc' FROM pos FOR len)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("SUBSTRING('abc', pos, len)", output);
    sql = "SUBSTRING(str,pos)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("SUBSTRING(str, pos)", output);
    sql = "SUBSTRING('abc',1,2)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("SUBSTRING('abc', 1, 2)", output);
    sql = "row(1,2,str)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("ROW(1, 2, str)", output);
    sql = "position(\"abc\" in '/*abc*/')";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("LOCATE('abc', '/*abc*/')", output);
    sql = "locate(localtime,b)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("LOCATE(NOW(), b)", output);
    sql = "locate(locate(a,b),`match`)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("LOCATE(LOCATE(a, b), `match`)", output);
    sql = "TRIM(LEADING 'x' FROM 'xxxbarxxx')";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("TRIM(LEADING 'x' FROM 'xxxbarxxx')", output);
    sql = "TRIM(BOTH 'x' FROM 'xxxbarxxx')";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("TRIM(BOTH 'x' FROM 'xxxbarxxx')", output);
    sql = "TRIM(TRAILING 'xyz' FROM 'barxxyz')";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("TRIM(TRAILING 'xyz' FROM 'barxxyz')", output);
    sql = "TRIM('  if   ')";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("TRIM('  if   ')", output);
    sql = "TRIM( 'x' FROM 'xxxbarxxx')";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("TRIM('x' FROM 'xxxbarxxx')", output);
    sql = "TRIM(both  FROM 'barxxyz')";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("TRIM(BOTH  FROM 'barxxyz')", output);
    sql = "TRIM(leading  FROM 'barxxyz')";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("TRIM(LEADING  FROM 'barxxyz')", output);
    sql = "TRIM(TRAILING  FROM 'barxxyz')";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("TRIM(TRAILING  FROM 'barxxyz')", output);
    sql = "avg(DISTINCT results)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("AVG(DISTINCT results)", output);
    sql = "avg(results)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("AVG(results)", output);
    sql = "max(DISTINCT results)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("MAX(DISTINCT results)", output);
    sql = "max(results)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("MAX(results)", output);
    sql = "min(DISTINCT results)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("MIN(DISTINCT results)", output);
    sql = "min(results)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("MIN(results)", output);
    sql = "sum(DISTINCT results)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("SUM(DISTINCT results)", output);
    sql = "sum(results)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("SUM(results)", output);
    sql = "count(DISTINCT expr1,expr2,expr3)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("COUNT(DISTINCT expr1, expr2, expr3)", output);
    sql = "count(*)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("COUNT(*)", output);
    sql = "GROUP_CONCAT(DISTINCT expr1,expr2,expr3 ORDER BY col_name1 DESC,col_name2 SEPARATOR ' ')";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("GROUP_CONCAT(DISTINCT expr1, expr2, expr3 ORDER BY col_name1 DESC, col_name2 SEPARATOR  )", output);
    sql = "GROUP_CONCAT(a||b,expr2,expr3 ORDER BY col_name1 asc,col_name2 SEPARATOR '@ ')";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("GROUP_CONCAT(a OR b, expr2, expr3 ORDER BY col_name1 ASC, col_name2 SEPARATOR @ )", output);
    sql = "GROUP_CONCAT(expr1 ORDER BY col_name1 asc,col_name2 SEPARATOR 'str_val ')";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("GROUP_CONCAT(expr1 ORDER BY col_name1 ASC, col_name2 SEPARATOR str_val )", output);
    sql = "GROUP_CONCAT(DISTINCT test_score ORDER BY test_score DESC )";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("GROUP_CONCAT(DISTINCT test_score ORDER BY test_score DESC SEPARATOR ,)", output);
    sql = "GROUP_CONCAT(DISTINCT test_score ORDER BY test_score asc )";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("GROUP_CONCAT(DISTINCT test_score ORDER BY test_score ASC SEPARATOR ,)", output);
    sql = "GROUP_CONCAT(c1)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("GROUP_CONCAT(c1 SEPARATOR ,)", output);
    sql = "GROUP_CONCAT(c1 separator '')";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("GROUP_CONCAT(c1 SEPARATOR )", output);
    sql = "default";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("DEFAULT", output);
    sql = "default(col)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("DEFAULT(col)", output);
    sql = "database()";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("DATABASE()", output);
    sql = "if(1>2,a+b,a:=1)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("IF(1 > 2, a + b, a := 1)", output);
    sql = "insert('abc',1,2,'')";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("INSERT('abc', 1, 2, '')", output);
    sql = "left(\"hjkafag\",4)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("LEFT('hjkafag', 4)", output);
    sql = "repeat('ag',2.1e1)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("REPEAT('ag', 21)", output);
    sql = "replace('anjd',\"df\",'af')";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("REPLACE('anjd', 'df', 'af')", output);
    sql = "right(\"hjkafag\",4)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("RIGHT('hjkafag', 4)", output);
    sql = "schema()";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("DATABASE()", output);
    sql = "utc_date()";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("UTC_DATE()", output);
    sql = "Utc_time()";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("UTC_TIME()", output);
    sql = "Utc_timestamp()";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("UTC_TIMESTAMP()", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) RegexpExpression(com.alibaba.cobar.parser.ast.expression.string.RegexpExpression) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) UnaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression) MinusExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.MinusExpression) ArithmeticMultiplyExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticMultiplyExpression) ComparisionLessThanOrEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessThanOrEqualsExpression) ComparisionNullSafeEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression) BetweenAndExpression(com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression) LikeExpression(com.alibaba.cobar.parser.ast.expression.string.LikeExpression) TernaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression) ArithmeticSubtractExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticSubtractExpression) BitAndExpression(com.alibaba.cobar.parser.ast.expression.bit.BitAndExpression) ComparisionEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression) NegativeValueExpression(com.alibaba.cobar.parser.ast.expression.logical.NegativeValueExpression) SoundsLikeExpression(com.alibaba.cobar.parser.ast.expression.string.SoundsLikeExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) CastBinaryExpression(com.alibaba.cobar.parser.ast.expression.type.CastBinaryExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) ArithmeticIntegerDivideExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticIntegerDivideExpression) CollateExpression(com.alibaba.cobar.parser.ast.expression.type.CollateExpression) BitInvertExpression(com.alibaba.cobar.parser.ast.expression.bit.BitInvertExpression) BitXORExpression(com.alibaba.cobar.parser.ast.expression.bit.BitXORExpression) LogicalNotExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalNotExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) BitShiftExpression(com.alibaba.cobar.parser.ast.expression.bit.BitShiftExpression) AssignmentExpression(com.alibaba.cobar.parser.ast.expression.misc.AssignmentExpression) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) BitOrExpression(com.alibaba.cobar.parser.ast.expression.bit.BitOrExpression) ComparisionGreaterThanOrEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionGreaterThanOrEqualsExpression) LogicalXORExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalXORExpression) UserExpression(com.alibaba.cobar.parser.ast.expression.misc.UserExpression) ArithmeticAddExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticAddExpression) PolyadicOperatorExpression(com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression) ComparisionNotEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNotEqualsExpression) BinaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) ComparisionLessThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessThanExpression) ArithmeticModExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticModExpression) ComparisionGreaterThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionGreaterThanExpression) ArithmeticDivideExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticDivideExpression) ComparisionLessOrGreaterThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessOrGreaterThanExpression) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString)

Example 9 with MySQLLexer

use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.

the class MySQLDMLInsertParserTest method testInsert.

public void testInsert() throws SQLSyntaxErrorException {
    String sql = "insErt HIGH_PRIORITY intO test.t1 seT t1.id1=?, id2 := '123'";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDMLInsertParser parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    DMLInsertStatement insert = parser.insert();
    parser.match(MySQLToken.EOF);
    String output = output2MySQL(insert, sql);
    Assert.assertNotNull(insert);
    Assert.assertEquals("INSERT HIGH_PRIORITY INTO test.t1 (t1.id1, id2) VALUES (?, '123')", output);
    sql = "insErt  IGNORE test.t1 seT t1.id1:=? oN dupLicatE key UPDATE ex.col1=?, col2=12";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    insert = parser.insert();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(insert, sql);
    Assert.assertEquals("INSERT IGNORE INTO test.t1 (t1.id1) VALUES (?) ON DUPLICATE KEY UPDATE ex.col1 = ?, col2 = 12", output);
    sql = "insErt t1 value (123,?) oN dupLicatE key UPDATE ex.col1=?";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    insert = parser.insert();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(insert, sql);
    Assert.assertEquals("INSERT INTO t1 VALUES (123, ?) ON DUPLICATE KEY UPDATE ex.col1 = ?", output);
    sql = "insErt LOW_PRIORITY t1 valueS (12e-2,1,2), (?),(default)";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    insert = parser.insert();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(insert, sql);
    Assert.assertEquals("INSERT LOW_PRIORITY INTO t1 VALUES (0.12, 1, 2), (?), (DEFAULT)", output);
    sql = "insErt LOW_PRIORITY t1 select id from t1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    insert = parser.insert();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(insert, sql);
    Assert.assertEquals("INSERT LOW_PRIORITY INTO t1 SELECT id FROM t1", output);
    sql = "insErt delayed t1 select id from t1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    insert = parser.insert();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(insert, sql);
    Assert.assertEquals("INSERT DELAYED INTO t1 SELECT id FROM t1", output);
    sql = "insErt LOW_PRIORITY t1 (select id from t1) oN dupLicatE key UPDATE ex.col1=?, col2=12";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    insert = parser.insert();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(insert, sql);
    Assert.assertEquals("INSERT LOW_PRIORITY INTO t1 SELECT id FROM t1 ON DUPLICATE KEY UPDATE ex.col1 = ?, col2 = 12", output);
    sql = "insErt LOW_PRIORITY t1 (t1.col1) valueS (123),('12''34')";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    insert = parser.insert();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(insert, sql);
    Assert.assertEquals("INSERT LOW_PRIORITY INTO t1 (t1.col1) VALUES (123), ('12\\'34')", output);
    sql = "insErt LOW_PRIORITY t1 (col1, t1.col2) VALUE (123,'123\\'4') oN dupLicatE key UPDATE ex.col1=?";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    insert = parser.insert();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(insert, sql);
    Assert.assertEquals("INSERT LOW_PRIORITY INTO t1 (col1, t1.col2) VALUES (123, '123\\'4') ON DUPLICATE KEY UPDATE ex.col1 = ?", output);
    sql = "insErt LOW_PRIORITY t1 (col1, t1.col2) select id from t3 oN dupLicatE key UPDATE ex.col1=?";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    insert = parser.insert();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(insert, sql);
    Assert.assertEquals("INSERT LOW_PRIORITY INTO t1 (col1, t1.col2) SELECT id FROM t3 ON DUPLICATE KEY UPDATE ex.col1 = ?", output);
    sql = "insErt LOW_PRIORITY IGNORE intO t1 (col1) ( select id from t3) ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    insert = parser.insert();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(insert, sql);
    Assert.assertEquals("INSERT LOW_PRIORITY IGNORE INTO t1 (col1) SELECT id FROM t3", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) DMLInsertStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLInsertStatement)

Example 10 with MySQLLexer

use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.

the class MySQLDMLParserTest method testGroupBy.

public void testGroupBy() throws SQLSyntaxErrorException {
    String sql = "group by c1 asc, c2 desc  , c3 with rollup";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDMLParser parser = getDMLParser(lexer);
    GroupBy groupBy = parser.groupBy();
    String output = output2MySQL(groupBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC), new Pair<Expression, SortOrder>(new Identifier(null, "c2"), SortOrder.DESC), new Pair<Expression, SortOrder>(new Identifier(null, "c3"), SortOrder.ASC)), groupBy.getOrderByList());
    Assert.assertEquals("GROUP BY c1, c2 DESC, c3 WITH ROLLUP", output);
    sql = "group by c1 asc, c2 desc  , c3 ";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    groupBy = parser.groupBy();
    output = output2MySQL(groupBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC), new Pair<Expression, SortOrder>(new Identifier(null, "c2"), SortOrder.DESC), new Pair<Expression, SortOrder>(new Identifier(null, "c3"), SortOrder.ASC)), groupBy.getOrderByList());
    Assert.assertEquals("GROUP BY c1, c2 DESC, c3", output);
    sql = "group by c1   ";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    groupBy = parser.groupBy();
    output = output2MySQL(groupBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC)), groupBy.getOrderByList());
    Assert.assertEquals("GROUP BY c1", output);
    sql = "group by c1 asc  ";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    groupBy = parser.groupBy();
    output = output2MySQL(groupBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC)), groupBy.getOrderByList());
    Assert.assertEquals("GROUP BY c1", output);
    sql = "group by c1 desc  ";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    groupBy = parser.groupBy();
    output = output2MySQL(groupBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.DESC)), groupBy.getOrderByList());
    Assert.assertEquals("GROUP BY c1 DESC", output);
    sql = "group by c1 with rollup  ";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    groupBy = parser.groupBy();
    output = output2MySQL(groupBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC)), groupBy.getOrderByList());
    Assert.assertEquals("GROUP BY c1 WITH ROLLUP", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) GroupBy(com.alibaba.cobar.parser.ast.fragment.GroupBy) Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) Pair(com.alibaba.cobar.parser.util.Pair)

Aggregations

MySQLLexer (com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer)40 Expression (com.alibaba.cobar.parser.ast.expression.Expression)18 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)17 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)17 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)17 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)16 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)16 TernaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression)16 UnaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression)16 ArithmeticAddExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticAddExpression)16 ArithmeticDivideExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticDivideExpression)16 ArithmeticIntegerDivideExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticIntegerDivideExpression)16 ArithmeticModExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticModExpression)16 ArithmeticMultiplyExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticMultiplyExpression)16 ArithmeticSubtractExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticSubtractExpression)16 MinusExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.MinusExpression)16 BitAndExpression (com.alibaba.cobar.parser.ast.expression.bit.BitAndExpression)16 BitInvertExpression (com.alibaba.cobar.parser.ast.expression.bit.BitInvertExpression)16 BitOrExpression (com.alibaba.cobar.parser.ast.expression.bit.BitOrExpression)16 BitShiftExpression (com.alibaba.cobar.parser.ast.expression.bit.BitShiftExpression)16