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-modeling): apply incremental lookback to date watermarks (#88714)

posthog[bot]46ae963265e4
diff --git a/products/data_modeling/backend/logic/incremental.py b/products/data_modeling/backend/logic/incremental.py
index f48d1de8f556..64dd8958a244 100644
--- a/products/data_modeling/backend/logic/incremental.py
+++ b/products/data_modeling/backend/logic/incremental.py
@@ -137,16 +137,28 @@ def definition_fingerprint(query: dict | None, config: IncrementalConfig) -> Opt
def window_start(state: IncrementalState, config: IncrementalConfig) -> Any:
"""The lower bound for this run: the stored watermark, deserialized, then pulled back by the
lookback. Deserializing first is load-bearing: a persisted temporal watermark is an ISO
- string, and shifting has to happen on the datetime it encodes, not on the string.
-
- Only datetime watermarks can be shifted; a numeric or string key has no meaningful notion of
- "seconds earlier", so its lookback is ignored rather than guessed at.
+ string, and shifting has to happen on the value it encodes, not on the string.
+
+ Both temporal watermarks shift. A datetime shifts by the exact seconds. A date has day
+ granularity (a day- or week-bucketed key such as ``toStartOfWeek`` yields one), so it shifts by
+ whole days, rounding a partial-day lookback up. A date cannot represent a sub-day cutoff and the
+ injected filter is inclusive, so rounding down would collapse any lookback below one day to a
+ no-op that silently skips corrections in earlier buckets. Rounding up re-scans the whole bucket
+ that holds the cutoff instead. A numeric or string key has no meaningful notion of "seconds
+ earlier", so its lookback is ignored rather than guessed at.
"""
watermark = deserialize_watermark(state.watermark, state.watermark_type)
if watermark is None:
return None
- if config.lookback_seconds and isinstance(watermark, datetime):
- return watermark - timedelta(seconds=config.lookback_seconds)
+ if config.lookback_seconds:
+ # datetime subclasses date, so test it first.
+ if isinstance(watermark, datetime):
+ return watermark - timedelta(seconds=config.lookback_seconds)
+ if isinstance(watermark, date):
+ # Ceiling division rounds a partial-day lookback up to a whole day (see above).
+ seconds_per_day = 60 * 60 * 24
+ days = (config.lookback_seconds + seconds_per_day - 1) // seconds_per_day
+ return watermark - timedelta(days=days)
return watermark
diff --git a/products/data_modeling/backend/test/test_incremental_config.py b/products/data_modeling/backend/test/test_incremental_config.py
index 2a9e6ad3139c..dd839368ebfd 100644
--- a/products/data_modeling/backend/test/test_incremental_config.py
+++ b/products/data_modeling/backend/test/test_incremental_config.py
@@ -215,6 +215,27 @@ class TestIncrementalConfig(BaseTest):
0,
datetime(2026, 8, 2, tzinfo=UTC),
),
+ (
+ "date_shifts_back_whole_days",
+ date(2026, 8, 10),
+ "date",
+ 60 * 60 * 24 * 5,
+ date(2026, 8, 5),
+ ),
+ (
+ "date_rounds_partial_day_lookback_up_to_whole_days",
+ date(2026, 8, 10),
+ "date",
+ 60 * 60 * 24 + 3600,
+ date(2026, 8, 8),
+ ),
+ (
+ "date_sub_day_lookback_reaches_previous_bucket",
+ date(2026, 8, 10),
+ "date",
+ 3600,
+ date(2026, 8, 9),
+ ),
("integer_key_ignores_lookback", 42, "int", 3600, 42),
("string_key_ignores_lookback", "2026-08-02", "string", 3600, "2026-08-02"),
]