static <T> int loopJoinOrderedResult(PersistenceManager pm, TreeMap<Long, T> tree,
      String queryText, Object[] parameters, int keyIndex, ApplyFunc<T> func) throws MetaException {
    boolean doTrace = LOG.isDebugEnabled();
    long start = doTrace ? System.nanoTime() : 0;
    int rv = 0;
    long queryTime = 0;
    try (Query query = pm.newQuery("javax.jdo.query.SQL", queryText)) {
      Object result = null;
      if (parameters == null || parameters.length == 0) {
        result = query.execute();
      } else {
        result = query.executeWithArray(parameters);
      }
      queryTime = doTrace ? System.nanoTime() : 0;
      if (result == null) {
        query.closeAll();
        return 0;
      }
      List<Object[]> list = ensureList(result);
      Iterator<Object[]> iter = list.iterator();
      Object[] fields = null;
      for (Map.Entry<Long, T> entry : tree.entrySet()) {
        if (fields == null && !iter.hasNext())
          break;
        long id = entry.getKey();
        while (fields != null || iter.hasNext()) {
          if (fields == null) {
            fields = iter.next();
          }
          long nestedId = extractSqlLong(fields[keyIndex]);
          if (nestedId < id) {
            throw new MetaException("Found entries for unknown ID " + nestedId);
          }
          if (nestedId > id)
            break; // fields belong to one of the next entries
          func.apply(entry.getValue(), fields);
          fields = null;
        }
        Deadline.checkTimeout();
      }
      rv = list.size();
    } catch (Exception e) {
      throwMetaOrRuntimeException(e);
    }
    timingTrace(doTrace, queryText, start, queryTime);
    return rv;
  }
Merges applies the result of a PM SQL query into a tree of object.
Essentially it's an object join. DN could do this for us, but it issues queries
separately for every object, which is suboptimal.
@param pm
@param tree The object tree, by ID.
@param queryText The query text.
@param keyIndex Index of the Long column corresponding to the map ID in query result rows.
@param func The function that is called on each (object,row) pair with the same id.
@return the count of results returned from the query.
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/MetastoreDirectSqlUtils.java