From 8a4709771be0daf90591a5a46e26afb3a741392d Mon Sep 17 00:00:00 2001 From: "Edgar Y. Walker" Date: Tue, 24 Nov 2026 01:27:21 -0800 Subject: [PATCH] fix(databases): recognise a table's create commit with deltalake 1.7 (ITL-1443) Co-Authored-By: Claude Opus 5.5 --- changelog.d/ITL-1443.md | 6 +++ src/orcapod/databases/delta_lake_databases.py | 5 +- tests/test_databases/test_append_by_name.py | 2 +- .../test_delta_commit_failures.py | 4 +- .../test_delta_committed_snapshot.py | 50 ++++++++++++++++++- .../test_delta_concurrent_writers.py | 2 +- .../test_pipeline/test_job_batched_commits.py | 10 ++-- .../test_mixed_function_commits.py | 2 +- 8 files changed, 69 insertions(+), 12 deletions(-) create mode 100644 changelog.d/ITL-1443.md diff --git a/changelog.d/ITL-1443.md b/changelog.d/ITL-1443.md new file mode 100644 index 000000000..d1e468a1d --- /dev/null +++ b/changelog.d/ITL-1443.md @@ -0,0 +1,6 @@ +### Fixed + +- With deltalake 1.7, creating a Delta table no longer logs `Could not reload ... after + committing: 'generator' object is not subscriptable`, and the record ids of the create are + kept in memory, as with earlier versions. Results and stored rows were never affected. + (ITL-1443) diff --git a/src/orcapod/databases/delta_lake_databases.py b/src/orcapod/databases/delta_lake_databases.py index 80f06eb9d..c88b322d8 100644 --- a/src/orcapod/databases/delta_lake_databases.py +++ b/src/orcapod/databases/delta_lake_databases.py @@ -1664,10 +1664,11 @@ def _created_with(delta_table: deltalake.DeltaTable, token: str) -> bool: if delta_table.version() != 0: return False try: - latest = delta_table.history(1) + # A list before deltalake 1.7, a lazy iterator (which may raise when read) from it. + latest = next(iter(delta_table.history(1)), None) except Exception: # noqa: BLE001 # unknown: the ids are read again on first use return False - return bool(latest) and latest[0].get(_CREATE_TOKEN_KEY) == token + return latest is not None and latest.get(_CREATE_TOKEN_KEY) == token def _table_id(delta_table: deltalake.DeltaTable) -> str: diff --git a/tests/test_databases/test_append_by_name.py b/tests/test_databases/test_append_by_name.py index f139f767f..c3f05014c 100644 --- a/tests/test_databases/test_append_by_name.py +++ b/tests/test_databases/test_append_by_name.py @@ -797,7 +797,7 @@ def interleave(*args: Any) -> Any: db.add_records(PATH, _abc(2), flush=True, **kwargs) else: db.add_records(PATH, _abc(2), flush=True, **kwargs) - assert DeltaTable(tmp_path / "t").history()[0]["operation"] == "MERGE" + assert list(DeltaTable(tmp_path / "t").history())[0]["operation"] == "MERGE" table = DeltaTableDatabase(tmp_path).get_all_records(PATH, record_id_column="rid") assert table is not None expected = [1, 2, 3] if race else [1, 2] diff --git a/tests/test_databases/test_delta_commit_failures.py b/tests/test_databases/test_delta_commit_failures.py index 4cebc60ab..9d5f62c86 100644 --- a/tests/test_databases/test_delta_commit_failures.py +++ b/tests/test_databases/test_delta_commit_failures.py @@ -148,8 +148,10 @@ def test_a_concurrent_commit_is_not_permanent(tmp_path: Path) -> None: root = str(tmp_path / "t") deltalake.write_deltalake(root, pa.table({"id": [1]})) stale = deltalake.DeltaTable(root) + # The same id: from deltalake 1.7 a commit whose id range, by file statistics, is + # apart from the other commit's is no conflict. _merge(deltalake.DeltaTable(root), pa.table({"id": [2]})) - error = _raised(lambda: _merge(stale, pa.table({"id": [3]}))) + error = _raised(lambda: _merge(stale, pa.table({"id": [2]}))) assert isinstance(error, CommitFailedError) assert not dld._permanent_commit_failure(error) # pyright: ignore[reportPrivateUsage] diff --git a/tests/test_databases/test_delta_committed_snapshot.py b/tests/test_databases/test_delta_committed_snapshot.py index 5ba5a1556..cd3847f8f 100644 --- a/tests/test_databases/test_delta_committed_snapshot.py +++ b/tests/test_databases/test_delta_committed_snapshot.py @@ -156,7 +156,7 @@ def test_a_failed_snapshot_extension_is_rebuilt( def _operations(path: Path) -> list[str]: """Return the Delta history's operations, oldest first.""" - history = deltalake.DeltaTable(str(path)).history() + history = list(deltalake.DeltaTable(str(path)).history()) return [str(entry["operation"]) for entry in reversed(history)] @@ -196,3 +196,51 @@ def test_ids_another_writer_stored_are_not_appended_twice(tmp_path: Path) -> Non assert stored is not None assert sorted(cast(list[bytes], stored["rid"].to_pylist())) == [b"a", b"b", b"c"] assert _values(stored) == [1, 2, 3] + + +@pytest.mark.parametrize("shape", ["list", "iterator"]) +def test_a_create_is_recognised_whatever_shape_history_has( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + caplog: pytest.LogCaptureFixture, + shape: str, +) -> None: + """ITL-1443: ``DeltaTable.history`` gives a list before deltalake 1.7 and an iterator + from it. Either way the commit that creates a table is recognised by its token: its + ids are carried forward in memory, and no failed reload is logged.""" + real_history = deltalake.DeltaTable.history + + def history(table: deltalake.DeltaTable, limit: int | None = None) -> Any: + entries = list(real_history(table, limit)) + return entries if shape == "list" else iter(entries) + + monkeypatch.setattr(deltalake.DeltaTable, "history", history) + db = DeltaTableDatabase(tmp_path) + db.add_records(PATH, _rows(["a", "b"], [1, 2]), flush=True) + assert "Could not reload" not in caplog.text + assert db._snapshots["table"].ids == {b"a", b"b"} # pyright: ignore[reportPrivateUsage] + db.add_records(PATH, _rows(["b", "c"], [9, 3]), skip_duplicates=True, flush=True) + assert _values(DeltaTableDatabase(tmp_path).get_all_records(PATH)) == [1, 2, 3] + + +@pytest.mark.parametrize("failure", ["empty", "raises when read"]) +def test_a_create_whose_history_cannot_be_read_is_not_seeded( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + caplog: pytest.LogCaptureFixture, + failure: str, +) -> None: + """ITL-1443: with no readable create commit the ids are left to the next read; the + reload itself does not fail, also when a lazy history raises only when read.""" + + def history(table: deltalake.DeltaTable, limit: int | None = None) -> Any: + if failure == "raises when read": + raise OSError("commit file unreadable") + yield from () + + monkeypatch.setattr(deltalake.DeltaTable, "history", history) + db = DeltaTableDatabase(tmp_path) + db.add_records(PATH, _rows(["a", "b"], [1, 2]), flush=True) + assert "Could not reload" not in caplog.text + assert db._snapshots["table"].ids is None # pyright: ignore[reportPrivateUsage] + assert _values(db.get_all_records(PATH)) == [1, 2] diff --git a/tests/test_databases/test_delta_concurrent_writers.py b/tests/test_databases/test_delta_concurrent_writers.py index bae954080..cd8e5a4dc 100644 --- a/tests/test_databases/test_delta_concurrent_writers.py +++ b/tests/test_databases/test_delta_concurrent_writers.py @@ -69,7 +69,7 @@ def _stored(root: Path) -> list[tuple[bytes, int]]: def _operations(root: Path) -> list[str]: """The table's commit operations, oldest first.""" - history = deltalake.DeltaTable(str(root / "t")).history() + history = list(deltalake.DeltaTable(str(root / "t")).history()) return [str(entry["operation"]) for entry in reversed(history)] diff --git a/tests/test_pipeline/test_job_batched_commits.py b/tests/test_pipeline/test_job_batched_commits.py index c67f7612d..52a91b8c8 100644 --- a/tests/test_pipeline/test_job_batched_commits.py +++ b/tests/test_pipeline/test_job_batched_commits.py @@ -96,7 +96,7 @@ def table_dirs(root: Path) -> dict[str, Path]: def operations(table: Path) -> list[str]: """The table's commit operations, oldest first.""" - history = deltalake.DeltaTable(str(table)).history() + history = list(deltalake.DeltaTable(str(table)).history()) return [str(entry["operation"]) for entry in reversed(history)] @@ -577,8 +577,8 @@ def test_results_are_committed_before_their_records_just_below_batch_size( orchestrator=ORCHESTRATORS[orchestrator](), commit_every_rows=49 ) tables = table_dirs(tmp_path) - pipeline_history = deltalake.DeltaTable(str(tables["pipeline"])).history() - result_history = deltalake.DeltaTable(str(tables["result"])).history() + pipeline_history = list(deltalake.DeltaTable(str(tables["pipeline"])).history()) + result_history = list(deltalake.DeltaTable(str(tables["result"])).history()) # 120 rows at 49 per commit: 3 pipeline-record commits, none by the database alone. assert len(pipeline_history) == 3 for entry in pipeline_history: @@ -609,7 +609,7 @@ def test_results_are_committed_before_their_records_just_below_batch_size( def _assert_records_point_at_committed_results(root: Path) -> None: """Every committed pipeline-record version references only committed results.""" tables = table_dirs(root) - result_history = deltalake.DeltaTable(str(tables["result"])).history() + result_history = list(deltalake.DeltaTable(str(tables["result"])).history()) for entry in deltalake.DeltaTable(str(tables["pipeline"])).history(): records = deltalake.DeltaTable( str(tables["pipeline"]), version=entry["version"] @@ -669,7 +669,7 @@ def test_writes_pending_before_the_run_are_committed_first(tmp_path: Path) -> No leftover = pa.table({"v": pa.array([1], pa.int64())}) store.add_record(("leftover",), b"left-1", leftover) # pending, never flushed build_job(store, 30).run() - leftover_commit = deltalake.DeltaTable(str(tmp_path / "leftover")).history() + leftover_commit = list(deltalake.DeltaTable(str(tmp_path / "leftover")).history()) first_job_commit = min( entry["timestamp"] for table in table_dirs_except(tmp_path, "leftover") diff --git a/tests/test_pipeline/test_mixed_function_commits.py b/tests/test_pipeline/test_mixed_function_commits.py index eb90a338c..ba34f81a8 100644 --- a/tests/test_pipeline/test_mixed_function_commits.py +++ b/tests/test_pipeline/test_mixed_function_commits.py @@ -115,7 +115,7 @@ def delta_history(root: Path, kind: str) -> list[tuple[str, int]]: """ (path,) = root.rglob(f"{kind}/_delta_log") table = deltalake.DeltaTable(str(path.parent)) - history = list(reversed(table.history())) + history = list(reversed(list(table.history()))) commits: list[tuple[str, int]] = [] for item in history: version = int(item["version"])