Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.d/ITL-1948.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Added

- Discover stored function pod results in a database and open them as read-only derived sources with `orcapod.discover_function_results()`.
2 changes: 2 additions & 0 deletions docs/api/sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ source id and hashes are not frozen with data context v1 (`PROVISIONAL_SOURCES`)

::: orcapod.core.sources.DerivedSource

::: orcapod.discover_function_results

::: orcapod.core.sources.CachedSource

::: orcapod.core.sources.SourceProxy
Expand Down
59 changes: 59 additions & 0 deletions docs/concepts/sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,65 @@ With the default lineage-based record IDs, a
`CachedSourceChangedRowError` if a newer stored result changes a lineage the cache already
holds, so use a new cache path for the updated results.

**Discovering results in a database.** Use `discover_function_results(database)` when
you have the database and want to find its stored function results. Each entry identifies
the pipeline, function and node content hash, and exposes the stored table paths and
tag and data Arrow schemas. Opening an entry returns a provisional `DerivedSource` you can pass to another
job. Discovery reads the current layout and data context. It needs access to both the
pipeline records and the function results and performs no writes or computation.
Discovery recovers the pipeline name relative to the database view. A job named
`"daily"` written through `database.at("shared")` is named `daily` from that view
and `shared/daily` from the root. Use the same database view as the job to recover
the identity of `node.as_source()`. Pass `entry.as_source(pipeline_name="alias")`
to open it under a surrogate name. A surrogate changes the source ID, content hash
and provenance values that contain the source ID. A job with no enclosing pipeline
folder is listed with `pipeline_name=None` and needs a surrogate to open.
For a job that used a separate result store, pass its `result_database=` as well.
Discovery relies on the stored field declarations, including Arrow types, nullability
and data-tag markers. It uses exact table paths from in-memory, Delta on POSIX or
cloud storage, SQLite, PostgreSQL, and writable keyed SpiralDB databases. Discovery
refuses connectors whose listing only exposes sanitized names.
The original job's database must also support writing its result schema.
A discovered source shows the rows committed when it is first read. To see rows the
original job commits after that first read, call `discover_function_results` again and
open a new source from the returned entry.

```python
from tempfile import TemporaryDirectory

from orcapod import PipelineJob, discover_function_results, function_pod
from orcapod.databases import DeltaTableDatabase
from orcapod.sources import DictSource


@function_pod(output_keys=["meters", "kilograms"])
def to_metric(centimeters: int, grams: int) -> tuple[float, float]:
return centimeters / 100, grams / 1000


with TemporaryDirectory() as directory:
job = PipelineJob(name="measurements", database=DeltaTableDatabase(directory))
readings = DictSource(
[
{"site": "north", "sample": "a", "centimeters": 160, "grams": 900},
{"site": "south", "sample": "b", "centimeters": 180, "grams": 1200},
],
tag_columns=["site", "sample"],
tag_only_columns=True,
source_id="readings",
)
with job:
to_metric.pod(readings)
job.run()

(entry,) = discover_function_results(DeltaTableDatabase(directory))
print(entry.function_name)
rows = entry.as_source().as_table().sort_by([("site", "ascending"), ("sample", "ascending")])
print(rows.to_pylist())
# to_metric
# [{'site': 'north', 'sample': 'a', 'meters': 1.6, 'kilograms': 0.9}, {'site': 'south', 'sample': 'b', 'meters': 1.8, 'kilograms': 1.2}]
```

**Using a tag value as an input to a computation.** Tags identify a row and data is what a
function computes on. A data-tag (the default for a declared tag) is both: a function
reads it through its data role, under the same name (see
Expand Down
Loading
Loading