: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): repartition the folder the pipeline wrote to (#75334)
Tom Owers06cd6051bc49
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/repartition_table.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/repartition_table.pyindex 3ff21145f5d3..a7bc22706173 100644--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/repartition_table.py+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/repartition_table.py@@ -241,7 +241,12 @@ def _maybe_repartition_table(inputs: RepartitionActivityInputs, logger: Filterin)return- helper = DeltaTableHelper(resource_name=schema.name, job=job, logger=logger)+ # `resolved_s3_folder_name` is authoritative for the Delta folder, not the row's own name: a row+ # renamed during the multi-schema migration keeps its folder pinned to the original path (name+ # `public.users`, folder `users`), and the pipeline writes there too. Deriving the folder from+ # `name` alone probes a path that was never written and the repartition skips as `no_delta_table`.+ resource_name = schema.resolved_s3_folder_name or schema.name+ helper = DeltaTableHelper(resource_name=resource_name, job=job, logger=logger)if pending is None and swap is None:# Nothing was queued by a prior run's post-load detection, but the gate flagged the table for andiff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_repartition_table.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_repartition_table.pynew file mode 100644index 000000000000..e2a846bba280--- /dev/null+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_repartition_table.py@@ -0,0 +1,85 @@+import uuid++import pytest+from unittest.mock import AsyncMock, MagicMock, patch++from products.warehouse_sources.backend.temporal.data_imports.workflow_activities.repartition_table import (+ RepartitionActivityInputs,+ _maybe_repartition_table,+)++MODULE = "products.warehouse_sources.backend.temporal.data_imports.workflow_activities.repartition_table"++TEAM_ID = 1+SCHEMA_ID = str(uuid.uuid4())+JOB_ID = str(uuid.uuid4())+SOURCE_ID = str(uuid.uuid4())++PENDING_TARGET = {+ "partition_mode": "datetime",+ "partition_format": "day",+ "partition_count": None,+ "partition_size": None,+ "partition_keys": ["created_at"],+ "trigger_reason": "proactive_threshold",+ "attempts": 0,+}+++def _schema(*, name: str, s3_folder_name: str | None) -> MagicMock:+ schema = MagicMock()+ schema.id = SCHEMA_ID+ schema.name = name+ schema.s3_folder_name = s3_folder_name+ schema.resolved_s3_folder_name = s3_folder_name+ schema.team_id = TEAM_ID+ schema.delta_revive_required = None+ schema.repartition_swap = None+ schema.repartition_pending = PENDING_TARGET+ return schema+++class TestRepartitionActivityDeltaFolder:+ @pytest.mark.parametrize(+ "name, s3_folder_name, expected_resource_name",+ [+ ("public.posthog_externaldatajob", "posthog_externaldatajob", "posthog_externaldatajob"),+ ("stripe_charge", None, "stripe_charge"),+ ],+ ids=["folder_pinned_by_multi_schema_rename", "folder_derived_from_name"],+ )+ @patch(f"{MODULE}.capture_repartition_event")+ @patch(f"{MODULE}.HeartbeaterSync")+ @patch(f"{MODULE}.repartition_table_in_place", new_callable=AsyncMock)+ @patch(f"{MODULE}.DeltaTableHelper")+ @patch(f"{MODULE}.is_auto_repartition_enabled", return_value=True)+ @patch(f"{MODULE}.ExternalDataJob")+ @patch(f"{MODULE}.ExternalDataSchema")+ def test_repartitions_the_folder_the_pipeline_wrote_to(+ self,+ mock_schema_model: MagicMock,+ mock_job_model: MagicMock,+ _mock_enabled: MagicMock,+ mock_helper_cls: MagicMock,+ mock_repartition: AsyncMock,+ _mock_heartbeater: MagicMock,+ _mock_capture: MagicMock,+ name: str,+ s3_folder_name: str | None,+ expected_resource_name: str,+ ) -> None:+ schema = _schema(name=name, s3_folder_name=s3_folder_name)+ mock_schema_model.objects.select_related.return_value.get.return_value = schema+ mock_repartition.return_value = {"outcome": "completed"}++ _maybe_repartition_table(+ RepartitionActivityInputs(team_id=TEAM_ID, schema_id=SCHEMA_ID, job_id=JOB_ID, source_id=SOURCE_ID),+ MagicMock(),+ )++ assert mock_helper_cls.call_args.kwargs["resource_name"] == expected_resource_name+ assert mock_repartition.await_count == 1+ await_args = mock_repartition.await_args+ assert await_args is not None+ assert await_args.kwargs["helper"] is mock_helper_cls.return_value+ assert mock_job_model.objects.get.call_args.kwargs == {"id": JOB_ID}