Skip to content

[data] Zarr datasource - #63003

Merged
richardliaw merged 53 commits into
ray-project:masterfrom
alexandrplashchinsky:zarr-datasource
Jun 22, 2026
Merged

richardliaw merged 53 commits into
ray-project:masterfrom
alexandrplashchinsky:zarr-datasource

Conversation

@alexandrplashchinsky

@alexandrplashchinsky alexandrplashchinsky commented Apr 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR introduces ray.data.read_zarr() and the backing ZarrV2Datasource for reading Zarr v2 stores with Ray Data.

This adds a dedicated public API and datasource implementation for Zarr v2 so users can read chunk metadata from consolidated Zarr v2 stores through the standard Ray Data read API surface.

Related issues

N/A

Additional information

This PR adds:

  • ray.data.read_zarr() as a new public Ray Data read API
  • ZarrV2Datasource as the datasource implementation used by the API
  • unit tests covering the datasource behavior and API wiring

Example usage:

import ray

ds = ray.data.read_zarr("/path/to/store")

@alexandrplashchinsky
alexandrplashchinsky requested a review from a team as a code owner April 28, 2026 21:19

@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 introduces support for reading Zarr v2 stores in Ray Data by adding the ZarrV2Datasource and a public read_zarrv2 API. The implementation includes support for various storage backends (local, S3, Azure) and handles chunk metadata, slice bounds, and padding. Feedback focuses on reducing logic duplication in chunk calculation, simplifying path normalization, converting a utility method to a static method, and adhering to standard Python formatting for keyword arguments.

Comment thread python/ray/data/read_api.py Resolved
@ayushk7102 ayushk7102 added the go add ONLY when ready to merge, run all tests label Apr 28, 2026
@ray-gardener ray-gardener Bot added the data Ray Data-related issues label Apr 29, 2026
@alexandrplashchinsky alexandrplashchinsky removed the go add ONLY when ready to merge, run all tests label Apr 29, 2026
@alexandrplashchinsky alexandrplashchinsky self-assigned this Apr 29, 2026
@alexandrplashchinsky
alexandrplashchinsky force-pushed the zarr-datasource branch 2 times, most recently from 901636d to 687c8fa Compare April 29, 2026 03:12
@alexandrplashchinsky
alexandrplashchinsky force-pushed the zarr-datasource branch 2 times, most recently from 34ab253 to a57fd8c Compare April 29, 2026 18:34
@alexandrplashchinsky alexandrplashchinsky added the go add ONLY when ready to merge, run all tests label May 1, 2026
@alexandrplashchinsky alexandrplashchinsky added go add ONLY when ready to merge, run all tests and removed go add ONLY when ready to merge, run all tests labels May 1, 2026
Comment thread python/ray/data/__init__.py Resolved
@richardliaw

Copy link
Copy Markdown
Contributor

Can we rename this so that it's read_zarr?

torchvision==0.24.0
confluent-kafka
zarr<3 ; python_version >= '3.11' # zarr 2.18.4+ requires py3.11+ (v2 API)
zarr>=2.18,<2.18.4 ; python_version < '3.11' # 2.18.3: last v2 line supporting py3.10

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

can you remove the upperbound? should recompile without issue since the lock files have a pinned version

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ArturNiederfahrenhorst and others added 6 commits June 19, 2026 09:57
Build read-task output with DelegatingBlockBuilder (-> ArrowBlockBuilder)
instead of hand-constructing pandas DataFrames, matching the tensor/per-row
datasources (image, audio, video, torch). Blocks are now pyarrow Tables and
the Arrow tensor extension handles the variable-shaped `chunk` column
(shorter trailing-edge chunks) automatically. Drops the pandas dependency
in the datasource.

Test updates:
- _execute_read_tasks converts each (now-Arrow) block to pandas.
- _reconstruct_array sorts by a tuple key, since chunk_index/chunk_slices
  round-trip as Arrow lists, not Python tuples.
