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
23 changes: 23 additions & 0 deletions RELEASING.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,29 @@ If the key may have been exposed (for example printed in a log), remove it from
first (`spiral workloads keys rm`), so it stops signing in at once, then add a new key and
replace the secret as above. The job fails in between; that is the price of containment.

### Live SpiralDB source tests

The read-only `SpiralDBTableSource` tests are opt-in. Sign in with spiral-sdk and set
`ORCAPOD_SPIRALDB_TEST_PROJECT`, `ORCAPOD_SPIRALDB_TEST_DATASET` and
`ORCAPOD_SPIRALDB_TEST_TABLE` to select a keyed table you can read. Set these optional
variables to match the table's column names.

| Variable | Default | Role |
|---|---|---|
| `ORCAPOD_SPIRALDB_TEST_PREFIX_COLUMN` | `sensor_id` | A key column for the equality filter |
| `ORCAPOD_SPIRALDB_TEST_TIME_COLUMN` | `timestamp` | The time key column for range filters |
| `ORCAPOD_SPIRALDB_TEST_VALUE_COLUMN` | `reading_value` | A floating-point data column |

The first row supplies the values used in the prefix filters. For full coverage, use more
than 1,000 rows and at least eight timestamps for the first full key prefix.

```sh
uv run --extra spiraldb pytest tests/test_core/sources/test_spiraldb_table_source_live.py
```

These tests skip when any of the three settings is missing. The `spiral-integration`
job runs the connector conformance tests on a separate test project.

### Moving the spiral-sdk pin

`pyproject.toml` pins one version of `spiral-sdk` (the `spiraldb` extra): a pre-alpha that
Expand Down
4 changes: 4 additions & 0 deletions changelog.d/ITL-186.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Changed

