: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-imports): tolerate an empty Delta table during schema validation (#80068)
Tom Owersa95c196c2a71
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 b07b6f2b9a76..f827ca736d45 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@@ -251,7 +251,9 @@ async def validate_schema_and_update_table(assert isinstance(table_created, DataWarehouseTable) and table_created is not None- raw_db_columns = table_created.get_columns()+ # safe_expose_ch_error=False keeps failures as ServerException (see except clause below)+ # instead of the generic, user-facing Exception get_columns() raises by default.+ raw_db_columns = table_created.get_columns(safe_expose_ch_error=False)db_columns = {key: str(column.get("clickhouse", "")) for key, column in raw_db_columns.items()}def _persist_columns() -> None:@@ -297,7 +299,10 @@ async def validate_schema_and_update_table(retry_on_db_connection_drop(_persist_columns)except ServerException as err:- if err.code == 636:+ # 636 (CANNOT_EXTRACT_TABLE_STRUCTURE) and 742 (DELTA_KERNEL_ERROR, "No files in log+ # segment") both mean the Delta table has no committed files yet - expected before a+ # schema's first successful sync, or when a run wrote zero new rows.+ if err.code in (636, 742):logger.exception(f"Data Warehouse: No data for schema {_schema_name} for external data job {job.pk}",exc_info=err,diff --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 8af0a0d88a8d..5082bc6cb022 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@@ -6,6 +6,8 @@ from unittest.mock import MagicMock, patchfrom django.db import OperationalError, transaction+from asgiref.sync import async_to_sync+from clickhouse_driver.errors import ServerExceptionfrom parameterized import parameterizedfrom products.warehouse_sources.backend.models.external_data_job import ExternalDataJob@@ -21,10 +23,12 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_refresh_cumulative_row_count,merge_columns,update_last_synced_at,+ validate_schema_and_update_table,)_PIPELINE_SYNC_MODULE = "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_sync"_DB_RETRY_MODULE = "products.warehouse_sources.backend.temporal.data_imports.pipelines.common.db_retry"+_TABLE_MODULE = "products.warehouse_sources.backend.models.table"class TestResolveTableAndFolderNames:@@ -306,6 +310,54 @@ class TestRegisterCDCCompanionTable(BaseTest):assert companions.count() == 0+# transaction=True: validate_schema_and_update_table writes to the DB from the async thread pool+# (database_sync_to_async_pool), which can't see an atomic TestCase's uncommitted rows.[email protected]_db(transaction=True)+class TestValidateSchemaAndUpdateTable:+ def _schema_and_job(self, team) -> tuple[ExternalDataSchema, ExternalDataJob]:+ source = ExternalDataSource.objects.create(+ source_id=str(uuid.uuid4()), connection_id=str(uuid.uuid4()), team=team, source_type="Postgres"+ )+ schema = ExternalDataSchema.objects.create(name="orders", team=team, source=source)+ job = ExternalDataJob.objects.create(+ team=team, pipeline=source, schema=schema, status=ExternalDataJob.Status.RUNNING, rows_synced=10+ )+ return schema, job++ def test_delta_table_with_no_committed_files_does_not_crash_the_sync(self, team):+ # A Delta table can pass load.py's "does a table exist" check while still having zero+ # committed add-file actions (e.g. an incremental run that wrote no new rows). ClickHouse then+ # raises DELTA_KERNEL_ERROR ("No files in log segment") when get_columns() tries to DESCRIBE+ # it. This must be tolerated like the sibling CANNOT_EXTRACT_TABLE_STRUCTURE (636) case rather+ # than crash the whole sync activity - regression for a real production failure where it did.+ schema, job = self._schema_and_job(team)+ no_files_error = ServerException(+ "DB::Exception: Received DeltaLake kernel error GenericError: Generic delta kernel error: "+ "No files in log segment (in snapshot).",+ code=742,+ )++ with (+ patch(f"{_TABLE_MODULE}.sync_execute", side_effect=no_files_error),+ patch(f"{_TABLE_MODULE}.time.sleep"),+ ):+ async_to_sync(validate_schema_and_update_table)(+ run_id=str(job.id),+ team_id=team.pk,+ schema_id=schema.id,+ row_count=10,+ table_format=DataWarehouseTable.TableFormat.DeltaS3Wrapper,+ queryable_folder="s3://bucket/orders",+ )++ schema.refresh_from_db()+ # get_columns() failing means the table never gets linked to the schema or given columns -+ # but critically, the sync activity itself must survive rather than crash the whole run.+ assert schema.table is None+ table = DataWarehouseTable.objects.get(external_data_source=schema.source, deleted=False)+ assert not table.columns++class TestUpdateLastSyncedAt:@pytest.mark.asyncioasync def test_retries_transient_query_wait_timeout_then_succeeds(self):