For the performance comment, I don't mean to rewrite GroupByHash, but just to take the for-loop in the MarkDistinctHash and externalize it here. So instead of MarkDistinctHash creating a boolean column marking each distinct field based on the group ids, we can just do the same check of group ids here and directly append tuples to the output.