private void resolveStages() {
        for (int i = 0; i < stageBeans.size(); i++) {
            final StageBean<?> currentStageBean = stageBeans.get(i);
            final HasComparableOperators<?, ?> foreignField = currentStageBean.getForeignField();
            if (foreignField == null) {
                currentStageBean.setReferencedStage(-1);
            } else {
                currentStageBean.setReferencedStage(stageIndexOf(stageBeans, foreignField, i));
            }
        }
        if (LOGGER_JOIN.getLevel().isEqualOrHigherThan(Level.DEBUG)) {
            final StringBuilder sb = new StringBuilder();
            sb.append("Resolving join with ").append(stageBeans.size()).append(" stages:");
            sb.append(
                format("%n%2s %-32s %-12s %2s %-32s %-16s %-12s %-16s",
                    "#", "Table Identifier", "Join Type", "R#", "Referenced Table Identifier", "Field", "Operation", "Referenced Field")
            );
            for (int i = 0; i < stageBeans.size(); i++) {
                final StageBean<?> stageBean = stageBeans.get(i);
                final Optional<StageBean<?>> referencedStageBean = Optional.of(stageBean.getReferencedStage())
                    .filter(rs -> rs != -1)
                    .map(stageBeans::get);
                sb.append(
                    format(
                        "%n%2d %-32s %-12s %2d %-32s %-16s %-12s %-16s",
                        i,
                        stageBean.getIdentifier(),
                        stageBean.getJoinType(),
                        stageBean.getReferencedStage(),
                        referencedStageBean.map(StageBean::getIdentifier).orElse(null),
                        Optional.ofNullable(stageBean.getField()).map(Field::identifier).map(ColumnIdentifier::getColumnId).orElse("null"),
                        stageBean.getJoinOperator(),
                        Optional.ofNullable(stageBean.getForeignField()).map(Field::identifier).map(ColumnIdentifier::getColumnId).orElse("null")
                    )
                );
            }
            LOGGER_JOIN.debug(sb.toString());
        }
    }
Calculates which reference to use taking "as()" columns into account.
runtime-parent/runtime-join/src/main/java/com/speedment/runtime/join/internal/component/join/AbstractJoinBuilder.java