public OrderedHashSet getSubqueries() {

    OrderedHashSet set = null;

    if (joinCondition != null) {
      set = joinCondition.collectAllSubqueries(set);
    }

    if (rangeTable instanceof TableDerived) {
      QueryExpression queryExpression = ((TableDerived) rangeTable).getQueryExpression();

      if (queryExpression == null) {
        Expression dataExpression = ((TableDerived) rangeTable).getDataExpression();

        if (dataExpression != null) {
          if (set == null) {
            set = new OrderedHashSet();
          }

          OrderedHashSet.addAll(set, dataExpression.getSubqueries());
        }
      } else {
        OrderedHashSet temp = queryExpression.getSubqueries();

        set = OrderedHashSet.addAll(set, temp);
        set = OrderedHashSet.add(set, rangeTable);
      }
    }

    return set;
  }
  OrderedHashSet getSubqueries() {

    OrderedHashSet set = null;

    if (joinCondition != null) {
      set = joinCondition.collectAllSubqueries(set);
    }

    if (rangeTable instanceof TableDerived) {
      QueryExpression baseQueryExpression = ((TableDerived) rangeTable).getQueryExpression();

      if (((TableDerived) rangeTable).view != null) {
        if (set == null) {
          set = new OrderedHashSet();
        }

        set.addAll(((TableDerived) rangeTable).view.getSubqueries());
      } else if (baseQueryExpression == null) {
        set = OrderedHashSet.add(set, rangeTable.getSubQuery());
      } else {
        OrderedHashSet temp = baseQueryExpression.getSubqueries();

        set = OrderedHashSet.addAll(set, temp);
        set = OrderedHashSet.add(set, rangeTable.getSubQuery());
      }
    }

    return set;
  }
  SubQuery[] getSubqueries(Session session) {

    OrderedHashSet subQueries = null;

    for (int i = 0; i < targetRangeVariables.length; i++) {
      if (targetRangeVariables[i] == null) {
        continue;
      }

      OrderedHashSet set = targetRangeVariables[i].getSubqueries();

      subQueries = OrderedHashSet.addAll(subQueries, set);
    }

    for (int i = 0; i < updateExpressions.length; i++) {
      subQueries = updateExpressions[i].collectAllSubqueries(subQueries);
    }

    if (insertExpression != null) {
      subQueries = insertExpression.collectAllSubqueries(subQueries);
    }

    if (condition != null) {
      subQueries = condition.collectAllSubqueries(subQueries);
    }

    if (queryExpression != null) {
      OrderedHashSet set = queryExpression.getSubqueries();

      subQueries = OrderedHashSet.addAll(subQueries, set);
    }

    if (subQueries == null || subQueries.size() == 0) {
      return SubQuery.emptySubqueryArray;
    }

    SubQuery[] subQueryArray = new SubQuery[subQueries.size()];

    subQueries.toArray(subQueryArray);
    ArraySort.sort(subQueryArray, 0, subQueryArray.length, subQueryArray[0]);

    for (int i = 0; i < subQueryArray.length; i++) {
      subQueryArray[i].prepareTable(session);
    }

    return subQueryArray;
  }
  void moveConditionsToInner(Session session, RangeVariable[] ranges) {

    Expression[] colExpr;
    int exclude;
    HsqlArrayList conditionsList;
    Expression condition = null;

    if (whereConditions.length > 1) {
      return;
    }

    if (joinConditions.length > 1) {
      return;
    }

    for (int i = 0; i < ranges.length; i++) {
      if (ranges[i].isLeftJoin || ranges[i].isRightJoin) {
        return;
      }
    }

    exclude = ArrayUtil.find(ranges, this);
    conditionsList = new HsqlArrayList();

    addConditionsToList(conditionsList, joinConditions[0].indexCond);

    if (joinConditions[0].indexCond != null
        && joinConditions[0].indexCond[0] != joinConditions[0].indexEndCond[0]) {
      addConditionsToList(conditionsList, joinConditions[0].indexEndCond);
    }

    addConditionsToList(conditionsList, whereConditions[0].indexCond);
    addConditionsToList(conditionsList, whereConditions[0].indexEndCond);
    RangeVariableResolver.decomposeAndConditions(
        session, joinConditions[0].nonIndexCondition, conditionsList);
    RangeVariableResolver.decomposeAndConditions(
        session, whereConditions[0].nonIndexCondition, conditionsList);

    for (int i = conditionsList.size() - 1; i >= 0; i--) {
      Expression e = (Expression) conditionsList.get(i);

      if (e == null || e.isTrue() || e.hasReference(ranges, exclude)) {
        conditionsList.remove(i);

        continue;
      }
    }

    if (conditionsList.size() == 0) {
      if (rangeTable.isView()) {
        ((TableDerived) rangeTable).resetToView();
      }

      return;
    }

    QueryExpression queryExpression = rangeTable.getQueryExpression();

    colExpr = ((QuerySpecification) queryExpression).exprColumns;

    for (int i = 0; i < conditionsList.size(); i++) {
      Expression e = (Expression) conditionsList.get(i);
      OrderedHashSet set = e.collectRangeVariables(null);

      e = e.duplicate();
      e = e.replaceColumnReferences(this, colExpr);

      if (e.collectAllSubqueries(null) != null) {
        return;
      }

      if (set != null) {
        for (int j = 0; j < set.size(); j++) {
          RangeVariable range = (RangeVariable) set.get(j);

          if (this != range && range.rangeType == RangeVariable.TABLE_RANGE) {
            queryExpression.setCorrelated();
          }
        }
      }

      condition = ExpressionLogical.andExpressions(condition, e);
    }

    queryExpression.addExtraConditions(condition);
  }