Fix sort_query support for polymorphic inheritance