From ecc0b9f75d1f723113514045ce985ff07ed3bcd0 Mon Sep 17 00:00:00 2001 From: "Edgar Y. Walker" <> Date: Fri, 20 Nov 2026 17:27:44 -0800 Subject: [PATCH 1/5] feat(sources)!: hash_in_key_order; ListSource takes the root-source identity (ITL-1272) hash_in_key_order (keyword-only, default False) on ArrowTableSource, DictSource, DataFrameSource, ListSource and SourceStreamBuilder.build: a source with key columns and no source_id hashes its table, for its default source id, with the rows sorted by the key columns in key order (arrow_utils.key_row_order: pyarrow sort_indices, then take), after the keys are validated. The same rows in any order then give one default source id and one content hash; the rows are presented as given. Refused without key columns and with a source_id. Without it nothing changes. ListSource drops its own identity_structure and takes RootSource's (class name, output schema, source_id, key columns): its elements and tag function enter its identity only through the default source id. tag_function_hash_mode and the tag-function hash are removed. Golden: hash_in_key_order moves nothing. The ListSource identity moves three pinned ListSource content hashes (developer approval, 2026-11-21); they are regenerated in a later commit, after PR 462. Co-Authored-By: Claude Opus 5.5 --- .zed/rules | 12 +- CLAUDE.md | 12 +- changelog.d/ITL-1272.md | 22 + docs/concepts/sources.md | 47 +- docs/dev/design_issues/ITL-1272-S3.md | 28 + docs/reference/hashing.md | 32 +- .../core/sources/arrow_table_source.py | 12 +- src/orcapod/core/sources/data_frame_source.py | 9 + src/orcapod/core/sources/dict_source.py | 9 + src/orcapod/core/sources/list_source.py | 85 +-- src/orcapod/core/sources/stream_builder.py | 72 ++- src/orcapod/utils/arrow_utils.py | 42 ++ .../sources/test_keyed_default_source_id.py | 534 ++++++++++++++++++ .../test_source_builder_integration.py | 9 +- .../sources/test_sources_comprehensive.py | 107 +--- tests/test_hashing/golden/cases.py | 5 +- tests/test_hashing/hash_site_trace.py | 14 - tests/test_hashing/test_hash_label_rule.py | 26 - 18 files changed, 856 insertions(+), 221 deletions(-) create mode 100644 changelog.d/ITL-1272.md create mode 100644 docs/dev/design_issues/ITL-1272-S3.md create mode 100644 tests/test_core/sources/test_keyed_default_source_id.py diff --git a/.zed/rules b/.zed/rules index 5571ece3d..8baff00b1 100644 --- a/.zed/rules +++ b/.zed/rules @@ -852,7 +852,11 @@ Every pipeline element has two parallel hashes: sources that differ only in how they name their rows are two sources, with one pipeline hash and one source id (developer, November 16, 2026, ITL-1154). Row content enters only where a source puts it into its own identity: the default source_id of the in-memory sources - (:: of the table hash, used when none is given) and the elements of a ListSource. A downstream stream or node hashes its + (:: of the table hash, used when none is given; the rows + in the order given, or sorted by the key columns with hash_in_key_order=True, ITL-1272). A + ListSource has no rule of its own (developer, November 21, 2026, ITL-1272): its elements + and tag function enter only through that default, and with a given source_id it is an + address like every other source (the review of every source's identity is ITL-1311). A downstream stream or node hashes its pod's identity plus its upstreams' content hashes; a function pod's identity is its uri, not its body. The uri holds the hash of the output schema, a set of named, typed outputs in no order (developer, November 17, 2026, ITL-1244): reordering output_keys changes it @@ -1180,7 +1184,11 @@ data. The rules: delta_table, sqlite_table, postgresql_table, db_table, spiraldb_table, polling_source, cached), or names the operation that derives the source (tags_as_data). A default built from a hash is ::, built - only by core/sources/source_ids.hash_source_id; one built from where the data lives is + only by core/sources/source_ids.hash_source_id (an in-memory source hashes its table as + given; hash_in_key_order=True, refused without key columns or with a source_id, hashes + it sorted by the key columns in key order, arrow_utils.key_row_order, an Arrow sort after + the keys are validated, rows presented as given; developer, November 21, 2026, ITL-1272); + one built from where the data lives is :, built only by location_source_id: csv:, delta_table:, sqlite_table::main., postgresql_table::/..
(the server the connection diff --git a/CLAUDE.md b/CLAUDE.md index e6171fd63..00e6cf744 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -872,7 +872,11 @@ Every pipeline element has two parallel hashes: sources that differ only in how they name their rows are two sources, with one pipeline hash and one source id (developer, November 16, 2026, ITL-1154). Row content enters only where a source puts it into its own identity: the default `source_id` of the in-memory sources - (`::` of the table hash, used when none is given) and the elements of a `ListSource`. A downstream stream or node hashes its + (`::` of the table hash, used when none is given; the rows + in the order given, or sorted by the key columns with `hash_in_key_order=True`, ITL-1272). A + `ListSource` has no rule of its own (developer, November 21, 2026, ITL-1272): its elements + and tag function enter only through that default, and with a given `source_id` it is an + address like every other source (the review of every source's identity is ITL-1311). A downstream stream or node hashes its pod's identity plus its upstreams' content hashes; a function pod's identity is its uri, not its body. The uri holds the hash of the output schema, a set of named, typed outputs in no order (developer, November 17, 2026, ITL-1244): reordering `output_keys` changes it @@ -1224,7 +1228,11 @@ data. The rules: `delta_table`, `sqlite_table`, `postgresql_table`, `db_table`, `spiraldb_table`, `polling_source`, `cached`), or names the operation that derives the source (`tags_as_data`). A default built from a hash is `::`, built - only by `core/sources/source_ids.hash_source_id`; one built from where the data lives is + only by `core/sources/source_ids.hash_source_id` (an in-memory source hashes its table as + given; `hash_in_key_order=True`, refused without key columns or with a `source_id`, hashes + it sorted by the key columns in key order, `arrow_utils.key_row_order`, an Arrow sort after + the keys are validated, rows presented as given; developer, November 21, 2026, ITL-1272); + one built from where the data lives is `:`, built only by `location_source_id`: `csv:`, `delta_table:`, `sqlite_table::main.
`, `postgresql_table::/..
` (the server the connection diff --git a/changelog.d/ITL-1272.md b/changelog.d/ITL-1272.md new file mode 100644 index 000000000..a463fde16 --- /dev/null +++ b/changelog.d/ITL-1272.md @@ -0,0 +1,22 @@ +### Breaking changes and migration + +- `ListSource` has the identity of every other root source: class name, output schema, + `source_id` and key columns (ITL-1272). Its elements and its tag function enter its + content hash only through its default `source_id` (the hash of its table), so a + `ListSource` given a `source_id` is an address like any other source: changing or + reordering its elements no longer changes its content hash, and two `ListSource`s with + the same elements but different `source_id`s no longer share one. Every `ListSource`'s + content hash changed, and with it the identity of every node over one. A review of + every built-in source's identity is ITL-1311. +- `ListSource` no longer takes `tag_function_hash_mode`: the tag function's identity has + no role left. Remove the argument. It was never written to a saved job. + +### Added + +- `hash_in_key_order` (keyword-only, default `False`) on `ArrowTableSource`, + `DictSource`, `DataFrameSource` and `ListSource` (ITL-1272): a source with key columns + (`record_id_columns`) and no `source_id` hashes its table, for its default source id, + with the rows sorted by the key columns in the order given, so the same rows in any + order give one default source id and one content hash. The rows are presented in the + order given. Refused without key columns and with a `source_id`. Without it, nothing + changes: the table is hashed as given. diff --git a/docs/concepts/sources.md b/docs/concepts/sources.md index 0e614235f..613b591c2 100644 --- a/docs/concepts/sources.md +++ b/docs/concepts/sources.md @@ -812,6 +812,51 @@ order or content can vary between runs, pass an explicit, stable `source_id` **a key columns that are unique per row; this avoids both the duplicate entries and the repeated deliveries. +An in-memory source with key columns and no `source_id` can instead hash its rows in key +order: pass `hash_in_key_order=True` (`ArrowTableSource`, `DictSource`, `DataFrameSource`, +`ListSource`; ITL-1272). The table is hashed sorted by the key columns, in the order you list +them, so the same rows in any order give one default `source_id` and one content hash; the +rows are still presented in the order you gave. It is off by default, because the order you +gave may matter to you. It is refused without key columns (there is no key order) and with a +`source_id` (nothing is hashed then). + +```python +from orcapod.sources import DictSource + +rows = [ + {"subject": "s1", "trial": 1, "weight": 20.5, "count": 3}, + {"subject": "s1", "trial": 2, "weight": 22.25, "count": 1}, + {"subject": "s2", "trial": 1, "weight": 19.0, "count": 4}, +] +orders = (rows, rows[::-1], [rows[1], rows[2], rows[0]]) +keyed = [ + DictSource(order, tag_columns=["subject", "trial"], + record_id_columns=["subject", "trial"], hash_in_key_order=True) + for order in orders +] +print(len({s.source_id for s in keyed}), len({s.content_hash() for s in keyed})) +# 1 1 +print(keyed[1].as_table().column("weight").to_pylist()) +# [19.0, 22.25, 20.5] +other_key_order = DictSource(rows, tag_columns=["subject", "trial"], + record_id_columns=["trial", "subject"], + hash_in_key_order=True) +print(other_key_order.source_id == keyed[0].source_id) +# False +as_given = [ + DictSource(order, tag_columns=["subject", "trial"], + record_id_columns=["subject", "trial"]) + for order in orders +] +print(len({s.source_id for s in as_given})) +# 3 +``` + +A source given a `source_id` is an address: its content hash is built from its class, schema, +`source_id` and key columns, never from its rows. This holds for `ListSource` too (developer, +November 21, 2026, ITL-1272): its elements and its tag function enter its identity only through +its default `source_id`. + These columns are hidden by default. You can reveal them using `ColumnConfig`: ```python @@ -842,7 +887,7 @@ print(table.column_names) ``` Pass `source_id` to give the source a readable name in these provenance tokens. Without it, -Orcapod derives an id from the source's content hash. +Orcapod derives an id from a hash of the source's table, as described above. ## Code example diff --git a/docs/dev/design_issues/ITL-1272-S3.md b/docs/dev/design_issues/ITL-1272-S3.md new file mode 100644 index 000000000..0f8f0bfc3 --- /dev/null +++ b/docs/dev/design_issues/ITL-1272-S3.md @@ -0,0 +1,28 @@ +## `src/orcapod/core/sources/` + +### S3 — The default source id of a keyed in-memory source followed the order of its rows, and `ListSource` had an identity of its own +**Status:** resolved +**Severity:** medium +**Issue:** ITL-1272 (related: ITL-1227, ITL-1311) + +An in-memory source given no `source_id` takes a default built from the Arrow hash of its +whole table (`SourceStreamBuilder.build`), and that hash includes the order of the rows. A +source with key columns (`record_id_columns`) names its rows by their keys and its reload +compares them without order, yet the same rows in another order gave another default source +id and content hash, with no way to ask otherwise. Separately, `ListSource` had an identity +of its own (class, name, its elements in order, a hash of its tag function chosen by +`tag_function_hash_mode`, key columns): unlike every other source its content hash followed +its elements under a given `source_id` and did not depend on that `source_id` at all (two +`ListSource`s with the same elements and different names shared a content hash). + +**Fix:** developer decisions, November 21, 2026 (replacing an earlier "always sort" +decision of November 19). `hash_in_key_order=True`, a keyword of the four in-memory sources +and of `SourceStreamBuilder.build`, hashes the table sorted by the key columns in key order +(`arrow_utils.key_row_order`, an Arrow `sort_indices` then `take`), after the keys are +validated; it is refused without key columns and with a `source_id`, and the rows are +presented as given. The default stays the table as given, since the order may be deliberate. +`ListSource` takes `RootSource`'s identity (class name, output schema, `source_id`, key +columns); `tag_function_hash_mode` and the tag-function hash are removed. Three golden +values moved, each a `ListSource`'s own content hash (developer approval, November 21, 2026). +Tests: `tests/test_core/sources/test_keyed_default_source_id.py`. A review of every +built-in source's identity is ITL-1311. diff --git a/docs/reference/hashing.md b/docs/reference/hashing.md index 6db505a7d..e6491b1a3 100644 --- a/docs/reference/hashing.md +++ b/docs/reference/hashing.md @@ -183,6 +183,23 @@ category, but we should make it totally overridable"; November 16, 2026, ITL-115 | `CachedSource` | `cached` | the wrapped source's id | | folded source id of `Batch` / `GroupBy` (a system-tag value, not a source) | | `batch_v1:` / `group_v1:` | +An in-memory default hashes the table as given, rows in the order they came in: the user may +have put them in that order on purpose. `hash_in_key_order=True` (a keyword of +`ArrowTableSource`, `DictSource`, `DataFrameSource` and `ListSource`; developer, November +21, 2026, ITL-1272: "we actually should take it explicitly as a parameter rather than always +hashing the sorted version of the data") hashes it with the rows sorted by the key columns +instead (`arrow_utils.key_row_order`): pyarrow's `sort_indices` over the key columns in the +order `record_id_columns` gives them, each ascending, nulls last, the rows then taken with +`take`. The same rows in any order then give one default source id and one content hash, +while the order of the key columns still counts. No tie-break is needed: the source refuses a +null and a repeated key before anything is hashed (`stream_builder.row_identifiers`), and +every key type (string, integer, boolean, timestamp, date) sorts, so the order is total. It +is the sort of the canonical row order of an identity table (below) restricted to the key +columns, which needs neither the other columns nor a row-hash tie-break, since every key type +is `canonically_sortable`. The argument is refused without key columns (no key order) and +with a `source_id` (nothing is hashed); the source presents its rows in the order given +either way. A saved job keeps the resolved `source_id`, never the argument. + A location default names the data where it lives, so two sources of one kind share it only when they read the same data. The generic `DBTableSource` knows no database, so it has no default and refuses to be built without a `source_id`. A location whose parts cannot be @@ -244,8 +261,9 @@ primary key is refused unless `record_id_columns` names a key or asks for the po position names a row only within its batch (ITL-1158). Name a source (`source_id`) and a key (`record_id_columns`) when its results must stay -reusable as its data changes: a default in-memory source id is the hash of the whole table, and -a position moves when rows are inserted or reordered. +reusable as its data changes: a default in-memory source id is the hash of the whole table (in +key order with `hash_in_key_order=True`), and a position moves when rows are inserted or +reordered. ## Hash site inventory @@ -304,7 +322,6 @@ test session with `-p tests.test_hashing.hash_site_trace`. | run snapshot | sink nodes' content hashes | the snapshot | `"