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
40 changes: 40 additions & 0 deletions changelog.d/ITL-1419.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
### Breaking changes and migration

- Reading argument symmetry raises `RepeatedOperandError` (an `InputValidationError`)
when a flat unordered group collapses repeated inputs. This applies to built-in and
custom operators, including `Rejoin` and the join used by a function pod. Two inputs
with the same identity count as repeats. The developer approved the refusal
on 2026-11-27 as a stopgap until ITL-1511 allows repeats with a unique/repeat toggle.
Previously `Join()(d)` and `Join()(d, d2)` could share a content hash and cache entry
despite yielding different rows when `d2` shared `d`'s identity. The error names the
pod that received the repeated inputs and points to the ordered `MergeJoin()(d, d)`
for self-joins. An operand whose identity cannot be computed raises its existing
hashing error in either argument order, even when another input repeats. Overlapping
data columns still raise an error when the step is built.

### Fixed

- A job runs an ordered operator given one stream for both inputs, `MergeJoin()(a, a)`
and `SemiJoin()(a, a)` (ITL-1419, stage 1 of ITL-1472). A pipeline's graph edges now
carry their input position, `(upstream, node, position)`. The position is the argument
index for an ordered operator, `None` for a commutative one (also with one input), and
0 for a function or side-effect pod. An operator using a nested ordered group also
keeps its argument indices. Both orchestrators give a node one input per
edge, so both inputs reach the operator when it runs in a job made from a `Pipeline`
or a `PipelineJob`, after `PipelineJob.load` and through `node.run()`. Positions
exist in memory. A saved file still lists each `(upstream, node)` pair once, and the
loader reconstructs positions from the operator's saved `inputs` list. When ITL-1511
allows repeats, identical edges will get occurrence numbers so each edge can be
identified individually.

### Known limitations

- Standalone row computation such as `Join()(d, d).as_table()` or a function pod's row
read over several inputs can skip identity formation and proceed. The same invocation
in a job raises `RepeatedOperandError`. The developer accepted this discrepancy as
part of the stopgap. ITL-1511 will remove it.
- `diagram()` draws one line for the two inputs of `MergeJoin()(a, a)`. ITL-1530 tracks
drawing both lines.
- An operator a loaded file keeps read-only (its config cannot be rebuilt) has its inputs
positioned by argument index, also when it is commutative. Such a node never runs.
Saved positions are stage 2 of ITL-1472.
14 changes: 14 additions & 0 deletions docs/concepts/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ order, so `Join()(a, b)` and `Join()(b, a)` have the same columns in the same or
ordered operators such as `SemiJoin` and `MergeJoin` keep operand order; either way, address
columns by name rather than by position.

A commutative operator such as `Join` or `Rejoin`, or a function pod, raises
`RepeatedOperandError` when it forms its identity from an operand set containing the same
stream twice. Two separately built sources with the same source ID and schema count as one
identity. Use the ordered `MergeJoin()(d, d)` for a self-join in a job.

The developer approved this stopgap on 2026-11-27. A standalone row read such as
`Join()(d, d).as_table()` or a function pod's row read over several inputs can proceed when
it skips identity formation. The same invocation is refused in a job. ITL-1511 will allow
repeated operands with a unique/repeat toggle and remove this discrepancy.

**Known limit.** The refusal covers flat unordered declarations, including every
commutative operator Orcapod ships. Mixed or nested unordered groups are unchecked.
ITL-1548 tracks how operators declare argument symmetry alongside ITL-1511.

