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(experiments): check precompute coverage on the job set that is served (#97252)

Juraj Majerikc6901d1784f6
diff --git a/products/analytics_platform/backend/lazy_computation/lazy_computation_executor.py b/products/analytics_platform/backend/lazy_computation/lazy_computation_executor.py
index d4d2e6a1f27c..d610bccf75e8 100644
--- a/products/analytics_platform/backend/lazy_computation/lazy_computation_executor.py
+++ b/products/analytics_platform/backend/lazy_computation/lazy_computation_executor.py
@@ -1073,8 +1073,14 @@ class LazyComputationExecutor:
fresh_jobs = self._filter_by_freshness(existing_jobs)
pending_jobs = [j for j in fresh_jobs if j.status == PreaggregationJob.Status.PENDING]
- # Step 2: Find missing ranges, split at TTL boundaries
- missing_ranges = find_missing_contiguous_windows(fresh_jobs, start, end)
+ # Step 2: Find missing ranges, split at TTL boundaries.
+ # Coverage is checked on the overlap-filtered set because that is
+ # the set the final return serves: the filter drops an older job
+ # that a newer one overlaps, so a window only the older job covered
+ # would read as zero if the unfiltered union counted it as covered.
+ # The filter can also hide a window covered only by an older
+ # PENDING job; recomputing it costs at most one duplicate build.
+ missing_ranges = find_missing_contiguous_windows(filter_overlapping_jobs(fresh_jobs), start, end)
ttl_ranges = split_ranges_by_ttl(missing_ranges, self.ttl_schedule)
if had_ready_at_start is None:
diff --git a/products/analytics_platform/backend/lazy_computation/tests/test_lazy_computation_executor.py b/products/analytics_platform/backend/lazy_computation/tests/test_lazy_computation_executor.py
index f9a0aa16d29f..131e4775f928 100644
--- a/products/analytics_platform/backend/lazy_computation/tests/test_lazy_computation_executor.py
+++ b/products/analytics_platform/backend/lazy_computation/tests/test_lazy_computation_executor.py
@@ -2153,6 +2153,43 @@ class TestComputationExecutorExecute(BaseTest):
assert result.ready is True
assert fresh_job.id in result.job_ids
+ def test_returned_jobs_cover_range_when_overlap_filter_evicts_broad_job(self):
+ query_info, query_hash = self._make_query_info()
+
+ now = django_timezone.now()
+ # Two fresh READY jobs whose union covers [Jan 1, Jan 9), but the overlap
+ # filter drops the older broad one, which leaves [Jan 8, Jan 9) uncovered
+ # in the filtered set. The executor must compute that window instead of
+ # serving the set with the hole.
+ for time_range, created_ago_h in [
+ ((datetime(2024, 1, 2, tzinfo=UTC), datetime(2024, 1, 9, tzinfo=UTC)), 2),
+ ((datetime(2024, 1, 1, tzinfo=UTC), datetime(2024, 1, 8, tzinfo=UTC)), 1),
+ ]:
+ job = PreaggregationJob.objects.create(
+ team=self.team,
+ query_hash=query_hash,
+ time_range_start=time_range[0],
+ time_range_end=time_range[1],
+ status=PreaggregationJob.Status.READY,
+ expires_at=now + timedelta(days=7),
+ )
+ PreaggregationJob.objects.filter(id=job.id).update(created_at=now - timedelta(hours=created_ago_h))
+
+ start = datetime(2024, 1, 1, tzinfo=UTC)
+ end = datetime(2024, 1, 9, tzinfo=UTC)
+ executor = LazyComputationExecutor()
+ result = executor.execute(
+ team=self.team,
+ query_info=query_info,
+ start=start,
+ end=end,
+ run_insert=lambda t, j: None,
+ )
+
+ assert result.ready is True
+ returned_jobs = list(PreaggregationJob.objects.filter(id__in=result.job_ids))
+ assert find_missing_contiguous_windows(returned_jobs, start, end) == []
+
def test_variable_ttl_creates_jobs_with_different_expiry(self):
query_info, _ = self._make_query_info()