@Override
  public RelBuilder join(JoinRelType joinType, RexNode condition,
      Set<CorrelationId> variablesSet) {
    if (Bug.CALCITE_4574_FIXED) {
      throw new IllegalStateException("Method overriding should be removed once CALCITE-4574 is fixed");
    }
    RelNode right = this.peek(0);
    RelNode left = this.peek(1);
    final boolean correlate = variablesSet.size() == 1;
    RexNode postCondition = literal(true);
    if (correlate) {
      final CorrelationId id = Iterables.getOnlyElement(variablesSet);
      if (!RelOptUtil.notContainsCorrelation(left, id, Litmus.IGNORE)) {
        throw new IllegalArgumentException("variable " + id
            + " must not be used by left input to correlation");
      }
      // Correlate does not have an ON clause.
      switch (joinType) {
      case LEFT:
      case SEMI:
      case ANTI:
        // For a LEFT/SEMI/ANTI, predicate must be evaluated first.
        filter(condition.accept(new Shifter(left, id, right)));
        right = this.peek(0);
        break;
      case INNER:
        // For INNER, we can defer.
        postCondition = condition;
        break;
      default:
        throw new IllegalArgumentException("Correlated " + joinType + " join is not supported");
      }
      final ImmutableBitSet requiredColumns = RelOptUtil.correlationColumns(id, right);
      List<RexNode> leftFields = this.fields(2, 0);
      List<RexNode> requiredFields = new ArrayList<>();
      for (int i = 0; i < leftFields.size(); i++) {
        if (requiredColumns.get(i)) {
          requiredFields.add(leftFields.get(i));
        }
      }
      correlate(joinType, id, requiredFields);
      filter(postCondition);
    } else {
      // When there is no correlation use the default logic which works OK for now
      // Cannot copy-paste the respective code here cause we don't have access to stack,
      // Frame etc. and we might lose existing aliases in the builder
      assert variablesSet.isEmpty();
      super.join(joinType,condition, variablesSet);
    }
    return this;
  }
Creates a {@link Join} with correlating variables.
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/HiveRelBuilder.java