@Override
  public PhysicalContext resolve(PhysicalContext pctx) throws SemanticException {
    inferBucketingSorting(Utilities.getMRTasks(pctx.rootTasks));
    return pctx;
  }
BucketingSortingInferenceOptimizer.

For each map reduce task, attempts to infer bucketing and sorting metadata for the outputs.

Currently, only map reduce tasks which produce final output have there output metadata inferred,
but it can be extended to intermediate tasks as well.

This should be run as the last physical optimizer, as other physical optimizers may invalidate
the inferences made.  If a physical optimizer depends on the results and is designed to
carefully maintain these inferences, it may follow this one.
ql/src/java/org/apache/hadoop/hive/ql/optimizer/physical/BucketingSortingInferenceOptimizer.java