- Drop ray_start_regular_shared from the two auto-init tests: building an
  Arrow block auto-inits Ray, which conflicted with the fixture's unguarded
  ray.init() (the rest of the module already relies on auto-init).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: Artur Niederfahrenhorst <[email protected]>
Signed-off-by: Artur Niederfahrenhorst <[email protected]>
Signed-off-by: Artur Niederfahrenhorst <[email protected]>
Remove three tests whose coverage is fully subsumed elsewhere:

- test_align_axis_0_accepts_per_array_chunk_shapes: dict chunk_shapes
  resolution is covered by test_chunk_shapes_resolution_across_mixed_rank
  (asserts _array_chunks directly), and aligned wide-row output by
  test_align_axis_0_emits_wide_rows; the aligned path consumes the resolved
  chunks regardless of dict vs sequence, so the combination adds no path.
- test_overlap_enables_windowing_without_cross_row_loss: its assertion is
  pure arithmetic on the per-row data extents already asserted by
  test_overlap_extends_chunk_data; it exercises no new datasource behavior.
- test_align_axis_0_column_set: the no-array_paths case duplicated the column
  assertion in test_align_axis_0_emits_wide_rows; de-parametrized to keep only
  the array_paths-filtering case, which is its unique coverage.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: Artur Niederfahrenhorst <[email protected]>
Fix ray-project#2 (limit pushdown): the read fns now slice their batch to
per_task_row_limit, so a downstream limit(K) reads ~K chunks instead of the
whole batch's I/O. Previously ReadTask only truncated the already-built block
(_iter_sliced_blocks), so every chunk in the batch was still fetched.

Fix ray-project#4 (retries): chunk reads are wrapped in
iterate_with_retry(match=DataContext.retried_io_errors) -- the same mechanism
FileBasedDatasource uses -- so zarr reads now honor Ray Data's retry config.
The underlying filesystem's own retry still applies underneath.

Tests: per_task_row_limit caps the number of _read_chunk calls (not just the
output row count); _read_chunk retries a transient error then succeeds.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: Artur Niederfahrenhorst <[email protected]>
_get_long_form_read_tasks no longer materializes a per-chunk descriptor list on
the driver -- the product(grid) enumeration was O(total chunks) and ran even for
take(1)/limit (e.g. ~64,800 descriptors for one MUR SST array). Read tasks now
describe a contiguous flat range of the chunk grid; the read fn unravels each
flat index to an N-D chunk_index lazily on the worker. Planning is O(n_tasks)
per array, independent of chunk count.

- New _ChunkRange (replaces per-chunk _ChunkDescriptor) + _unravel (row-major,
  preserving the previous itertools.product ordering).
- size_bytes is now an O(1) upper-bound estimate (full-size chunk per index)
  instead of an O(chunks) exact sum.
- per_task_row_limit caps the range, not a list slice; aligned path unchanged
  (already O(output rows)).

Adds a test asserting chunk_index order is identical to grid enumeration.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: Artur Niederfahrenhorst <[email protected]>
Replace the hand-rolled flat-index -> N-D unravel helper with numpy's
np.unravel_index (the recognized primitive for exactly this). Its default
C-order matches the previous ordering, so the emitted chunk_index sequence is
unchanged (the ordering test still passes); int() keeps the indices as Python
ints. Drops the _unravel helper.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: Artur Niederfahrenhorst <[email protected]>
Signed-off-by: Artur Niederfahrenhorst <[email protected]>
@ArturNiederfahrenhorst

Copy link
Copy Markdown
Contributor

I did another round of self-review and also asked CC to view to make this quicker.
Also ran some Anyscale jobs to test again.

pyrefly (CI lint) flagged 3 type errors in the new files. None are runtime bugs
(the suite passes); the fixes clarify intent or suppress an intentional fake:
- _create_aligned_read_fn: annotate `row: dict[str, Any]` so assigning a chunk
  ndarray isn't checked against the all-int TypedDict pyrefly inferred from the
  t_start/t_stop literals.
