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

    Map<SemanticRule, SemanticNodeProcessor> opRules = new LinkedHashMap<SemanticRule, SemanticNodeProcessor>();

    // process reduce sink added by hive.enforce.bucketing or hive.enforce.sorting
    opRules.put(new RuleRegExp("R1",
        ReduceSinkOperator.getOperatorName() + "%" +
            SelectOperator.getOperatorName() + "%" +
            FileSinkOperator.getOperatorName() + "%"),
        getBucketSortReduceSinkProc(pctx));

    // The dispatcher fires the processor corresponding to the closest matching rule
    SemanticDispatcher disp = new DefaultRuleDispatcher(getDefaultProc(), opRules, null);
    SemanticGraphWalker ogw = new DefaultGraphWalker(disp);

    // Create a list of top nodes
    ArrayList<Node> topNodes = new ArrayList<Node>();
    topNodes.addAll(pctx.getTopOps().values());
    ogw.startWalking(topNodes, null);

    return pctx;
  }
This transformation does optimization for enforcing bucketing and sorting.
For a query of the form:
insert overwrite table T1 select * from T2;
where T1 and T2 are bucketized/sorted on the same keys, we don't need a reducer to
enforce bucketing and sorting.

It also optimizes queries of the form:
insert overwrite table T1
select * from T1 join T2 on T1.key = T2.key
where T1, T2 and T3 are bucketized/sorted on the same key 'key', we don't need a reducer
to enforce bucketing and sorting
ql/src/java/org/apache/hadoop/hive/ql/optimizer/BucketingSortingReduceSinkOptimizer.java