use of com.alibaba.cobar.parser.ast.expression.misc.InExpressionList in project cobar by alibaba.
the class MySQLExprParser method rightOprandOfIn.
/**
* @return {@link QueryExpression} or {@link InExpressionList}
*/
private Expression rightOprandOfIn() throws SQLSyntaxErrorException {
match(PUNC_LEFT_PAREN);
if (KW_SELECT == lexer.token()) {
QueryExpression subq = subQuery();
match(PUNC_RIGHT_PAREN);
return subq;
}
return new InExpressionList(expressionList(new LinkedList<Expression>())).setCacheEvalRst(cacheEvalRst);
}
use of com.alibaba.cobar.parser.ast.expression.misc.InExpressionList in project cobar by alibaba.
the class PartitionKeyVisitor method visit.
@Override
public void visit(InExpression node) {
Expression left = node.getLeftOprand();
Expression right = node.getRightOprand();
visitChild(2, false, false, left, right);
if (verdictColumn && !node.isNot() && left instanceof Identifier && right instanceof InExpressionList) {
Identifier col = (Identifier) left;
String colName = col.getIdTextUpUnescape();
String table = tableAlias.get(col.getLevelUnescapeUpName(2));
if (isRuledColumn(table, colName)) {
List<Object> valList = ensureColumnValueList(ensureColumnValueByTable(table), colName);
Map<Object, Set<Pair<Expression, ASTNode>>> valMap = ensureColumnValueIndexObjMap(ensureColumnValueIndexByTable(table), colName);
InExpressionList inlist = (InExpressionList) right;
for (Expression expr : inlist.getList()) {
Object value = expr.evaluation(evaluationParameter);
if (value != Expression.UNEVALUATABLE) {
valList.add(value);
addIntoColumnValueIndex(valMap, value, expr, node);
}
}
}
}
}
use of com.alibaba.cobar.parser.ast.expression.misc.InExpressionList 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