Repository navigation
[Data] Vectorize row-group compaction in _compact_file_ids - #67820
goutamvenkat-anyscale merged 3 commits into
Conversation
Eliminate O(N * G) per-row-group full-array equality scans, O(N) redundant dictionary string materialization for file-level metadata fields, and per-group np.unique sorting in _compact_file_ids. Signed-off-by: Parth Vadera <[email protected]> Co-authored-by: Copilot <[email protected]> Signed-off-by: Parth Vadera <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request optimizes the compaction of file IDs in generated_id.py by slicing only the first ID for metadata extraction and introducing sorting and grouping logic to handle unsorted and interleaved row groups. A corresponding unit test is added to verify this behavior. The review feedback highlights a potential IndexError when the input ids array is empty and suggests adding an early exit guard along with a unit test to cover this edge case.
| """ | ||
| path_prefix = _id_field(ids, PATH_PREFIX_FIELD, pa.string())[0] | ||
| file_name = _id_field(ids, FILE_NAME_FIELD, pa.string())[0] | ||
| first_id = ids.slice(0, 1) |
There was a problem hiding this comment.
If ids is empty (e.g., when there are no committed IDs for a file), slicing it via ids.slice(0, 1) will return an empty array. Consequently, _id_field(first_id, ...) will return an empty numpy array, and accessing index [0] will raise an IndexError. Adding an early exit guard for empty ids prevents this crash and gracefully returns an empty table.
| first_id = ids.slice(0, 1) | |
| if len(ids) == 0: | |
| return _COMPACTED_CHECKPOINT_SCHEMA.empty_table() | |
| first_id = ids.slice(0, 1) |
There was a problem hiding this comment.
Addressed in bf0f1bf066 (and carried forward in de2e9a78dc): added the early return if len(ids) == 0: return _COMPACTED_CHECKPOINT_SCHEMA.empty_table() before slicing ids.slice(0, 1).
| ] | ||
|
|
||
|
|
||
| def test_compact_handles_unsorted_and_interleaved_row_groups(): |
There was a problem hiding this comment.
It would be beneficial to add a unit test to verify that _compact_file_ids handles empty input arrays gracefully without raising an IndexError.
def test_compact_empty_ids():
from ray.data.checkpoint.generated_id import GENERATED_ID_COLUMN_TYPE
compacted = _compact_file_ids(pa.array([], type=GENERATED_ID_COLUMN_TYPE))
assert len(compacted) == 0
assert compacted.schema == _COMPACTED_CHECKPOINT_SCHEMA
def test_compact_handles_unsorted_and_interleaved_row_groups():There was a problem hiding this comment.
Added test_compact_empty_ids in python/ray/data/tests/unit/test_generated_id_checkpoint.py in bf0f1bf066 (de2e9a78dc).
Co-authored-by: Copilot <[email protected]> Signed-off-by: Parth Vadera <[email protected]>
| first_id = ids.slice(0, 1) | ||
| path_prefix = _id_field(first_id, PATH_PREFIX_FIELD, pa.string())[0] | ||
| file_name = _id_field(first_id, FILE_NAME_FIELD, pa.string())[0] |
There was a problem hiding this comment.
Probably want to add a comment saying that it's because all the ids belong to the same file/path_prefix, since it may not be obvious at first
There was a problem hiding this comment.
Good call — added a comment explaining that all IDs passed to _compact_file_ids belong to the same file (path_prefix, file_name, and num_row_groups), so slicing the first element suffices (de2e9a78dc). Thanks for the review!
…_file_ids Signed-off-by: Parth Vadera <[email protected]>
Why are these changes needed?
In
ray.data.checkpoint.generated_id._compact_file_ids, compacting committed generated row IDs for a Parquet file previously performed three avoidable (N)$ / (N \cdot G)$ operations ($ = committed rows in the file, $ = distinct row groups in the file):_id_field(ids, PATH_PREFIX_FIELD, pa.string())[0],_id_field(ids, FILE_NAME_FIELD, pa.string())[0], and_id_field(ids, NUM_FRAGMENTS_FIELD, pa.int32())[0]cast and materialized all $ entries into NumPy arrays just to read index[0]. Slicingids.slice(0, 1)first decodes only a single element ((1)$).for row_group in np.unique(row_groups): in_group = row_groups == row_groupscanned all $ rows once for each of the $ unique row groups. Sorting once vianp.argsort(row_groups, kind="stable")only whenrow_groupsis not already non-decreasing and finding contiguous group boundaries vianp.flatnonzero(row_groups[1:] != row_groups[:-1]) + 1reduces grouping to a single (N)$ pass when ordered ((N \log N)$ when unordered).np.uniquesorting:committed = np.unique(row_ids[in_group])sorted each group's row IDs to count unique valid indices or populate the boolean mask. Because boolean mask indexing (mask[valid_ids] = True) is idempotent on duplicate row IDs and all valid row IDs lie in[0, size), populatingmaskdirectly and checkinglen(valid_ids) >= size and mask.all()determines whether the row group is fully committed in (K + \text{size})$ time without sorting or allocating a separate array.On a file with 4,000 row groups (126,000 committed rows),
_compact_file_idswall time drops from 817.8 ms to 166.1 ms (4.92x speedup).Duplicate-work & attribution check
ray-project/rayfor_compact_file_idsandgenerated_id; no existing issue or PR addresses this optimization.generated_id_columncheckpointing: row IDs as a DSv2 synthesized column #66732 and [Data][Chkpt] [2/4] Load generated-ID checkpoints as done read units and partial row masks #66733.Checks
by: git commit -s) in this PR.pytest -v python/ray/data/tests/unit/test_generated_id_checkpoint.pylocally (16 passed in 0.36s, including a new unit test covering unsorted and interleaved row-group chunks).