'Determine the fields needed to generate the report
@param fields: fields to include in the report (all fields)'

def _get_fields(self, fields=None):

    resource = self.resource
    table = resource.table
    alias = resource.alias
    def prefix(s):
        if isinstance(s, (tuple, list)):
            return prefix(s[(-1)])
        if ('.' not in s.split('$', 1)[0]):
            return ('%s.%s' % (alias, s))
        elif (s[:2] == '~.'):
            return ('%s.%s' % (alias, s[2:]))
        else:
            return s
    self.pkey = pkey = prefix(table._id.name)
    self.rows = rows = ((self.rows and prefix(self.rows)) or None)
    self.cols = cols = ((self.cols and prefix(self.cols)) or None)
    if (not fields):
        fields = ()
    dfields = [prefix(s) for s in fields]
    if (rows and (rows not in dfields)):
        dfields.append(rows)
    if (cols and (cols not in dfields)):
        dfields.append(cols)
    if (pkey not in dfields):
        dfields.append(pkey)
    for fact in self.facts:
        selector = fact.selector = prefix(fact.selector)
        if (selector not in dfields):
            dfields.append(selector)
    self.dfields = dfields
    rfields = resource.resolve_selectors(dfields)[0]
    rfields = Storage([(f.selector.replace('~', alias), f) for f in rfields])
    self.rfields = rfields
    self.gfields = {pkey: rfields[pkey].colname, rows: (rfields[rows].colname if (rows and (rows in rfields)) else None), cols: (rfields[cols].colname if (cols and (cols in rfields)) else None)}
