public PhysicalContext resolve(PhysicalContext pctx) throws SemanticException {
    for (Task<?> task : pctx.getRootTasks()) {
      if (!(task instanceof MapRedTask) || !((MapRedTask)task).getWork().isFinalMapRed()) {
        continue; // this could be replaced by bucketing on RS + bucketed fetcher for next MR
      }
      MapredWork mrWork = ((MapRedTask) task).getWork();
      MapWork mapWork = mrWork.getMapWork();
      ReduceWork reduceWork = mrWork.getReduceWork();

      if (reduceWork == null || reduceWork.getNumReduceTasks() != 1
          || mapWork.getAliasToWork().size() != 1 || mapWork.getSamplingType() > 0
          || reduceWork.getReducer() == null) {
        continue;
      }
      // GROUPBY operator in reducer may not be processed in parallel. Skip optimizing.
      if (OperatorUtils.findSingleOperator(reduceWork.getReducer(), GroupByOperator.class) != null) {
        continue;
      }
      Operator<?> operator = mapWork.getAliasToWork().values().iterator().next();
      if (!(operator instanceof TableScanOperator)) {
        continue;
      }
      TableScanOperator tsop = (TableScanOperator)operator;
      Table tbl = tsop.getConf().getTableMetadata();
      if (tbl == null) {
        continue;
      }
      if (AcidUtils.isInsertOnlyTable(tbl.getParameters())) {
        // Not supported for MM tables - sampler breaks separate MM dirs into splits, resulting in
        // mismatch when the downstream task looks at them again assuming they are MM table roots.
        // We could somehow unset the MM flag for the main job when the sampler succeeds, since the
        // sampler will limit the input to the the correct directories, but we don't care about MR.
        continue;
      }
      ReduceSinkOperator child =
          OperatorUtils.findSingleOperator(operator, ReduceSinkOperator.class);
      if (child == null ||
          child.getConf().getNumReducers() != 1 || !child.getConf().getPartitionCols().isEmpty()) {
        continue;
      }
      child.getConf().setNumReducers(-1);
      reduceWork.setNumReduceTasks(-1);
      mapWork.setSamplingType(MapWork.SAMPLING_ON_START);
    }
    return pctx;
  }
Mark final MapredWork for ORDER BY to use sampling and set number of reduce task as -1
ql/src/java/org/apache/hadoop/hive/ql/optimizer/physical/SamplingOptimizer.java