:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-warehouse): stop a resumed repartition re-reading copied rows (#95747)
Tom Owers137e70564d94
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition.pyindex f82bfd0b4ecc..9d833163e202 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition.py@@ -28,6 +28,7 @@ from typing import TYPE_CHECKING, Anyimport pyarrow as paimport deltalake as deltalakeimport pyarrow.compute as pc+import pyarrow.dataset as padsimport deltalake.exceptionsfrom structlog.types import FilteringBoundLogger@@ -854,6 +855,54 @@ def select_coarsen_target(return None, "unsupported_mode"+def _rows_per_source_file(old_delta: deltalake.DeltaTable) -> dict[str, int]:+ """Row count per data file, keyed by file name, read from the Delta log (metadata only)."""+ actions = old_delta.get_add_actions(flatten=True)+ names = actions.schema.names+ if "path" not in names or "num_records" not in names:+ return {}+ paths = actions.column("path").to_pylist()+ counts = actions.column("num_records").to_pylist()+ return {path.rsplit("/", 1)[-1]: count or 0 for path, count in zip(paths, counts) if path}+++def _drop_copied_source_files(+ old_delta: deltalake.DeltaTable, dataset: pads.Dataset, skip_rows: int+) -> tuple[pads.Dataset, int]:+ """Trim the source files a resumed rewrite already copied, returning the rows left to skip.++ The scan hands batches over one file at a time in the order `get_fragments` lists them, so the+ `skip_rows` prefix temp already holds is exactly the leading whole files whose row counts sum+ under it, plus part of the file that straddles the boundary. Dropping those files costs one+ Delta-log read; discarding their rows batch by batch costs a full decode of every one of them, on+ the same activity budget as the rows the attempt still has to write. So on a table that needs+ several budgets the prefix grows until re-reading it fills a budget on its own, and an attempt that+ appends nothing is what the controller counts against its give-up cap.++ Only whole files are dropped, so the boundary file is still skipped row by row.+ """+ if not isinstance(dataset, pads.FileSystemDataset):+ return dataset, skip_rows++ per_file = _rows_per_source_file(old_delta)+ fragments = list(dataset.get_fragments())+ copied = 0+ boundary = 0+ for fragment in fragments:+ rows = per_file.get(fragment.path.rsplit("/", 1)[-1])+ if rows is None:+ rows = fragment.count_rows()+ if copied + rows > skip_rows:+ break+ copied += rows+ boundary += 1++ if boundary == 0:+ return dataset, skip_rows+ trimmed = pads.FileSystemDataset(fragments[boundary:], dataset.schema, dataset.format, dataset.filesystem)+ return trimmed, skip_rows - copied++def _read_next_batch(reader: pa.RecordBatchReader) -> pa.RecordBatch | None:try:return reader.read_next_batch()@@ -902,9 +951,11 @@ async def _rewrite_into_temp(`total_rows` is the source row count, used only to report progress as a percentage and an ETA.`skip_rows` resumes a prior attempt that ran out of budget: temp already holds a scan-ordered- prefix of `skip_rows` rows, so this call reads-and-discards that many source rows (the source is- immutable during the rewrite, so the scan order is stable) and appends only the remainder. The- rewrite writes in `append` mode, so resuming builds on the existing temp rather than replacing it.+ prefix of `skip_rows` rows, so this call skips that many source rows (the source is immutable+ during the rewrite, so the scan order is stable) and appends only the remainder. Whole source files+ inside the prefix are dropped from the scan on their recorded row counts, so only the file that+ straddles the boundary is read-and-discarded. The rewrite writes in `append` mode, so resuming+ builds on the existing temp rather than replacing it."""await logger.ainfo(f"repartition: rewrite starting target_scheme={_format_scheme(target)} total_rows={total_rows} "@@ -916,6 +967,12 @@ async def _rewrite_into_temp()dataset = await asyncio.to_thread(old_delta.to_pyarrow_dataset)+ if skip_rows:+ dataset, skip_rows = await asyncio.to_thread(_drop_copied_source_files, old_delta, dataset, skip_rows)+ await logger.ainfo(+ f"repartition: resume dropped the source files already copied, {skip_rows} rows left to skip",+ rows_to_skip=skip_rows,+ )reader = await asyncio.to_thread(lambda: dataset.scanner(batch_size=batch_size,diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition.pyindex 87a4462696ae..760de8eb592c 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition.py@@ -809,6 +809,67 @@ class TestRewriteIntoTemp:assert final.num_rows == len(rows)assert set(final.column("id").to_pylist()) == set(range(len(rows)))+ def test_resume_does_not_re_read_the_source_files_it_already_copied(self, tmp_path):+ # The prefix temp already holds is charged to the resumed attempt's own budget when it is+ # skipped row by row, so on a table that needs several budgets the re-read grows until it fills+ # a budget on its own and the attempt appends nothing — which is what the controller counts+ # against its give-up cap before abandoning the table. One row per month means one source file+ # per row, so a copied prefix is a whole number of files and the scan must never open them.+ rows = [(i, datetime.datetime(2024, 1, 1) + datetime.timedelta(days=40 * i)) for i in range(6)]+ live = _write_month_partitioned(str(tmp_path / "live"), rows)+ temp_uri = str(tmp_path / "tmp")+ target = RepartitionTarget(+ partition_keys=["created_at"], trigger_reason="t", partition_mode="datetime", partition_format="day"+ )++ clock = Mock(side_effect=itertools.chain([0.0] * 8, itertools.repeat(100.0)))+ with (+ patch.object(repartition_module, "time", Mock(monotonic=clock)),+ patch.object(repartition_module, "REWRITE_BUFFER_MAX_ROWS", 1),+ ):+ with pytest.raises(RepartitionBudgetExceededError):+ asyncio.run(+ _rewrite_into_temp(+ old_delta=live,+ temp_uri=temp_uri,+ storage_options={},+ target=target,+ batch_size=1,+ logger=logger,+ deadline=50.0,+ )+ )+ copied = deltalake.DeltaTable(temp_uri).to_pyarrow_table().num_rows+ assert 0 < copied < len(rows)++ reads = 0+ read_batch = repartition_module._read_next_batch++ def counting_read(reader):+ nonlocal reads+ reads += 1+ return read_batch(reader)++ with patch.object(repartition_module, "_read_next_batch", counting_read):+ rows_written, _ = asyncio.run(+ _rewrite_into_temp(+ old_delta=live,+ temp_uri=temp_uri,+ storage_options={},+ target=target,+ batch_size=1,+ logger=logger,+ skip_rows=copied,+ )+ )++ # One read per uncopied file, plus the read that exhausts the scan.+ assert reads == (len(rows) - copied) + 1+ assert rows_written == len(rows) - copied+ final = deltalake.DeltaTable(temp_uri).to_pyarrow_table()+ assert final.num_rows == len(rows)+ assert set(final.column("id").to_pylist()) == set(range(len(rows)))+def test_a_finished_rewrite_beats_the_deadline(self, tmp_path):# One source file, one batch, so the reader is exhausted on the second loop iteration. The# clock is over the deadline by then: a rewrite that has already copied every row must still