Skip to content
Closed
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
10 changes: 10 additions & 0 deletions changelog.d/ITL-1464.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
### Changed

- `run()` of a `SpiralDBTableSource`, and of column selections, renames, `add_tags`,
`drop_nulls` and `filter` over one, reads the table in batches and no longer holds a result
of more than 50,000 rows or 64 MiB of values (ITL-1464). The view then has the row count and the first and the
last 1,000 rows; it is displayed as before, and its `table`, `as_table()`,
`as_polars_df()`, `sort_by_tags()` and a longer `head` or `tail` raise `BoundedViewError`,
which names the stream's own methods. `stream.as_table()` and iteration are unchanged; after
such a run `as_table()` reads the source again. Smaller results, and every other stream,
are as before.
29 changes: 24 additions & 5 deletions docs/concepts/streams.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,10 @@ print(months.head(3).header())
### Computing a stream outside a job: `run()`

Every stream, source and node has `run()`, with no arguments: it is the one way to compute
any of them outside a job (developer, November 13, 2026). It does exactly the work
`as_table()` does, through the same cache, and returns a `StreamView` of every row with the
default columns. A stream already computed is not computed a second time; one whose input
any of them outside a job (developer, November 13, 2026). It computes every row, with every
effect, in the order of `as_table()`, and returns a `StreamView` with the default columns. It
promises nothing about how the rows are produced or what is held meanwhile (developer,
November 24, 2026). A stream already computed is not computed a second time; one whose input
changed is recomputed. In a notebook, `stream.run()` at the end of a cell computes and displays.

The view's own methods choose what is shown. Each returns a new view and never reads or
Expand All @@ -209,21 +210,39 @@ view does not hold, so it keeps them in the order it has them. The display of a
such as the one `run()` returns, shows its first and last five rows when it has more than
ten, and its header gives the count.

A view holds every row of the stream, with one exception, the bounded view. A
`SpiralDBTableSource` holds no rows, and `run()` reads it in batches; so are read the
operators that choose or rename columns, add tags or drop rows (`select_*`, `drop_*`,
`map_*`, `add_tags`, `drop_nulls`, `filter`) and the chains of them over such a source. A
result of at most 50,000 rows and 64 MiB of values (the values themselves, whatever their
layout in memory), or of at most 2,000 rows, is held, as everywhere else. A larger one is not: its view has
the row count, the columns, and the first and the last 1,000 rows. It is displayed as any
view, and `head(n)` and `tail(n)` up to 1,000 rows give what any view gives. What needs the
other rows (`table`, `as_table()`, `as_polars_df()`, `sort_by_tags()`, a longer `head` or
`tail`) raises `BoundedViewError`: a part is never given as the whole. Ask the stream itself,
`stream.as_table()`, which reads the source again and applies the operators again (no function
of yours runs a second time). `stream.head(n)` after such a run shows its first rows. Whether
a view is bounded depends only on the kind of stream and on its rows, so equal data
always gives the same kind of view. A `polars_filter`, `pick`, `index`, `tag_by`, join,
`group_by` or `batch`, a function pod, and everything after one, computes its whole result
and holds it, also over a `SpiralDBTableSource`.

