-
-
Notifications
You must be signed in to change notification settings - Fork 434
feat: url-pipeline core — parser, adapter ABC, registry, store hooks #4192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jhamman
wants to merge
1
commit into
zarr-developers:main
Choose a base branch
from
jhamman:feature/url-pipeline-core
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| Added core support for URL pipelines (https://github.com/jbms/url-pipeline): | ||
| `|`-chained URLs that address zarr data through nested storage layers, e.g. | ||
| `s3://bucket/data.zip|zip:|zarr3:`. This PR adds the parser, the single-method | ||
| `zarr.abc.url_pipeline.URLPipelineAdapter` interface, and the | ||
| `zarr.url_adapters` entry-point group through which third-party packages | ||
| (e.g. Icechunk) register adapters for their own schemes. Adapters for a scheme | ||
| are loaded lazily and individually; URLs without a `|` separator (and without | ||
| a registered root scheme) are handled exactly as before. Builtin adapters | ||
| (`zip:`, `zarr2:`/`zarr3:`) follow in separate pull requests. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| """ | ||
| Abstract base class and data model for URL pipeline adapters. | ||
|
|
||
| A URL pipeline is a `|`-separated chain of sub-URLs, read outer-to-inner, | ||
| as specified by https://github.com/jbms/url-pipeline. The first sub-URL (the | ||
| *root*) locates a resource using a conventional URL, and each subsequent | ||
| sub-URL names an *adapter* that reinterprets everything to its left: | ||
|
|
||
| s3://bucket/data.zip|zip:path/inside|zarr3: | ||
|
|
||
| Third-party packages provide adapters by subclassing | ||
| [`URLPipelineAdapter`][zarr.abc.url_pipeline.URLPipelineAdapter] and | ||
| registering the class under the `zarr.url_adapters` entry-point group, | ||
| using the URL scheme as the entry-point name. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from dataclasses import dataclass, field | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Awaitable, Callable | ||
|
|
||
| from zarr.abc.store import Store | ||
| from zarr.core.common import AccessModeLiteral | ||
|
|
||
| __all__ = [ | ||
| "AdapterResolution", | ||
| "PipelineContext", | ||
| "PipelineSegment", | ||
| "URLPipelineAdapter", | ||
| ] | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class PipelineSegment: | ||
| """ | ||
| One `|`-delimited sub-URL of a URL pipeline. | ||
|
|
||
| Attributes | ||
| ---------- | ||
| scheme : str | ||
| The lowercased URL scheme. Empty string only for a schemeless root | ||
| (a bare local path). | ||
| body : str | ||
| The text after `scheme:` and before any `?`. Interpretation is | ||
| scheme-defined; it is **not** URL-normalized, so case-significant | ||
| content (e.g. icechunk snapshot IDs) is preserved. | ||
| query : str | None | ||
| The raw query string after `?`, or None. Interpretation is | ||
| scheme-defined. | ||
| raw : str | ||
| The exact original sub-URL text, preserved for lossless | ||
| reconstruction of the pipeline. | ||
| """ | ||
|
|
||
| scheme: str | ||
| body: str | ||
| query: str | None | ||
| raw: str | ||
|
|
||
| def __str__(self) -> str: | ||
| return self.raw | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class AdapterResolution: | ||
| """ | ||
| The result of resolving a URL pipeline (or a prefix of one). | ||
|
|
||
| Attributes | ||
| ---------- | ||
| store : Store | ||
| The resolved store. | ||
| path : str | ||
| Residual path *within* the store that the pipeline addresses | ||
| (e.g. `"path/to/node"` for `...|icechunk://tag.v1/path/to/node`). | ||
| Empty string when the pipeline addresses the store root. | ||
| """ | ||
|
|
||
| store: Store | ||
| path: str = "" | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class PipelineContext: | ||
| """ | ||
| Context handed to a [`URLPipelineAdapter`][zarr.abc.url_pipeline.URLPipelineAdapter] | ||
| describing the pipeline to the left of its segment. | ||
|
|
||
| Attributes | ||
| ---------- | ||
| preceding : tuple[PipelineSegment, ...] | ||
| The parsed sub-URLs to the left of the adapter's segment, outer to | ||
| inner. Empty when the adapter's segment is the pipeline root. | ||
| mode : AccessModeLiteral | None | ||
| The access mode requested by the caller (e.g. `zarr.open(mode=...)`), | ||
| or None when unspecified. Adapters for read-only resources should | ||
| raise for unambiguous write modes (`"w"`, `"w-"`, `"r+"`) and | ||
| open read-only otherwise. `"a"` (the `zarr.open` default) means | ||
| open-or-create: read-only adapters serve the "open" half, and any | ||
| subsequent write fails at the store level. | ||
| read_only : bool | ||
| True when the caller requires a read-only store (`mode == "r"`). | ||
| Adapters must construct their store read-only when this is set; | ||
| when it is False, they may construct a writable store if the | ||
| underlying resource supports writing. | ||
| storage_options : dict[str, Any] | None | ||
| Options passed by the caller. By convention these configure the | ||
| *root* sub-URL (e.g. fsspec options), but adapters may consume | ||
| adapter-specific keys. | ||
| """ | ||
|
|
||
| preceding: tuple[PipelineSegment, ...] | ||
| mode: AccessModeLiteral | None | ||
| read_only: bool | ||
| storage_options: dict[str, Any] | None | ||
| _resolver: Callable[[tuple[PipelineSegment, ...]], Awaitable[AdapterResolution]] = field( | ||
| repr=False | ||
| ) | ||
|
|
||
| @property | ||
| def preceding_url(self) -> str: | ||
| """ | ||
| The pipeline to the left of this segment, reconstructed exactly. | ||
|
|
||
| An adapter that consumes this string instead of calling | ||
| [`resolve_preceding`][zarr.abc.url_pipeline.PipelineContext.resolve_preceding] | ||
| takes ownership of the *entire* preceding pipeline: it must | ||
| validate every preceding segment itself and raise | ||
| [`URLPipelineError`][zarr.errors.URLPipelineError] for segments it | ||
| does not understand, so that no segment is ever silently ignored. | ||
| """ | ||
| return "|".join(segment.raw for segment in self.preceding) | ||
|
|
||
| async def resolve_preceding(self) -> AdapterResolution: | ||
| """ | ||
| Resolve the preceding pipeline into a store. | ||
|
|
||
| This is the entry point for *wrapper* adapters (e.g. `zip:`) that | ||
| operate on the resource produced by the segments to their left. It | ||
| composes with any preceding adapters, because each segment is | ||
| resolved by its own adapter. Adapters backed by their own I/O | ||
| machinery (e.g. `icechunk:`) may instead consume | ||
| [`preceding_url`][zarr.abc.url_pipeline.PipelineContext.preceding_url] | ||
| and never materialize the intermediate store — subject to the | ||
| ownership contract documented there. | ||
| """ | ||
| return await self._resolver(self.preceding) | ||
|
|
||
|
|
||
| class URLPipelineAdapter(ABC): | ||
| """ | ||
| Handler for one URL pipeline scheme. | ||
|
|
||
| Subclasses implement a single classmethod, | ||
| [`open_pipeline_segment`][zarr.abc.url_pipeline.URLPipelineAdapter.open_pipeline_segment], | ||
| and are registered under the `zarr.url_adapters` entry-point group with | ||
| the URL scheme as the entry-point name: | ||
|
|
||
| [project.entry-points."zarr.url_adapters"] | ||
| myscheme = "mypackage.zarr_adapter:MyAdapter" | ||
|
|
||
| An adapter is used in two positions: | ||
|
|
||
| - as an *adapter segment*: `s3://bucket/repo|icechunk://tag.v1` — the | ||
| context carries the preceding sub-URLs; | ||
| - as a *root scheme*: `gh://org/repo` — `context.preceding` is empty. | ||
| """ | ||
|
|
||
| @classmethod | ||
| @abstractmethod | ||
| async def open_pipeline_segment( | ||
| cls, segment: PipelineSegment, context: PipelineContext | ||
| ) -> AdapterResolution: | ||
| """ | ||
| Resolve `segment` (in the context of the pipeline to its left) | ||
| into a store and an optional residual path within that store. | ||
|
|
||
| The returned store must already be open and must honor | ||
| `context.read_only`. | ||
| """ | ||
| ... | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably need a link to jbms/url-pipeline so people can see the actual spec