Title: ValueError when a split is empty
Author: jplu (CONTRIBUTOR)
State: closed
Created: 2020-04-15T13:25:13Z

When a split is empty either TEST, VALIDATION or TRAIN I get the following error:
```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jplu/dev/jplu/datasets/src/nlp/load.py", line 295, in load
    ds = dbuilder.as_dataset(**as_dataset_kwargs)
  File "/home/jplu/dev/jplu/datasets/src/nlp/builder.py", line 587, in as_dataset
    datasets = utils.map_nested(build_single_dataset, split, map_tuple=True)
  File "/home/jplu/dev/jplu/datasets/src/nlp/utils/py_utils.py", line 158, in map_nested
    for k, v in data_struct.items()
  File "/home/jplu/dev/jplu/datasets/src/nlp/utils/py_utils.py", line 158, in <dictcomp>
    for k, v in data_struct.items()
  File "/home/jplu/dev/jplu/datasets/src/nlp/utils/py_utils.py", line 172, in map_nested
    return function(data_struct)
  File "/home/jplu/dev/jplu/datasets/src/nlp/builder.py", line 601, in _build_single_dataset
    split=split,
  File "/home/jplu/dev/jplu/datasets/src/nlp/builder.py", line 625, in _as_dataset
    split_infos=self.info.splits.values(),
  File "/home/jplu/dev/jplu/datasets/src/nlp/arrow_reader.py", line 200, in read
    return py_utils.map_nested(_read_instruction_to_ds, instructions)
  File "/home/jplu/dev/jplu/datasets/src/nlp/utils/py_utils.py", line 172, in map_nested
    return function(data_struct)
  File "/home/jplu/dev/jplu/datasets/src/nlp/arrow_reader.py", line 191, in _read_instruction_to_ds
    file_instructions = make_file_instructions(name, split_infos, instruction)
  File "/home/jplu/dev/jplu/datasets/src/nlp/arrow_reader.py", line 104, in make_file_instructions
    absolute_instructions=absolute_instructions,
  File "/home/jplu/dev/jplu/datasets/src/nlp/arrow_reader.py", line 122, in _make_file_instructions_from_absolutes
    'Split empty. This might means that dataset hasn\'t been generated '
ValueError: Split empty. This might means that dataset hasn't been generated yet and info not restored from GCS, or that legacy dataset is used.
``` 

How to reproduce:
```python
import csv

import nlp


class Bbc(nlp.GeneratorBasedBuilder):
    VERSION = nlp.Version("1.0.0")

    def __init__(self, **config):
        self.train = config.pop("train", None)
        self.validation = config.pop("validation", None)
        super(Bbc, self).__init__(**config)

    def _info(self):
        return nlp.DatasetInfo(builder=self, description="bla", features=nlp.features.FeaturesDict({"id": nlp.int32, "text": nlp.string, "label": nlp.string}))

    def _split_generators(self, dl_manager):
        return [nlp.SplitGenerator(name=nlp.Split.TRAIN, gen_kwargs={"filepath": self.train}),
                nlp.SplitGenerator(name=nlp.Split.VALIDATION, gen_kwargs={"filepath": self.validation}),
                nlp.SplitGenerator(name=nlp.Split.TEST, gen_kwargs={"filepath": None})]

    def _generate_examples(self, filepath):
        if not filepath:
            return None, {}

        with open(filepath) as f:
            reader = csv.reader(f, delimiter=',', quotechar="\"")
            lines = list(reader)[1:]

            for idx, line in enumerate(lines):
                yield idx, {"id": idx, "text": line[1], "label": line[0]}
```

```python
import nlp
dataset = nlp.load("bbc", builder_kwargs={"train": "bbc/data/train.csv", "validation": "bbc/data/test.csv"})
```

Comments:
To fix this I propose to modify only the file `arrow_reader.py` with few updates. First update, the following method:
```python
def _make_file_instructions_from_absolutes(
        name,
        name2len,
        absolute_instructions,
):
    """Returns the files instructions from the absolute instructions list."""
    # For each split, return the files instruction (skip/take)
    file_instructions = []
    num_examples = 0
    for abs_instr in absolute_instructions:
        length = name2len[abs_instr.splitname]
        if not length:
            raise ValueError(
                    'Split empty. This might means that dataset hasn\'t been generated '
                    'yet and info not restored from GCS, or that legacy dataset is used.')
        filename = filename_for_dataset_split(
                dataset_name=name,
                split=abs_instr.splitname,
                filetype_suffix='arrow')
        from_ = 0 if abs_instr.from_ is None else abs_instr.from_
        to = length if abs_instr.to is None else abs_instr.to
        num_examples += to - from_
        single_file_instructions = [{"filename": filename, "skip": from_, "take": to - from_}]
        file_instructions.extend(single_file_instructions)
    return FileInstructions(
            num_examples=num_examples,
            file_instructions=file_instructions,
    )
```
Becomes:
```python
def _make_file_instructions_from_absolutes(
        name,
        name2len,
        absolute_instructions,
):
    """Returns the files instructions from the absolute instructions list."""
    # For each split, return the files instruction (skip/take)
    file_instructions = []
    num_examples = 0
    for abs_instr in absolute_instructions:
        length = name2len[abs_instr.splitname]
        ## Delete the if not length and the raise
        filename = filename_for_dataset_split(
                dataset_name=name,
                split=abs_instr.splitname,
                filetype_suffix='arrow')
        from_ = 0 if abs_instr.from_ is None else abs_instr.from_
        to = length if abs_instr.to is None else abs_instr.to
        num_examples += to - from_
        single_file_instructions = [{"filename": filename, "skip": from_, "take": to - from_}]
        file_instructions.extend(single_file_instructions)
    return FileInstructions(
            num_examples=num_examples,
            file_instructions=file_instructions,
    )
```

Second update the following method:
```python
def _read_files(files, info):
    """Returns Dataset for given file instructions.

    Args:
        files: List[dict(filename, skip, take)], the files information.
            The filenames contain the absolute path, not relative.
            skip/take indicates which example read in the file: `ds.slice(skip, take)`
    """
    pa_batches = []
    for f_dict in files:
        pa_table: pa.Table = _get_dataset_from_filename(f_dict)
        pa_batches.extend(pa_table.to_batches())
    pa_table = pa.Table.from_batches(pa_batches)
    ds = Dataset(arrow_table=pa_table, data_files=files, info=info)
    return ds
```
Becomes:
```python
def _read_files(files, info):
    """Returns Dataset for given file instructions.

    Args:
        files: List[dict(filename, skip, take)], the files information.
            The filenames contain the absolute path, not relative.
            skip/take indicates which example read in the file: `ds.slice(skip, take)`
    """
    pa_batches = []
    for f_dict in files:
        pa_table: pa.Table = _get_dataset_from_filename(f_dict)
        pa_batches.extend(pa_table.to_batches())
    ## we modify the table only if there are some batches
    if pa_batches:
        pa_table = pa.Table.from_batches(pa_batches)
    ds = Dataset(arrow_table=pa_table, data_files=files, info=info)
    return ds
```

With these two updates it works now. @thomwolf are you ok with this changes?

Yes sounds good to me!
Do you want to make a PR? or I can do it as well

Fixed.