@Override
  public Object process(Node nd, Stack<Node> stack, NodeProcessorCtx procCtx,
                        Object... nodeOutputs) throws SemanticException {

    // Get ReduceSinkOperator
    ReduceSinkOperator reduceSinkOperator = (ReduceSinkOperator) nd;
    ReduceSinkDesc reduceSinkDesc = reduceSinkOperator.getConf();

    // Check whether the reduce sink operator contains top n
    if (reduceSinkDesc.getTopN() < 0 || !reduceSinkDesc.isOrdering()) {
      return null;
    }

    if (reduceSinkDesc.getTopN() > maxTopNAllowed) {
      return null;
    }

    // HIVE-26671: We do not want to create a TopNKey processor when the reduce sink
    // operator contains a count distinct. This would result in a topnkey operator
    // with an extra group in its sort order. The TopNKey Pushdown Processor could then
    // push down this operator and it would be incorrect since the count distinct adds
    // a group that is only temporarily used for calculating a value.
    if (reduceSinkDesc.hasADistinctColumnIndex()) {
      return null;
    }

    // Check whether there already is a top n key operator
    Operator<? extends OperatorDesc> parentOperator = reduceSinkOperator.getParentOperators().get(0);
    if (parentOperator instanceof TopNKeyOperator) {
      return null;
    }

    List<ExprNodeDesc> partitionCols = Collections.emptyList();
    if (reduceSinkDesc.isPTFReduceSink()) {
      // All keys are partition keys or no keys at all
      // Note: partition cols are prefix of key cols
      if (reduceSinkDesc.getPartitionCols().size() >= reduceSinkDesc.getKeyCols().size()) {
        return null;
      }

      partitionCols = reduceSinkDesc.getPartitionCols();
    }

    TopNKeyDesc topNKeyDesc = new TopNKeyDesc(reduceSinkDesc.getTopN(), reduceSinkDesc.getOrder(),
    reduceSinkDesc.getNullOrder(), reduceSinkDesc.getKeyCols(), partitionCols,
      efficiencyThreshold, checkEfficiencyNumBatches, maxNumberOfPartitions);


    copyDown(reduceSinkOperator, topNKeyDesc);
    reduceSinkDesc.setTopN(-1);
    return null;
  }
TopNKeyProcessor is a processor for TopNKeyOperator.
A TopNKeyOperator will be placed before any ReduceSinkOperator which has a topN property &gt;= 0.
ql/src/java/org/apache/hadoop/hive/ql/optimizer/topnkey/TopNKeyProcessor.java