@Override
  public ParseContext transform(ParseContext pCtx) throws SemanticException {

    // create a walker which walks the tree in a DFS manner while maintaining the
    // operator stack. The dispatcher generates the plan from the operator tree
    Map<SemanticRule, SemanticNodeProcessor> opRules = new LinkedHashMap<SemanticRule, SemanticNodeProcessor>();

    String FS = FileSinkOperator.getOperatorName() + "%";

    opRules.put(new RuleRegExp("Sorted Dynamic Partition", FS), getSortDynPartProc(pCtx));

    SemanticDispatcher disp = new DefaultRuleDispatcher(null, opRules, null);
    SemanticGraphWalker ogw = new DefaultGraphWalker(disp);

    ArrayList<Node> topNodes = new ArrayList<Node>();
    topNodes.addAll(pCtx.getTopOps().values());
    ogw.startWalking(topNodes, null);

    return pCtx;
  }
When dynamic partitioning (with or without bucketing and sorting) is enabled, this optimization
sorts the records on partition, bucket and sort columns respectively before inserting records
into the destination table. This enables reducers to keep only one record writer all the time
thereby reducing the the memory pressure on the reducers.
Sorting is based on the Dynamic Partitioning context that is already created in the file sink operator.
If that contains instructions for custom expression sorting, then this optimizer will disregard any partitioning or
bucketing information of the Hive (table format) table, and will arrange the plan solely as per the custom exprs.
ql/src/java/org/apache/hadoop/hive/ql/optimizer/SortedDynPartitionOptimizer.java