: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): stop the statistics fold raising on a retyped column (#108898)
Tom Owersf6f04475d824
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.pyindex cf2e7939859a..c7c95e94da6d 100644--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py@@ -424,6 +424,12 @@ def _fold_commit_stats(return Nonerow_counts.add(stored.row_count)delta_type = delta_schema_fields.get(name)+ # Bounds stored under a type the fold can no longer maintain: schema evolution turned the+ # column nested, or took it out of the Delta schema altogether. `_FoldState.fold` skips such+ # a column, so carrying its bounds forward would freeze them while new files land — the full+ # scan re-derives them instead.+ if not isinstance(delta_type, str) and (stored.min_value is not None or stored.max_value is not None):+ return Nonestates[name] = _FoldState(delta_type=delta_type,null_count=stored.null_count,diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.pyindex 76cf4550aa45..377f70864377 100644--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py@@ -207,7 +207,8 @@ class TestComputeTableStatisticsSync:delta_table.version.return_value = versiondelta_table.get_add_actions.return_value = add_actionsdelta_table.table_uri = "s3://bucket/data/stripe_charge/"- fields = [{"name": name, "type": kind} for name, kind in (delta_types or {"amount": "long"}).items()]+ types = {"amount": "long"} if delta_types is None else delta_types+ fields = [{"name": name, "type": kind} for name, kind in types.items()]delta_table.schema.return_value.to_json.return_value = json.dumps({"type": "struct", "fields": fields})helper = MagicMock()helper.get_delta_table = AsyncMock(return_value=delta_table)@@ -545,6 +546,35 @@ class TestComputeTableStatisticsSync:reported = str(mock_capture.call_args[0][0])assert "not-a-number" not in reported+ @parameterized.expand(+ [+ ("turned_nested", {"amount": {"type": "struct", "fields": []}}),+ ("left_the_schema", {}),+ ]+ )+ def test_a_column_whose_stored_bounds_lost_their_type_falls_back(self, _name: str, delta_types: dict) -> None:+ # Schema evolution can turn a column the last full scan recorded bounds for into a nested+ # type, or drop it from the Delta schema. The fold cannot keep those bounds current, so it+ # has to fall back to the full scan rather than raise.+ team = self._team()+ schema, table, _ = self._schema_table_job(team)+ self._stored(team, table, min_value="2", max_value="50")+ add_actions = pa.table({"num_records": [99], "null_count.amount": [0], "min.amount": [1], "max.amount": [1]})+ helper = self._mock_delta(add_actions, version=8, delta_types=delta_types)+ with (+ patch.object(comp, "statistics_enabled", return_value=True),+ patch.object(comp, "_read_commit_actions", return_value=[self._add(1, amount=(1, 1, 0))]) as mock_read,+ patch.object(comp, "capture_exception") as mock_capture,+ patch(DELTA_HELPER_PATH, return_value=helper),+ ):+ result = compute_table_statistics_sync(team.id, schema.id)++ assert result["basis"] == "full"+ assert not mock_read.called+ mock_capture.assert_not_called()+ stat = WarehouseColumnStatistics.objects.for_team(team.id).get(table_id=table.id, column_name="amount")+ assert stat.row_count == 99+def test_a_nested_column_neither_blocks_nor_gains_bounds_from_the_fold(self) -> None:team = self._team()schema, table, _ = self._schema_table_job(