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
12 changes: 12 additions & 0 deletions changelog.d/ITL-1295.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### Fixed

- **A function node's stored results read back exactly as they were stored.** Every read
of stored results (`node.as_source()`, a second run of a job over the same database,
`run(load_only=True)`, a saved and loaded job) carried every column through Polars.
A user could see a `date64` tag read back at midnight (also inside a list or a
struct); a join of the reused node's results with another input finding no rows on the
second run although it found them on the first; a node added downstream storing the
changed value; duplicate pipeline entries recorded on every such run; and, with a
`decimal256` tag, the second run crashing the process. Now only the bookkeeping columns
pass through Polars, and every value is taken from the stored tables. No hash, entry id
or stored table changes. (ITL-1295)
34 changes: 34 additions & 0 deletions docs/dev/design_issues/ITL-1295-FN3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## `src/orcapod/core/nodes/function_node.py`

### FN3 — Reading a function node's stored results carried every column through Polars
**Status:** resolved
**Severity:** critical
**Issue:** ITL-1295 (related: ITL-1284, ITL-1249)

`FunctionJobNode._fetch_joined_records` joined the stored pipeline table and result
table as `pl.DataFrame` and converted the result back to Arrow. Every column went
through Polars, which changes or refuses some Arrow types: a `date64` tag off midnight
came back at midnight (also in a list or a struct), and a `decimal256` tag made Polars
panic, which no caller can catch. The stored tables were exact and a node's first run
showed exact values; every read of stored results did not: `node.as_source()`, a run of
a new job over the same database (results reused), `run(load_only=True)` and a saved and
loaded job. Downstream, a join of a reused node with a fresh source on the same tags found
nothing; a node added over a reused node stored the changed tag; and because a pipeline
entry id includes the tag values, every reuse run recorded new entries for the same
inputs under the changed tag (a downstream node's table grew from 2 to 4 rows). With a
`decimal256` tag, the reuse run itself crashed.

**Fix:** Polars only decides which stored rows belong together. It sees the bookkeeping
columns it filters and joins on (node content hash, ephemeral flag, record id, base
entry id, recomputation index) and a row number per table (`_row_frame`); every column
of the joined records is taken from the stored Arrow tables by those row numbers
(`_joined_from_stored`), in the layout Polars gave (a result column the pipeline table
also names takes `_right`), and the rows of a cross-session ephemeral miss are read from
the pipeline table. For every type Polars held exactly the result is what it was:
columns, order, names, types, nullability, rows (`docs/dev/scripts/itl_1295/dump_reads.py`
compares two checkouts). No hash, entry id, stored path or column moves. Tests:
`tests/test_pipeline/test_function_node_stored_values.py`. Remaining: the sort fallback of
`arrow_utils.sort_by_tag_columns` (list and struct tags, `as_table(sort_by_tags=True)`,
`head().sort_by_tags()`) still sorts through Polars; it needs the key preparation of
`core/operators/arrow_keys.py` (ITL-1284) and is the second part of ITL-1295 (strict
expected failures name it).
193 changes: 193 additions & 0 deletions docs/dev/scripts/itl_1295/dump_reads.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
"""ITL-1295: dump every read of stored function results, to compare two checkouts.

usage: uv run python docs/dev/scripts/itl_1295/dump_reads.py <output.pickle>
uv run python docs/dev/scripts/itl_1295/dump_reads.py --compare <a.pickle> <b.pickle>

Run it on ``main`` and on the fix (one process at a time), then compare: every table
must be equal with its schema metadata (types, nullability, field metadata, column order,
row order, values), and every ephemeral-miss row and loaded tag the same. Only types
Polars held exactly are used, so ``main`` is the reference.

Scenarios, each over an in-memory database (pipeline and results) and a separate
in-memory ephemeral database:

* ``persistent``: one job run, read back by a new job;
* ``mixed``: a first run stores two inputs persistently; a second run, with the
ephemeral database attached, adds two inputs whose results go to it; read back by a
new job with the same ephemeral database (both parts of the fetch);
* ``ephemeral miss``: the same, read back by a new job with a NEW, empty ephemeral
database (cross-session miss: ``EmptyData`` rows).

Reads: ``_fetch_joined_records()`` (table, ephemeral-miss rows), ``get_all_records()``
with every column, ``as_source().as_table()`` with every column, and the tags and data
of ``_load_cached_entries()`` as Arrow tables.
"""

from __future__ import annotations

import pickle
import sys
from pathlib import Path
from typing import Any, cast

import pyarrow as pa

from orcapod import PipelineJob, function_pod
from orcapod.contexts import get_default_context
from orcapod.core.sources import ArrowTableSource
from orcapod.databases import InMemoryArrowDatabase
from orcapod.pipeline import SyncPipelineOrchestrator
from orcapod.types import NodeConfig

CTX = get_default_context()


@function_pod(output_keys=("y", "w"))
def two_outputs(x: int) -> tuple[int, float]:
return x + 1, -0.0 if x % 2 else float(x)


def source(rows: list[int]) -> ArrowTableSource:
path_type = cast(
pa.BaseExtensionType, CTX.type_converter.python_type_to_arrow_type(Path)
)
arrays = {
"k": pa.array([r % 3 for r in rows], pa.int8()),
"s": pa.array([f"é{r}" for r in rows], pa.large_string()),
"l": pa.array([[r, None] for r in rows], pa.large_list(pa.int64())),
"st": pa.array(
[{"a": r, "b": str(r)} for r in rows],
pa.struct([("a", pa.int64()), ("b", pa.large_string())]),
),
"p": pa.ExtensionArray.from_storage(
path_type, pa.array([f"/d/{r}" for r in rows], pa.large_string())
),
"x": pa.array(rows, pa.int64()),
"rid": pa.array([f"r{r}" for r in rows], pa.large_string()),
}
schema = pa.schema(
pa.field(name, a.type, nullable=False) for name, a in arrays.items()
)
return ArrowTableSource(
pa.Table.from_arrays(list(arrays.values()), schema=schema),
tag_columns=["k", "s", "l", "st", "p"],
tag_only_columns=True,
source_id="s",
record_id_columns=["rid"],
)


def build(db: Any, rows: list[int], ephemeral: Any | None) -> PipelineJob:
job = PipelineJob(name="itl1295dump", database=db)
with job:
two_outputs.pod(source(rows), label="node")
if ephemeral is not None:
job.set_ephemeral_database(ephemeral)
job.apply_node_config(NodeConfig(is_result_ephemeral=True))
return job


# Values that differ between any two runs (a random record id, the source-info tokens
# naming it, a time stamp, the checkout's git state): compared by type, not value.
PER_RUN = ("__data_id", "__pod_ts", "__pf_var_git_hash", "_source_y", "_source_w")


def plain(table: pa.Table) -> tuple[Any, ...]:
"""A table as comparable plain values: each field (name, type, nullability,
metadata) and its values (a logical column as its storage), and the schema metadata."""
fields = []
for field, column in zip(table.schema, table.columns):
chunks = [
c.storage if isinstance(c, pa.ExtensionArray) else c for c in column.chunks
]
values = [repr(v) for chunk in chunks for v in chunk.to_pylist()]
if field.name in PER_RUN:
values = [f"<{len(values)} per-run values>"]
fields.append(
(field.name, str(field.type), field.nullable, field.metadata, values)
)
return (tuple(fields), table.schema.metadata)


def reads(job: PipelineJob) -> dict[str, Any]:
node: Any = job.nodes["node"]
fetched = node._fetch_joined_records() # noqa: SLF001
loaded = node._load_cached_entries() # noqa: SLF001

def attempt(read: Any) -> Any:
try:
return read()
except Exception as error: # noqa: BLE001 # the same error on both checkouts is the same read
return f"RAISES {type(error).__name__}: {error}"

tags = [
(
base,
attempt(lambda: plain(tag.as_table(columns={"system_tags": True}))),
attempt(lambda: plain(data.as_table(columns={"source": True})))
if hasattr(data, "as_table")
else repr(data),
)
for base, (tag, data) in sorted(loaded.items())
]
return {
"fetched": plain(fetched.table),
"fetched taginfo columns": fetched.taginfo_columns,
"ephemeral miss rows": {
k: repr(
sorted((c, "<per-run>" if c in PER_RUN else x) for c, x in v.items())
)
for k, v in fetched.empty_taginfo_rows.items()
},
"get_all_records": plain(node.get_all_records(all_info=True)),
"as_source": plain(node.as_source().as_table(all_info=True)),
"loaded": tags,
}


def dump(path: str) -> None:
out: dict[str, Any] = {}
sync = SyncPipelineOrchestrator
db, eph = InMemoryArrowDatabase(), InMemoryArrowDatabase()
build(db, [1, 2, 3], None).run(orchestrator=sync())
out["persistent"] = reads(build(db, [1, 2, 3], None))
build(db, [1, 2, 3, 4, 5], eph).run(orchestrator=sync())
out["mixed"] = reads(build(db, [1, 2, 3, 4, 5], eph))
out["ephemeral miss"] = reads(build(db, [1, 2, 3, 4, 5], InMemoryArrowDatabase()))
with open(path, "wb") as file:
pickle.dump(out, file)
for scenario, values in out.items():
flags = next(f[4] for f in values["fetched"][0] if f[0] == "__is_ephemeral")
print(
f"{scenario}: fetched rows={len(flags)} ephemeral={flags.count('True')} "
+ f"ephemeral misses={len(values['ephemeral miss rows'])} loaded={len(values['loaded'])} "
+ f"loaded reads raising={sum('RAISES' in repr(t) for t in values['loaded'])}"
)


def same(a: Any, b: Any) -> bool:
if isinstance(a, pa.Table):
return isinstance(b, pa.Table) and bool(a.equals(b, check_metadata=True))
if isinstance(a, (list, tuple)):
return len(a) == len(b) and all(same(x, y) for x, y in zip(a, b))
if isinstance(a, dict):
return a.keys() == b.keys() and all(same(a[k], b[k]) for k in a)
return a == b


def compare(first: str, second: str) -> None:
with open(first, "rb") as file:
a = pickle.load(file) # noqa: S301
with open(second, "rb") as file:
b = pickle.load(file) # noqa: S301
for scenario in a:
for key in a[scenario]:
verdict = "same" if same(a[scenario][key], b[scenario][key]) else "DIFFER"
print(f"{scenario:15} {key:25} {verdict}")


if __name__ == "__main__":
if sys.argv[1] == "--compare":
compare(sys.argv[2], sys.argv[3])
else:
dump(sys.argv[1])
52 changes: 52 additions & 0 deletions docs/dev/scripts/itl_1295/measure_loaded_job.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""ITL-1295: a saved and loaded job reads its function node's stored results.

usage: uv run python docs/dev/scripts/itl_1295/measure_loaded_job.py

Runs node ``one`` of ``measure_read_path.py`` over an in-memory database, saves the job,
loads it over the same database and reads ``one``'s stored view (``as_source``) and
``get_all_records()``; ``k`` is compared with the input exactly.
"""

from __future__ import annotations

import sys
import tempfile
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))
import measure_read_path as m # noqa: E402 # pyright: ignore[reportImplicitRelativeImport] # a sibling script

from orcapod import PipelineJob # noqa: E402
from orcapod.databases import InMemoryArrowDatabase # noqa: E402
from orcapod.pipeline import SyncPipelineOrchestrator # noqa: E402

for name in (
"int64 (control)",
"date64 off midnight",
"decimal256(76,40)",
"large_list<date64>",
):
values = m.CASES[name]
db = InMemoryArrowDatabase()
job = m.build(values, db)
job.run(orchestrator=SyncPipelineOrchestrator())
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "job.json"
job.save(path)
loaded = PipelineJob.load(path, database=db)
out = []
for read, action in (
(
"as_source",
lambda: loaded.nodes["one"].as_source().as_table().column("k"),
),
(
"get_all_records",
lambda: loaded.nodes["one"].get_all_records().column("k"),
),
):
try:
out.append(f"{read}: {m.verdict(m.exact(action()), m.exact(values))}")
except BaseException as error: # noqa: BLE001
out.append(f"{read}: {type(error).__name__}: {str(error)[:60]}")
print(f"{name:22} " + " | ".join(out))
Loading
Loading