def query_topology(self, _alias_map_items=None):
        # SOQL for SFDC requires:
        # - multiple (N-1) relations between (N) tables are possible
        # - exactly one top controlling table
        # - every relation is a join from exactly one foreign key to
        #   one primary key named "Id".
        #
        # Reorder relations to be from the left to the right
        if self.soql_trans is not None:
            return self.soql_trans
        if not _alias_map_items and not self.query.alias_map:
            assert DJANGO_18_PLUS, "empty alias_map is possible due to field expr in Django 1.8"
            return []
        # Unified interface:
        #   alias_map_items = [(lhs, table, join_cols_, rhs),...]
        query = self.query
        if _alias_map_items:
            alias_map_items = _alias_map_items
        elif DJANGO_18_PLUS:
            alias_map_items = [(getattr(v, 'parent_alias', None), v.table_name,
                               getattr(v, 'join_cols', None), v.table_alias
                              ) for v in query.alias_map.values()]
        else: # Django 1.7
            alias_map_items = [(v.lhs_alias, v.table_name, v.join_cols, v.rhs_alias)
                               for v in query.alias_map.values()]
        # Analyze
        alias2table = {}
        side_l, side_r = set(), set()
        for (lhs, table, join_cols_, rhs) in alias_map_items:
            alias2table[rhs] = table
            if lhs is not None:
                (join_cols,) = join_cols_
                assert len(join_cols) == 2
                # swap left-right if necessary. The left should be the top.
                if join_cols[0] == 'Id':
                    assert join_cols[1] != 'Id'
                    lhs, rhs = rhs, lhs
                    join_cols = join_cols[1], join_cols[0]
                assert join_cols[1] == 'Id'
                side_l.add(lhs)
                side_r.add(rhs)
            else:
                side_l.add(rhs)
        assert len(alias2table) == len(alias_map_items)
        # Recognize the top table
        assert len(side_l.union(side_r)) == len(alias_map_items)
        top_lhs_set = set(side_l).difference(side_r)
        assert len(top_lhs_set) == 1, ("Only queries with one top child model are "
                                       "supported by Salesforce. Use a subquery.")
        (top_lhs,) = top_lhs_set
        self.root_alias = top_lhs
        # translation rules into SOQL
        soql_trans = {top_lhs: alias2table[top_lhs]}
        work_lhses = set([top_lhs])
        while work_lhses:
            new_work = set()
            for (lhs, table, join_cols_, rhs) in alias_map_items:
                if lhs is not None:
                    (join_cols,) = join_cols_
                    if join_cols[0] == 'Id':
                        # swap lhs rhs
                        lhs, rhs = rhs, lhs
                        join_cols = join_cols[1], join_cols[0]
                    if lhs in work_lhses:
                        assert not soql_trans in rhs
                        if join_cols[0].endswith('__c'):
                            fkey = re.sub('__c$', '__r', join_cols[0])
                        else:
                            assert join_cols[0].endswith('Id')
                            fkey = re.sub('Id$', '', join_cols[0])
                        soql_trans[rhs] = '%s.%s' % (soql_trans[lhs], fkey)
                        new_work.add(rhs)
            work_lhses = new_work
        assert len(soql_trans) == len(alias_map_items)
        self.soql_trans = soql_trans
        return self.soql_trans
dataset/ETHPy150Open django-salesforce/django-salesforce/salesforce/backend/compiler.py/SQLCompiler.query_topology/swapped operands of `in`