private void removeJoin(
      HiveLoptMultiJoin multiJoin,
      LogicalJoin semiJoin,
      int factIdx,
      int dimIdx) {
    // if the dimension can be removed because of another semijoin, then
    // no need to proceed any further
    if (multiJoin.getJoinRemovalFactor(dimIdx) != null) {
      return;
    }

    // Check if the semijoin keys corresponding to the dimension table
    // are unique.  The semijoin will filter out the nulls.
    final ImmutableBitSet dimKeys = ImmutableBitSet.of(semiJoin.analyzeCondition().rightKeys);
    final RelNode dimRel = multiJoin.getJoinFactor(dimIdx);
    if (!RelMdUtil.areColumnsDefinitelyUniqueWhenNullsFiltered(mq, dimRel,
        dimKeys)) {
      return;
    }

    // check that the only fields referenced from the dimension table
    // in either its projection or join conditions are the dimension
    // keys
    ImmutableBitSet dimProjRefs = multiJoin.getProjFields(dimIdx);
    if (dimProjRefs == null) {
      int nDimFields = multiJoin.getNumFieldsInJoinFactor(dimIdx);
      dimProjRefs = ImmutableBitSet.range(0, nDimFields);
    }
    if (!dimKeys.contains(dimProjRefs)) {
      return;
    }
    int [] dimJoinRefCounts = multiJoin.getJoinFieldRefCounts(dimIdx);
    for (int i = 0; i < dimJoinRefCounts.length; i++) {
      if (dimJoinRefCounts[i] > 0) {
        if (!dimKeys.get(i)) {
          return;
        }
      }
    }

    // criteria met; keep track of the fact table and the semijoin that
    // allow the join of this dimension table to be removed
    multiJoin.setJoinRemovalFactor(dimIdx, factIdx);
    multiJoin.setJoinRemovalSemiJoin(dimIdx, semiJoin);

    // if the dimension table doesn't reference anything in its projection
    // and the only fields referenced in its joins are the dimension keys
    // of this semijoin, then we can decrement the join reference counts
    // corresponding to the fact table's semijoin keys, since the
    // dimension table doesn't need to use those keys
    if (dimProjRefs.cardinality() != 0) {
      return;
    }
    for (int i = 0; i < dimJoinRefCounts.length; i++) {
      if (dimJoinRefCounts[i] > 1) {
        return;
      } else if (dimJoinRefCounts[i] == 1) {
        if (!dimKeys.get(i)) {
          return;
        }
      }
    }
    int [] factJoinRefCounts = multiJoin.getJoinFieldRefCounts(factIdx);
    for (Integer key : semiJoin.analyzeCondition().leftKeys) {
      factJoinRefCounts[key]--;
    }
  }
Determines whether a join of the dimension table in a semijoin can be
removed. It can be if the dimension keys are unique and the only fields
referenced from the dimension table are its semijoin keys. The semijoin
keys can be mapped to the corresponding keys from the fact table (because
of the equality condition associated with the semijoin keys). Therefore,
that's why the dimension table can be removed even though those fields
are referenced elsewhere in the query tree.

@param multiJoin join factors being optimized
@param semiJoin semijoin under consideration
@param factIdx id of the fact table in the semijoin
@param dimIdx id of the dimension table in the semijoin
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveLoptSemiJoinOptimizer.java