|
11 | 11 | from daft.table.table import Table
|
12 | 12 |
|
13 | 13 |
|
14 |
| -def _range_generators(start: int, end: int, step: int) -> Iterator[Callable[[], Iterator[Table]]]: |
15 |
| - def generator_for_value(value: int) -> Callable[[], Iterator[Table]]: |
16 |
| - def generator() -> Iterator[Table]: |
17 |
| - yield Table.from_pydict({"id": [value]}) |
| 14 | +def _range_generators(start: int, end: int, step: int, partitions: int) -> Iterator[Callable[[], Iterator[Table]]]: |
| 15 | + # TODO: Partitioning with range scan is currently untested and unused. |
| 16 | + # There may be issues with balanced partitions and step size. |
18 | 17 |
|
19 |
| - return generator |
| 18 | + # Calculate partition bounds upfront |
| 19 | + partition_size = (end - start) // partitions |
| 20 | + partition_bounds = [ |
| 21 | + (start + (i * partition_size), start + ((i + 1) * partition_size) if i < partitions - 1 else end) |
| 22 | + for i in range(partitions) |
| 23 | + ] |
20 | 24 |
|
21 |
| - for value in range(start, end, step): |
22 |
| - yield generator_for_value(value) |
| 25 | + def generator(partition_idx: int) -> Iterator[Table]: |
| 26 | + partition_start, partition_end = partition_bounds[partition_idx] |
| 27 | + values = list(range(partition_start, partition_end, step)) |
| 28 | + yield Table.from_pydict({"id": values}) |
| 29 | + |
| 30 | + from functools import partial |
| 31 | + |
| 32 | + for partition_idx in range(partitions): |
| 33 | + yield partial(generator, partition_idx) |
23 | 34 |
|
24 | 35 |
|
25 | 36 | class RangeScanOperator(GeneratorScanOperator):
|
26 |
| - def __init__(self, start: int, end: int, step: int = 1) -> None: |
| 37 | + def __init__(self, start: int, end: int, step: int = 1, partitions: int = 1) -> None: |
27 | 38 | schema = Schema._from_field_name_and_types([("id", DataType.int64())])
|
28 | 39 |
|
29 |
| - super().__init__(schema=schema, generators=_range_generators(start, end, step)) |
| 40 | + super().__init__(schema=schema, generators=_range_generators(start, end, step, partitions)) |
0 commit comments