private static boolean deduplicateReduceTraits(ReduceSinkDesc retainable,
      ReduceSinkDesc discardable, boolean apply) {

    final EnumSet<ReduceSinkDesc.ReducerTraits> retainableTraits = retainable.getReducerTraits();
    final EnumSet<ReduceSinkDesc.ReducerTraits> discardableTraits = discardable.getReducerTraits();

    final boolean x1 = retainableTraits.contains(UNSET);
    final boolean f1 = retainableTraits.contains(FIXED);
    final boolean u1 = retainableTraits.contains(UNIFORM);
    final boolean a1 = retainableTraits.contains(AUTOPARALLEL);
    final int n1 = retainable.getNumReducers();

    final boolean x2 = discardableTraits.contains(UNSET);
    final boolean f2 = discardableTraits.contains(FIXED);
    final boolean u2 = discardableTraits.contains(UNIFORM);
    final boolean a2 = discardableTraits.contains(AUTOPARALLEL);
    final int n2 = discardable.getNumReducers();

    boolean dedup = false;
    boolean x3 = false;
    boolean f3 = false;
    boolean u3 = false;
    boolean a3 = false;
    int n3 = n1;

    // NOTE: UNSET is exclusive from other traits, so FIXED is.

    if (x1 || x2) {
      // UNSET + X = X
      dedup = true;
      n3 = Math.max(n1, n2);
      x3 = x1 && x2;
      f3 = f1 || f2;
      u3 = u1 || u2;
      a3 = a1 || a2;
    } else if (f1 || f2) {
      if (f1 && f2) {
        // FIXED(x) + FIXED(x) = FIXED(x)
        // FIXED(x) + FIXED(y) = no deduplication (where x != y)
        if (n1 == n2) {
          dedup = true;
          f3 = true;
        }
      } else {
        // FIXED(x) + others = FIXED(x)
        dedup = true;
        f3 = true;
        if (f1) {
          n3 = n1;
        } else {
          n3 = n2;
        }
      }
    } else {
      if (u1 && u2) {
        // UNIFORM(x) + UNIFORM(y) = UNIFORM(max(x, y))
        dedup = true;
        u3 = true;
        n3 = Math.max(n1, n2);
      }
      if (a1 && a2) {
        // AUTOPARALLEL(x) + AUTOPARALLEL(y) = AUTOPARALLEL(max(x, y))
        dedup = true;
        a3 = true;
        n3 = Math.max(n1, n2);
      }
    }

    // Gether the results into the retainable object
    if (apply && dedup) {
      retainable.setNumReducers(n3);

      if (x3) {
        retainableTraits.add(UNSET);
      } else {
        retainableTraits.remove(UNSET);
      }

      if (f3) {
        retainableTraits.add(FIXED);
      } else {
        retainableTraits.remove(FIXED);
      }

      if (u3) {
        retainableTraits.add(UNIFORM);
      } else {
        retainableTraits.remove(UNIFORM);
      }

      if (a3) {
        retainableTraits.add(AUTOPARALLEL);
      } else {
        retainableTraits.remove(AUTOPARALLEL);
      }
    }
    return dedup;
  }
Orders TS operators in decreasing order by "weight".
ql/src/java/org/apache/hadoop/hive/ql/optimizer/SharedWorkOptimizer.java