Fix issue with reversed group by in task and final query in the rewriter