use of com.alibaba.cobar.parser.ast.expression.Expression 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());
}
use of com.alibaba.cobar.parser.ast.expression.Expression 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);
}
use of com.alibaba.cobar.parser.ast.expression.Expression 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.ast.expression.Expression in project cobar by alibaba.
the class PartitionByStringTest method testPartitionStartEqEnd.
/**
* start == end , except 0:0,
*/
@SuppressWarnings("unchecked")
public void testPartitionStartEqEnd() {
// 同号,不越界
PartitionByString sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("1:1");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("-5:-5");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
// 异号,不越界
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("3:-7");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("5:-5");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
// 同号,边界值
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("0:0");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(641, (int) execute(sut, "skkdifisd-"));
Assert.assertEquals(74, (int) execute(sut, "sdsdfsafaw"));
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("10:10");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
// 异号,边界值
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("0:-10");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("-1:9");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
// 同号,越界
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("-15:-15");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("15:15");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
// 异号,越界,不存在
}
use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.
the class PartitionByStringTest method testPartitionStartgtEnd.
@SuppressWarnings("unchecked")
public void testPartitionStartgtEnd() {
String testKey = "abcdefghij";
// 同号,不越界
PartitionByString sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("1:6");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(36, (int) execute(sut, testKey));
Assert.assertEquals(36, (int) execute(sut, "a" + testKey.substring(1, 6) + "abcd"));
Assert.assertEquals(36, (int) execute(sut, "b" + testKey.substring(1, 6) + "sila"));
Assert.assertTrue((36 != (int) execute(sut, "c" + testKey.substring(1, 5) + "sil2")));
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("-8:-5");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(36, (int) execute(sut, testKey));
Assert.assertEquals(36, (int) execute(sut, "12" + testKey.substring(2, 5) + "12345"));
Assert.assertEquals(36, (int) execute(sut, "45" + testKey.substring(2, 5) + "78923"));
// 异号,不越界
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("-9:9");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(260, (int) execute(sut, "a" + testKey.substring(1, 9) + "8"));
Assert.assertEquals(260, (int) execute(sut, "f" + testKey.substring(1, 9) + "*"));
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("2:-1");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(934, (int) execute(sut, "ab" + testKey.substring(2, 9) + "8"));
Assert.assertEquals(934, (int) execute(sut, "fj" + testKey.substring(2, 9) + "*"));
// 同号,边界值, 双边界
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("0:9");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(101, (int) execute(sut, testKey.substring(0, 9) + "#"));
Assert.assertEquals(101, (int) execute(sut, testKey.substring(0, 9) + "*"));
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("-10:-1");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(101, (int) execute(sut, testKey.substring(0, 9) + "#"));
Assert.assertEquals(101, (int) execute(sut, testKey.substring(0, 9) + "*"));
// 同号,边界值, 单边界
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("0:5");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(99, (int) execute(sut, testKey.substring(0, 5) + "#uiyt"));
Assert.assertEquals(99, (int) execute(sut, testKey.substring(0, 5) + "*rfsj"));
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("5:9");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(386, (int) execute(sut, "#uiyt" + testKey.substring(5, 9) + "a"));
Assert.assertEquals(386, (int) execute(sut, "*rfsj" + testKey.substring(5, 9) + "%"));
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("-10:-7");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(36, (int) execute(sut, testKey.substring(0, 5) + "#uiyt45"));
Assert.assertEquals(36, (int) execute(sut, testKey.substring(0, 5) + "*rfsjkm"));
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("-4:-1");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(936, (int) execute(sut, "#uiyt4" + testKey.substring(5, 9) + "a"));
Assert.assertEquals(936, (int) execute(sut, "*rfsj$" + testKey.substring(5, 9) + "%"));
// 异号,边界值,双边界
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("-10:9");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(101, (int) execute(sut, testKey.substring(0, 9) + "a"));
Assert.assertEquals(101, (int) execute(sut, testKey.substring(0, 9) + "%"));
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("0:-1");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(101, (int) execute(sut, testKey.substring(0, 9) + "a"));
Assert.assertEquals(101, (int) execute(sut, testKey.substring(0, 9) + "%"));
// 异号,边界值,单边界
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("-10:4");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(66, (int) execute(sut, testKey.substring(0, 4) + "asdebh"));
Assert.assertEquals(66, (int) execute(sut, testKey.substring(0, 4) + "%^&*()"));
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("0:-6");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(66, (int) execute(sut, testKey.substring(0, 4) + "asdebh"));
Assert.assertEquals(66, (int) execute(sut, testKey.substring(0, 4) + "%^&*()"));
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("-5:9");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(386, (int) execute(sut, "#uiyt" + testKey.substring(5, 9) + "a"));
Assert.assertEquals(386, (int) execute(sut, "*rfsj" + testKey.substring(5, 9) + "%"));
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("5:-1");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(386, (int) execute(sut, "#uiyt" + testKey.substring(5, 9) + "a"));
Assert.assertEquals(386, (int) execute(sut, "*rfsj" + testKey.substring(5, 9) + "%"));
// 同号,双越界
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("11:15");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("-20:-15");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
// 同号,单越界
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("-15:-8");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(33, (int) execute(sut, testKey.substring(0, 2) + "dskfdijc"));
Assert.assertEquals(33, (int) execute(sut, testKey.substring(0, 2) + "cuiejdjj"));
Assert.assertEquals(129, (int) execute(sut, "$%cuiejdjj"));
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("6:15");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(450, (int) execute(sut, "#uiyt#" + testKey.substring(6, 10)));
Assert.assertEquals(450, (int) execute(sut, "*rfsj*" + testKey.substring(6, 10)));
Assert.assertEquals(345, (int) execute(sut, "#uiyt#" + "dkug"));
// 异号,双越界
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("-20:19");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(165, (int) execute(sut, testKey));
Assert.assertEquals(725, (int) execute(sut, "1" + testKey.substring(1, 10)));
// 异号,单越界
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("-8:15");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(388, (int) execute(sut, "1q" + testKey.substring(2, 10)));
Assert.assertEquals(388, (int) execute(sut, "sd" + testKey.substring(2, 10)));
sut = new PartitionByString("test ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
sut.setCacheEvalRst(false);
sut.setHashSlice("-15:6");
sut.setPartitionCount("1024");
sut.setPartitionLength("1");
sut.init();
Assert.assertEquals(99, (int) execute(sut, testKey.substring(0, 6) + "abcd"));
Assert.assertEquals(99, (int) execute(sut, testKey.substring(0, 6) + "efgh"));
}
Aggregations