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
19 changes: 19 additions & 0 deletions .zed/rules
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,25 @@ operator. The shared family of such inputs is `tests/test_core/operators/several
the inventory is `docs/dev/specs/itl-1236-several-item-test-audit.md`. The docs show the
several-item form of such an argument, not only the one-item form.

**Order never counts, except where you give it** (developer, November 18, 2026, ITL-1271):
no operator result and no stream depends on the order of the tags or columns fed into it
(declared `tag_columns`, table layout, which tags are data-tags), on the order of the items
of an argument that names a set (names, mapping entries, keywords, predicates), or on the
order of the inputs of a commutative operator or a function pod. "If any operations COULD
vary output by tag order, then explicit ordering of tags to be used should be an explicit
input into the function/operator." Nothing that enters a hash, an identity, a stored path or
a stored value may follow such an order: hold a set of names sorted (or as a dict), and
place columns by an explicit argument or by name. The places where order is meant to count
are listed once, `ORDER_EXCEPTIONS` in `tests/test_core/order_rules.py`, each with its
reason, and rendered in `docs/concepts/identity.md` ("Order never counts, except where you
give it"); add one only with the developer's decision. Column placement following the
declared order is presentation only. `tests/test_core/test_order_invariance.py` checks every
exported operator, source and stream kind, the function pod forms and a job: a new operator
or stream kind fails there until it has a case; a case owned by an open issue is
`xfail(strict=True, raises=AssertionError)` naming it (`KNOWN_VIOLATIONS`, exactly the
issues the marks name), on the comparison only, removed when the issue lands. The
audit is `docs/dev/specs/itl-1271-order-invariance-audit.md`.

---

## Project layout
Expand Down
19 changes: 19 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,25 @@ operator. The shared family of such inputs is `tests/test_core/operators/several
the inventory is `docs/dev/specs/itl-1236-several-item-test-audit.md`. The docs show the
several-item form of such an argument, not only the one-item form.

