def _group_records_for_split(record_gen, split_size, reducer_key=None):
    """Helper for _split_records()."""
    split_num = 0
    bytes_in_split = 0
    last_key_value = None
    for record in record_gen:
        same_key = False
        if reducer_key:
            key_value = reducer_key(record)
            same_key = key_value == last_key_value
            last_key_value = key_value
        if bytes_in_split >= split_size and not same_key:
            split_num += 1
            bytes_in_split = 0
        yield split_num, record
        bytes_in_split += len(record)