- SpiralDB source examples use synthetic identifiers. The read-only live source tests accept
the project, dataset, table and column roles through environment variables.
2 changes: 1 addition & 1 deletion docs/concepts/notebooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ What it shows ([Looking at data](streams.md#looking-at-data) states each rule in
- **The header line** names the object (its kind and its name) and says which rows are shown:
here `first 10 of 64 rows, sorted by tags`. A source that reads only part of its table
says so, as a [SpiralDB source](sources.md) does ("10 rows read, sorted by tags, of about
63,349").
25,000").
- **Tag columns** come first and are marked, and so are data columns: in a notebook a tag's
header is bold (in an accent colour), a data column's is underlined, a vertical rule
separates the tags from the data columns, and a line under the header names the marks,
Expand Down
131 changes: 69 additions & 62 deletions docs/concepts/sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -571,15 +571,16 @@ Orcapod only reads SpiralDB for now. Writing a SpiralDB table (`SpiralDBConnecto
whose database is on one) raises `SpiralDBWriteNotSupportedError`; store a job's results in
another database, such as `DeltaTableDatabase`.

To find the tables of a project and their key columns:
Replace the example project, dataset and table names with identifiers you can access.
To find the tables of a dataset and their key columns:

<!-- docs-test: skip -->
```python
from orcapod.databases import SpiralDBConnector

with SpiralDBConnector("edi-sample-v1-855632") as spiral:
with SpiralDBConnector("my-project-123456", dataset="my_dataset") as spiral:
print(spiral.get_table_names())
print(spiral.get_pk_columns("session"))
print(spiral.get_pk_columns("sensor_reading"))
```

The table's key columns become the tag columns, and every other column is a data column:
Expand All @@ -588,10 +589,9 @@ The table's key columns become the tag columns, and every other column is a data
```python
from orcapod.sources import SpiralDBTableSource

sessions = SpiralDBTableSource("edi-sample-v1-855632", "session")
experiments = SpiralDBTableSource("edi-sample-v1-855632", "experiment")
print(sessions.output_schema())
sessions.as_df() # also as_polars_df(), as_pandas_df(), as_lazy_frame(), as_table()
readings = SpiralDBTableSource("my-project-123456", "sensor_reading", dataset="my_dataset")
print(readings.output_schema())
readings.as_df() # also as_polars_df(), as_pandas_df(), as_lazy_frame(), as_table()
```

Things to know about Spiral tables:
Expand All @@ -609,12 +609,12 @@ Things to know about Spiral tables:
- **Source identity.** A Spiral source is identified by where its table is, never by its
rows. The default `source_id` is `spiraldb_table_v1:<project id>.<dataset>.<table>@<Spiral
table id>`, for example
`spiraldb_table_v1:edi-tutorial-v1-841226.default.session@table_Jo7J0lpkLcOM`. The table id is
`spiraldb_table_v1:my-project-123456.my_dataset.sensor_reading@table_example12345`. The table id is
SpiralDB's own, so tables with the same name in different projects stay apart, and a table
that is dropped and created again under the same name is a different source: nothing
recorded for the old table is reused. A renamed table also gets a new `source_id`.
- **Record identity.** A row is identified by its primary key: its record ID is the
canonical JSON of its key columns (`{"session_id":"s1"}`). A row keeps its record ID when
canonical JSON of its key columns (`{"sensor_id":106,"site_id":2,"timestamp":0}`). A row keeps its record ID when
other rows are added, and has
the same one whichever read returned it.

Expand All @@ -640,9 +640,11 @@ Orcapod applies to the rows it receives, and then the limit):

<!-- docs-test: skip -->
```python
first = SpiralDBTableSource("edi-tutorial-v1-841226", "spike_event", limit=1000)
unit = SpiralDBTableSource(
"edi-tutorial-v1-841226", "spike_event", where={"unit_id": 106}
first = SpiralDBTableSource(
"my-project-123456", "sensor_reading", dataset="my_dataset", limit=1000
)
sensor = SpiralDBTableSource(
"my-project-123456", "sensor_reading", dataset="my_dataset", where={"sensor_id": 106}
)
print(first.row_count_estimate) # rows in the whole table; reads every key to count them
```
Expand Down Expand Up @@ -701,12 +703,12 @@ rows, in one scan to which the row cap does not apply, so a table of any size ca
at; `head(n)` reads `n`. The preview shows the rows it read in tag order (it asks for
`sort_by_tags`, as does `head(columns={"sort_by_tags": True})` or `all_info=True`): the rows
read, sorted, not the first `n` of the table in tag order. The header line says what is
shown, for example `SpiralDBTableSource[spike_event] — 10 rows read, sorted by tags, of
about 63,349` (the table's row count, from its keys; above 100,000 rows, or `max_rows` when
shown, for example `SpiralDBTableSource[sensor_reading] — 10 rows read, sorted by tags, of
about 25,000` (the table's row count, from its keys; above 100,000 rows, or `max_rows` when
that is smaller, it says `of more than 100,000`), `all 4 rows, sorted
by tags` when the source reads no more than that, or `10 matching rows read, sorted by tags
(where unit_id=106); the table has about 63,349 rows` with `where=`. The unsorted `head()`
says `first 10 rows of about 63,349`. Tag columns come first, and the line above the table
(where sensor_id=106); the table has about 25,000 rows` with `where=`. The unsorted `head()`
says `first 10 rows of about 25,000`. Tag columns come first, and the line above the table
names them. To see more rows, use `as_df()` with a `limit=` or `where=` source.

**Restrictions are not part of identity.** `where`, `limit` and `asof` choose rows of the
Expand All @@ -722,11 +724,11 @@ sent downstream during a run are only the rows that run read.
`source.where(column=value, ...)` returns a new source on the same table that reads only the
rows in which every named column equals its value. Several conditions can be given in one
call, as keywords or as a mapping, or across chained calls:
`events.where(unit_id=106, session_index=2)` and
`events.where(unit_id=106).where(session_index=2)` are the same restriction, reading the
`readings.where(sensor_id=106, site_id=2)` and
`readings.where(sensor_id=106).where(site_id=2)` are the same restriction, reading the
same rows. The conditions are a `Condition` (`orcapod.types`): `where(...)` builds one with
`Condition.of(...)`, which takes the same arguments, and a `Condition` built beforehand can be
given instead (`events.where(Condition.of(unit_id=106, session_index=2))`). A source
given instead (`readings.where(Condition.of(sensor_id=106, site_id=2))`). A source
restricted by a condition, a `limit=`, or both is **restricted**. Every source states its
restriction through two read-only properties: `condition` (the empty `Condition` when it
reads every row) and `row_limit` (`None` without a limit).
Expand All @@ -745,9 +747,9 @@ when it has none.
from orcapod import col

hour = 3_600_000_000
first_hour = events.where(col("timestamp") >= 0, col("timestamp") < hour)
second_hour = events.where(col("timestamp") >= hour, col("timestamp") < 2 * hour)
one_unit = first_hour.where(unit_id=106) # a range and an equality
first_hour = readings.where(col("timestamp") >= 0, col("timestamp") < hour)
second_hour = readings.where(col("timestamp") >= hour, col("timestamp") < 2 * hour)
one_sensor = first_hour.where(sensor_id=106) # a range and an equality
```

Ranges and equalities all must hold, on one column or several, given in one call or across
Expand Down Expand Up @@ -781,7 +783,7 @@ A restriction is not an operator:
still keeps its record ID. It adds no node, so a job bound to a restricted source is the
same job, run over fewer rows.

Use a restriction to work on part of a table (one subject, one session) with the pipeline you
Use a restriction to work on part of a table (one sensor, one site) with the pipeline you
will later run on all of it: results computed under the restriction are stored under the same
identities and reused by the full run. Use an operator when selecting rows is part of what
the pipeline computes. Restrict on tag (key) columns where you can; a condition on a data
Expand All @@ -802,14 +804,18 @@ selection lives:
```python
from orcapod import col

readings = SpiralDBTableSource(
"my-project-123456", "sensor_reading", dataset="my_dataset", source_id="readings"
)

# Exploring: the source reads only these rows; no step is added and no hash moves.
explore = events.where(unit_id=106, session_index=2)
explore = readings.where(sensor_id=106, site_id=2)

# The pipeline: the same selection as a step, part of every downstream identity.
kept = events.filter(unit_id=106, session_index=2)
kept = readings.filter(sensor_id=106, site_id=2)

# Use a filter for conditions such as is_in that where cannot apply.
late = events.filter(col("spike_time_us") >= 1_000_000, col("unit_id").is_in([106, 107]))
late = readings.filter(col("timestamp") >= 1_000_000, col("sensor_id").is_in([106, 107]))
```

A filter directly over a source reads every row the source reads (the whole table, or what
Expand All @@ -819,53 +825,55 @@ pushed into the source. `where` reads only the matching rows. On a large table,
above apply to the source. This holds for equalities on integer, string, bytes, bool, date and
timestamp columns, on a source without a `limit`. With a `limit` it does not: the source
reads the first rows that match the `where`, so the `filter` sees other rows than it would
over the source alone (with `limit=2` and unit ids `[1, 2, 2]`, `filter(unit_id=2)` alone
over the source alone (with `limit=2` and sensor ids `[1, 2, 2]`, `filter(sensor_id=2)` alone
keeps one row, the `where` and the `filter` together keep two). On a float column it does
not either when the value is `0.0`: `where` matches by the table system's equality (`0.0`
does not match `-0.0`), `filter` by Orcapod's (it does), so `where` can drop rows the `filter`
alone would keep. For such a value, use `filter` on its own.

<!-- docs-test: skip -->
```python
events = SpiralDBTableSource("my-project-123456", "spike_event", source_id="spikes")
one_unit = events.where(unit_id=106) # or events.where({"unit_id": 106})
one_session = one_unit.where(session_index=2) # conditions add up
same_session = events.where(unit_id=106, session_index=2) # both in one call
print(one_session.condition)
# unit_id=106, session_index=2
print(one_session.row_limit)
readings = SpiralDBTableSource(
"my-project-123456", "sensor_reading", dataset="my_dataset", source_id="readings"
)
one_sensor = readings.where(sensor_id=106) # or readings.where({"sensor_id": 106})
one_site = one_sensor.where(site_id=2) # conditions add up
same_site = readings.where(sensor_id=106, site_id=2) # both in one call
print(one_site.condition)
# sensor_id=106, site_id=2
print(one_site.row_limit)
# None

assert same_session.to_config() == one_session.to_config()
assert one_unit.content_hash() == events.content_hash()
job.bind(sources={"spikes": one_unit}) # the slot the unrestricted source fills
assert same_site.to_config() == one_site.to_config()
assert one_sensor.content_hash() == readings.content_hash()
job.bind(sources={"readings": one_sensor}) # the slot the unrestricted source fills
summary = job.run()
print(summary.conditions)
# {'spikes': Condition.of({'unit_id': 106})}
# {'readings': Condition.of({'sensor_id': 106})}
print(summary.restricted_source_ids)
# {'spikes': 'spikes'}
# {'readings': 'readings'}
```

(This sample needs a SpiralDB project, so it is not run with the documentation; `job` is a
job built over `events`, whose slot is named by the source id.)
job built over `readings`, whose slot is named by the source id.)

To restrict on two columns, pass separate equality keywords such as
`events.where(unit_id=106, session_index=2)`. Combining comparisons as
`(col("unit_id") == 106) and (col("session_index") == 2)` raises `TypeError`.
`readings.where(sensor_id=106, site_id=2)`. Combining comparisons as
`(col("sensor_id") == 106) and (col("site_id") == 2)` raises `TypeError`.
When building a `Condition` to pass to `where()`, combine parenthesized equalities with `&`.

```python
from orcapod import col
from orcapod.types import Condition

try:
Condition.of((col("unit_id") == 106) and (col("session_index") == 2))
Condition.of((col("sensor_id") == 106) and (col("site_id") == 2))
except TypeError as error:
print(str(error).split(".")[0])
# Condition has no truth value

both = (col("unit_id") == 106) & (col("session_index") == 2)
assert both == Condition.of(unit_id=106, session_index=2)
both = (col("sensor_id") == 106) & (col("site_id") == 2)
assert both == Condition.of(sensor_id=106, site_id=2)
```

A date or a time written as text is converted explicitly, with the function of its column's
Expand Down Expand Up @@ -931,7 +939,7 @@ print(Condition.of(day=day, started=started, recorded=recorded))
two 01:30 of a daylight-saving overlap (`fold`) are two. This equality decides whether two
restrictions are one restriction (for the one-restriction rule above); it does not decide
which rows a source returns.
`str(condition)` (or `condition.text()`) reads `unit_id=106, session_index=2`, and
`str(condition)` (or `condition.text()`) reads `sensor_id=106, site_id=2`, and
`condition.equalities` maps each column to its value. A `Condition` has a Python hash,
consistent with that equality, so it can be a dictionary key or a set member; it never
enters an Orcapod source or pipeline hash.
Expand Down Expand Up @@ -991,30 +999,29 @@ type, never null:

<!-- docs-test: skip -->
```python
spikes = SpiralDBTableSource(
"edi-tutorial-v1-841226", "spike_event", tags_as_data={"timestamp": "spike_time_us"}
readings = SpiralDBTableSource(
"my-project-123456",
"sensor_reading",
dataset="my_dataset",
tag_only_columns=True,
tags_as_data={"timestamp": "reading_time"},
)
unit_key = [
"spike_extraction_hash", "subject_id", "session_start_datetime",
"recording_index", "probe_index", "unit_id",
]
per_unit = spikes.group_by(unit_key)
print(per_unit.keys())
# (('spike_extraction_hash', 'subject_id', 'session_start_datetime', 'recording_index', 'probe_index', 'unit_id'), ('spike_amplitude_uv', 'spike_time_us'))
per_sensor = readings.group_by(["sensor_id", "site_id"])
print(per_sensor.keys())
# (('sensor_id', 'site_id'), ('temperature', 'reading_time'))
```

`per_unit` has one row per unit, and its `spike_time_us` list is in time order: `group_by`
orders the members of a group by the tags outside the group key, and on the full unit key
the only such tag is `timestamp`. The `timestamp` tag itself is dropped, as `group_by` drops
every tag outside the key; its copy `spike_time_us` is data and stays. `group_by(["unit_id"])` alone sorts by
`spike_extraction_hash`, subject, session, recording, probe, then `timestamp`, so time order
holds only within one session and probe; group on the full unit key.
The example table is keyed by `sensor_id`, `site_id` and `timestamp` and stores `temperature`.
`per_sensor` has one row per sensor and site. `group_by` orders the members by `timestamp`,
the only tag outside the group key. It drops the `timestamp` tag and keeps its copy as the
time-ordered `reading_time` list. Grouping by `sensor_id` alone would order the members by
`site_id` and then `timestamp`, so time order would hold only within a site.

The copies are data columns, so they are part of the schema, the `content_hash` and the
`pipeline_hash`; the mapping itself is part of the `content_hash` too, because two mappings
can give the same schema while the copy holds different values. The `source_id` and every
row's record id are the same as without the option. Compared with
`DerivedSource(spikes, tags_as_data=...)`, which reads the whole table as soon as its schema
`DerivedSource(readings, tags_as_data=...)`, which reads the whole table as soon as its schema
or rows are asked for (building a job asks for the schema), the option keeps a Spiral source,
whose schema is known without a scan.

Expand Down
2 changes: 1 addition & 1 deletion docs/concepts/streams.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ whatever order the rows were computed in. A stream that has only some of its row
those: a source that reads a bounded number of rows (such as a `SpiralDBTableSource`) shows
the rows it read, in tag order, and a stream computed only in part shows the rows computed so
far in tag order. It is not "the first `n` rows in tag order" of rows it never read, and the
preview's header line says so ("10 rows read, sorted by tags, of about 63,349"). A stream
preview's header line says so ("10 rows read, sorted by tags, of about 25,000"). A stream
with no tag columns previews its rows in stream order.

A head of an operator is never the operator applied to the heads of its inputs: a group over
Expand Down
2 changes: 1 addition & 1 deletion docs/dev/design_issues/ITL-186-SP4.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
**Issue:** ITL-186

`SpiralDBTableSource` inherited `DBTableSource`'s constructor, which read every row and built
provenance for the whole table: about 4.6 GB for the 8,087,906-row `spike_event`. Design and
provenance for the whole table: about 4.6 GB for a table with roughly 8 million rows. Design and
measurements: `docs/dev/specs/2026-11-12-itl-186-streaming-spiral-source-design.md`.

**Fix:** the source is built from metadata only (schema, key columns, SpiralDB's table id) and
Expand Down
Loading
Loading