'Custom rows lookup function
@param key: the key field
@param values: the values to look up
@param fields: unused (retained for API compatibility)'

def lookup_rows(self, key, values, fields=[]):

    db = current.db
    s3db = current.s3db
    instance_fields = {'event_asset': ['incident_id', 'asset_id'], 'event_site': ['incident_id', 'site_id'], 'event_human_resource': ['incident_id', 'human_resource_id']}
    etable = s3db.budget_cost_item
    rows = db(key.belongs(values)).select(key, etable.instance_type)
    self.queries += 1
    keyname = key.name
    types = {}
    for row in rows:
        instance_type = row.instance_type
        cost_item_id = row[keyname]
        if (instance_type not in types):
            types[instance_type] = {cost_item_id: row}
        else:
            types[instance_type][cost_item_id] = row
    results = []
    append = results.append
    for instance_type in types:
        table = s3db.table(instance_type)
        if (not table):
            continue
        fields = []
        bulk_repr = {}
        if (instance_type in instance_fields):
            for fname in instance_fields[instance_type]:
                field = table[fname]
                if (fname in self.represent):
                    represent = self.represent[fname]
                else:
                    represent = field.represent
                if (represent and hasattr(represent, 'bulk')):
                    bulk_repr[fname] = {'method': represent, 'values': []}
                fields.append(field)
        else:
            continue
        fields.insert(0, table[keyname])
        query = table[keyname].belongs(types[instance_type].keys())
        rows = db(query).select(*fields)
        self.queries += 1
        sdata = types[instance_type]
        for row in rows:
            for (fname, frepr) in bulk_repr.items():
                frepr['values'].append(row[fname])
            append(Row(budget_cost_item=sdata[row[keyname]], **{instance_type: row}))
        for (fname, frepr) in bulk_repr.items():
            frepr['method'].bulk(frepr['values'])
    return results
