: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): resolve ducklake copy source from the written delta folder (#73134)
Tom Owers130152fb652e
diff --git a/posthog/temporal/ducklake/ducklake_copy_data_imports_workflow.py b/posthog/temporal/ducklake/ducklake_copy_data_imports_workflow.pyindex 817d13152835..3d5711f75daa 100644--- a/posthog/temporal/ducklake/ducklake_copy_data_imports_workflow.py+++ b/posthog/temporal/ducklake/ducklake_copy_data_imports_workflow.py@@ -259,7 +259,7 @@ async def prepare_data_imports_ducklake_metadata_activity(source_type=source_type,)continue- source_table_uri = f"{settings.BUCKET_URL}/{schema.folder_path()}/{normalized_name}"+ source_table_uri = _data_imports_source_table_uri(schema)staging_uri = await database_sync_to_async(_resolve_data_imports_staging_uri)(source_table_uri, team_id=inputs.team_id)@@ -386,6 +386,17 @@ def cleanup_data_imports_staging_activity(inputs: DuckLakeDataImportsStagingClea)+def _data_imports_source_table_uri(schema: ExternalDataSchema) -> str:+ """S3 URI of the schema's Delta table, matching the folder the loader actually wrote.++ Uses ``normalized_s3_folder_name`` (the resolved folder leaf), which diverges from+ ``normalized_name`` for folder-pinned sources (e.g. Postgres ``public.users`` → folder+ ``users``). Reading ``normalized_name`` here points at a prefix with no ``_delta_log``+ and surfaces as "No files in log segment".+ """+ return f"{settings.BUCKET_URL}/{schema.folder_path()}/{schema.normalized_s3_folder_name}"++def _resolve_data_imports_staging_uri(source_uri: str, *, team_id: int) -> str | None:"""Return the staged Delta URI required by prod duckgres, or None for local dev."""if is_dev_mode():diff --git a/posthog/temporal/tests/ducklake/test_ducklake_copy_data_imports_workflow.py b/posthog/temporal/tests/ducklake/test_ducklake_copy_data_imports_workflow.pyindex 79affa76fa0c..884783afe955 100644--- a/posthog/temporal/tests/ducklake/test_ducklake_copy_data_imports_workflow.py+++ b/posthog/temporal/tests/ducklake/test_ducklake_copy_data_imports_workflow.py@@ -5,6 +5,8 @@ from collections.abc import Sequenceimport pytestfrom unittest.mock import MagicMock+from django.conf import settings+import temporalio.workerimport temporalio.converterfrom temporalio import activity as temporal_activity@@ -241,6 +243,47 @@ async def test_prepare_data_imports_ducklake_metadata_activity_basic(ateam, monkassert metadata.source_partition_column == "created_at"+ "schema_name,s3_folder_name,expected_leaf",+ [+ ("orders", None, "orders"),+ # Folder-pinned source: the loader wrote the Delta table under the resolved folder+ # ("users"), not the schema's normalized name ("public_users"). Reading normalized_name+ # points at a prefix with no _delta_log -> "No files in log segment".+ ("public.users", "users", "users"),+ ],+)+async def test_prepare_resolves_source_table_uri_from_written_folder(+ ateam, monkeypatch, schema_name, s3_folder_name, expected_leaf+):+ monkeypatch.setattr(ducklake_module, "_fetch_delta_partition_columns", lambda table_uri, *, team_id: [])++ source = await database_sync_to_async(ExternalDataSource.objects.create)(+ team=ateam,+ source_id="test_source",+ connection_id="test_connection",+ source_type="Postgres",+ status="Running",+ )+ schema = await database_sync_to_async(ExternalDataSchema.objects.create)(+ team=ateam,+ name=schema_name,+ source=source,+ sync_type=ExternalDataSchema.SyncType.FULL_REFRESH,+ s3_folder_name=s3_folder_name,+ )++ inputs = DataImportsDuckLakeCopyInputs(team_id=ateam.id, job_id="job-uri", schema_ids=[schema.id])++ result = await prepare_data_imports_ducklake_metadata_activity(inputs)++ assert len(result) == 1+ folder_path = await database_sync_to_async(schema.folder_path)()+ assert result[0].source_table_uri == f"{settings.BUCKET_URL}/{folder_path}/{expected_leaf}"++@pytest.mark.asyncio@pytest.mark.django_dbasync def test_prepare_data_imports_ducklake_metadata_activity_no_partition(ateam, monkeypatch):diff --git a/products/warehouse_sources/backend/models/external_data_schema.py b/products/warehouse_sources/backend/models/external_data_schema.pyindex b045a120a7c7..cec8bfbccead 100644--- a/products/warehouse_sources/backend/models/external_data_schema.py+++ b/products/warehouse_sources/backend/models/external_data_schema.py@@ -130,6 +130,16 @@ class ExternalDataSchema(ModelActivityMixin, CreatedMetaFields, UpdatedMetaFielddef normalized_name(self):return NamingConvention.normalize_identifier(self.name)+ @property+ def normalized_s3_folder_name(self) -> str:+ """Normalized Delta folder leaf the loader actually wrote the table under.++ Diverges from ``normalized_name`` for folder-pinned rows (e.g. Postgres ``public.users``+ → folder ``users``); readers that resolve ``normalized_name`` point at a prefix with no+ ``_delta_log`` and surface "No files in log segment".+ """+ return NamingConvention.normalize_identifier(self.resolved_s3_folder_name or self.name)+@propertydef is_incremental(self):return self.sync_type == self.SyncType.INCREMENTAL