public String rewriteQuery(Query query) {
    query = beforeRewrite(query);

    final StringBuilder sb = new StringBuilder();
    sb.append(rewriteSelectClause(query, query.getSelectClause()));
    sb.append(rewriteFromClause(query, query.getFromClause()));
    sb.append(rewriteWhereClause(query, query.getWhereClause()));
    sb.append(rewriteGroupByClause(query, query.getGroupByClause()));
    sb.append(rewriteHavingClause(query, query.getHavingClause()));
    sb.append(rewriteOrderByClause(query, query.getOrderByClause()));
    return sb.toString();
  }
  @Override
  public DataSet executeQuery(Query query) {
    final List<FromItem> fromItems = query.getFromClause().getItems();
    if (fromItems.size() != 1) {
      // not a simple SELECT ... FROM [table] ... query, we need to use
      // query post processing.
      return super.executeQuery(query);
    }

    final Table table = fromItems.get(0).getTable();
    if (table == null) {
      return super.executeQuery(query);
    }

    if (!query.getGroupByClause().isEmpty()) {
      return super.executeQuery(query);
    }

    if (!query.getHavingClause().isEmpty()) {
      return super.executeQuery(query);
    }

    final List<SelectItem> selectItems = query.getSelectClause().getItems();
    final StringBuilder sb = new StringBuilder();

    try {
      sb.append("SELECT ");
      int i = 0;
      final Column[] columns = new Column[selectItems.size()];
      for (SelectItem selectItem : selectItems) {
        validateSoqlSupportedSelectItem(selectItem);
        columns[i] = selectItem.getColumn();
        if (i != 0) {
          sb.append(", ");
        }
        sb.append(columns[i].getName());
        i++;
      }

      sb.append(" FROM ");
      sb.append(table.getName());

      boolean firstWhere = true;
      for (FilterItem filterItem : query.getWhereClause().getItems()) {
        if (firstWhere) {
          sb.append(" WHERE ");
          firstWhere = false;
        } else {
          sb.append(" AND ");
        }
        rewriteFilterItem(sb, filterItem);
      }

      i = 0;
      final List<OrderByItem> items = query.getOrderByClause().getItems();
      for (OrderByItem orderByItem : items) {
        if (i == 0) {
          sb.append(" ORDER BY ");
        } else {
          sb.append(", ");
        }

        final SelectItem selectItem = orderByItem.getSelectItem();
        validateSoqlSupportedSelectItem(selectItem);

        final Column column = selectItem.getColumn();
        sb.append(column.getName());
        sb.append(' ');
        sb.append(orderByItem.getDirection());

        i++;
      }

      final Integer firstRow = query.getFirstRow();
      final Integer maxRows = query.getMaxRows();
      if (maxRows != null && maxRows > 0) {
        if (firstRow != null) {
          // add first row / offset to avoid missing some records.
          sb.append(" LIMIT " + (maxRows + firstRow - 1));
        } else {
          sb.append(" LIMIT " + maxRows);
        }
      }

      final QueryResult result = executeSoqlQuery(sb.toString());

      DataSet dataSet = new SalesforceDataSet(columns, result, _connection);

      if (firstRow != null) {
        // OFFSET is still only a developer preview feature of SFDC. See
        // http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_select_offset.htm
        dataSet = new FirstRowDataSet(dataSet, firstRow.intValue());
      }

      return dataSet;

    } catch (UnsupportedOperationException e) {
      logger.debug(
          "Failed to rewrite query to SOQL, falling back to regular query post-processing", e);
      return super.executeQuery(query);
    }
  }