'Execute a query to determine the number/record IDs of all
matching rows
@param query: the query to execute
@param join: the inner joins for this query
@param left: the left joins for this query
@param getids: also extract the IDs if all matching records
@param orderby: ORDERBY expression for this query
@return: tuple of (TotalNumberOfRecords, RecordIDs)'

def filter_query(self, query, join=None, left=None, getids=False, orderby=None):

    db = current.db
    table = self.table
    if getids:
        field = table._id
        groupby = field
    else:
        field = table._id.count()
        groupby = None
        orderby = None
    vf = table.virtualfields
    osetattr(table, 'virtualfields', [])
    rows = db(query).select(field, join=join, left=left, orderby=orderby, groupby=groupby, cacheable=True)
    osetattr(table, 'virtualfields', vf)
    if getids:
        pkey = str(table._id)
        ids = [row[pkey] for row in rows]
        totalrows = len(ids)
    else:
        ids = None
        totalrows = rows.first()[field]
    return (totalrows, ids)
