: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-imports): handle numeric-string incremental cursors on date/time fields (#74137)
Tom Owers12e4bc0cd0eb
diff --git a/products/warehouse_sources/backend/models/external_data_schema.py b/products/warehouse_sources/backend/models/external_data_schema.pyindex a68fbdae7cd2..b13f2849c35c 100644--- a/products/warehouse_sources/backend/models/external_data_schema.py+++ b/products/warehouse_sources/backend/models/external_data_schema.py@@ -723,6 +723,26 @@ def _parse_datetime_string(value: str) -> datetime:return parser.parse(stripped)+def _coerce_incremental_datetime(value: str) -> datetime | int:+ """Parse a DateTime/Timestamp/Date cursor string, falling back to a raw integer.++ Some drivers surface a numeric cursor as a bare digit string even though the field is+ typed as a date/time type (e.g. a ClickHouse column Arrow can't emit natively, cast to+ String, whose current type no longer matches the incremental field's stored type).+ dateutil's heuristics then misread the digits as a calendar year and overflow past+ datetime's year-9999 ceiling (`ParserError`), or raise a bare `OverflowError` for longer+ digit runs. Legitimate compact date strings like "20240115" (YYYYMMDD) parse correctly+ above and never reach this fallback.+ """+ try:+ return _parse_datetime_string(value)+ except (parser.ParserError, OverflowError):+ stripped = value.strip()+ if stripped.lstrip("-").isdigit():+ return int(stripped)+ raise++def process_incremental_value(value: Any | None, field_type: IncrementalFieldType | None) -> Any:if value is None or value == "None" or field_type is None:return None@@ -743,7 +763,7 @@ def process_incremental_value(value: Any | None, field_type: IncrementalFieldTypif isinstance(value, int | float) and not isinstance(value, bool):return value- return _parse_datetime_string(value)+ return _coerce_incremental_datetime(value)if field_type == IncrementalFieldType.Date:if isinstance(value, datetime):@@ -755,7 +775,8 @@ def process_incremental_value(value: Any | None, field_type: IncrementalFieldTypif isinstance(value, int | float) and not isinstance(value, bool):return value- return _parse_datetime_string(value).date()+ parsed = _coerce_incremental_datetime(value)+ return parsed if isinstance(parsed, int) else parsed.date()if field_type == IncrementalFieldType.ObjectID:return str(value)diff --git a/products/warehouse_sources/backend/tests/test_models.py b/products/warehouse_sources/backend/tests/test_models.pyindex fe1e9832e021..13c650d0da90 100644--- a/products/warehouse_sources/backend/tests/test_models.py+++ b/products/warehouse_sources/backend/tests/test_models.py@@ -9,6 +9,7 @@ from django.db.models import Modelfrom django.test import SimpleTestCasefrom django.utils import timezone+from dateutil import parserfrom parameterized import parameterizedfrom posthog.models.signals import model_activity_signal@@ -748,12 +749,33 @@ def test_process_incremental_value_xid_returns_value_as_is() -> None:IncrementalFieldType.Date,date(2026, 1, 5),),+ # A bare digit-string cursor on a date/time-typed field (e.g. a ClickHouse column Arrow+ # casts to String) crashed here: dateutil misreads it as a calendar year and overflows+ # past datetime's year-9999 ceiling. Fall back to the raw integer instead of crashing.+ ("20662", IncrementalFieldType.Timestamp, 20662),+ ("20662", IncrementalFieldType.DateTime, 20662),+ ("20662", IncrementalFieldType.Date, 20662),+ # Longer digit runs overflow C's int range and raise `OverflowError` instead of+ # `ParserError` - same fallback must catch both.+ ("20662123456", IncrementalFieldType.DateTime, 20662123456),+ # A genuine compact date string (YYYYMMDD) must still parse as a real date, not fall+ # back to the raw-integer path.+ ("20240115", IncrementalFieldType.Date, date(2024, 1, 15)),],)def test_process_incremental_value_datetime_handles_epoch_numbers(value, field_type, expected) -> None:assert process_incremental_value(value, field_type) == expected+ "field_type",+ [IncrementalFieldType.DateTime, IncrementalFieldType.Timestamp, IncrementalFieldType.Date],+)+def test_process_incremental_value_datetime_reraises_unparseable_non_numeric_string(field_type) -> None:+ with pytest.raises(parser.ParserError):+ process_incremental_value("not-a-date-at-all", field_type)++@pytest.mark.parametrize("value,field_type,lookback_seconds,expected",[