use of com.alibaba.cobar.parser.ast.expression.ReplacableExpression in project cobar by alibaba.
the class ServerRouter method replacePartitionKeyOperand.
private static void replacePartitionKeyOperand(Map<String, Map<Object, Set<Pair<Expression, ASTNode>>>> index, List<String> cols) {
if (cols == null) {
return;
}
for (String col : cols) {
Map<Object, Set<Pair<Expression, ASTNode>>> map = index.get(col);
if (map == null) {
continue;
}
for (Set<Pair<Expression, ASTNode>> set : map.values()) {
if (set == null) {
continue;
}
for (Pair<Expression, ASTNode> p : set) {
Expression expr = p.getKey();
ASTNode parent = p.getValue();
if (PartitionKeyVisitor.isPartitionKeyOperandSingle(expr, parent)) {
((ReplacableExpression) expr).setReplaceExpr(ReplacableExpression.BOOL_FALSE);
} else if (PartitionKeyVisitor.isPartitionKeyOperandIn(expr, parent)) {
((ReplacableExpression) parent).setReplaceExpr(ReplacableExpression.BOOL_FALSE);
}
}
}
}
}
use of com.alibaba.cobar.parser.ast.expression.ReplacableExpression in project cobar by alibaba.
the class ServerRouter method dispatchWhereBasedStmt.
private static void dispatchWhereBasedStmt(RouteResultsetNode[] rn, SQLStatement stmtAST, List<String> ruleColumns, Map<Integer, List<Object[]>> dataNodeMap, TableConfig matchedTable, String originalSQL, PartitionKeyVisitor visitor) {
// [perf tag] 11.617 us: sharding multivalue
if (ruleColumns.size() > 1) {
String sql;
if (visitor.isSchemaTrimmed()) {
sql = genSQL(stmtAST, originalSQL);
} else {
sql = originalSQL;
}
int i = -1;
for (Integer dataNodeId : dataNodeMap.keySet()) {
String dataNode = matchedTable.getDataNodes()[dataNodeId];
rn[++i] = new RouteResultsetNode(dataNode, sql);
}
return;
}
final String table = matchedTable.getName();
Map<String, Map<Object, Set<Pair<Expression, ASTNode>>>> columnIndex = visitor.getColumnIndex(table);
Map<Object, Set<Pair<Expression, ASTNode>>> valueMap = columnIndex.get(ruleColumns.get(0));
replacePartitionKeyOperand(columnIndex, ruleColumns);
Map<InExpression, Set<Expression>> unreplacedInExpr = new HashMap<InExpression, Set<Expression>>(1, 1);
Set<ReplacableExpression> unreplacedSingleExprs = new HashSet<ReplacableExpression>();
// [perf tag] 12.2755 us: sharding multivalue
int nodeId = -1;
for (Entry<Integer, List<Object[]>> en : dataNodeMap.entrySet()) {
List<Object[]> tuples = en.getValue();
unreplacedSingleExprs.clear();
unreplacedInExpr.clear();
for (Object[] tuple : tuples) {
Object value = tuple[0];
Set<Pair<Expression, ASTNode>> indexedExpressionPair = getExpressionSet(valueMap, value);
for (Pair<Expression, ASTNode> pair : indexedExpressionPair) {
Expression expr = pair.getKey();
ASTNode parent = pair.getValue();
if (PartitionKeyVisitor.isPartitionKeyOperandSingle(expr, parent)) {
unreplacedSingleExprs.add((ReplacableExpression) expr);
} else if (PartitionKeyVisitor.isPartitionKeyOperandIn(expr, parent)) {
Set<Expression> newInSet = unreplacedInExpr.get(parent);
if (newInSet == null) {
newInSet = new HashSet<Expression>(indexedExpressionPair.size(), 1);
unreplacedInExpr.put((InExpression) parent, newInSet);
}
newInSet.add(expr);
}
}
}
for (ReplacableExpression expr : unreplacedSingleExprs) {
expr.clearReplaceExpr();
}
for (Entry<InExpression, Set<Expression>> entemp : unreplacedInExpr.entrySet()) {
InExpression in = entemp.getKey();
Set<Expression> set = entemp.getValue();
if (set == null || set.isEmpty()) {
in.setReplaceExpr(ReplacableExpression.BOOL_FALSE);
} else {
in.clearReplaceExpr();
InExpressionList inlist = in.getInExpressionList();
if (inlist != null)
inlist.setReplaceExpr(new ArrayList<Expression>(set));
}
}
// [perf tag] 16.506 us: sharding multivalue
String sql = genSQL(stmtAST, originalSQL);
// [perf tag] 21.3425 us: sharding multivalue
String dataNodeName = matchedTable.getDataNodes()[en.getKey()];
rn[++nodeId] = new RouteResultsetNode(dataNodeName, sql);
for (ReplacableExpression expr : unreplacedSingleExprs) {
expr.setReplaceExpr(ReplacableExpression.BOOL_FALSE);
}
for (InExpression in : unreplacedInExpr.keySet()) {
in.setReplaceExpr(ReplacableExpression.BOOL_FALSE);
InExpressionList list = in.getInExpressionList();
if (list != null)
list.clearReplaceExpr();
}
// [perf tag] 22.0965 us: sharding multivalue
}
}
Aggregations