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): handle empty team-day in duckling file registration (#65467)

Benjamin Knofe-Vidercf0b566251ce
diff --git a/posthog/dags/events_backfill_to_duckling.py b/posthog/dags/events_backfill_to_duckling.py
index f2ad1ff5d86a..04d4d136cf86 100644
--- a/posthog/dags/events_backfill_to_duckling.py
+++ b/posthog/dags/events_backfill_to_duckling.py
@@ -1486,10 +1486,22 @@ def _glob_run_files(conn: psycopg.Connection[Any], s3_glob: str) -> list[str]:
non-empty PARTITION BY bucket — so registration discovers them by globbing the
run's output rather than predicting names. The glob is run-scoped, so it never
returns a prior run's objects.
+
+ A zero-event team-day is normal in a historical backfill: ClickHouse writes no
+ Parquet for an empty partition, so the glob matches nothing. duckgres returns
+ that empty glob as a command-complete with no result set (rather than an empty
+ row set), which psycopg surfaces as "the last operation didn't produce a result"
+ when the rows are fetched — catch that and treat it as "no files".
"""
with conn.cursor() as cur:
cur.execute("SELECT file FROM glob(%s) ORDER BY file", (s3_glob,))
- return [row[0] for row in cur.fetchall()]
+ try:
+ rows = cur.fetchall()
+ except psycopg.ProgrammingError as exc:
+ if "didn't produce a result" in str(exc):
+ return []
+ raise
+ return [row[0] for row in rows]
def register_files_with_duckling(
diff --git a/posthog/dags/test_events_backfill_to_duckling.py b/posthog/dags/test_events_backfill_to_duckling.py
index 73e58558004d..06fce038d994 100644
--- a/posthog/dags/test_events_backfill_to_duckling.py
+++ b/posthog/dags/test_events_backfill_to_duckling.py
@@ -1358,6 +1358,30 @@ class TestRegisterFilesWithDuckling:
assert "glob(%s)" in sql
assert params == ("s3://bkt/x/run1_*.parquet",)
+ def test_glob_run_files_empty_day_returns_no_files(self):
+ # A zero-event team-day writes no Parquet, so the glob matches nothing and
+ # duckgres returns a command-complete with no result set: fetchall() raises
+ # "the last operation didn't produce a result". _glob_run_files must absorb
+ # that and return [] (nothing to register).
+ conn = MagicMock()
+ cur = MagicMock()
+ cur.fetchall.side_effect = psycopg.ProgrammingError("the last operation didn't produce a result")
+ conn.cursor.return_value.__enter__.return_value = cur
+ conn.cursor.return_value.__exit__.return_value = False
+
+ assert _glob_run_files(conn, "s3://bkt/x/run1_*.parquet") == []
+
+ def test_glob_run_files_propagates_other_programming_errors(self):
+ # A genuine SQL error must not be swallowed as "no files".
+ conn = MagicMock()
+ cur = MagicMock()
+ cur.fetchall.side_effect = psycopg.ProgrammingError("syntax error at or near")
+ conn.cursor.return_value.__enter__.return_value = cur
+ conn.cursor.return_value.__exit__.return_value = False
+
+ with pytest.raises(psycopg.ProgrammingError, match="syntax error"):
+ _glob_run_files(conn, "s3://bkt/x/run1_*.parquet")
+
# Column SELECT used to synthesize events Parquet files for the round-trip test.
def _events_rows_select(n_rows, uuid_prefix, ts):