implement and test group-by-cluster function