Skip to content

: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.

fix(data-warehouse): resolve duckgres backfill Delta path from url_pattern (#70180)

Eric Duongd87e097f17d4
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/duckgres/backfill_snapshot.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/duckgres/backfill_snapshot.py
index 21db3995fd31..0cce77db925b 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/duckgres/backfill_snapshot.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/duckgres/backfill_snapshot.py
@@ -15,6 +15,7 @@ from urllib.parse import unquote
from django.conf import settings
from products.warehouse_sources.backend.models import ExternalDataSchema
+from products.warehouse_sources.backend.temporal.data_imports.naming_convention import NamingConvention
CHUNK_TARGET_BYTES = 1024**3 # ~1 GiB of parquet per chunk statement
MAX_FILES_PER_CHUNK = 512 # bound the read_parquet([...]) literal list
@@ -40,7 +41,32 @@ class BackfillSnapshotPlan:
def delta_table_uri(schema: ExternalDataSchema) -> str:
- return f"{settings.BUCKET_URL}/{schema.folder_path()}/{schema.normalized_name}"
+ return f"{settings.BUCKET_URL}/{schema.folder_path()}/{_delta_table_folder(schema)}"
+
+
+def _delta_table_folder(schema: ExternalDataSchema) -> str:
+ """The Delta table's own folder segment, as the loader actually wrote it.
+
+ The loader names the folder after the DLT resource (the unqualified table
+ name), so it diverges from schema.normalized_name for schema-qualified
+ sources: Postgres "public.foo" normalizes to "public_foo", but the Delta
+ folder is "foo". Reading normalized_name here points the backfill at a
+ prefix with no _delta_log (surfaces as "No files in log segment"). The
+ catalog table's url_pattern is the authoritative location — it is what the
+ query engine reads — so take the leaf from there, and fall back to
+ normalized_name only when no table row exists yet (nothing to backfill).
+
+ url_pattern is a user-writable field, so the leaf is normalized through the
+ same convention the writer used to produce it. This is a no-op for every
+ legitimate folder (they are already normalize_identifier output) and strips
+ any injected separators so the leaf can never escape the schema's own prefix.
+ """
+ table = schema.table
+ if table and table.url_pattern:
+ segments = [seg for seg in table.url_pattern.rstrip("/").split("/") if seg and seg not in ("*", "**")]
+ if segments:
+ return NamingConvention.normalize_identifier(segments[-1])
+ return schema.normalized_name
def _delta_storage_options() -> dict[str, str]:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/duckgres/test_backfill_snapshot.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/duckgres/test_backfill_snapshot.py
new file mode 100644
index 000000000000..226e327bad4e
--- /dev/null
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/duckgres/test_backfill_snapshot.py
@@ -0,0 +1,76 @@
+import uuid
+
+from django.test import SimpleTestCase, override_settings
+
+from parameterized import parameterized
+
+from products.warehouse_sources.backend.models.external_data_schema import ExternalDataSchema
+from products.warehouse_sources.backend.models.external_data_source import ExternalDataSource
+from products.warehouse_sources.backend.models.table import DataWarehouseTable
+from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.duckgres.backfill_snapshot import (
+ delta_table_uri,
+)
+
+_SCHEMA_ID = uuid.UUID("0190c6a4-6641-0000-0f7a-d9482771b742")
+
+
+@override_settings(BUCKET_URL="s3://test-bucket/dlt")
+class TestDeltaTableUri(SimpleTestCase):
+ def _schema(self, name: str, leaf: str | None) -> ExternalDataSchema:
+ # Built in memory (no save): exercises the real normalized_name and
+ # folder_path. leaf=None models a schema with no catalog table yet.
+ schema = ExternalDataSchema(id=_SCHEMA_ID, team_id=1, name=name)
+ schema.source = ExternalDataSource(source_type="Postgres")
+ if leaf is None:
+ schema.table = None
+ else:
+ url = f"https://bucket.s3.amazonaws.com/dlt/{schema.folder_path()}/{leaf}"
+ schema.table = DataWarehouseTable(url_pattern=url)
+ return schema
+
+ @parameterized.expand(
+ [
+ # Schema-qualified Postgres source: the loader wrote the Delta folder
+ # under the unqualified table name, but normalized_name keeps the
+ # "public_" qualifier. Reading normalized_name pointed the backfill at
+ # a prefix with no _delta_log ("No files in log segment").
+ ("schema_qualified", "public.posthog_hogfunction", "posthog_hogfunction/", "posthog_hogfunction"),
+ # Unqualified name: folder and normalized_name already agree.
+ (
+ "unqualified",
+ "campaign_performance_report",
+ "campaign_performance_report/",
+ "campaign_performance_report",
+ ),
+ # url_pattern may carry a trailing glob token instead of a bare slash.
+ ("glob_suffix", "public.posthog_team", "posthog_team/*", "posthog_team"),
+ ]
+ )
+ def test_uri_folder_comes_from_url_pattern_not_normalized_name(
+ self, _name: str, schema_name: str, leaf: str, expected_leaf: str
+ ) -> None:
+ schema = self._schema(schema_name, leaf)
+
+ uri = delta_table_uri(schema)
+
+ assert uri == f"s3://test-bucket/dlt/{schema.folder_path()}/{expected_leaf}"
+
+ def test_falls_back_to_normalized_name_when_no_table(self) -> None:
+ schema = self._schema("public.posthog_hogfunction", leaf=None)
+
+ uri = delta_table_uri(schema)
+
+ assert uri == f"s3://test-bucket/dlt/{schema.folder_path()}/{schema.normalized_name}"
+
+ def test_injected_url_pattern_leaf_cannot_escape_schema_prefix(self) -> None:
+ # url_pattern is a user-writable field. A leaf crafted with encoded
+ # separators (percent-encoded slashes survive split("/") as one segment)
+ # must not let the backfill target a prefix outside the schema's folder.
+ schema = self._schema("public.posthog_hogfunction", leaf="..%2f..%2fteam_2_postgres_secret%2fusers")
+
+ uri = delta_table_uri(schema)
+
+ prefix = f"s3://test-bucket/dlt/{schema.folder_path()}/"
+ assert uri.startswith(prefix)
+ leaf = uri[len(prefix) :]
+ assert "/" not in leaf and "." not in leaf and "%" not in leaf