/**
   * Executes a generic CompiledStatement. Execution includes first building any subquery result
   * dependencies and clearing them after the main result is built.
   *
   * @return the result of executing the statement
   * @param cs any valid CompiledStatement
   */
  Result execute(CompiledStatement cs) {

    Result result = null;

    DatabaseManager.gc();

    try {
      cs.materializeSubQueries(session);

      result = executeImpl(cs);
    } catch (Throwable t) {

      // t.printStackTrace();
      result = new Result(t, cs.sql);
    }

    // clear redundant data
    cs.dematerializeSubQueries();

    if (result == null) {
      result = emptyResult;
    }

    return result;
  }