Improved query join promotion logic

This pull request improves the logic for join promotion in .annotate() and .values() cases. In addition there is now join demotion logic.