Skip to content

[Data] Vectorize row-group compaction in _compact_file_ids - #67820

Merged
goutamvenkat-anyscale merged 3 commits into
ray-project:masterfrom
deep1blue:perf/compact-file-ids-vectorize
Nov 9, 2026
Merged

goutamvenkat-anyscale merged 3 commits into
ray-project:masterfrom
deep1blue:perf/compact-file-ids-vectorize

Conversation

@deep1blue

Copy link
Copy Markdown
Contributor

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):

  1. Redundant (N)$ dictionary string decoding for file-constant metadata: _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]. Slicing ids.slice(0, 1) first decodes only a single element ((1)$).
  2. (N \cdot G)$ full-array equality scans across row groups: for row_group in np.unique(row_groups): in_group = row_groups == row_group scanned all $ rows once for each of the $ unique row groups. Sorting once via np.argsort(row_groups, kind="stable") only when row_groups is not already non-decreasing and finding contiguous group boundaries via np.flatnonzero(row_groups[1:] != row_groups[:-1]) + 1 reduces grouping to a single (N)$ pass when ordered ((N \log N)$ when unordered).
  3. Per-group (K \log K)$ np.unique sorting: 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), populating mask directly and checking len(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_ids wall time drops from 817.8 ms to 166.1 ms (4.92x speedup).

Duplicate-work & attribution check

Checks

  • I've signed off every commit(by: git commit -s) in this PR.
  • I've run pytest -v python/ray/data/tests/unit/test_generated_id_checkpoint.py locally (16 passed in 0.36s, including a new unit test covering unsorted and interleaved row-group chunks).

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]>
@deep1blue
deep1blue requested a review from a team as a code owner November 4, 2026 16:40

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Suggested change
first_id = ids.slice(0, 1)
if len(ids) == 0:
return _COMPACTED_CHECKPOINT_SCHEMA.empty_table()
first_id = ids.slice(0, 1)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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():

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test_compact_empty_ids in python/ray/data/tests/unit/test_generated_id_checkpoint.py in bf0f1bf066 (de2e9a78dc).

@ray-gardener ray-gardener Bot added data Ray Data-related issues community-contribution Contributed by the community labels Nov 4, 2026

@iamjustinhsu iamjustinhsu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Comment on lines +209 to +211
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]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@iamjustinhsu iamjustinhsu added the go add ONLY when ready to merge, run all tests label Nov 4, 2026
@goutamvenkat-anyscale
goutamvenkat-anyscale merged commit b95b872 into ray-project:master Nov 9, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community data Ray Data-related issues go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants