|
7 | 7 | from litdata.constants import _NUMPY_DTYPES_MAPPING, _TORCH_DTYPES_MAPPING |
8 | 8 | from litdata.streaming import Cache, item_loader |
9 | 9 | from litdata.streaming.dataset import StreamingDataset |
10 | | -from litdata.streaming.item_loader import PyTreeLoader, TokensLoader |
| 10 | +from litdata.streaming.item_loader import ParquetLoader, PyTreeLoader, TokensLoader |
| 11 | +from litdata.streaming.writer import index_parquet_dataset |
11 | 12 |
|
12 | 13 |
|
13 | 14 | def test_serializer_setup(): |
@@ -89,3 +90,91 @@ def test_force_download(monkeypatch, tmpdir): |
89 | 90 |
|
90 | 91 | with pytest.raises(Exception, match="worked"): |
91 | 92 | loader.load_item_from_chunk(0, 0, "chunk_filepath", 0, 1) |
| 93 | + |
| 94 | + |
| 95 | +def _write_parquet_with_row_groups(path, row_group_values): |
| 96 | + """Write a parquet file where each element of row_group_values becomes its own row group.""" |
| 97 | + import pyarrow as pa |
| 98 | + import pyarrow.parquet as pq |
| 99 | + |
| 100 | + schema = pa.schema([("col", pa.int64())]) |
| 101 | + with pq.ParquetWriter(path, schema) as writer: |
| 102 | + for values in row_group_values: |
| 103 | + writer.write_table(pa.table({"col": list(values)}, schema=schema)) |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.parametrize( |
| 107 | + "row_group_sizes", |
| 108 | + [ |
| 109 | + [10, 5, 5], # regression: uneven groups, shrinking |
| 110 | + [3, 7, 2, 8], # uneven groups, varying |
| 111 | + [20], # single group |
| 112 | + [1, 1, 1, 1, 1], # many size-1 groups |
| 113 | + [5, 5, 5], # uniform control case |
| 114 | + ], |
| 115 | +) |
| 116 | +@pytest.mark.parametrize("low_memory", [True, False]) |
| 117 | +def test_parquet_loader_row_group_sizes(tmp_path, row_group_sizes, low_memory): |
| 118 | + """ParquetLoader must correctly read every row regardless of row-group layout.""" |
| 119 | + parquet_dir = tmp_path / "pq" |
| 120 | + parquet_dir.mkdir() |
| 121 | + |
| 122 | + row_group_values = [] |
| 123 | + expected = [] |
| 124 | + |
| 125 | + for value, size in enumerate(row_group_sizes): |
| 126 | + row_group_values.append([value] * size) |
| 127 | + expected.extend([value] * size) |
| 128 | + value += 1 |
| 129 | + _write_parquet_with_row_groups(parquet_dir / "data.parquet", row_group_values) |
| 130 | + |
| 131 | + index_parquet_dataset(str(parquet_dir)) |
| 132 | + dataset = StreamingDataset(str(parquet_dir), item_loader=ParquetLoader(low_memory=low_memory)) |
| 133 | + |
| 134 | + assert len(dataset) == sum(row_group_sizes) |
| 135 | + actual = [dataset[i]["col"] for i in range(len(dataset))] |
| 136 | + assert actual == expected |
| 137 | + |
| 138 | + |
| 139 | +def test_parquet_loader_row_group_boundaries(tmp_path): |
| 140 | + """First and last row of each group (the modulo edges in the old implementation).""" |
| 141 | + parquet_dir = tmp_path / "pq" |
| 142 | + parquet_dir.mkdir() |
| 143 | + |
| 144 | + row_group_sizes = [10, 5, 5] |
| 145 | + _write_parquet_with_row_groups( |
| 146 | + parquet_dir / "data.parquet", |
| 147 | + [[v] * s for v, s in enumerate(row_group_sizes)], |
| 148 | + ) |
| 149 | + |
| 150 | + index_parquet_dataset(str(parquet_dir)) |
| 151 | + dataset = StreamingDataset(str(parquet_dir), item_loader=ParquetLoader(low_memory=True)) |
| 152 | + |
| 153 | + boundaries = [0, 9, 10, 14, 15, 19] |
| 154 | + expected = [0, 0, 1, 1, 2, 2] |
| 155 | + for idx, exp in zip(boundaries, expected): |
| 156 | + assert dataset[idx]["col"] == exp |
| 157 | + |
| 158 | + |
| 159 | +def test_parquet_loader_cache_eviction_with_uneven_groups(tmp_path): |
| 160 | + """After fully reading a row group, it must be evicted from the in-memory cache.""" |
| 161 | + parquet_dir = tmp_path / "pq" |
| 162 | + parquet_dir.mkdir() |
| 163 | + |
| 164 | + row_group_sizes = [10, 5, 5] |
| 165 | + _write_parquet_with_row_groups( |
| 166 | + parquet_dir / "data.parquet", |
| 167 | + [[v] * s for v, s in enumerate(row_group_sizes)], |
| 168 | + ) |
| 169 | + |
| 170 | + index_parquet_dataset(str(parquet_dir)) |
| 171 | + loader = ParquetLoader(low_memory=True) |
| 172 | + dataset = StreamingDataset(str(parquet_dir), item_loader=loader) |
| 173 | + |
| 174 | + # Iterate through the whole dataset sequentially. |
| 175 | + for i in range(len(dataset)): |
| 176 | + dataset[i] |
| 177 | + |
| 178 | + # After a sequential pass every row group in the chunk should have been evicted. |
| 179 | + for chunk_index, groups in loader._chunk_row_groups.items(): |
| 180 | + assert groups == {}, f"chunk {chunk_index} still holds row groups: {list(groups)}" |
0 commit comments