class CoalescingDecorator {

    public static QueryResults coalesce(QueryResults queryResults, String keyColumnName) {
        final Map<String, List<Object>> resultsMap = queryResults.copyData();
        final Map<String, List<Object>> combinedMap = newEmptyResultMap(resultsMap);

        List<Object>  keyColumn = queryResults.getValues(keyColumnName);
        List<Object> distinctKeys = keyColumn.stream()
                .distinct()
                .collect(Collectors.toList());
        distinctKeys.forEach(key -> {
            //System.out.print("key = " + key);
            Map<String, List<Object>> rows = extractRowsWithSameKey(resultsMap, keyColumn, key);
            //System.out.println(". Found " + rows.values().iterator().next().size() + " rows");
            if (rows.values().iterator().next().size() == 1) {
                addRowToMap(combinedMap, rows);
            }
            else {
                Map<String, Object> row = combineRows(rows);
                addNewRowToMap(combinedMap, row);
            }

        });

        return new QueryResults(combinedMap);


    }

}