use of com.alibaba.cobar.parser.ast.expression.primary.RowExpression in project cobar by alibaba.
the class MySQLDMLInsertReplaceParser method rowList.
protected List<RowExpression> rowList() throws SQLSyntaxErrorException {
List<RowExpression> valuesList;
List<Expression> tempRowValue = rowValue();
if (lexer.token() == PUNC_COMMA) {
valuesList = new LinkedList<RowExpression>();
valuesList.add(new RowExpression(tempRowValue));
for (; lexer.token() == PUNC_COMMA; ) {
lexer.nextToken();
tempRowValue = rowValue();
valuesList.add(new RowExpression(tempRowValue));
}
} else {
valuesList = new ArrayList<RowExpression>(1);
valuesList.add(new RowExpression(tempRowValue));
}
return valuesList;
}
use of com.alibaba.cobar.parser.ast.expression.primary.RowExpression in project cobar by alibaba.
the class MySQLOutputASTVisitor method visit.
@Override
public void visit(DMLReplaceStatement node) {
appendable.append("REPLACE ");
switch(node.getMode()) {
case DELAY:
appendable.append("DELAYED ");
break;
case LOW:
appendable.append("LOW_PRIORITY ");
break;
case UNDEF:
break;
default:
throw new IllegalArgumentException("unknown mode for INSERT: " + node.getMode());
}
appendable.append("INTO ");
node.getTable().accept(this);
appendable.append(' ');
List<Identifier> cols = node.getColumnNameList();
if (cols != null && !cols.isEmpty()) {
appendable.append('(');
printList(cols);
appendable.append(") ");
}
QueryExpression select = node.getSelect();
if (select == null) {
appendable.append("VALUES ");
List<RowExpression> rows = node.getRowList();
if (rows != null && !rows.isEmpty()) {
boolean isFst = true;
for (RowExpression row : rows) {
if (row == null || row.getRowExprList().isEmpty())
continue;
if (isFst)
isFst = false;
else
appendable.append(", ");
appendable.append('(');
printList(row.getRowExprList());
appendable.append(')');
}
} else {
throw new IllegalArgumentException("at least one row for REPLACE");
}
} else {
select.accept(this);
}
}
use of com.alibaba.cobar.parser.ast.expression.primary.RowExpression in project cobar by alibaba.
the class PartitionKeyVisitor method insertReplace.
private void insertReplace(DMLInsertReplaceStatement node) {
Identifier table = node.getTable();
List<Identifier> collist = node.getColumnNameList();
QueryExpression query = node.getSelect();
List<RowExpression> rows = node.getRowList();
tableAsTableFactor(table);
String tableName = table.getIdTextUpUnescape();
visitChild(2, false, false, collist);
if (query != null) {
query.accept(this);
return;
}
for (RowExpression row : rows) {
visitChild(2, false, false, row);
}
Map<String, List<Object>> colVals = ensureColumnValueByTable(tableName);
Map<String, Map<Object, Set<Pair<Expression, ASTNode>>>> colValsIndex = ensureColumnValueIndexByTable(tableName);
if (collist != null) {
for (int i = 0; i < collist.size(); ++i) {
String colName = collist.get(i).getIdTextUpUnescape();
if (isRuledColumn(tableName, colName)) {
List<Object> valueList = ensureColumnValueList(colVals, colName);
Map<Object, Set<Pair<Expression, ASTNode>>> valMap = ensureColumnValueIndexObjMap(colValsIndex, colName);
for (RowExpression row : rows) {
Expression expr = row.getRowExprList().get(i);
Object value = expr == null ? null : expr.evaluation(evaluationParameter);
if (value != Expression.UNEVALUATABLE) {
valueList.add(value);
addIntoColumnValueIndex(valMap, value, row, node);
}
}
}
}
}
}
use of com.alibaba.cobar.parser.ast.expression.primary.RowExpression in project cobar by alibaba.
the class ServerRouter method dispatchInsertReplace.
@SuppressWarnings("unchecked")
private static void dispatchInsertReplace(RouteResultsetNode[] rn, DMLInsertReplaceStatement stmt, List<String> ruleColumns, Map<Integer, List<Object[]>> dataNodeMap, TableConfig matchedTable, String originalSQL, PartitionKeyVisitor visitor) {
if (stmt.getSelect() != null) {
dispatchWhereBasedStmt(rn, stmt, ruleColumns, dataNodeMap, matchedTable, originalSQL, visitor);
return;
}
Map<String, Map<Object, Set<Pair<Expression, ASTNode>>>> colsIndex = visitor.getColumnIndex(stmt.getTable().getIdTextUpUnescape());
if (colsIndex == null || colsIndex.isEmpty()) {
throw new IllegalArgumentException("columns index is empty: " + originalSQL);
}
ArrayList<Map<Object, Set<Pair<Expression, ASTNode>>>> colsIndexList = new ArrayList<Map<Object, Set<Pair<Expression, ASTNode>>>>(ruleColumns.size());
for (int i = 0, len = ruleColumns.size(); i < len; ++i) {
colsIndexList.add(colsIndex.get(ruleColumns.get(i)));
}
int dataNodeId = -1;
for (Entry<Integer, List<Object[]>> en : dataNodeMap.entrySet()) {
List<Object[]> tuples = en.getValue();
HashSet<RowExpression> replaceRowList = new HashSet<RowExpression>(tuples.size());
for (Object[] tuple : tuples) {
Set<Pair<Expression, ASTNode>> tupleExprs = null;
for (int i = 0; i < tuple.length; ++i) {
Map<Object, Set<Pair<Expression, ASTNode>>> valueMap = colsIndexList.get(i);
Object value = tuple[i];
Set<Pair<Expression, ASTNode>> set = getExpressionSet(valueMap, value);
tupleExprs = (Set<Pair<Expression, ASTNode>>) CollectionUtil.intersectSet(tupleExprs, set);
}
if (tupleExprs == null || tupleExprs.isEmpty()) {
throw new IllegalArgumentException("route: empty expression list for insertReplace stmt: " + originalSQL);
}
for (Pair<Expression, ASTNode> p : tupleExprs) {
if (p.getValue() == stmt && p.getKey() instanceof RowExpression) {
replaceRowList.add((RowExpression) p.getKey());
}
}
}
stmt.setReplaceRowList(new ArrayList<RowExpression>(replaceRowList));
String sql = genSQL(stmt, originalSQL);
stmt.clearReplaceRowList();
String dataNodeName = matchedTable.getDataNodes()[en.getKey()];
rn[++dataNodeId] = new RouteResultsetNode(dataNodeName, sql);
}
}
use of com.alibaba.cobar.parser.ast.expression.primary.RowExpression in project cobar by alibaba.
the class MySQLExprParserTest method testPrimary.
public void testPrimary() throws Exception {
String sql = "(1,2,existS (select id.* from t1))";
MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("ROW(1, 2, EXISTS (SELECT id.* FROM t1))", output);
RowExpression row = (RowExpression) expr;
Assert.assertEquals(3, row.getRowExprList().size());
sql = "*";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("*", output);
Assert.assertTrue(Wildcard.class.isAssignableFrom(expr.getClass()));
sql = "case v1 when `index` then a when 2 then b else c end";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("CASE v1 WHEN `index` THEN a WHEN 2 THEN b ELSE c END", output);
CaseWhenOperatorExpression cw = (CaseWhenOperatorExpression) expr;
Assert.assertEquals("v1", ((Identifier) cw.getComparee()).getIdText());
Assert.assertEquals(2, cw.getWhenList().size());
Assert.assertEquals("c", ((Identifier) cw.getElseResult()).getIdText());
sql = "case when 1=value then a end";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("CASE WHEN 1 = value THEN a END", output);
cw = (CaseWhenOperatorExpression) expr;
Assert.assertNull(cw.getComparee());
Assert.assertEquals(1, cw.getWhenList().size());
Assert.assertNull(cw.getElseResult());
sql = "case when 1=`in` then a end";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("CASE WHEN 1 = `in` THEN a END", output);
sql = " ${INSENSITIVE}. ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("${INSENSITIVE}", output);
sql = "current_date, ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("CURDATE()", output);
sql = "CurRent_Date ( ) ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("CURDATE()", output);
sql = "CurRent_TiMe ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("CURTIME()", output);
sql = "CurRent_TiMe () ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("CURTIME()", output);
sql = "CurRent_TimesTamp ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("NOW()", output);
sql = "CurRent_TimesTamp ()";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("NOW()", output);
sql = "localTimE";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("NOW()", output);
sql = "localTimE () ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("NOW()", output);
sql = "localTimesTamP ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("NOW()", output);
sql = "localTimesTamP () ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("NOW()", output);
sql = "CurRent_user ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("CURRENT_USER()", output);
sql = "CurRent_user () ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("CURRENT_USER()", output);
sql = "default () ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("DEFAULT()", output);
sql = "vaLueS(1,col1*2)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("VALUES(1, col1 * 2)", output);
sql = "(1,2,mod(m,n))";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("ROW(1, 2, m % n)", output);
sql = "chaR (77,121,'77.3')";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("CHAR(77, 121, '77.3')", output);
sql = "CHARSET(CHAR(0x65 USING utf8))";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("CHARSET(CHAR(x'65' USING utf8))", output);
sql = "CONVERT(_latin1'Müller' USING utf8)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("CONVERT(_latin1'Müller' USING utf8)", output);
// QS_TODO
}
Aggregations