Skip to content

docs(zarr-indexing): add an asyncio example of consumer-owned I/O - #497

Draft
d-v-b wants to merge 5 commits into
feat/zarr-indexing-public-loweringfrom
docs/zarr-indexing-asyncio-example
Draft

d-v-b wants to merge 5 commits into
feat/zarr-indexing-public-loweringfrom
docs/zarr-indexing-asyncio-example

Conversation

@d-v-b

@d-v-b d-v-b commented Dec 4, 2026

Copy link
Copy Markdown
Owner

🤖 AI text below 🤖

A new lazy_indexing_asyncio example plans reads with LazyArray.parts() and fetches them through its own asyncio code over zarr.AsyncArray, including a chunk cache that overlapping views share. The integrations guide gains a section on consumer-owned I/O with Partition.source_selection and IndexTransform.decompose().

Stacked on #496 (public basic-selection lowering), which must merge first; this PR targets its branch and stays a draft until then. Re-cut from #292 (piece 3 of 3). #292's result_into was dropped because it overlaps upstream's iter_filled.

Agent notes

What the example does. The planner is LazyArray(source).with_reader(RefuseReads()), so any read through the wrapper fails. All data comes from the consumer's own asyncio.gather over zarr.AsyncArray.getitem. A CountingStore asserts the I/O. Five views read one request per part: a box, a strided column, an added axis, reversed strides and a gather, totaling 42 chunk reads for 42 parts. A shared chunk cache then serves three overlapping views with 12 chunk reads.

  • A part whose source_selection Zarr accepts is fetched with it. Any other part (an added axis, a negative step, a gather) fetches the ascending cover from decompose() and applies the residual in memory with numpy_reader.read_into.
  • The cache holds asyncio.Tasks keyed by the whole projection.chunk_domain and shields fetches, so one cancelled read doesn't cancel the others.
  • It does not use zarr_indexing.iteration: those functions read through the wrapper's own reader, and SelectionPlan.chunks keys are opaque, so they can't drive the consumer's own fetches. The README and guide point to iteration for when the wrapper's reader can do the I/O.

Changes from #292's version.

Verification. just test: 2117 passed, 48 skipped (tensorstore, dask and xarray not installed). The example's tests: 5 passed. The example also ran as its README says, with PEP 723 deps from PyPI and with zarr==3.1.0. Typecheck has 0 errors, lint and format are clean, and just docs-check (strict) builds. Not type-checked: examples/ is outside pyright's include, like the other examples.

Adversarial review. Three rounds by fresh reviewer agents.

  • Round 1 (full diff) found 2 serious bugs, both reproduced and fixed in 4f39646:
    • The cache was keyed by chunk_domain.inclusive_min, so a view on a different grid silently read a chunk of the wrong extent. It is now keyed by the IndexDomain.
    • Cancelling one read cancelled fetches other reads awaited, and left cancelled tasks cached. Fixed with asyncio.shield.
    • Five minor wording and accuracy fixes went into the same commit. Not acted on: an assertion counting direct versus cover parts (it would add code to a docs example), and keeping the snippet's recorder.keys == [] check.
  • Round 2 (fix diff): nothing serious. Two nits were fixed in 844358b. One minor was left: if every reader of a shielded fetch is cancelled and the fetch then fails, asyncio logs "Task exception was never retrieved". The example never cancels, and the docstring says failed fetches stay cached.
  • Round 3 (wording fix): clean.

Noted for #496, outside this diff: the Partition.box docstring says the box "may be narrower than projection.chunk_domain", but parts() builds box from chunk_domain.

🤖 Generated with Claude Code

d-v-b added 5 commits December 4, 2026 16:47
The new lazy_indexing_asyncio example plans reads with `LazyArray.parts()`
and performs every read itself, with `asyncio.gather` over
`zarr.AsyncArray`. A part whose `source_selection` Zarr accepts is fetched
with it; any other part (a new axis, a negative step, a gather) fetches the
ascending cover from `IndexTransform.decompose()` and reads the residual
from the block with `numpy_reader`. A cache of decoded chunks, keyed by
each chunk's origin and read with `chunk_local_selection`, lets
overlapping views share fetches. A reader that refuses every read and a
store that counts chunk reads check that the consumer did all the I/O.

The example is a plain PEP 723 script run from `main()`, and
`tests/test_doc_examples.py` runs it in process, skipping without zarr.

Assisted-by: ClaudeCode:claude-opus-5-5
…guide

A new section explains how a consumer with its own I/O layer plans with
`parts()` and reads each part itself: `Partition.source_selection` where
the conservative lowering spells the read, and the cover and residual of
`IndexTransform.decompose()` where it refuses. It covers
`chunk_local_selection` and `projection.chunk_domain` for chunk caches,
backends narrower than NumPy, and links the asyncio example and
`zarr_indexing.iteration`. An executable snippet reads a box and a gather
without touching the wrapped source.

Assisted-by: ClaudeCode:claude-opus-5-5
Review found two bugs in the example's `ChunkCache`. Keyed by the chunk's
origin alone, a view partitioned with a different grid read a cached chunk
of the wrong extent and got silently wrong values; the cache now keys by
the whole `projection.chunk_domain`. Gathering the shared fetch tasks
directly meant cancelling one read cancelled fetches other reads awaited,
and left the cancelled tasks cached; the fetches are now shielded.

The README and guide also say that only queries of more than one
coordinate refuse `source_selection`, that the first scenario reads once
per part rather than once per chunk, and that the whole domain is the
cache key.

Assisted-by: ClaudeCode:claude-opus-5-5
Rewrap the guide's new opening sentence, and say that views indexed from
one `LazyArray` share a grid; `with_parts()` also derives a view, with a
different one.

Assisted-by: ClaudeCode:claude-opus-5-5
Assisted-by: ClaudeCode:claude-opus-5-5

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant