private static @Nullable Double computeJoinCardinality(
          RelMetadataQuery mq,
          HiveLoptMultiJoin multiJoin,
          HiveLoptSemiJoinOptimizer semiJoinOpt,
          HiveLoptJoinTree joinTree,
          List<RexNode> filters,
          int factor) {
    final ImmutableBitSet childFactors =
            ImmutableBitSet.builder()
                    .addAll(joinTree.getTreeOrder())
                    .set(factor)
                    .build();

    int factorStart = multiJoin.getJoinStart(factor);
    int nFields = multiJoin.getNumFieldsInJoinFactor(factor);
    final ImmutableBitSet.Builder joinKeys = ImmutableBitSet.builder();

    // first loop through the inner join filters, picking out the ones
    // that reference only the factors in either the join tree or the
    // factor that will be added
    setFactorJoinKeys(
            multiJoin,
            filters,
            childFactors,
            factorStart,
            nFields,
            joinKeys);

    // then loop through the outer join filters where the factor being
    // added is the null generating factor in the outer join
    setFactorJoinKeys(
            multiJoin,
            RelOptUtil.conjunctions(multiJoin.getOuterJoinCond(factor)),
            childFactors,
            factorStart,
            nFields,
            joinKeys);

    // if the join tree doesn't contain all the necessary factors in
    // any of the join filters, then joinKeys will be empty, so return
    // null in that case
    if (joinKeys.isEmpty()) {
      return null;
    } else {
      return mq.getDistinctRowCount(semiJoinOpt.getChosenSemiJoin(factor),
              joinKeys.build(), null);
    }
  }
Computes the cardinality of the join columns from a particular factor,
when that factor is joined with another join tree.

@param multiJoin join factors being optimized
@param semiJoinOpt optimal semijoins chosen for each factor
@param joinTree the join tree that the factor is being joined with
@param filters possible join filters to select from
@param factor the factor being added

@return computed cardinality
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveLoptOptimizeJoinRule.java