public Frame decorrelateRel(Sort rel) {
    //
    // Rewrite logic:
    //
    // 1. change the collations field to reference the new input.
    //

    // Sort itself should not reference cor vars.
    assert !cm.mapRefRelToCorRef.containsKey(rel);

    // Sort only references field positions in collations field.
    // The collations field in the newRel now need to refer to the
    // new output positions in its input.
    // Its output does not change the input ordering, so there's no
    // need to call propagateExpr.

    final RelNode oldInput = rel.getInput();
    final Frame frame = getInvoke(oldInput, rel);
    if (frame == null) {
      // If input has not been rewritten, do not rewrite this rel.
      return null;
    }
    final RelNode newInput = frame.r;

    Mappings.TargetMapping mapping =
            Mappings.target(
                    frame.oldToNewOutputs,
                    oldInput.getRowType().getFieldCount(),
                    newInput.getRowType().getFieldCount());

    RelCollation oldCollation = rel.getCollation();
    RelCollation newCollation = RexUtil.apply(mapping, oldCollation);

    final RelNode newSort = HiveSortLimit.create(newInput, newCollation, rel.offset, rel.fetch);

    // Sort does not change input ordering
    return register(rel, newSort, frame.oldToNewOutputs,
            frame.corDefOutputs);
  }
Rewrite Sort.

@param rel Sort to be rewritten
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveRelDecorrelator.java