A view holds the default column groups only. For system tags, source info, the data context
or content hashes, ask the stream after `run()`. A function pod's or operator's output stream,
a job node (a job's side-effect node included), an in-memory source and a `CachedSource` keep
the rows `run()` gave: their `as_table(columns=...)` and `head(columns=...)` read those rows,
compute nothing and deliver no side effect. Other streams do their `as_table()` work again: a
side-effect stream outside a job delivers its side effects again (its `head()` shows no rows),
a polling source polls again and a `SpiralDBTableSource` reads its whole table again. A view never asks its stream for more columns, because for those
a polling source polls again and a `SpiralDBTableSource` reads its whole table again (so does
an operator's stream whose `run()` gave a bounded view). A view never asks its stream for more columns, because for those
streams asking again is not a read.

What `run()` does on each kind of object:

| Object | `run()` |
|---|---|
| An in-memory source (`DictSource`, `ArrowTableSource`, ...), a Delta or SQL table source | nothing to compute: a view of the rows it holds |
| A `SpiralDBTableSource` | reads the whole table into memory (`head()` is the bounded read); every call reads again |
| A `SpiralDBTableSource` | reads the whole table in batches (`head()` is the bounded read); every call reads again; a bounded view above 50,000 rows or 64 MiB |
| A `PollingSource` (provisional, see [Provisional sources](sources.md#provisional-sources)) | polls, as `as_table()` does |
| A `CachedSource` | reads its cache and writes the wrapped source's new rows to it |
| A function pod's or operator's output stream | computes what is not computed yet |
Expand Down
158 changes: 158 additions & 0 deletions docs/dev/specs/itl-1464-views.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# `run()`, its view and `preview(n)` without a full table (ITL-1464, track VIEWS)

Status: V1 to V12 approved by the coordinator, November 25, 2026 (V2, V3 and V8 flagged for
the developer); the bounded view is implemented, `preview(n)` is not. Written, against `origin/main` 0d1e017c. Companion of
`itl-1464-streaming-design.md` (sections 1, 3 and 4 of it). Nothing here changes a hash,
identity, stored layout, stored byte or golden value.

## Scope

- Now: the bounded view of a plain stream (outside a job). Files: `core/streams/base.py`,
`core/streams/view.py`, with small hooks in `DynamicPodStream` and
`SpiralDBTableSource`.
- With TRANSPORT: a job node's view, which pages from the node's reference to its rows.
- After approval of its decisions: `preview(n)` (designed here, not in the first PR).

Outside a job, only `SpiralDBTableSource` reads its rows on demand and holds none. Every
other source holds its table, so a table over those rows is not a new cost (a filter's
result over it is, and stays as today). A function pod's stream keeps its results, so
that no function is called twice: it stays as today. So the first step applies to a
**batched chain**: zero or more operators that declare `BATCHES_GIVE_THE_WHOLE` (the column
selections, the mappers, `AddTags`, `DropNulls`, `Filter`) over a source that reads in
batches. `Pick` and `Index` are row by row but excluded: they build their result from rows,
so its types and nullable flags follow the values of the batch. The flag is read from the
class itself, so a subclass declares it again. Joins, `GroupBy`, `Batch`, `PolarsFilter` and function pods
end the chain: they compute as today.

## The `run()` contract

`run()` computes every row, with every effect, in the order of `as_table()`, and returns a
`StreamView`. It promises nothing about how rows are produced or held. For a batched chain:

1. The source's batches are read in order (one scan, its checks unchanged: `max_rows`,
schema, repeated keys). Each batch, shaped as `as_table()` shapes the whole, goes
through each operator's own `static_process`: the one sync body, no second kernel.
2. Result batches are kept whole up to **50,000 rows and 64 MiB of values** (default columns; offsets,
validity and chunks not counted, so equal data is treated alike however its batches are
cut). A result that ends
within it is complete: the view holds it, and an operator stream keeps it, exactly as
today.
3. A larger result drops the kept batches to a **window**: the first and last 1,000 rows
(default columns, copied, so no batch stays alive behind a slice), the row count and
the schema. The operator stream keeps the window, not the rows.

`as_table()`, iteration and conversion are unchanged and still give every row. After a
large `run()` of a batched chain, `as_table()` reads the source again and applies the
operators again. No user function runs twice: a function pod is never in a batched chain.
An error in any batch propagates and nothing is kept, as today. When an operator raises, the
rest of the source is read first: an error of the source, which a whole read raises before
the operator runs, still comes first.

## The view

A *complete* view holds every row and is today's view, unchanged. A *bounded* view holds
the window.

| Member | Bounded view |
|---|---|
| `total_rows`, schema, `status`, `header()` | as the complete view: `all 1,234,567 rows` |
| `head(n)`, `tail(n)`, `n` within the window | the same rows and header as the complete view; the result is an ordinary view |
| `head(n)`, `tail(n)`, larger `n` | raise `BoundedViewError`, naming the window and `stream.as_table()` |
| `sort_by_tags()` | raise `BoundedViewError`: sorting needs every row |
| `as_table()`, `as_polars_df()`, `table`, `len()` | `len()` is the row count; the others raise, never a part presented as the whole |
| display | identical to the complete view's: header, legend, shape line, first and last five rows |

A view that has no cut of its own and is refused tells the caller the one call that does
it. Nothing a view does reads or computes (as today).

`stream.head(n)` after a large run of an operator stream shows the window's first rows
(`n` within the window, default columns) while the inputs are unchanged. The preview, which
asks for tag order, sorts those and says so: the present rule of a stream that has only
some of its rows. Another column group shows `not_computed`, as before a run.

### Inside a job

The same class. The window of a job node's view is filled by one pass over the node's
reference (TRANSPORT's local cache or the stored entries), or, once a reference can be read
by position, `head`, `tail` and `sort_by_tags` page from it and nothing is refused: the
refusals above are the meaning of "rows not held and not addressable". This needs two
operations from TRANSPORT: `row_count` and `read(start, length, columns)`.

## `preview(n)`

`stream.preview(n=10)` computes what the first `n` rows need and returns a `StreamView`
with status `PARTIAL` (or `ROWS`, when the stream has at most `n` rows). `head()` still
never computes.

- A source reads a limit: SpiralDB as a pushed-down scan limit (with its `where`), others a
slice of what they hold. `max_rows` does not apply, as for `head`.
- Row-by-row operators and function pods take batches until `n` rows have passed. The
first batch is `n` rows, or 1,024 when a step can drop rows; it doubles while too few
pass. A pushed-down limit is used only when no step drops rows; otherwise the scan is
read in batches and closed at `n`.
- `Join`, `MergeJoin`, `SemiJoin`, `Rejoin`, `GroupBy`, `Batch`, `TagBy`, `PolarsFilter`
and user operators need a whole input. Refused, naming the step and `run()`;
`preview(n, compute_all=True)` computes that step in full.
- A side-effect pod, or a function that takes the invocation context, is refused: a
preview has no effects.
- A kept result (an already computed stream) is sliced and nothing is computed.

## Decisions

| Id | Decision | Recommended |
|---|---|---|
| V1 | Bounded window now, disk-backed later, or wait for the local cache (N2) | window now: no file is written, and paging later only lifts refusals |
| V2 | `as_table()` / `table` / `as_polars_df()` of a bounded view: kept rows, recompute, or refuse | refuse, pointing to the stream (a part must never read as the whole; a view never recomputes) |
| V3 | `head`/`tail` beyond the window, `sort_by_tags` | refuse |
| V4 | Threshold and window | 50,000 rows and 64 MiB of values, both layout-independent (`RunSize`, `value_bytes`: Arrow's own `nbytes` differs with the batches); a result that fits in the two windows is always kept; first and last 1,000 rows; constants, no config yet |
| V5 | `as_table()` after a large run of a batched chain reads and applies operators again | yes (no user function runs again) |
| V6 | Streams in the middle of a chain keep nothing after a run of the last one (today each keeps its result) | yes, above the threshold (below it each keeps its result, as today): keeping one table per step is the cost being removed; each `run()`s by itself |
| V7 | `PolarsFilter` in a batched chain | not until B1 decides expressions that read other rows |
| V8 | `preview` calls user functions | yes, at most on the rows it looks at |
| V9 | `preview` stores or keeps anything | no: not in memory, not in a database. A cached pod reads its stored results and writes none |
| V10 | `preview` order | the first `n` rows of the sync order |
| V11 | Whole-input steps in `preview` | refuse; `compute_all=True` |
| V12 | `preview` on a job node | not offered at first: raises, naming `run()` and `head()` |

## Tests

- Differential: for each operator of a batched chain and chains of several, the result
pieces put together equal the unbatched `as_table()` (values, types, nullability, column
order, metadata) with several batch sizes, for every column configuration, with several
tags and data columns, data-tags, zero rows, and all rows dropped.
- Golden display: text and HTML of a bounded view equal those of the complete view of the
same rows, narrow and wide, with large cells, at sizes 11, 12, 1,000 and more; a small
stream's display is unchanged.
- The refusals; `head` after `run`; staleness after the source moves.
- Memory, about 2.2 million rows of a time series, before and after, for the source, a rename, a
`drop_nulls` and a `filter`.

## Measured

`run()` of a synthetic SpiralDB table, a time series (about 2.2 million rows, 13
columns, about 250 MiB of Arrow data), peak resident memory, one process
per row:

| Stream | Before | After | Arrow pool peak before | after | Time before | after |
|---|---|---|---|---|---|---|
| the source | 10.8 GiB | 1.24 GiB | 9.3 GiB | 0.07 GiB | 8.9 s | 10.4 s |
| `map_data` | 10.8 GiB | 1.40 GiB | 9.3 GiB | 0.25 GiB | 8.8 s | 10.5 s |
| `drop_nulls` | 19.7 GiB | 1.44 GiB | 18.6 GiB | 0.28 GiB | 9.8 s | 11.1 s |
| `filter` | 15.1 GiB | 1.45 GiB | 14.1 GiB | 0.29 GiB | 9.2 s | 10.2 s |
| `drop_nulls`, `filter`, `map_data` | 20.1 GiB | 1.55 GiB | 18.6 GiB | 0.39 GiB | 10.2 s | 9.9 s |

The Arrow pool holds nothing large after the run. What remains is mostly the set of keys the
source remembers to refuse a repeat (outside this track), which still grows with the table.

## Staleness of a chain that keeps nothing

An operator stream tells a change of an operator stream beneath it by that stream's reset
generation, which moves when it discards a kept result. A stream of a batched chain may have
been read without keeping one, and then discards nothing. So the state of such a stream is
its generation, its operator's modified time and the state of its own input, down to the
source (`_chain_state`): a change anywhere beneath is seen by every kept result and window
above it, whichever streams of the chain hold rows. A source with no row gives one batch of
none, so its run scans once.

One outermost question (`_upstream_states`) asks each stream once, so the walk is linear in
the graph: settling a stream asks for the states beneath it, and so does its own state.
1 change: 1 addition & 0 deletions src/orcapod/core/operators/add_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class AddTags(UnaryOperator):
# Data-tags pass through as they are; the new tags are plain (ITL-1023).
# Each output row comes from one input row (ITL-1347).
ROW_BY_ROW: ClassVar[bool] = True
BATCHES_GIVE_THE_WHOLE: ClassVar[bool] = True
DATA_TAGS_ISSUE: ClassVar[str | None] = None

def __init__(self, tags: Mapping[str, TagConstant], **kwargs: Any):
Expand Down
8 changes: 8 additions & 0 deletions src/orcapod/core/operators/column_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class SelectTagColumns(UnaryOperator):
# Data-tags (ITL-1023): handled.
# Each output row comes from one input row (ITL-1347).
ROW_BY_ROW: ClassVar[bool] = True
BATCHES_GIVE_THE_WHOLE: ClassVar[bool] = True
DATA_TAGS_ISSUE: ClassVar[str | None] = None

def __init__(self, columns: str | Collection[str], strict: bool = True, **kwargs):
Expand Down Expand Up @@ -199,6 +200,7 @@ class SelectDataColumns(UnaryOperator):
# Data-tags (ITL-1023): handled.
# Each output row comes from one input row (ITL-1347).
ROW_BY_ROW: ClassVar[bool] = True
BATCHES_GIVE_THE_WHOLE: ClassVar[bool] = True
DATA_TAGS_ISSUE: ClassVar[str | None] = None

def __init__(self, columns: str | Collection[str], strict: bool = True, **kwargs):
Expand Down Expand Up @@ -355,6 +357,7 @@ class DropTagColumns(UnaryOperator):
# Data-tags (ITL-1023): handled.
# Each output row comes from one input row (ITL-1347).
ROW_BY_ROW: ClassVar[bool] = True
BATCHES_GIVE_THE_WHOLE: ClassVar[bool] = True
DATA_TAGS_ISSUE: ClassVar[str | None] = None

def __init__(self, columns: str | Collection[str], strict: bool = True, **kwargs):
Expand Down Expand Up @@ -499,6 +502,7 @@ class DropDataColumns(UnaryOperator):
# Data-tags (ITL-1023): handled.
# Each output row comes from one input row (ITL-1347).
ROW_BY_ROW: ClassVar[bool] = True
BATCHES_GIVE_THE_WHOLE: ClassVar[bool] = True
DATA_TAGS_ISSUE: ClassVar[str | None] = None

def __init__(self, columns: str | Collection[str], strict: bool = True, **kwargs):
Expand Down Expand Up @@ -881,6 +885,8 @@ class DropColumns(_TagAndDataColumnsOperator):
be dropped: a stream needs at least one data column.
"""

BATCHES_GIVE_THE_WHOLE: ClassVar[bool] = True

_verb: ClassVar[str] = "drop"

def _removed(self, columns: Collection[str]) -> list[str]:
Expand All @@ -905,6 +911,8 @@ class SelectColumns(_TagAndDataColumnsOperator):
named: a stream needs at least one data column.
"""

BATCHES_GIVE_THE_WHOLE: ClassVar[bool] = True

_verb: ClassVar[str] = "select"

def _removed(self, columns: Collection[str]) -> list[str]:
Expand Down
1 change: 1 addition & 0 deletions src/orcapod/core/operators/drop_nulls.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class DropNulls(UnaryOperator):
# Data-tags (ITL-1023): handled.
# Each output row comes from one input row (ITL-1347).
ROW_BY_ROW: ClassVar[bool] = True
BATCHES_GIVE_THE_WHOLE: ClassVar[bool] = True
DATA_TAGS_ISSUE: ClassVar[str | None] = None

def __init__(
Expand Down
1 change: 1 addition & 0 deletions src/orcapod/core/operators/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class Filter(UnaryOperator):

# Its head shows the matching rows of its input's head (decision 13, ITL-1374).
HEAD_FROM_INPUT_HEAD: ClassVar[bool] = True
BATCHES_GIVE_THE_WHOLE: ClassVar[bool] = True

# Its identity is not frozen with data context v1 (ITL-1374).
PROVISIONAL: ClassVar[bool] = True
Expand Down
3 changes: 3 additions & 0 deletions src/orcapod/core/operators/mappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class MapData(UnaryOperator):
# Data-tags (ITL-1023): handled.
# Each output row comes from one input row (ITL-1347).
ROW_BY_ROW: ClassVar[bool] = True
BATCHES_GIVE_THE_WHOLE: ClassVar[bool] = True
DATA_TAGS_ISSUE: ClassVar[str | None] = None

def __init__(
Expand Down Expand Up @@ -371,6 +372,7 @@ class MapTags(UnaryOperator):
# Data-tags (ITL-1023): handled.
# Each output row comes from one input row (ITL-1347).
ROW_BY_ROW: ClassVar[bool] = True
BATCHES_GIVE_THE_WHOLE: ClassVar[bool] = True
DATA_TAGS_ISSUE: ClassVar[str | None] = None

def __init__(
Expand Down Expand Up @@ -667,6 +669,7 @@ class MapColumns(UnaryOperator):
# Provisional: its identity is not frozen with data context v1 (ITL-1279).
# Each output row comes from one input row (ITL-1347).
ROW_BY_ROW: ClassVar[bool] = True
BATCHES_GIVE_THE_WHOLE: ClassVar[bool] = True
PROVISIONAL: ClassVar[bool] = True

def __init__(
Expand Down
Loading
Loading