Rewritten the sort join query planner in a sane way.