public Wrapper getOrderByComparatorAndLimitForQuery(String orderByQuery, int unorderedResultSize)
      throws FunctionDomainException, TypeMismatchException, NameResolutionException,
      QueryInvocationTargetException, NoSuchFieldException, SecurityException,
      IllegalArgumentException, IllegalAccessException, NoSuchMethodException,
      InvocationTargetException {
    DefaultQuery q = (DefaultQuery) CacheUtils.getQueryService().newQuery(orderByQuery);
    CompiledSelect cs = q.getSimpleSelect();
    List<CompiledSortCriterion> orderByAttribs = null;
    if (cs.getType() == CompiledValue.GROUP_BY_SELECT) {
      Field originalOrderByMethod =
          CompiledGroupBySelect.class.getDeclaredField("originalOrderByClause");
      originalOrderByMethod.setAccessible(true);
      orderByAttribs = (List<CompiledSortCriterion>) originalOrderByMethod.get(cs);
    } else {
      orderByAttribs = cs.getOrderByAttrs();
    }
    ObjectType resultType = cs.getElementTypeForOrderByQueries();
    ExecutionContext context = new ExecutionContext(null, CacheUtils.getCache());
    final OrderByComparator obc = new OrderByComparator(orderByAttribs, resultType, context);
    Comparator baseComparator = obc;
    if (resultType.isStructType()) {
      baseComparator =
          (Comparator<Struct>) (o1, o2) -> obc.compare(o1.getFieldValues(), o2.getFieldValues());
    }
    final Comparator secondLevelComparator = baseComparator;
    final Comparator finalComparator = (o1, o2) -> {
      final boolean[] orderByColsEqual = new boolean[] {false};
      QueryObserverHolder.setInstance(new QueryObserverAdapter() {
        @Override
        public void orderByColumnsEqual() {
          orderByColsEqual[0] = true;
        }
      });
      int result = secondLevelComparator.compare(o1, o2);
      if (result != 0 && orderByColsEqual[0]) {
        result = 0;
      }
      return result;
    };

    Field hasUnmappedOrderByColsField =
        CompiledSelect.class.getDeclaredField("hasUnmappedOrderByCols");
    hasUnmappedOrderByColsField.setAccessible(true);
    boolean skip = (Boolean) hasUnmappedOrderByColsField.get(cs);
    ValidationLevel validationLevel = ValidationLevel.ALL;

    int limit;

    if (cs.getType() == CompiledValue.GROUP_BY_SELECT) {
      Field limitCVField = CompiledGroupBySelect.class.getDeclaredField("limit");
      limitCVField.setAccessible(true);
      CompiledValue limitCV = (CompiledValue) limitCVField.get(cs);
      Method evaluateLimitMethod = CompiledSelect.class.getDeclaredMethod("evaluateLimitValue",
          ExecutionContext.class, CompiledValue.class);
      evaluateLimitMethod.setAccessible(true);
      limit = (Integer) evaluateLimitMethod.invoke(null, context, limitCV);
    } else {
      limit = cs.getLimitValue(null);
    }

    if (limit != -1 && limit < unorderedResultSize) {
      // chances are that results will not match
      if (skip) {
        validationLevel = ValidationLevel.NONE;
      } else {
        validationLevel = ValidationLevel.ORDER_BY_ONLY;
      }
    } else {
      if (skip) {
        validationLevel = ValidationLevel.MATCH_ONLY;
      }
    }
    return new Wrapper(finalComparator, limit, validationLevel);
  }
Used by these tests:
<ul>
<li>EquiJoinIntegrationTest</li>
<li>IUMRCompositeIteratorJUnitTest</li>
<li>IUMRMultiIndexesMultiRegionJUnitTest</li>
<li>IUMRShuffleIteratorsJUnitTest</li>
<li>IUMRSingleRegionJUnitTest</li>
<li>IndexCreationJUnitTest</li>
<li>IndexHintJUnitTest</li>
<li>IndexMaintainceJUnitTest</li>
<li>IndexUseJUnitTest</li>
<li>IndexedMergeEquiJoinScenariosJUnitTest</li>
<li>MultiRegionIndexUsageJUnitTest</li>
<li>NonDistinctOrderByPartitionedJUnitTest</li>
<li>NonDistinctOrderByReplicatedJUnitTest</li>
<li>NonDistinctOrderByTestImplementation</li>
<li>OrderByPartitionedJUnitTest</li>
<li>OrderByReplicatedJUnitTest</li>
<li>OrderByTestImplementation</li>
<li>QueryIndexUsingXMLDUnitTest</li>
<li>QueryREUpdateInProgressJUnitTest</li>
<li>QueryUsingFunctionContextDUnitTest</li>
<li>QueryUsingPoolDUnitTest</li>
<li>TestNewFunctionSSorRSIntegrationTest</li>
</ul>
Also used by:
<ul>
<li>GroupByTestImpl</li>
<li>PdxGroupByTestImpl</li>
<li>PRQueryDUnitHelper</li>
</ul>
geode-junit/src/main/java/org/apache/geode/cache/query/functional/StructSetOrResultsSet.java