'A helper method for get_distinct and get_ordering. This method will
trim extra not-needed joins from the tail of the join chain.
This is very similar to what is done in trim_joins, but we will
trim LEFT JOINS here. It would be a good idea to consolidate this
method and query.trim_joins().'

def _final_join_removal(self, col, alias):

    if alias:
        while 1:
            join = self.query.alias_map[alias]
            if (col != join.rhs_join_col):
                break
            self.query.unref_alias(alias)
            alias = join.lhs_alias
            col = join.lhs_join_col
    return (col, alias)
