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-imports): tolerate a missing S3 job folder on first materialization (#78935)

Tom Owers4eaf77b27723
diff --git a/products/warehouse_sources/backend/temporal/data_imports/tests/test_util.py b/products/warehouse_sources/backend/temporal/data_imports/tests/test_util.py
index e1fe016455dc..4de2e87c73fc 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/tests/test_util.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/tests/test_util.py
@@ -163,6 +163,24 @@ class TestPrepareS3FilesForQuerying:
delete_existing=False,
)
+ async def test_tolerates_job_folder_missing_on_first_materialization(self):
+ # A brand new table/model has no prior content in S3, so listing the job folder to
+ # find old timestamped query folders to clean up raises FileNotFoundError (s3fs's
+ # `_ls` behavior for a prefix with zero objects under it). That's an expected first-run
+ # state, not a failure - regresses the crash this caused on first materialization.
+ s3 = _fake_s3(_ls=AsyncMock(side_effect=FileNotFoundError("job")))
+
+ with patch.object(util_module, "aget_s3_client", return_value=_FakeS3CM(s3)):
+ await prepare_s3_files_for_querying(
+ folder_path="job",
+ table_name="my_table",
+ file_uris=["s3://bucket/job/my_table/part-0.parquet"],
+ delete_existing=True,
+ use_timestamped_folders=True,
+ )
+
+ s3._cp_file.assert_awaited_once()
+
@parameterized.expand(
[
diff --git a/products/warehouse_sources/backend/temporal/data_imports/util.py b/products/warehouse_sources/backend/temporal/data_imports/util.py
index 45ba484a94e6..a7a0def83aaa 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/util.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/util.py
@@ -139,7 +139,12 @@ async def prepare_s3_files_for_querying(
rf"(?:^|.+/){re.escape(normalized_table_name)}\_\_query\_(\d+)(?:_[0-9a-f]{{8}})?\/?$"
)
- all_files = await s3._ls(s3_folder_for_job, detail=True)
+ try:
+ all_files = await s3._ls(s3_folder_for_job, detail=True)
+ except FileNotFoundError:
+ # First materialization for this table/model: the job folder has no
+ # prior content in S3 yet, so there's nothing to clean up.
+ all_files = []
all_file_values = all_files.values() if isinstance(all_files, dict) else all_files
directories = [f["Key"] for f in all_file_values if f["type"] == "directory"]