Esempio n. 1
0
 private RoutingResult doRoute(Select prepare) {
   List<TableFilter> filters = filterNotTableMate(prepare.getTopTableFilter());
   List<TableFilter> shards = New.arrayList(filters.size());
   List<TableFilter> globals = New.arrayList(filters.size());
   List<TableFilter> fixeds = New.arrayList(filters.size());
   for (TableFilter tf : filters) {
     TableMate table = getTableMate(tf);
     switch (table.getTableRule().getType()) {
       case TableRule.SHARDED_NODE_TABLE:
         shards.add(tf);
         break;
       case TableRule.GLOBAL_NODE_TABLE:
         globals.add(tf);
         break;
       case TableRule.FIXED_NODE_TABLE:
         fixeds.add(tf);
         break;
       default:
         break;
     }
   }
   RoutingResult result = null;
   if (!shards.isEmpty()) {
     for (TableFilter f : shards) {
       f.setEvaluatable(f, false);
       if (f.isJoinOuter() || f.isJoinOuterIndirect()) {
         prepare.getCondition().createIndexConditions(session, f);
       }
       TableMate table = getTableMate(f);
       ConditionExtractor extractor = new ConditionExtractor(f);
       RoutingResult r =
           routingHandler.doRoute(
               table, extractor.getStart(), extractor.getEnd(), extractor.getInColumns());
       result = (result == null || r.compareTo(result) < 0) ? r : result;
     }
     for (TableFilter f : shards) {
       f.setEvaluatable(f, true);
     }
   } else if (!fixeds.isEmpty()) {
     for (TableFilter tf : shards) {
       TableMate table = getTableMate(tf);
       RoutingResult r = routingHandler.doRoute(table);
       result = r;
     }
   } else if (!globals.isEmpty()) {
     // 全部为全局表查询,随机取一个第一个表结点
     GlobalTableRule tableRule = (GlobalTableRule) getTableRule(globals.iterator().next());
     RoutingResult r = tableRule.getRandomRoutingResult();
     result = r;
   } else {
     throw DbException.throwInternalError("SQL_ROUTING_ERROR");
   }
   ObjectNode[] selectNodes = result.getSelectNodes();
   if (selectNodes.length == 0) {
     throw DbException.throwInternalError("SQL_ROUTING_ERROR,empty result");
   }
   setConsistencyTableNodes(selectNodes, filters);
   return result;
 }
Esempio n. 2
0
 @Override
 protected void doPrepare() {
   expressions = prepared.getExpressions();
   if (prepared.isGroupQuery()) {
     ArrayList<Expression> selectExprs = New.arrayList(10);
     int[] groupIndex = prepared.getGroupIndex();
     for (int i = 0; i < groupIndex.length; i++) {
       int idx = groupIndex[i];
       Expression expr = expressions.get(idx);
       selectExprs.add(expr);
     }
     HashSet<Aggregate> aggregates = New.hashSet();
     for (Expression expr : expressions) {
       expr.isEverything(ExpressionVisitor.getAggregateVisitor(aggregates));
     }
     selectExprs.addAll(aggregates);
     expressions = selectExprs;
   }
   Expression[] exprList = expressions.toArray(new Expression[expressions.size()]);
   Integer limit = null, offset = null;
   Expression limitExpr = prepared.getLimit();
   Expression offsetExpr = prepared.getOffset();
   if (limitExpr != null) {
     limit = limitExpr.getValue(session).getInt();
   }
   if (offsetExpr != null) {
     offset = offsetExpr.getValue(session).getInt();
   }
   RoutingResult rr = doRoute(prepared);
   if (rr.isMultipleNode() && offset != null) {
     if (offset > database.getSettings().analyzeSample) {
       throw DbException.get(
           ErrorCode.INVALID_VALUE_2,
           "offset",
           offset
               + ", the max support offset "
               + database.getSettings().analyzeSample
               + " is defined by analyzeSample.");
     }
     offset = offset != null ? 0 : offset;
   }
   ObjectNode[] selectNodes = rr.getSelectNodes();
   if (session.getDatabase().getSettings().optimizeMerging) {
     selectNodes = rr.group();
   }
   workers = New.arrayList(selectNodes.length);
   for (ObjectNode node : selectNodes) {
     QueryWorker queryHandler =
         queryHandlerFactory.createQueryWorker(
             prepared, node, consistencyTableNodes, exprList, limit, offset);
     workers.add(queryHandler);
   }
 }