- _is_positive_int: `int(x) > 0` (pyrefly can't type `>` on numbers.Integral).
- test_read_chunk_retries_transient_io: `# pyrefly: ignore[bad-argument-type]`
  for the deliberate fake _Root() passed to _read_chunk (repo convention).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: Artur Niederfahrenhorst <[email protected]>
ArturNiederfahrenhorst added a commit to shorbaji/ray that referenced this pull request Jun 19, 2026
Use the public example dataset in the docs and add an integration test (matching
read_zarr, ray-project#63003): the read_lerobot and LeRobotDatasource docstring examples now
read s3://anonymous@ray-example-data/lerobot/libero-mini, and
test_read_lerobot_integration_public_s3 reads it end-to-end.

Review fixes (Ray Data conventions):
- Expose num_cpus/num_gpus/memory/ray_remote_args/concurrency on read_lerobot,
  forwarded to read_datasource (mirrors read_images) so the decode-heavy read
  tasks can be tuned; document the override_num_blocks vs partitioning
  interaction.
- Dictionary-encode the per-dataset-constant stats and task columns instead of
  repeating the multi-KB stats JSON on every row, and count the appended columns
  in the in-memory size estimate.
- Close the fsspec file handles in _CredsVideoDecoderCache.clear() (was leaking a
  file descriptor per decoded video file).
- Add image-based v3 unit coverage: a synthetic image_camera fixture and
  test_read_lerobot_image_camera.
- Drop the redundant per-task driver ray.get(roots_ref); pass the already-
  materialized roots to _LeRobotReadTask, keeping the ref only for the worker
  read path.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: Artur Niederfahrenhorst <[email protected]>
Signed-off-by: Artur Niederfahrenhorst <[email protected]>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

There are 3 total unresolved issues (including 2 from previous reviews).

Fix All in Cursor

Reviewed by Cursor Bugbot for commit fa16bd3. Configure here.

Comment on lines +864 to +878
if ray.is_initialized():
ray.shutdown()
ray.init(
num_cpus=1,
logging_level=logging.ERROR,
log_to_driver=False,
runtime_env={"worker_process_setup_hook": _register_codec},
)
try:
ds = ray.data.read_zarr(str(store_path))
rows = sorted(ds.take_all(), key=lambda r: tuple(r["chunk_index"]))
recon = np.concatenate([r["chunk"] for r in rows])
np.testing.assert_array_equal(recon, np.arange(8, dtype="u1"))
finally:
ray.shutdown()

@richardliaw richardliaw Jun 19, 2026 •

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.

is there no existing ray fixture for this? if not, can we use a fixture instead?

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.

Done

zarrv2_datasource.ZarrV2Datasource(str(tmp_path))


def test_explicit_filesystem_strips_uri_scheme(tmp_path):

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.

none of these tests are well-isolated, i think you need the shutdown_only fixture or something

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! Isolated.

ArturNiederfahrenhorst and others added 4 commits June 21, 2026 23:03
Signed-off-by: Artur Niederfahrenhorst <[email protected]>
Signed-off-by: Artur Niederfahrenhorst <[email protected]>
The datasource is backend-agnostic: path/filesystem resolution is delegated to
pyarrow/fsspec (shared Ray Data machinery), so a live remote read exercises
generic pyarrow/fsspec, not datasource logic. Filesystem handling is already
covered hermetically by test_read_zarr_basic_across_filesystems (parametrized
over fs flavors on local paths), so the unit-test file stays network-free.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: Artur Niederfahrenhorst <[email protected]>
@richardliaw
richardliaw merged commit 38045ff into ray-project:master Jun 22, 2026
3 checks passed
ArturNiederfahrenhorst added a commit to shorbaji/ray that referenced this pull request Jun 24, 2026
lerobot/av/torchcodec (gated python>=3.12) were dropped from the committed data
locks when the branch merged current master: zarr (ray-project#63003) and the HF stack
upgrade (ray-project#64054) rewrote the same py3.12 locks, and the merge took master's
lerobot-free version while keeping lerobot in data-test-requirements.txt. That
left the lock inconsistent with the .txt, so the py3.12 CI image never installed
lerobot and test_lerobot skip-passed.

Re-ran `raydepsets build ci/raydepsets/configs/ci_data.depsets.yaml` (same relock
as 6321043, with lerobot_overrides.txt). Re-adds lerobot==0.5.1 / av==15.1.0 /
torchcodec==0.10.0 (torch 2.9.0+cpu) to the py3.12 data-base, pyarrow-latest, and
relaxed locks; py3.10/3.11 untouched (gated). Resolves cleanly against master's
HF stack.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: Artur Niederfahrenhorst <[email protected]>
ArturNiederfahrenhorst added a commit that referenced this pull request Jun 30, 2026
This reverses the doc relocation from #63003: the output schemas,
metadata discovery, chunk re-tiling, aligned/sliding-window reads,
custom codecs, and .zattrs notes now live in the read_zarr API docstring
(rendered on its autodoc page). The dedicated guide and its
user-guide.rst toctree entry are removed, along with the three
:ref:`working_with_zarr` links, so the docs build has no dangling
cross-references.

---------

Signed-off-by: Artur Niederfahrenhorst <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
limarkdcunha pushed a commit to limarkdcunha/ray that referenced this pull request Jun 30, 2026
## Description
This PR introduces `ray.data.read_zarr()` and the backing
`ZarrV2Datasource` for reading Zarr v2 stores with Ray Data.

This adds a dedicated public API and datasource implementation for Zarr
v2 so users can read chunk metadata from consolidated Zarr v2 stores
through the standard Ray Data read API surface.

## Related issues
N/A

## Additional information
This PR adds:
- `ray.data.read_zarr()` as a new public Ray Data read API
- `ZarrV2Datasource` as the datasource implementation used by the API
- unit tests covering the datasource behavior and API wiring

Example usage:

```python
import ray

ds = ray.data.read_zarr("/path/to/store")

---------

Signed-off-by: Alexandr Plashchinsky <[email protected]>
Signed-off-by: Artur Niederfahrenhorst <[email protected]>
Co-authored-by: Alexandr Plashchinsky <[email protected]>
Co-authored-by: Artur Niederfahrenhorst <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
## Description
This PR introduces `ray.data.read_zarr()` and the backing
`ZarrV2Datasource` for reading Zarr v2 stores with Ray Data.

This adds a dedicated public API and datasource implementation for Zarr
v2 so users can read chunk metadata from consolidated Zarr v2 stores
through the standard Ray Data read API surface.

## Related issues
N/A

## Additional information
This PR adds:
- `ray.data.read_zarr()` as a new public Ray Data read API
- `ZarrV2Datasource` as the datasource implementation used by the API
- unit tests covering the datasource behavior and API wiring

Example usage:

```python
import ray

ds = ray.data.read_zarr("/path/to/store")

---------

Signed-off-by: Alexandr Plashchinsky <[email protected]>
Signed-off-by: Artur Niederfahrenhorst <[email protected]>
Co-authored-by: Alexandr Plashchinsky <[email protected]>
Co-authored-by: Artur Niederfahrenhorst <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: 400Ping <[email protected]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
This reverses the doc relocation from ray-project#63003: the output schemas,
metadata discovery, chunk re-tiling, aligned/sliding-window reads,
custom codecs, and .zattrs notes now live in the read_zarr API docstring
(rendered on its autodoc page). The dedicated guide and its
user-guide.rst toctree entry are removed, along with the three
:ref:`working_with_zarr` links, so the docs build has no dangling
cross-references.

---------

Signed-off-by: Artur Niederfahrenhorst <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: 400Ping <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

5 participants