```python
from orcapod.sources import DictSource
from orcapod.operators import Join
Expand Down
9 changes: 9 additions & 0 deletions docs/concepts/pipeline-job.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,15 @@ Every job also carries its blueprint as `job.pipeline`.

## Seeing the graph

In memory, a job keeps one edge per input. Ordered operators use argument indices, so
`MergeJoin()(a, a)` runs with two edges, at positions 0 and 1. Commutative operators use
`None` for the position, even with one input. ITL-1511 will give identical edges occurrence
numbers when repeated operands are allowed, so each edge can be identified individually.
Saved pipelines reconstruct positions from the saved input lists.

`diagram()` currently draws one line for the two inputs of `MergeJoin()(a, a)`. ITL-1530
tracks drawing both lines.

!!! caution "In development"
`Pipeline.diagram()`, `PipelineJob.diagram()`, `PipelineDiagram` and its methods and
arguments, the look of the picture and the notebook display may change in any release
Expand Down
113 changes: 113 additions & 0 deletions docs/dev/plans/itl-1419-edge-positions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# ITL-1419: graph edges carry their input position (stage 1 of ITL-1472)

The design is `docs/dev/specs/itl-1472-edge-registration.md` (draft PR 580) and its plan,
`docs/dev/plans/itl-1472-plan.md`, "Stage 1". This is that stage only.

Developer decision (2026-11-26): `MergeJoin(A, A)` is well defined, because its two input
positions are two edges, and it must run in a job. Refusing it (ITL-1419's other option) is
not taken.

## The bug on `main`

A pipeline's graph is a set of `(upstream key, node key)` pairs (`_hash_graph`, `_graph_edges`,
and the `OrcaDAG` of node objects the orchestrators walk). One upstream given to two inputs of
an operator is one pair, so `MergeJoin()(a, a)` and `SemiJoin()(a, a)` reach the orchestrator
with one predecessor: the sync orchestrator hands the operator one stream ("BinaryOperator
requires exactly two input streams"), the async one one reader. Pinned by
`tests/test_pipeline/test_loaded_argument_order.py::test_one_stream_given_twice_fails_to_run_in_a_job`.

## One stream given twice to a commutative pod: refused for now (a stopgap until ITL-1511)

The refusal below is temporary, not a principle. A commutative operator taking one stream
more than once is meaningful, and ITL-1511 ("Allow a commutative operator to take one stream
more than once: multiset identity and the unique/repeat toggle") is to allow it; it must be
settled before 0.4.0.

Independent review of the first head found that a commutative operator hashes its operands
as a set, so `Join()(d)` and `Join()(d, d)` (where `d` has no data column besides its
data-tags, so no overlap refuses it) shared one content hash and cache path but gave
different rows (5 against 3 for `id = [1, 1, 2]`), and which one a job kept followed the
order the steps were recorded in. The round-2 review found that a check by object identity
missed two separate sources of one identity (standalone hashes still collided, and a step
recorded over them compiled in one order and failed in the other), and that the check
added its own error for an operand that cannot be hashed.

On 2026-11-27 the developer approved refusing repeated operands as a stopgap until
ITL-1511. `core/operators/repeated_operands.read_argument_symmetry` reads the operator's
grouping and raises `RepeatedOperandError` (an `InputValidationError`) if a flat
unordered group has collapsed repeated operands. The error names the operator whose own
operands repeat. A downstream pod or the function pod whose join received the operands
can add context with `add_context`. Hashing, recording and compiling form the identity.
Set formation completes before checking for collapsed operands, so the existing hashing
error for an unhashable operand wins in either argument order. Standalone row reads such as
`Join()(d, d).as_table()` or a function pod's row read over several inputs can proceed
when they skip identity formation. A job refuses the same invocation. The developer
accepted this discrepancy as part of the stopgap, and ITL-1511 will remove it.

## Change

1. **One rule for positions** (new `src/orcapod/pipeline/edges.py`): `input_positions(pod,
inputs)` gives each input of an operator its argument index for ordered groups
(`MergeJoin`, `SemiJoin`: 0 and 1), including nested ordered groups. A flat
`frozenset` of the inputs gives each position `None` (`Join`, `Rejoin`, even with
one input). This is the same distinction used by the legacy loader. Changes to how
operators declare symmetry are tracked separately in ITL-1548. A function or side-effect pod's
one input is at 0. Positions exist in memory. When ITL-1511 allows repeats, each
identical edge will get an occurrence number so it can be identified individually. Used by
`_record_invocation`, `compile`, the `PipelineJob` rebuild of a blueprint and the loader.
2. **Hash-level edges are triples.** `_graph_edges` becomes a sorted list of
`(upstream key, node key, position)`. `_hash_graph` (networkx, used for topological order and
ancestors) stays a graph of keys. Every consumer of `_graph_edges` in `job.py` and `graph.py`
reads the triple.
3. **No saved-format change.** `save()` writes the distinct `(upstream, node)` pairs, sorted, as
today (byte-equal files). The loader builds the triples from each operator's saved `inputs`
list (ITL-1417: argument order), position 0 for a function or side-effect node; a legacy
file without `inputs` keeps today's refusal/commutative rule.
4. **One function gives a node its inputs** (`node_input_edges(node, graph)` in `edges.py`):
the node's input edges in position order, one per position, each naming the upstream node
of the graph. It reads the node's own `upstreams` (built from the invocation's inputs in
argument order, the same order as the triples) and checks each is a predecessor in the
graph, by identity. It replaces `_single_input` / `_ordered_inputs` (sync) and the
predecessor sorting (async).
5. **Async: one reader per edge.** Channels are wired per positioned edge, so an upstream feeding
two positions of one node gets two readers of its broadcast channel. A held node drains
every one of its edge readers. A source node still executes once per run; its observer
events are unchanged.
6. **Held rows** (`node.run()`, the state rule) feed every position alike: the sync orchestrator
reads `outputs[upstream]` per edge, the async one sends the held rows to every edge reader.
7. **Diagram.** `diagram()` draws one line for both inputs of `MergeJoin(A, A)` because
the diagram keys its edges by node pair and role (`(start, end, constrains)`).
ITL-1530 tracks routing parallel edges in the layout.
`SemiJoin(A, A)` already draws two lines (plain and constraining, ITL-1455).

Not in this stage: any public name (`job.edges`), any restriction, and
nothing in the id code of `core/nodes/function_node.py` / `operator_node.py` (ITL-1466).

## Hashes

No content hash, pipeline hash, stored path, stored column or golden value moves: node keys
are unchanged (invocation content hashes), and positions enter no hash.
`generate_golden_hashes.py --check` must report no drift.

## Tests (first, shown failing on `main`)

New file `tests/test_pipeline/test_edge_positions.py`, inputs with two tag and two data columns
(`id`, `key`; `x`, `y`), in two layouts (tag order and column order swapped):

- `MergeJoin(A, A)` and `SemiJoin(A, A)` run, sync and async, in a `Pipeline` bound to a job and
in a `PipelineJob`; after `save` and `PipelineJob.load` (`load_only`); and through
`node.run()` (with a held upstream). Rows equal the standalone operator's.
- `MergeJoin(A, B)` and `MergeJoin(B, A)`, and a chain where one upstream is at position 0 and
another at 1 and the reverse, give the standalone rows.
- A source node's observer events are the same for `MergeJoin(A, A)` as for `MergeJoin(A, B)`
per source (one start and end per run).
- Exercise `RepeatedOperandError` when hashing and recording `Join`, `Rejoin` and function
pods with repeated operands (ITL-1511), including both recording orders and sync and
async jobs.
- The saved file of a job with `MergeJoin(A, A)` has one edge pair and `inputs` `[A, A]`;
saving a loaded job writes the same bytes.
- `test_one_stream_given_twice_fails_to_run_in_a_job` becomes the test that it runs.

Also run: the golden suite, `test_order_invariance.py`, `test_graph_walk_cost.py`, the
sync/async equivalence tests, `test_sync_orchestrator.py`, `test_rows_on_read.py`,
`tests/test_pipeline/`.
2 changes: 1 addition & 1 deletion docs/reference/hashing.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ A record id is the row identifier (rule 8), built from row positions or key valu
|---|---|
| observer events (`node_hash`) | `core/nodes/function_node.py:FunctionJobNode.execute`, `core/nodes/function_node.py:FunctionJobNode.async_execute`, `core/nodes/function_node.py:FunctionJobNode._execute_load_only`, `core/nodes/function_node.py:FunctionJobNode._async_execute_load_only`, `core/nodes/function_node.py:FunctionJobNode._async_execute_cache_only`, `core/nodes/operator_node.py:OperatorJobNode.execute`, `core/nodes/operator_node.py:OperatorJobNode.async_execute`, `core/nodes/source_node.py:SourceNodeBase.execute`, `core/nodes/source_node.py:SourceNodeBase.async_execute` |
| hooks (`pod_hash`) | `core/function_pod.py:_FunctionPodBase._build_post_run_payload` |
| in-memory graph keys and sort keys | `pipeline/base.py:AbstractPipelineBase.compile`, `pipeline/base.py:AbstractPipelineBase.compile.<locals>.<lambda>`, `pipeline/base.py:AbstractPipelineBase._record_invocation`, `pipeline/base.py:AbstractPipelineBase.to_invocations`, `pipeline/base.py:AbstractPipelineBase.from_invocations`, `pipeline/job.py:PipelineJob.compile`, `pipeline/restrictions.py:sources_read` (the source identity a restriction is kept under, ITL-1218) |
| in-memory graph keys and sort keys | `pipeline/base.py:AbstractPipelineBase.compile`, `pipeline/base.py:AbstractPipelineBase.compile.<locals>.<lambda>`, `pipeline/base.py:AbstractPipelineBase._record_invocation`, `pipeline/base.py:AbstractPipelineBase.to_invocations`, `pipeline/base.py:AbstractPipelineBase.from_invocations`, `pipeline/base.py:_invocation_edges` (the upstream keys of positioned edges, ITL-1419), `pipeline/job.py:PipelineJob.compile`, `pipeline/restrictions.py:sources_read` (the source identity a restriction is kept under, ITL-1218) |
| in-memory lookup keys for a validated operator input or input pair (ITL-1358) | `core/operators/index.py:Index.validate_unary_input`, `core/operators/index.py:Index.async_execute`, `core/operators/pick.py:Pick.validate_unary_input`, `core/operators/pick.py:Pick.async_execute`, `core/operators/semijoin.py:SemiJoin.validate_binary_inputs`, `core/operators/semijoin.py:SemiJoin.async_execute` |
| in-memory topological sort of compiled and restored nodes, using the pipeline hash to order ready invocations | `pipeline/base.py:AbstractPipelineBase._ordered_node_map.<locals>.order_key` |
| canonical input order (sort by digest). Inputs with equal pipeline hashes (developer decision, November 16, 2026, ITL-1154: kept as it is): system-tag column names follow the stable pipeline-hash sort; each row's values are sorted by (source id, record id) by `sort_system_tag_values`, so the result does not depend on argument order; the operator's identity holds its inputs as a frozenset | `core/operators/join.py:Join.order_input_streams.<locals>.<lambda>` |
Expand Down
4 changes: 2 additions & 2 deletions scripts/hash_audit_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ def grade_simple(math: int) -> float:

sub("Site 13 — snapshot_hash (deterministic from leaf content hashes)")
# Reproduce PipelineJob.run() algorithm from the compiled node map.
# Edge (u_hash, v_hash): u is upstream → v is downstream.
# Edge (u_hash, v_hash, position): u is upstream → v is downstream.
# Leaf = node whose hash does NOT appear as the upstream (u) in any edge.
upstream_set = {u_hash for u_hash, _ in job._graph_edges}
upstream_set = {u_hash for u_hash, _, _ in job._graph_edges}
# Each leaf hash enters with its label, as PipelineJob.run() feeds it (ITL-1154).
leaf_hashes = sorted(
n.content_hash().to_string()
Expand Down
35 changes: 33 additions & 2 deletions src/orcapod/core/function_pod.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,8 +644,21 @@ def handle_input_streams(self, *streams: StreamProtocol) -> StreamProtocol:
raise ValueError("At least one input stream is required")
elif len(streams) > 1:
# TODO: simplify the multi-stream handling logic
from orcapod.core.operators.repeated_operands import (
RepeatedOperandError,
)

multi_stream_handler = self.multi_stream_handler()
joined_stream = multi_stream_handler.process(*streams)
try:
joined_stream = multi_stream_handler.process(*streams)
except RepeatedOperandError as err:
# Recording the join forms its identity: the join is named, this
# function added as context (ITL-1419). Nothing is formed here that
# would not be formed anyway.
err.add_context(
f"the inputs of the function pod of {self.canonical_function_name!r}"
)
raise
return joined_stream
return streams[0]

Expand Down Expand Up @@ -859,7 +872,25 @@ def __call__(
return self.process(input_stream, label=label)

def argument_symmetry(self, streams: Collection[StreamProtocol]) -> ArgumentGroup:
return self.multi_stream_handler().argument_symmetry(streams)
"""The symmetry of the join the pod puts its inputs through.

Raises:
RepeatedOperandError: If two inputs have one identity (ITL-1419; not
supported yet, a stopgap until ITL-1511): the join that formed the set
is named, and this function added as context.
"""
from orcapod.core.operators.repeated_operands import (
RepeatedOperandError,
read_argument_symmetry,
)

try:
return read_argument_symmetry(self.multi_stream_handler(), streams)
except RepeatedOperandError as err:
err.add_context(
f"the inputs of the function pod of {self.canonical_function_name!r}"
)
raise

def output_schema(
self,
Expand Down
7 changes: 6 additions & 1 deletion src/orcapod/core/nodes/operator_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,12 @@ def identity_structure(self) -> Any:
return self.pipeline_identity_structure()

def pipeline_identity_structure(self) -> Any:
return (self._operator, self._operator.argument_symmetry(self._input_streams))
from orcapod.core.operators.repeated_operands import read_argument_symmetry

return (
self._operator,
read_argument_symmetry(self._operator, self._input_streams),
)

# ------------------------------------------------------------------
# Read-only overrides (for deserialized nodes without live operator)
Expand Down
5 changes: 1 addition & 4 deletions src/orcapod/core/operators/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,8 @@ def validate_inputs(self, *streams: StreamProtocol) -> None:

def argument_symmetry(self, streams: Collection[StreamProtocol]) -> ArgumentGroup:
if self.is_commutative():
# return as symmetric group
return frozenset(streams)
else:
# return as ordered group
return tuple(streams)
return tuple(streams)

async def async_execute(
self,
Expand Down
7 changes: 7 additions & 0 deletions src/orcapod/core/operators/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,15 @@ def kernel_id(self) -> tuple[str, ...]:

def validate_nonzero_inputs(self, *streams: StreamProtocol) -> None:
"""Validate that input streams are compatible for joining."""
from orcapod.core.operators.repeated_operands import RepeatedOperandError

# TODO: add more helpful validation
try:
self.output_schema(*streams)
except RepeatedOperandError:
# An input's own refusal (one stream given twice upstream, ITL-1419) passes
# through as it is: its type and the pod that got the repeat.
raise
except Exception as e:
raise InputValidationError(f"Input streams are not compatible: {e}") from e

Expand All @@ -215,6 +221,7 @@ def order_input_streams(self, *streams: StreamProtocol) -> list[StreamProtocol]:
return sorted(streams, key=lambda s: s.pipeline_hash().to_hex())

def argument_symmetry(self, streams: Collection) -> ArgumentGroup:
"""The inputs as a set because ``Join`` is commutative."""
return frozenset(streams)

def output_schema(
Expand Down
Loading
Loading