: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(warehouse-sources): repoint existing table when sync reports zero rows (#92669)
posthog[bot]4fb5fb30ffaa
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_sync.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_sync.pyindex 2617d5ffd9c5..4532b6136d94 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_sync.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_sync.py@@ -203,10 +203,6 @@ async def validate_schema_and_update_table("""logger = LOGGER.bind(team_id=team_id)- if row_count == 0:- logger.warning("Skipping `validate_schema_and_update_table` due to `row_count` being 0")- return-@database_sync_to_async_pooldef _validate_and_update():job = ExternalDataJob.objects.prefetch_related(@@ -247,12 +243,25 @@ async def validate_schema_and_update_table(# minutes, surfacing as "idle in transaction" connections that stalled vacuum and# exhausted the connection pool.table_created: DataWarehouseTable | None = external_data_schema.table++ # A reported row_count of 0 does not always mean the run wrote nothing: the v3 load+ # consumer reads it from a batch notification that can arrive as 0 on a redelivered final+ # batch after a real write. Skip only when no table exists yet, so a genuinely empty first+ # sync does not create an empty table. An existing table still gets repointed below at the+ # freshly published files — stranding it on the previous queryable_folder would serve stale+ # data under a green sync.+ if row_count == 0 and table_created is None:+ logger.warning("Skipping table creation: row_count is 0 and no table exists yet")+ return+if table_created:table = table_createdtable.format = table_params["format"]table.url_pattern = new_url_patterntable.queryable_folder = queryable_folder- if external_data_schema.table_row_count_is_cumulative:+ if external_data_schema.table_row_count_is_cumulative or row_count == 0:+ # A reported 0 can under-count a real write (see above), so read the true count+ # from the just-published files rather than zero a table we are republishing._refresh_cumulative_row_count(table, logger, f"{_schema_name} ({_schema_id})")else:table.row_count = row_countdiff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/test_pipeline_sync.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/test_pipeline_sync.pyindex a6e1288fa697..ef931a31a7ce 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/test_pipeline_sync.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/test_pipeline_sync.py@@ -363,6 +363,69 @@ class TestValidateSchemaAndUpdateTable:assert not table.columnsassert table.created_via == DataWarehouseTableCreatedVia.SOURCE+ def _linked_table(self, team, schema, job, *, queryable_folder: str) -> DataWarehouseTable:+ names = resolve_table_and_folder_names(schema.name, schema.resolved_s3_folder_name)+ table = DataWarehouseTable.objects.create(+ name=build_table_name(job.pipeline, names.table_storage_name),+ format=DataWarehouseTableFormat.DeltaS3Wrapper,+ url_pattern="s3://bucket/orders_v1/*.parquet",+ team=team,+ row_count=100,+ queryable_folder=queryable_folder,+ external_data_source=schema.source,+ created_via=DataWarehouseTableCreatedVia.SOURCE,+ )+ schema.table = table+ schema.save()+ return table++ def test_zero_reported_row_count_still_repoints_existing_table(self, team):+ # The v3 load consumer can report row_count 0 on a redelivered final batch after a real write.+ # An existing table must then be repointed at the freshly published files, not stranded on the+ # previous queryable_folder - stranding it serves stale data under a green sync.+ schema, job = self._schema_and_job(team)+ table = self._linked_table(team, schema, job, queryable_folder="s3://bucket/orders_v1")++ with (+ patch.object(DataWarehouseTable, "get_columns", return_value={}),+ patch.object(DataWarehouseTable, "get_count", return_value=150),+ ):+ async_to_sync(validate_schema_and_update_table)(+ run_id=str(job.id),+ team_id=team.pk,+ schema_id=schema.id,+ row_count=0,+ table_format=DataWarehouseTableFormat.DeltaS3Wrapper,+ queryable_folder="s3://bucket/orders_v2",+ )++ table.refresh_from_db()+ assert table.queryable_folder == "s3://bucket/orders_v2"+ # A reported 0 must not zero a table that was just republished.+ assert table.row_count == 150++ def test_zero_row_first_sync_creates_no_table(self, team):+ # No table yet plus zero rows is a genuinely empty first sync - do not create an empty table.+ schema, job = self._schema_and_job(team)+ assert schema.table is None++ with (+ patch.object(DataWarehouseTable, "get_columns", return_value={}),+ patch.object(DataWarehouseTable, "get_count", return_value=0),+ ):+ async_to_sync(validate_schema_and_update_table)(+ run_id=str(job.id),+ team_id=team.pk,+ schema_id=schema.id,+ row_count=0,+ table_format=DataWarehouseTableFormat.DeltaS3Wrapper,+ queryable_folder="s3://bucket/orders",+ )++ schema.refresh_from_db()+ assert schema.table is None+ assert not DataWarehouseTable.objects.filter(external_data_source=schema.source, deleted=False).exists()+class TestUpdateLastSyncedAt:@pytest.mark.asyncio