private boolean checkSortColsAndJoinCols(List<Order> sortCols,
      List<String> joinCols,
      List<Order> sortColumnsFirstPartition) {

    if (sortCols == null || sortCols.size() < joinCols.size()) {
      return false;
    }

    // A join is eligible for a sort-merge join, only if it is eligible for
    // a bucketized map join. So, we dont need to check for bucketized map
    // join here. We are guaranteed that the join keys contain all the
    // bucketized keys (note that the order need not be the same).
    List<String> sortColNames = new ArrayList<String>();

    // The join columns should contain all the sort columns
    // The sort columns of all the tables should be in the same order
    // compare the column names and the order with the first table/partition.
    for (int pos = 0; pos < sortCols.size(); pos++) {
      Order o = sortCols.get(pos);

      if (pos < sortColumnsFirstPartition.size()) {
        if (o.getOrder() != sortColumnsFirstPartition.get(pos).getOrder()) {
          return false;
        }
      }
      sortColNames.add(o.getCol());
    }

    // The column names and order (ascending/descending) matched
    // The first 'n' sorted columns should be the same as the joinCols, where
    // 'n' is the size of join columns.
    // For eg: if the table is sorted by (a,b,c), it is OK to convert if the join is
    // on (a), (a,b), or any combination of (a,b,c):
    //   (a,b,c), (a,c,b), (c,a,b), (c,b,a), (b,c,a), (b,a,c)
    // but it is not OK to convert if the join is on (a,c)
    return sortColNames.subList(0, joinCols.size()).containsAll(joinCols);
  }
Whether this table is eligible for a sort-merge join.

@param pctx                  parse context
@param op                    map join operator being considered
@param joinTree              join tree being considered
@param alias                 table alias in the join tree being checked
@param pos                   position of the table
@param sortColumnsFirstTable The names and order of the sorted columns for the first table.
                             It is not initialized when pos = 0.
@return
@throws SemanticException
ql/src/java/org/apache/hadoop/hive/ql/optimizer/AbstractSMBJoinProc.java