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
6 changes: 6 additions & 0 deletions changelog.d/ITL-1443.md
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 3 additions & 2 deletions src/orcapod/databases/delta_lake_databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_databases/test_append_by_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 3 additions & 1 deletion tests/test_databases/test_delta_commit_failures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
50 changes: 49 additions & 1 deletion tests/test_databases/test_delta_committed_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]


Expand Down Expand Up @@ -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]
2 changes: 1 addition & 1 deletion tests/test_databases/test_delta_concurrent_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]


Expand Down
10 changes: 5 additions & 5 deletions tests/test_pipeline/test_job_batched_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pipeline/test_mixed_function_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
Loading