Make grouping sorting working for distributed queries