Program: 13296
Project: 13296
Buggy source:
/** 
 * Collect stats from unassigned/empty shards. On groupBy all existing results are merged together with the unassigned shards results, so it is very important, that this method is called AFTER all partial reducers finished.
 * @param clusterState
 * @param concreteIndices
 * @throws Exception
 */
private void collectUnassignedShardStats(ClusterState clusterState,String[] concreteIndices) throws Exception {
  final GroupShardsIterator shardsItsAll=clusterState.routingTable().allAssignedShardsGrouped(concreteIndices,true);
  String dummyReducer="String_Node_Str";
  CrateException exception=null;
  SQLReduceJobStatus reduceJobStatus=null;
  List<List<Object>> collectResults=null;
  if (parsedStatement.hasGroupBy()) {
    reduceJobStatus=new SQLReduceJobStatus(parsedStatement,threadPool,ConcurrentCollections.<GroupByKey,GroupByRow>newConcurrentMap());
  }
 else {
    collectResults=new ArrayList<>(shardsItsAll.size() - shardsIts.size());
  }
  String lastIndex=null;
  for (  final ShardIterator shardIt : shardsItsAll) {
    final ShardRouting shard=shardIt.firstOrNull();
    if (shard == null) {
      try {
        if (parsedStatement.hasGroupBy() || parsedStatement.isGlobalAggregate()) {
          Map<String,Map<GroupByKey,GroupByRow>> shardGroupByCollectResults=statsService.queryGroupBy(parsedStatement.virtualTableName(),new String[]{dummyReducer},lastIndex,parsedStatement,shardIt.shardId().id(),null);
          reduceJobStatus.merge(new SQLGroupByResult(shardGroupByCollectResults.get(dummyReducer).values()));
        }
 else {
          List<List<Object>> shardCollectResults=statsService.query(parsedStatement.virtualTableName(),lastIndex,parsedStatement,shardIt.shardId().id(),null);
          if (shardCollectResults != null) {
            collectResults.addAll(shardCollectResults);
          }
        }
      }
 catch (      CrateException e) {
        exception=e;
      }
catch (      Exception e) {
        exception=new CrateException(e);
      }
      if (exception != null) {
        throw exception;
      }
    }
 else {
      lastIndex=shard.index();
    }
  }
  if (!parsedStatement.hasGroupBy()) {
    onMapperResults(collectResults);
  }
 else {
synchronized (groupByResult) {
      reduceJobStatus.merge(new SQLGroupByResult(groupByResult));
      groupByResult.clear();
      groupByResult.addAll(reduceJobStatus.trimRows(reduceJobStatus.reducedResult.values()));
    }
  }
}

Fixed version (ground truth):
/** 
 * Collect stats from unassigned/empty shards. On groupBy all existing results are merged together with the unassigned shards results, so it is very important, that this method is called AFTER all partial reducers finished.
 * @param clusterState
 * @param concreteIndices
 * @throws Exception
 */
private void collectUnassignedShardStats(ClusterState clusterState,String[] concreteIndices) throws Exception {
  final GroupShardsIterator shardsItsAll=clusterState.routingTable().allAssignedShardsGrouped(concreteIndices,true);
  String dummyReducer="String_Node_Str";
  CrateException exception=null;
  SQLReduceJobStatus reduceJobStatus=null;
  List<List<Object>> collectResults=null;
  if (parsedStatement.hasGroupBy()) {
    reduceJobStatus=new SQLReduceJobStatus(parsedStatement,threadPool);
  }
 else {
    collectResults=new ArrayList<>(shardsItsAll.size() - shardsIts.size());
  }
  String lastIndex=null;
  for (  final ShardIterator shardIt : shardsItsAll) {
    final ShardRouting shard=shardIt.firstOrNull();
    if (shard == null) {
      try {
        if (parsedStatement.hasGroupBy() || parsedStatement.isGlobalAggregate()) {
          Rows shardGroupByCollectResults=statsService.queryGroupBy(parsedStatement.virtualTableName(),1,lastIndex,parsedStatement,shardIt.shardId().id(),null);
          reduceJobStatus.merge(new SQLGroupByResult(0,shardGroupByCollectResults));
        }
 else {
          List<List<Object>> shardCollectResults=statsService.query(parsedStatement.virtualTableName(),lastIndex,parsedStatement,shardIt.shardId().id(),null);
          if (shardCollectResults != null) {
            collectResults.addAll(shardCollectResults);
          }
        }
      }
 catch (      CrateException e) {
        exception=e;
      }
catch (      Exception e) {
        exception=new CrateException(e);
      }
      if (exception != null) {
        throw exception;
      }
    }
 else {
      lastIndex=shard.index();
    }
  }
  if (!parsedStatement.hasGroupBy()) {
    onMapperResults(collectResults);
  }
 else {
  }
}