**Order never counts, except where you give it** (developer, November 18, 2026, ITL-1271):
no operator result and no stream depends on the order of the tags or columns fed into it
(declared `tag_columns`, table layout, which tags are data-tags), on the order of the items
of an argument that names a set (names, mapping entries, keywords, predicates), or on the
order of the inputs of a commutative operator or a function pod. "If any operations COULD
vary output by tag order, then explicit ordering of tags to be used should be an explicit
input into the function/operator." Nothing that enters a hash, an identity, a stored path or
a stored value may follow such an order: hold a set of names sorted (or as a dict), and
place columns by an explicit argument or by name. The places where order is meant to count
are listed once, `ORDER_EXCEPTIONS` in `tests/test_core/order_rules.py`, each with its
reason, and rendered in `docs/concepts/identity.md` ("Order never counts, except where you
give it"); add one only with the developer's decision. Column placement following the
declared order is presentation only. `tests/test_core/test_order_invariance.py` checks every
exported operator, source and stream kind, the function pod forms and a job: a new operator
or stream kind fails there until it has a case; a case owned by an open issue is
`xfail(strict=True, raises=AssertionError)` naming it (`KNOWN_VIOLATIONS`, exactly the
issues the marks name), on the comparison only, removed when the issue lands. The
audit is `docs/dev/specs/itl-1271-order-invariance-audit.md`.

---

## Project layout
Expand Down
26 changes: 26 additions & 0 deletions changelog.d/ITL-1271.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
### Added

- The rule that no operator result and no stream depends on the order of the tags or
columns fed into it, nor on the order of the items of an argument that names a set, nor
on the order of a commutative operator's inputs, is stated in
the Identity & Hashing page ("Order never counts, except where you give
it") with the places where an order is meant to count (`record_id_columns`, `GroupBy`'s
`member_order`, a non-commutative operator's inputs, row order where it is the point).
One test checks it for every exported operator, source and stream kind, the function
pod forms and a job (ITL-1271).

### Changed

- `DerivedSource(origin, tags_as_data=...)` places the tag copies after the origin's data
columns in the order of their new names, not in the order the mapping was given
(ITL-1271). The mapping names a set: its order changes neither the columns, their order,
nor any hash. No hash or stored value changes.

### Fixed

- `PollingSource`'s identity holds its tag columns in name order (ITL-1271), so declaring
`tag_columns` in another order no longer gives the same source another content hash (and
every content hash downstream of it another value). The key columns
(`record_id_columns`) keep their order, the explicit order of a record id. A polling
source with two or more tags declared out of name order gets a new content hash; no
pinned golden value moves.
99 changes: 99 additions & 0 deletions docs/concepts/identity.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,105 @@ print(step.pod(source).pipeline_hash() == original)
When you change what a function computes, bump its major version (or rename it) so that
stale results are not reused.

## Order never counts, except where you give it

The rule: no hash, identity, stored value or row depends on the order in which you give
Orcapod a set of things. If an order matters, it is an explicit argument. The rule does not
hold everywhere yet: a few cases still break it, each owned by an open issue, and they are
listed under [What does not hold yet](#what-does-not-hold-yet) below. They are defects to be
fixed, not exceptions. The places where an order is meant to count are the table of
exceptions further down. By the rule, none of these changes a content hash, a pipeline
hash, a system-tag column name, a stored table path, a stored row, a record id or a row's
hashes:

- the order of a source's `tag_columns` (plain tags and data-tags alike) and the order of
the columns in its table;
- the order of the names, mapping entries or keywords of an argument that names several:
the columns of `select_columns`, `drop_columns`, `drop_nulls` and the other selections,
the entries of `map_tags`, `map_data` and `map_columns`, the constraints of
`polars_filter`, `tag_only_columns`, `tags_as_data`;
- the order of the inputs of an operator that declares itself commutative (`Join`,
`MergeJoin`), and of the inputs of a function pod;
- the path the rows take: the sync and the async run give the same rows.

What follows your order is where the columns are placed: `keys()`, `as_table()` and a
stored table place them after your declared `tag_columns` and your table's columns
(data-tags first among the data columns). That placement is presentation only.

```python
from orcapod.sources import DictSource

visits = [
{"subject": "m1", "session": 1, "score": 0.5},
{"subject": "m1", "session": 2, "score": None},
{"subject": "m2", "session": 1, "score": 0.75},
]
by_subject = DictSource(
data=visits, tag_columns=["subject", "session"], tag_only_columns=True, source_id="visits"
)
by_session = DictSource(
data=visits, tag_columns=["session", "subject"], tag_only_columns=True, source_id="visits"
)
print(by_subject.content_hash() == by_session.content_hash())
# True
print(by_subject.keys()[0], by_session.keys()[0])
# ('subject', 'session') ('session', 'subject')
kept = by_subject.drop_nulls(["score", "subject"])
same = by_session.drop_nulls(["subject", "score"])
print(kept.content_hash() == same.content_hash(), kept.as_table().num_rows)
# True 2
```

The order counts only where it is the point of the argument:

| Order that counts | Why |
|---|---|
| `record_id_columns` of every source (by default a database table's primary key) | A record id is the canonical JSON of the key columns in key order, so the key order is part of every record id and of the source's identity. It is the explicit argument that states the order. |
| `member_order` of `GroupBy` (ITL-1226; not in a release yet) | The explicit order of the members inside each group's lists, the argument that replaces the input's tag order there. |
| the inputs of an operator whose `argument_symmetry` is a tuple (`SemiJoin`) | The inputs have roles: `SemiJoin(left, right)` keeps the rows of `left`. |
| the order of the ROWS given to a source without key columns | Its record ids are the row positions, so a reorder is a change (by decision). Row order, never column order. |
| the order of the ROWS given to `Batch` | `Batch` groups consecutive rows: row order, never column order. |
| the fields of a user class (`@dataclass`, `orcapod.dataclass`, pydantic) | A class is identified with its fields in declared order: reordering them declares another type. |
| the declared `tag_columns` order and a table's data column order | Presentation only: they place the columns of `keys()`, `as_table()` and stored tables (data-tags first, ITL-1205), and enter no hash, identity or stored value. That this placement is consistent across layouts and paths is ITL-1249. |

A record id names a row by its key columns in key order, so the key order is part of the
source:

```python
keyed = DictSource(
data=visits,
tag_columns=["subject", "session"],
tag_only_columns=True,
source_id="visits",
record_id_columns=["subject", "session"],
)
rekeyed = DictSource(
data=visits,
tag_columns=["subject", "session"],
tag_only_columns=True,
source_id="visits",
record_id_columns=["session", "subject"],
)
print(keyed.content_hash() == rekeyed.content_hash())
# False
```

### What does not hold yet

`tests/test_core/test_order_invariance.py` checks the rule for every exported operator,
source and stream kind, the function pod forms and a job. Today these cases still break it
(the test marks each as an expected failure naming its issue); they are being fixed before
data context v1 is frozen:

- the order of `polars_filter`'s predicates is in its identity (ITL-1247, undecided);
- a function pod over data-tags: its content and pipeline hash and its node's stored paths
depend on the input's tag order and table layout (ITL-1246);
- a stream built directly from a table (`ArrowTableStream`): its content hash depends on
the order of its tags (ITL-1227);
- which rows `head()` shows, sorted by tags, follows the tag order (ITL-1270);
- where the columns are placed is not always the declared order: an operator places its
output tags in its input table's column order (ITL-1249, presentation only).

## Your own classes in the identity

A class enters both chains through its **identity**: its kind, its name and the names and
Expand Down
3 changes: 3 additions & 0 deletions docs/concepts/sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,9 @@ before. Things to know:
- The mapping is `{tag column: new data column}`. The new name must not already be a column
and must not start with `_`; a wrong name raises `InputValidationError` when the source is
built.
- The copies follow the origin's data columns, in the order of their new names. The mapping
names a set of copies: the order you give it in changes neither the columns, their order,
nor any hash.
- The copy has exactly the tag's type, including whether it may be null. If the tag may be
null, annotate the function parameter as optional (`datetime | None`).
- The copy equals the tag when the source is read. After that it is ordinary data: a function
Expand Down
Loading
Loading