use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.
the class MySQLDMLParserTest method testTR1.
public void testTR1() throws SQLSyntaxErrorException {
String sql = "(select * from `select`) as `select`";
MySQLLexer lexer = new MySQLLexer(sql);
MySQLDMLParser parser = getDMLParser(lexer);
TableReferences trs = parser.tableRefs();
String output = output2MySQL(trs, sql);
List<TableReference> list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(SubqueryFactor.class, list.get(0).getClass());
Assert.assertEquals("(SELECT * FROM `select`) AS `SELECT`", output);
sql = "(((selecT * from any)union select `select` from `from` order by dd) as 'a1', (((t2)))), t3";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(2, list.size());
Assert.assertEquals(TableReferences.class, list.get(0).getClass());
Assert.assertEquals(TableRefFactor.class, list.get(1).getClass());
list = ((TableReferences) list.get(0)).getTableReferenceList();
Assert.assertEquals(2, list.size());
Assert.assertEquals(SubqueryFactor.class, list.get(0).getClass());
Assert.assertEquals(TableReferences.class, list.get(1).getClass());
Assert.assertEquals("((SELECT * FROM any) UNION (SELECT `select` FROM `from` ORDER BY dd)) AS 'a1', t2, t3", output);
sql = "(t1)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
TableReference tr = list.get(0);
Assert.assertEquals(TableReferences.class, tr.getClass());
list = ((TableReferences) tr).getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(TableRefFactor.class, list.get(0).getClass());
Assert.assertEquals("t1", output);
sql = "(t1,t2,(t3))";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(TableReferences.class, list.get(0).getClass());
tr = (TableReferences) list.get(0);
Assert.assertEquals(TableReferences.class, tr.getClass());
list = ((TableReferences) tr).getTableReferenceList();
Assert.assertEquals(3, list.size());
Assert.assertEquals(TableRefFactor.class, list.get(0).getClass());
Assert.assertEquals(TableRefFactor.class, list.get(1).getClass());
Assert.assertEquals(TableRefFactor.class, list.get(1).getClass());
Assert.assertEquals("t1, t2, t3", output);
sql = "(tb1 as t1)inner join (tb2 as t2) on t1.name=t2.name";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(InnerJoin.class, list.get(0).getClass());
tr = ((InnerJoin) list.get(0)).getLeftTableRef();
Assert.assertEquals(TableReferences.class, tr.getClass());
list = ((TableReferences) tr).getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(TableRefFactor.class, list.get(0).getClass());
Expression ex = ((InnerJoin) (trs.getTableReferenceList()).get(0)).getOnCond();
Assert.assertEquals(ex.getClass(), ComparisionEqualsExpression.class);
Assert.assertEquals("(tb1 AS T1) INNER JOIN (tb2 AS T2) ON t1.name = t2.name", output);
sql = "(tb1 as t1)inner join tb2 as t2 using (c1)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
List<String> using_list = ((InnerJoin) (trs.getTableReferenceList()).get(0)).getUsing();
Assert.assertEquals(1, using_list.size());
Assert.assertEquals("(tb1 AS T1) INNER JOIN tb2 AS T2 USING (c1)", output);
sql = "(tb1 as t1)inner join tb2 as t2 using (c1,c2)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
using_list = ((InnerJoin) (trs.getTableReferenceList()).get(0)).getUsing();
Assert.assertEquals(2, using_list.size());
Assert.assertEquals("(tb1 AS T1) INNER JOIN tb2 AS T2 USING (c1, c2)", output);
sql = "tb1 as t1 use index (i1,i2,i3)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(TableRefFactor.class, list.get(0).getClass());
List<IndexHint> hintlist = ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getHintList();
Assert.assertEquals(1, hintlist.size());
IndexHint indexhint = hintlist.get(0);
Assert.assertEquals(3, indexhint.getIndexList().size());
Assert.assertEquals("USE", indexhint.getAction().name());
Assert.assertEquals("INDEX", indexhint.getType().name());
Assert.assertEquals("tb1 AS T1 USE INDEX (i1, i2, i3)", output);
sql = "tb1 as t1 use index (i1,i2,i3),tb2 as t2 use index (i1,i2,i3)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(2, list.size());
Assert.assertEquals(TableRefFactor.class, list.get(0).getClass());
hintlist = ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getHintList();
Assert.assertEquals(1, hintlist.size());
indexhint = hintlist.get(0);
Assert.assertEquals(3, indexhint.getIndexList().size());
Assert.assertEquals("USE", indexhint.getAction().name());
Assert.assertEquals("INDEX", indexhint.getType().name());
hintlist = ((TableRefFactor) (trs.getTableReferenceList()).get(1)).getHintList();
Assert.assertEquals(1, hintlist.size());
indexhint = hintlist.get(0);
Assert.assertEquals(3, indexhint.getIndexList().size());
Assert.assertEquals("USE", indexhint.getAction().name());
Assert.assertEquals("INDEX", indexhint.getType().name());
Assert.assertEquals("tb1 AS T1 USE INDEX (i1, i2, i3), tb2 AS T2 USE INDEX (i1, i2, i3)", output);
sql = "tb1 as t1";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(TableRefFactor.class, list.get(0).getClass());
Assert.assertEquals("tb1", ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getTable().getIdText());
Assert.assertEquals("T1", ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getAlias());
Assert.assertEquals("tb1 AS T1", output);
sql = "tb1 t1";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(TableRefFactor.class, list.get(0).getClass());
Assert.assertEquals("tb1", ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getTable().getIdText());
Assert.assertEquals("T1", ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getAlias());
Assert.assertEquals("tb1 AS T1", output);
sql = "tb1,tb2,tb3";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(3, list.size());
Assert.assertEquals(TableRefFactor.class, list.get(0).getClass());
Assert.assertEquals("tb1", ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getTable().getIdText());
Assert.assertEquals(null, ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getAlias());
Assert.assertEquals("tb2", ((TableRefFactor) (trs.getTableReferenceList()).get(1)).getTable().getIdText());
Assert.assertEquals("tb3", ((TableRefFactor) (trs.getTableReferenceList()).get(2)).getTable().getIdText());
Assert.assertEquals("tb1, tb2, tb3", output);
sql = "tb1 use key for join (i1,i2)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(TableRefFactor.class, list.get(0).getClass());
hintlist = ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getHintList();
Assert.assertEquals(1, hintlist.size());
indexhint = hintlist.get(0);
Assert.assertEquals(2, indexhint.getIndexList().size());
Assert.assertEquals("USE", indexhint.getAction().name());
Assert.assertEquals("KEY", indexhint.getType().name());
Assert.assertEquals("JOIN", indexhint.getScope().name());
Assert.assertEquals("tb1 USE KEY FOR JOIN (i1, i2)", output);
sql = "tb1 use index for group by(i1,i2)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(TableRefFactor.class, list.get(0).getClass());
hintlist = ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getHintList();
Assert.assertEquals(1, hintlist.size());
indexhint = hintlist.get(0);
Assert.assertEquals(2, indexhint.getIndexList().size());
Assert.assertEquals("USE", indexhint.getAction().name());
Assert.assertEquals("INDEX", indexhint.getType().name());
Assert.assertEquals("GROUP_BY", indexhint.getScope().name());
Assert.assertEquals("tb1 USE INDEX FOR GROUP BY (i1, i2)", output);
sql = "tb1 use key for order by (i1,i2) use key for group by () " + "ignore index for group by (i1,i2)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(TableRefFactor.class, list.get(0).getClass());
Assert.assertEquals("tb1", ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getTable().getIdText());
Assert.assertEquals(null, ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getAlias());
hintlist = ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getHintList();
Assert.assertEquals(3, hintlist.size());
indexhint = hintlist.get(0);
Assert.assertEquals(2, indexhint.getIndexList().size());
Assert.assertEquals("USE", indexhint.getAction().name());
Assert.assertEquals("KEY", indexhint.getType().name());
Assert.assertEquals("ORDER_BY", indexhint.getScope().name());
indexhint = hintlist.get(1);
Assert.assertEquals(0, indexhint.getIndexList().size());
Assert.assertEquals("USE", indexhint.getAction().name());
Assert.assertEquals("KEY", indexhint.getType().name());
Assert.assertEquals("GROUP_BY", indexhint.getScope().name());
indexhint = hintlist.get(2);
Assert.assertEquals(2, indexhint.getIndexList().size());
Assert.assertEquals("IGNORE", indexhint.getAction().name());
Assert.assertEquals("INDEX", indexhint.getType().name());
Assert.assertEquals("GROUP_BY", indexhint.getScope().name());
Assert.assertEquals("tb1 USE KEY FOR ORDER BY (i1, i2) " + "USE KEY FOR GROUP BY () IGNORE INDEX FOR GROUP BY (i1, i2)", output);
sql = "tb1 use index for order by (i1,i2) force index for group by (i1)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(TableRefFactor.class, list.get(0).getClass());
Assert.assertEquals("tb1", ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getTable().getIdText());
Assert.assertEquals(null, ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getAlias());
hintlist = ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getHintList();
Assert.assertEquals(2, hintlist.size());
indexhint = hintlist.get(0);
Assert.assertEquals(2, indexhint.getIndexList().size());
Assert.assertEquals("i1", indexhint.getIndexList().get(0));
Assert.assertEquals("i2", indexhint.getIndexList().get(1));
Assert.assertEquals("USE", indexhint.getAction().name());
Assert.assertEquals("INDEX", indexhint.getType().name());
Assert.assertEquals("ORDER_BY", indexhint.getScope().name());
indexhint = hintlist.get(1);
Assert.assertEquals(1, indexhint.getIndexList().size());
Assert.assertEquals("FORCE", indexhint.getAction().name());
Assert.assertEquals("INDEX", indexhint.getType().name());
Assert.assertEquals("GROUP_BY", indexhint.getScope().name());
Assert.assertEquals("tb1 USE INDEX FOR ORDER BY (i1, i2) FORCE INDEX FOR GROUP BY (i1)", output);
sql = "tb1 ignore key for join (i1,i2)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(TableRefFactor.class, list.get(0).getClass());
Assert.assertEquals("tb1", ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getTable().getIdText());
Assert.assertEquals(null, ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getAlias());
hintlist = ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getHintList();
Assert.assertEquals(1, hintlist.size());
indexhint = hintlist.get(0);
Assert.assertEquals(2, indexhint.getIndexList().size());
Assert.assertEquals("i1", indexhint.getIndexList().get(0));
Assert.assertEquals("i2", indexhint.getIndexList().get(1));
Assert.assertEquals("IGNORE", indexhint.getAction().name());
Assert.assertEquals("KEY", indexhint.getType().name());
Assert.assertEquals("JOIN", indexhint.getScope().name());
Assert.assertEquals("tb1 IGNORE KEY FOR JOIN (i1, i2)", output);
sql = "tb1 ignore index for group by (i1,i2)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(TableRefFactor.class, list.get(0).getClass());
Assert.assertEquals("tb1", ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getTable().getIdText());
Assert.assertEquals(null, ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getAlias());
hintlist = ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getHintList();
Assert.assertEquals(1, hintlist.size());
indexhint = hintlist.get(0);
Assert.assertEquals(2, indexhint.getIndexList().size());
Assert.assertEquals("i1", indexhint.getIndexList().get(0));
Assert.assertEquals("i2", indexhint.getIndexList().get(1));
Assert.assertEquals("IGNORE", indexhint.getAction().name());
Assert.assertEquals("INDEX", indexhint.getType().name());
Assert.assertEquals("GROUP_BY", indexhint.getScope().name());
Assert.assertEquals("tb1 IGNORE INDEX FOR GROUP BY (i1, i2)", output);
sql = "(offer a straight_join wp_image b use key for join(t1,t2) on a.member_id=b.member_id inner join product_visit c )";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
Assert.assertEquals("offer AS A STRAIGHT_JOIN wp_image AS B USE KEY FOR JOIN (t1, t2) ON a.member_id = b.member_id INNER JOIN product_visit AS C", output);
sql = "tb1 ignore index for order by(i1)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(TableRefFactor.class, list.get(0).getClass());
Assert.assertEquals("tb1", ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getTable().getIdText());
Assert.assertEquals(null, ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getAlias());
hintlist = ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getHintList();
Assert.assertEquals(1, hintlist.size());
indexhint = hintlist.get(0);
Assert.assertEquals(1, indexhint.getIndexList().size());
Assert.assertEquals("i1", indexhint.getIndexList().get(0));
Assert.assertEquals("IGNORE", indexhint.getAction().name());
Assert.assertEquals("INDEX", indexhint.getType().name());
Assert.assertEquals("ORDER_BY", indexhint.getScope().name());
Assert.assertEquals("tb1 IGNORE INDEX FOR ORDER BY (i1)", output);
sql = "tb1 force key for group by (i1,i2)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(TableRefFactor.class, list.get(0).getClass());
Assert.assertEquals("tb1", ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getTable().getIdText());
Assert.assertEquals(null, ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getAlias());
hintlist = ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getHintList();
Assert.assertEquals(1, hintlist.size());
indexhint = hintlist.get(0);
Assert.assertEquals(2, indexhint.getIndexList().size());
Assert.assertEquals("i1", indexhint.getIndexList().get(0));
Assert.assertEquals("i2", indexhint.getIndexList().get(1));
Assert.assertEquals("FORCE", indexhint.getAction().name());
Assert.assertEquals("KEY", indexhint.getType().name());
Assert.assertEquals("GROUP_BY", indexhint.getScope().name());
Assert.assertEquals("tb1 FORCE KEY FOR GROUP BY (i1, i2)", output);
sql = "tb1 force index for group by (i1,i2)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(TableRefFactor.class, list.get(0).getClass());
Assert.assertEquals("tb1", ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getTable().getIdText());
Assert.assertEquals(null, ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getAlias());
hintlist = ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getHintList();
Assert.assertEquals(1, hintlist.size());
indexhint = hintlist.get(0);
Assert.assertEquals(2, indexhint.getIndexList().size());
Assert.assertEquals("i1", indexhint.getIndexList().get(0));
Assert.assertEquals("i2", indexhint.getIndexList().get(1));
Assert.assertEquals("FORCE", indexhint.getAction().name());
Assert.assertEquals("INDEX", indexhint.getType().name());
Assert.assertEquals("GROUP_BY", indexhint.getScope().name());
Assert.assertEquals("tb1 FORCE INDEX FOR GROUP BY (i1, i2)", output);
sql = "tb1 force index for join (i1,i2)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(TableRefFactor.class, list.get(0).getClass());
Assert.assertEquals("tb1", ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getTable().getIdText());
Assert.assertEquals(null, ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getAlias());
hintlist = ((TableRefFactor) (trs.getTableReferenceList()).get(0)).getHintList();
Assert.assertEquals(1, hintlist.size());
indexhint = hintlist.get(0);
Assert.assertEquals(2, indexhint.getIndexList().size());
Assert.assertEquals("i1", indexhint.getIndexList().get(0));
Assert.assertEquals("i2", indexhint.getIndexList().get(1));
Assert.assertEquals("FORCE", indexhint.getAction().name());
Assert.assertEquals("INDEX", indexhint.getType().name());
Assert.assertEquals("JOIN", indexhint.getScope().name());
Assert.assertEquals("tb1 FORCE INDEX FOR JOIN (i1, i2)", output);
sql = "(tb1 force index for join (i1,i2) )left outer join tb2 as t2 " + "use index (i1,i2,i3) on t1.id1=t2.id1";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(OuterJoin.class, list.get(0).getClass());
Assert.assertEquals(true, (((OuterJoin) list.get(0)).isLeftJoin()));
TableReferences ltr = (TableReferences) ((OuterJoin) list.get(0)).getLeftTableRef();
Assert.assertEquals(1, ltr.getTableReferenceList().size());
Assert.assertEquals(TableRefFactor.class, ltr.getTableReferenceList().get(0).getClass());
Assert.assertEquals(null, ((TableRefFactor) (ltr.getTableReferenceList().get(0))).getAlias());
Assert.assertEquals("tb1", ((TableRefFactor) (ltr.getTableReferenceList().get(0))).getTable().getIdText());
hintlist = ((TableRefFactor) (ltr.getTableReferenceList()).get(0)).getHintList();
Assert.assertEquals(1, hintlist.size());
indexhint = hintlist.get(0);
Assert.assertEquals(2, indexhint.getIndexList().size());
Assert.assertEquals("i1", indexhint.getIndexList().get(0));
Assert.assertEquals("i2", indexhint.getIndexList().get(1));
Assert.assertEquals("FORCE", indexhint.getAction().name());
Assert.assertEquals("INDEX", indexhint.getType().name());
Assert.assertEquals("JOIN", indexhint.getScope().name());
TableRefFactor rtf = (TableRefFactor) ((OuterJoin) list.get(0)).getRightTableRef();
Assert.assertEquals("T2", rtf.getAlias());
Assert.assertEquals("tb2", rtf.getTable().getIdText());
hintlist = rtf.getHintList();
Assert.assertEquals(1, hintlist.size());
indexhint = hintlist.get(0);
Assert.assertEquals(3, indexhint.getIndexList().size());
Assert.assertEquals("i1", indexhint.getIndexList().get(0));
Assert.assertEquals("i2", indexhint.getIndexList().get(1));
Assert.assertEquals("USE", indexhint.getAction().name());
Assert.assertEquals("INDEX", indexhint.getType().name());
Assert.assertEquals("ALL", indexhint.getScope().name());
Assert.assertEquals(ComparisionEqualsExpression.class, ((OuterJoin) list.get(0)).getOnCond().getClass());
Assert.assertEquals("(tb1 FORCE INDEX FOR JOIN (i1, i2)) " + "LEFT JOIN tb2 AS T2 USE INDEX (i1, i2, i3) ON t1.id1 = t2.id1", output);
sql = " (((tb1 force index for join (i1,i2),tb3),tb4),tb5) " + "left outer join (tb2 as t2 use index (i1,i2,i3)) using(id1)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(OuterJoin.class, list.get(0).getClass());
Assert.assertEquals(true, (((OuterJoin) list.get(0)).isLeftJoin()));
ltr = (TableReferences) ((OuterJoin) list.get(0)).getLeftTableRef();
Assert.assertEquals(2, ltr.getTableReferenceList().size());
Assert.assertEquals(TableReferences.class, ltr.getTableReferenceList().get(0).getClass());
TableReferences ltr1 = (TableReferences) (ltr.getTableReferenceList()).get(0);
Assert.assertEquals(2, ltr1.getTableReferenceList().size());
Assert.assertEquals(TableReferences.class, ltr1.getTableReferenceList().get(0).getClass());
TableReferences ltr2 = (TableReferences) (ltr1.getTableReferenceList()).get(0);
Assert.assertEquals(2, ltr2.getTableReferenceList().size());
Assert.assertEquals(TableRefFactor.class, ltr2.getTableReferenceList().get(0).getClass());
Assert.assertEquals(null, ((TableRefFactor) (ltr2.getTableReferenceList().get(0))).getAlias());
Assert.assertEquals("tb1", ((TableRefFactor) (ltr2.getTableReferenceList().get(0))).getTable().getIdText());
hintlist = ((TableRefFactor) (ltr2.getTableReferenceList()).get(0)).getHintList();
Assert.assertEquals(1, hintlist.size());
indexhint = hintlist.get(0);
Assert.assertEquals(2, indexhint.getIndexList().size());
Assert.assertEquals("i1", indexhint.getIndexList().get(0));
Assert.assertEquals("i2", indexhint.getIndexList().get(1));
Assert.assertEquals("FORCE", indexhint.getAction().name());
Assert.assertEquals("INDEX", indexhint.getType().name());
Assert.assertEquals("JOIN", indexhint.getScope().name());
Assert.assertEquals(TableRefFactor.class, ltr2.getTableReferenceList().get(1).getClass());
Assert.assertEquals("tb3", ((TableRefFactor) (ltr2.getTableReferenceList().get(1))).getTable().getIdText());
Assert.assertEquals(TableRefFactor.class, ltr1.getTableReferenceList().get(1).getClass());
Assert.assertEquals("tb4", ((TableRefFactor) (ltr1.getTableReferenceList().get(1))).getTable().getIdText());
Assert.assertEquals(TableRefFactor.class, ltr.getTableReferenceList().get(1).getClass());
Assert.assertEquals("tb5", ((TableRefFactor) (ltr.getTableReferenceList().get(1))).getTable().getIdText());
TableReferences rtr = (TableReferences) ((OuterJoin) list.get(0)).getRightTableRef();
Assert.assertEquals("T2", ((TableRefFactor) rtr.getTableReferenceList().get(0)).getAlias());
Assert.assertEquals("tb2", ((TableRefFactor) rtr.getTableReferenceList().get(0)).getTable().getIdText());
hintlist = ((TableRefFactor) rtr.getTableReferenceList().get(0)).getHintList();
Assert.assertEquals(1, hintlist.size());
indexhint = hintlist.get(0);
Assert.assertEquals(3, indexhint.getIndexList().size());
Assert.assertEquals("i1", indexhint.getIndexList().get(0));
Assert.assertEquals("i2", indexhint.getIndexList().get(1));
Assert.assertEquals("USE", indexhint.getAction().name());
Assert.assertEquals("INDEX", indexhint.getType().name());
Assert.assertEquals("ALL", indexhint.getScope().name());
using_list = ((OuterJoin) (trs.getTableReferenceList()).get(0)).getUsing();
Assert.assertEquals(1, using_list.size());
Assert.assertEquals("(tb1 FORCE INDEX FOR JOIN (i1, i2), tb3, tb4, tb5) " + "LEFT JOIN (tb2 AS T2 USE INDEX (i1, i2, i3)) USING (id1)", output);
sql = "(tb1 force index for join (i1,i2),tb3) " + "left outer join tb2 as t2 use index (i1,i2,i3) using(id1,id2)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
Assert.assertEquals("(tb1 FORCE INDEX FOR JOIN (i1, i2), tb3) " + "LEFT JOIN tb2 AS T2 USE INDEX (i1, i2, i3) USING (id1, id2)", output);
sql = "(tb1 force index for join (i1,i2),tb3) left outer join (tb2 as t2 use index (i1,i2,i3)) using(id1,id2)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
Assert.assertEquals("(tb1 FORCE INDEX FOR JOIN (i1, i2), tb3) " + "LEFT JOIN (tb2 AS T2 USE INDEX (i1, i2, i3)) USING (id1, id2)", output);
sql = "tb1 as t1 cross join tb2 as t2 use index(i1)using(id1)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
Assert.assertEquals("tb1 AS T1 INNER JOIN tb2 AS T2 USE INDEX (i1) USING (id1)", output);
sql = "(tb1 as t1) cross join tb2 as t2 use index(i1)using(id1)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
Assert.assertEquals("(tb1 AS T1) INNER JOIN tb2 AS T2 USE INDEX (i1) USING (id1)", output);
sql = "tb1 as _latin't1' cross join tb2 as t2 use index(i1)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
Assert.assertEquals("tb1 AS _LATIN't1' INNER JOIN tb2 AS T2 USE INDEX (i1)", output);
sql = "((select ' @ from' from `from`)) as t1 cross join tb2 as t2 use index()";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(InnerJoin.class, list.get(0).getClass());
SubqueryFactor lsf = (SubqueryFactor) ((InnerJoin) list.get(0)).getLeftTableRef();
Assert.assertEquals("T1", lsf.getAlias());
Assert.assertEquals(DMLSelectStatement.class, lsf.getSubquery().getClass());
rtf = (TableRefFactor) ((InnerJoin) list.get(0)).getRightTableRef();
Assert.assertEquals("T2", rtf.getAlias());
hintlist = rtf.getHintList();
Assert.assertEquals(1, hintlist.size());
indexhint = hintlist.get(0);
Assert.assertEquals("USE", indexhint.getAction().name());
Assert.assertEquals("INDEX", indexhint.getType().name());
Assert.assertEquals("ALL", indexhint.getScope().name());
Assert.assertEquals("tb2", rtf.getTable().getIdText());
Assert.assertEquals("(SELECT ' @ from' FROM `from`) AS T1 " + "INNER JOIN tb2 AS T2 USE INDEX ()", output);
sql = "(tb1 as t1) straight_join (tb2 as t2)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
Assert.assertEquals("(tb1 AS T1) STRAIGHT_JOIN (tb2 AS T2)", output);
sql = "tb1 straight_join tb2 as t2 on tb1.id=tb2.id";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
Assert.assertEquals("tb1 STRAIGHT_JOIN tb2 AS T2 ON tb1.id = tb2.id", output);
sql = "tb1 left outer join tb2 on tb1.id=tb2.id";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
Assert.assertEquals("tb1 LEFT JOIN tb2 ON tb1.id = tb2.id", output);
sql = "tb1 left outer join tb2 using(id)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
Assert.assertEquals("tb1 LEFT JOIN tb2 USING (id)", output);
sql = "(tb1 right outer join tb2 using()) join tb3 on tb1.id=tb2.id and tb2.id=tb3.id";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(InnerJoin.class, list.get(0).getClass());
ltr = (TableReferences) ((InnerJoin) list.get(0)).getLeftTableRef();
Assert.assertEquals(1, ltr.getTableReferenceList().size());
TableRefFactor lltrf = (TableRefFactor) ((OuterJoin) ltr.getTableReferenceList().get(0)).getLeftTableRef();
Assert.assertEquals(null, lltrf.getAlias());
Assert.assertEquals("tb1", lltrf.getTable().getIdText());
using_list = ((OuterJoin) ltr.getTableReferenceList().get(0)).getUsing();
Assert.assertEquals(0, using_list.size());
rtf = (TableRefFactor) ((InnerJoin) list.get(0)).getRightTableRef();
Assert.assertEquals(null, rtf.getAlias());
hintlist = rtf.getHintList();
Assert.assertEquals(0, hintlist.size());
Assert.assertEquals("tb3", rtf.getTable().getIdText());
Assert.assertEquals(LogicalAndExpression.class, ((InnerJoin) list.get(0)).getOnCond().getClass());
Assert.assertEquals("(tb1 RIGHT JOIN tb2 USING ()) " + "INNER JOIN tb3 ON tb1.id = tb2.id AND tb2.id = tb3.id", output);
sql = "tb1 right outer join tb2 using(id1,id2) " + "join (tb3,tb4) on tb1.id=tb2.id and tb2.id=tb3.id";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(InnerJoin.class, list.get(0).getClass());
OuterJoin loj = (OuterJoin) ((InnerJoin) list.get(0)).getLeftTableRef();
lltrf = (TableRefFactor) loj.getLeftTableRef();
Assert.assertEquals(null, lltrf.getAlias());
Assert.assertEquals("tb1", lltrf.getTable().getIdText());
using_list = loj.getUsing();
Assert.assertEquals(2, using_list.size());
rtr = (TableReferences) ((InnerJoin) list.get(0)).getRightTableRef();
Assert.assertEquals(2, rtr.getTableReferenceList().size());
Assert.assertEquals("tb3", ((TableRefFactor) (rtr.getTableReferenceList().get(0))).getTable().getIdText());
Assert.assertEquals("tb4", ((TableRefFactor) (rtr.getTableReferenceList().get(1))).getTable().getIdText());
Assert.assertEquals(LogicalAndExpression.class, ((InnerJoin) list.get(0)).getOnCond().getClass());
Assert.assertEquals("tb1 RIGHT JOIN tb2 USING (id1, id2) " + "INNER JOIN (tb3, tb4) ON tb1.id = tb2.id AND tb2.id = tb3.id", output);
sql = "tb1 left outer join tb2 join tb3 using(id)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
Assert.assertEquals("tb1 LEFT JOIN (tb2 INNER JOIN tb3) USING (id)", output);
sql = "tb1 right join tb2 on tb1.id=tb2.id";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
Assert.assertEquals("tb1 RIGHT JOIN tb2 ON tb1.id = tb2.id", output);
sql = "tb1 natural right join tb2 ";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
Assert.assertEquals("tb1 NATURAL RIGHT JOIN tb2", output);
sql = "tb1 natural right outer join tb2 natural left outer join tb3";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(NaturalJoin.class, list.get(0).getClass());
NaturalJoin lnj = (NaturalJoin) ((NaturalJoin) list.get(0)).getLeftTableRef();
lltrf = (TableRefFactor) lnj.getLeftTableRef();
Assert.assertEquals(null, lltrf.getAlias());
Assert.assertEquals("tb1", lltrf.getTable().getIdText());
TableRefFactor rltrf = (TableRefFactor) lnj.getRightTableRef();
Assert.assertEquals(null, rltrf.getAlias());
Assert.assertEquals("tb2", rltrf.getTable().getIdText());
rtf = (TableRefFactor) ((NaturalJoin) list.get(0)).getRightTableRef();
Assert.assertEquals(null, rtf.getAlias());
Assert.assertEquals("tb3", rtf.getTable().getIdText());
Assert.assertEquals("tb1 NATURAL RIGHT JOIN tb2 NATURAL LEFT JOIN tb3", output);
sql = "tb1 natural left outer join tb2 ";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
Assert.assertEquals("tb1 NATURAL LEFT JOIN tb2", output);
sql = "(tb1 t1) natural join (tb2 as t2) ";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
Assert.assertEquals("(tb1 AS T1) NATURAL JOIN (tb2 AS T2)", output);
sql = "(select (select * from tb1) from `select` " + "where `any`=any(select id2 from tb2))any ";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(SubqueryFactor.class, list.get(0).getClass());
Assert.assertEquals("ANY", ((SubqueryFactor) list.get(0)).getAlias());
Assert.assertEquals("(SELECT SELECT * FROM tb1 FROM `select` " + "WHERE `any` = ANY (SELECT id2 FROM tb2)) AS ANY", output);
sql = "((tb1),(tb3 as t3,`select`),tb2 use key for join (i1,i2))" + " left join tb4 join tb5 using ()";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(OuterJoin.class, list.get(0).getClass());
Assert.assertEquals(TableReferences.class, ((OuterJoin) list.get(0)).getLeftTableRef().getClass());
Assert.assertEquals(InnerJoin.class, ((OuterJoin) list.get(0)).getRightTableRef().getClass());
list = ((TableReferences) ((OuterJoin) list.get(0)).getLeftTableRef()).getTableReferenceList();
list = ((TableReferences) list.get(1)).getTableReferenceList();
Assert.assertEquals(2, list.size());
Assert.assertEquals("(tb1, tb3 AS T3, `select`, tb2 USE KEY FOR JOIN (i1, i2))" + " LEFT JOIN (tb4 INNER JOIN tb5) USING ()", output);
sql = "((select `select` from `from` ) tb1),(tb3 as t3,`select`),tb2 use key for join (i1,i2) " + "left join tb4 using (i1,i2)straight_join tb5";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(3, list.size());
Assert.assertEquals(TableReferences.class, list.get(0).getClass());
Assert.assertEquals(TableReferences.class, list.get(1).getClass());
Assert.assertEquals(StraightJoin.class, list.get(2).getClass());
list = ((TableReferences) list.get(0)).getTableReferenceList();
Assert.assertEquals(SubqueryFactor.class, list.get(0).getClass());
list = trs.getTableReferenceList();
list = ((TableReferences) list.get(1)).getTableReferenceList();
Assert.assertEquals(TableRefFactor.class, list.get(0).getClass());
Assert.assertEquals(TableRefFactor.class, list.get(1).getClass());
list = trs.getTableReferenceList();
StraightJoin sj = (StraightJoin) list.get(2);
Assert.assertEquals(OuterJoin.class, sj.getLeftTableRef().getClass());
Assert.assertEquals(TableRefFactor.class, sj.getRightTableRef().getClass());
OuterJoin oj = (OuterJoin) sj.getLeftTableRef();
using_list = oj.getUsing();
Assert.assertEquals(2, using_list.size());
Assert.assertEquals("(SELECT `select` FROM `from`) AS TB1, tb3 AS T3, `select`, tb2 USE KEY FOR JOIN (i1, i2) LEFT JOIN tb4 USING (i1, i2) STRAIGHT_JOIN tb5", output);
sql = "(`select`,(tb1 as t1 use index for join()ignore key for group by (i1)))" + "join tb2 on cd1=any " + "right join " + "tb3 straight_join " + "(tb4 use index() left outer join (tb6,tb7) on id3=all(select `all` from `all`)) " + " on id2=any(select * from any) using (i1)";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
trs = parser.tableRefs();
output = output2MySQL(trs, sql);
list = trs.getTableReferenceList();
Assert.assertEquals(1, list.size());
Assert.assertEquals(OuterJoin.class, list.get(0).getClass());
using_list = ((OuterJoin) list.get(0)).getUsing();
Assert.assertEquals(1, using_list.size());
Assert.assertEquals(InnerJoin.class, ((OuterJoin) (list.get(0))).getLeftTableRef().getClass());
Assert.assertEquals(StraightJoin.class, ((OuterJoin) (list.get(0))).getRightTableRef().getClass());
StraightJoin rsj = (StraightJoin) ((OuterJoin) (list.get(0))).getRightTableRef();
Assert.assertEquals(TableRefFactor.class, rsj.getLeftTableRef().getClass());
Assert.assertEquals(TableReferences.class, rsj.getRightTableRef().getClass());
list = ((TableReferences) rsj.getRightTableRef()).getTableReferenceList();
Assert.assertEquals(OuterJoin.class, list.get(0).getClass());
Assert.assertEquals("(`select`, tb1 AS T1 USE INDEX FOR JOIN () IGNORE KEY FOR GROUP BY (i1)) " + "INNER JOIN tb2 ON cd1 = any RIGHT JOIN (tb3 STRAIGHT_JOIN (tb4 USE INDEX () " + "LEFT JOIN (tb6, tb7) ON id3 = ALL (SELECT `all` FROM `all`)) ON id2 = ANY (SELECT * FROM any))" + " USING (i1)", output);
}
use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.
the class MySQLDMLParserTest method testOrderBy.
public void testOrderBy() throws SQLSyntaxErrorException {
String sql = "order by c1 asc, c2 desc , c3 ";
MySQLLexer lexer = new MySQLLexer(sql);
MySQLDMLParser parser = getDMLParser(lexer);
OrderBy orderBy = parser.orderBy();
String output = output2MySQL(orderBy, 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)), orderBy.getOrderByList());
Assert.assertEquals("ORDER BY c1, c2 DESC, c3", output);
sql = "order by c1 ";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
orderBy = parser.orderBy();
output = output2MySQL(orderBy, sql);
ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC)), orderBy.getOrderByList());
Assert.assertEquals("ORDER BY c1", output);
sql = "order by c1 asc ";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
orderBy = parser.orderBy();
output = output2MySQL(orderBy, sql);
ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC)), orderBy.getOrderByList());
Assert.assertEquals("ORDER BY c1", output);
sql = "order by c1 desc ";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
orderBy = parser.orderBy();
output = output2MySQL(orderBy, sql);
ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.DESC)), orderBy.getOrderByList());
Assert.assertEquals("ORDER BY c1 DESC", output);
}
use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.
the class MySQLDMLSelectParserTest method testSelectUnion.
public void testSelectUnion() throws SQLSyntaxErrorException {
String sql = "(select id from t1) union all (select id from t2) union all (select id from t3) ordeR By d desC limit 1 offset ?";
MySQLLexer lexer = new MySQLLexer(sql);
MySQLDMLSelectParser parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
DMLSelectUnionStatement select = (DMLSelectUnionStatement) parser.selectUnion();
Assert.assertEquals(0, select.getFirstDistinctIndex());
Assert.assertEquals(3, select.getSelectStmtList().size());
String output = output2MySQL(select, sql);
Assert.assertEquals("(SELECT id FROM t1) UNION ALL (SELECT id FROM t2) UNION ALL (SELECT id FROM t3) ORDER BY d DESC LIMIT ?, 1", output);
sql = "(select id from t1) union select id from t2 order by id union aLl (select id from t3) ordeR By d asC";
lexer = new MySQLLexer(sql);
parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
select = (DMLSelectUnionStatement) parser.selectUnion();
Assert.assertEquals(1, select.getFirstDistinctIndex());
Assert.assertEquals(3, select.getSelectStmtList().size());
output = output2MySQL(select, sql);
Assert.assertEquals("(SELECT id FROM t1) UNION (SELECT id FROM t2 ORDER BY id) UNION ALL (SELECT id FROM t3) ORDER BY d", output);
sql = "(select id from t1) union distInct (select id from t2) union select id from t3";
lexer = new MySQLLexer(sql);
parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
select = (DMLSelectUnionStatement) parser.selectUnion();
Assert.assertEquals(2, select.getFirstDistinctIndex());
Assert.assertEquals(3, select.getSelectStmtList().size());
output = output2MySQL(select, sql);
Assert.assertEquals("(SELECT id FROM t1) UNION (SELECT id FROM t2) UNION (SELECT id FROM t3)", output);
}
use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.
the class MySQLDMLSelectParserTest method testSelect.
public void testSelect() throws SQLSyntaxErrorException {
String sql = "SELect t1.id , t2.* from t1, test.t2 where test.t1.id=1 and t1.id=test.t2.id";
MySQLLexer lexer = new MySQLLexer(sql);
MySQLDMLSelectParser parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
DMLSelectStatement select = parser.select();
Assert.assertNotNull(select);
String output = output2MySQL(select, sql);
Assert.assertEquals("SELECT t1.id, t2.* FROM t1, test.t2 WHERE test.t1.id = 1 AND t1.id = test.t2.id", output);
sql = "select * from offer a straight_join wp_image b use key for join(t1,t2) on a.member_id=b.member_id inner join product_visit c where a.member_id=c.member_id and c.member_id='abc' ";
lexer = new MySQLLexer(sql);
parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
select = parser.select();
Assert.assertNotNull(select);
output = output2MySQL(select, sql);
Assert.assertEquals("SELECT * FROM offer AS A STRAIGHT_JOIN wp_image AS B USE KEY FOR JOIN (t1, t2) ON a.member_id = b.member_id INNER JOIN product_visit AS C WHERE a.member_id = c.member_id AND c.member_id = 'abc'", output);
sql = "SELect all tb1.id,tb2.id from tb1,tb2 where tb1.id2=tb2.id2";
lexer = new MySQLLexer(sql);
parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
select = parser.select();
Assert.assertNotNull(select);
output = output2MySQL(select, sql);
Assert.assertEquals("SELECT tb1.id, tb2.id FROM tb1, tb2 WHERE tb1.id2 = tb2.id2", output);
sql = "SELect distinct high_priority tb1.id,tb2.id from tb1,tb2 where tb1.id2=tb2.id2";
lexer = new MySQLLexer(sql);
parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
select = parser.select();
Assert.assertNotNull(select);
output = output2MySQL(select, sql);
Assert.assertEquals("SELECT DISTINCT HIGH_PRIORITY tb1.id, tb2.id FROM tb1, tb2 WHERE tb1.id2 = tb2.id2", output);
sql = "SELect distinctrow high_priority sql_small_result tb1.id,tb2.id " + "from tb1,tb2 where tb1.id2=tb2.id2";
lexer = new MySQLLexer(sql);
parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
select = parser.select();
Assert.assertNotNull(select);
output = output2MySQL(select, sql);
Assert.assertEquals("SELECT DISTINCTROW HIGH_PRIORITY SQL_SMALL_RESULT tb1.id, tb2.id FROM tb1, tb2 WHERE tb1.id2 = tb2.id2", output);
sql = "SELect sql_cache id1,id2 from tb1,tb2 where tb1.id1=tb2.id1 ";
lexer = new MySQLLexer(sql);
parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
select = parser.select();
Assert.assertNotNull(select);
output = output2MySQL(select, sql);
Assert.assertEquals("SELECT SQL_CACHE id1, id2 FROM tb1, tb2 WHERE tb1.id1 = tb2.id1", output);
sql = "SELect sql_cache id1,max(id2) from tb1 group by id1 having id1>10 order by id3 desc";
lexer = new MySQLLexer(sql);
parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
select = parser.select();
Assert.assertNotNull(select);
output = output2MySQL(select, sql);
Assert.assertEquals("SELECT SQL_CACHE id1, MAX(id2) FROM tb1 GROUP BY id1 HAVING id1 > 10 ORDER BY id3 DESC", output);
sql = "SELect SQL_BUFFER_RESULT tb1.id1,id2 from tb1";
lexer = new MySQLLexer(sql);
parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
select = parser.select();
Assert.assertNotNull(select);
output = output2MySQL(select, sql);
Assert.assertEquals("SELECT SQL_BUFFER_RESULT tb1.id1, id2 FROM tb1", output);
sql = "SELect SQL_no_cache tb1.id1,id2 from tb1";
lexer = new MySQLLexer(sql);
parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
select = parser.select();
Assert.assertNotNull(select);
output = output2MySQL(select, sql);
Assert.assertEquals("SELECT SQL_NO_CACHE tb1.id1, id2 FROM tb1", output);
sql = "SELect SQL_CALC_FOUND_ROWS tb1.id1,id2 from tb1";
lexer = new MySQLLexer(sql);
parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
select = parser.select();
Assert.assertNotNull(select);
output = output2MySQL(select, sql);
Assert.assertEquals("SELECT SQL_CALC_FOUND_ROWS tb1.id1, id2 FROM tb1", output);
sql = "SELect 1+1 ";
lexer = new MySQLLexer(sql);
parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
select = parser.select();
Assert.assertNotNull(select);
output = output2MySQL(select, sql);
Assert.assertEquals("SELECT 1 + 1", output);
sql = "SELect t1.* from tb ";
lexer = new MySQLLexer(sql);
parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
select = parser.select();
Assert.assertNotNull(select);
output = output2MySQL(select, sql);
Assert.assertEquals("SELECT t1.* FROM tb", output);
sql = "SELect distinct high_priority straight_join sql_big_result sql_cache tb1.id,tb2.id " + "from tb1,tb2 where tb1.id2=tb2.id2";
lexer = new MySQLLexer(sql);
parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
select = parser.select();
Assert.assertNotNull(select);
output = output2MySQL(select, sql);
Assert.assertEquals("SELECT DISTINCT HIGH_PRIORITY STRAIGHT_JOIN SQL_BIG_RESULT" + " SQL_CACHE tb1.id, tb2.id FROM tb1, tb2 WHERE tb1.id2 = tb2.id2", output);
sql = "SELect distinct id1,id2 from tb1,tb2 where tb1.id1=tb2.id2 for update";
lexer = new MySQLLexer(sql);
parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
select = parser.select();
Assert.assertNotNull(select);
output = output2MySQL(select, sql);
Assert.assertEquals("SELECT DISTINCT id1, id2 FROM tb1, tb2 WHERE tb1.id1 = tb2.id2 FOR UPDATE", output);
sql = "SELect distinct id1,id2 from tb1,tb2 where tb1.id1=tb2.id2 lock in share mode";
lexer = new MySQLLexer(sql);
parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
select = parser.select();
Assert.assertNotNull(select);
output = output2MySQL(select, sql);
Assert.assertEquals("SELECT DISTINCT id1, id2 FROM tb1, tb2 WHERE tb1.id1 = tb2.id2 LOCK IN SHARE MODE", output);
}
use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.
the class MySQLDMLSelectParserTest method main.
@SuppressWarnings("unused")
public static void main(String[] ars) throws Exception {
String sql = Performance.SQL_BENCHMARK_SELECT;
for (int i = 0; i < 3; ++i) {
MySQLLexer lexer = new MySQLLexer(sql);
MySQLExprParser exprParser = new MySQLExprParser(lexer);
MySQLDMLSelectParser parser = new MySQLDMLSelectParser(lexer, exprParser);
QueryExpression stmt = parser.select();
// System.out.println(stmt);
}
Thread.sleep(1000);
long loop = 300 * 10000;
long t1 = System.currentTimeMillis();
t1 = System.currentTimeMillis();
for (long i = 0; i < loop; ++i) {
MySQLLexer lexer = new MySQLLexer(sql);
MySQLExprParser exprParser = new MySQLExprParser(lexer);
MySQLDMLSelectParser parser = new MySQLDMLSelectParser(lexer, exprParser);
QueryExpression stmt = parser.select();
}
long t2 = System.currentTimeMillis();
System.out.println((t2 - t1) * 1000.0d / loop + " us");
}
Aggregations