'Changes the aliases in change_map (which maps old-alias -> new-alias),
relabelling any references to them in select columns and the where
clause.'

def change_aliases(self, change_map):

    assert (set(change_map.keys()).intersection(set(change_map.values())) == set())
    def relabel_column(col):
        if isinstance(col, (list, tuple)):
            old_alias = col[0]
            return (change_map.get(old_alias, old_alias), col[1])
        else:
            return col.relabeled_clone(change_map)
    self.where.relabel_aliases(change_map)
    self.having.relabel_aliases(change_map)
    if self.group_by:
        self.group_by = [relabel_column(col) for col in self.group_by]
    self.select = [SelectInfo(relabel_column(s.col), s.field) for s in self.select]
    self.aggregates = SortedDict(((key, relabel_column(col)) for (key, col) in self.aggregates.items()))
    for (ident, aliases) in self.join_map.items():
        del self.join_map[ident]
        aliases = tuple([change_map.get(a, a) for a in aliases])
        ident = ((change_map.get(ident[0], ident[0]),) + ident[1:])
        self.join_map[ident] = aliases
    for (old_alias, new_alias) in six.iteritems(change_map):
        alias_data = self.alias_map[old_alias]
        alias_data = alias_data._replace(rhs_alias=new_alias)
        self.alias_refcount[new_alias] = self.alias_refcount[old_alias]
        del self.alias_refcount[old_alias]
        self.alias_map[new_alias] = alias_data
        del self.alias_map[old_alias]
        table_aliases = self.table_map[alias_data.table_name]
        for (pos, alias) in enumerate(table_aliases):
            if (alias == old_alias):
                table_aliases[pos] = new_alias
                break
        for (pos, alias) in enumerate(self.tables):
            if (alias == old_alias):
                self.tables[pos] = new_alias
                break
    for (key, alias) in self.included_inherited_models.items():
        if (alias in change_map):
            self.included_inherited_models[key] = change_map[alias]
    for (alias, data) in six.iteritems(self.alias_map):
        lhs = data.lhs_alias
        if (lhs in change_map):
            data = data._replace(lhs_alias=change_map[lhs])
            self.alias_map[alias] = data
    if hasattr(self, '_lookup_joins'):
        self._lookup_joins = [change_map.get(lj, lj) for lj in self._lookup_joins]
