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): handle epoch-number cursors on datetime incremental fields (#73132)

Tom Owers82cd80da76f2
diff --git a/products/warehouse_sources/backend/models/external_data_schema.py b/products/warehouse_sources/backend/models/external_data_schema.py
index 696b0f8bb571..b045a120a7c7 100644
--- a/products/warehouse_sources/backend/models/external_data_schema.py
+++ b/products/warehouse_sources/backend/models/external_data_schema.py
@@ -577,6 +577,8 @@ class ExternalDataSchema(ModelActivityMixin, CreatedMetaFields, UpdatedMetaField
):
if isinstance(value, datetime):
return value.isoformat()
+ elif isinstance(value, int | float) and not isinstance(value, bool):
+ return value
else:
return str(value)
return str(value)
@@ -640,6 +642,8 @@ class ExternalDataSchema(ModelActivityMixin, CreatedMetaFields, UpdatedMetaField
):
if isinstance(last_value_py, datetime):
last_value_json = last_value_py.isoformat()
+ elif isinstance(last_value_py, int | float) and not isinstance(last_value_py, bool):
+ last_value_json = last_value_py
else:
last_value_json = str(last_value_py)
else:
@@ -707,6 +711,11 @@ def process_incremental_value(value: Any | None, field_type: IncrementalFieldTyp
if isinstance(value, datetime):
return value
+ # Some sources (e.g. Stripe `created`) expose datetime cursors as Unix-epoch numbers.
+ # dateutil can't parse a non-string, so pass epochs through unchanged for the source query.
+ if isinstance(value, int | float) and not isinstance(value, bool):
+ return value
+
return parser.parse(value)
if field_type == IncrementalFieldType.Date:
@@ -716,6 +725,9 @@ def process_incremental_value(value: Any | None, field_type: IncrementalFieldTyp
if isinstance(value, date):
return value
+ if isinstance(value, int | float) and not isinstance(value, bool):
+ return value
+
return parser.parse(value).date()
if field_type == IncrementalFieldType.ObjectID:
@@ -736,6 +748,10 @@ def apply_incremental_lookback(
return value
if field_type in (IncrementalFieldType.DateTime, IncrementalFieldType.Timestamp, IncrementalFieldType.Date):
+ # Epoch-number cursors (e.g. Stripe `created`) are in seconds, so shift them directly since
+ # timedelta subtraction only supports datetime/date operands.
+ if isinstance(value, int | float) and not isinstance(value, bool):
+ return value - lookback_seconds
return value - timedelta(seconds=lookback_seconds)
return value
diff --git a/products/warehouse_sources/backend/tests/test_models.py b/products/warehouse_sources/backend/tests/test_models.py
index aaa00f5d93cf..83996c5a34c2 100644
--- a/products/warehouse_sources/backend/tests/test_models.py
+++ b/products/warehouse_sources/backend/tests/test_models.py
@@ -705,6 +705,24 @@ def test_process_incremental_value_xid_returns_value_as_is() -> None:
assert process_incremental_value(None, IncrementalFieldType.XID) is None
+ "value,field_type,expected",
+ [
+ # Unix-epoch cursors (e.g. Stripe `created`) arrive as numbers on datetime-typed fields;
+ # dateutil raised "Parser must be a string or character stream, not int" before this passthrough.
+ (1718377611, IncrementalFieldType.DateTime, 1718377611),
+ (1718377611, IncrementalFieldType.Timestamp, 1718377611),
+ (1718377611, IncrementalFieldType.Date, 1718377611),
+ (1718377611.5, IncrementalFieldType.DateTime, 1718377611.5),
+ (datetime(2024, 6, 14, 15, 33, 31), IncrementalFieldType.DateTime, datetime(2024, 6, 14, 15, 33, 31)),
+ ("2024-06-14T15:33:31", IncrementalFieldType.DateTime, datetime(2024, 6, 14, 15, 33, 31)),
+ ("2024-06-14", IncrementalFieldType.Date, date(2024, 6, 14)),
+ ],
+)
+def test_process_incremental_value_datetime_handles_epoch_numbers(value, field_type, expected) -> None:
+ assert process_incremental_value(value, field_type) == expected
+
+
@pytest.mark.parametrize(
"value,field_type,lookback_seconds,expected",
[
@@ -718,6 +736,8 @@ def test_process_incremental_value_xid_returns_value_as_is() -> None:
(datetime(2026, 6, 14, 15, 33, 31), IncrementalFieldType.Timestamp, -5, datetime(2026, 6, 14, 15, 33, 31)),
(100, IncrementalFieldType.Integer, 3600, 100),
(100, IncrementalFieldType.Numeric, 3600, 100),
+ # Epoch-second cursor on a datetime field shifts directly instead of crashing on int - timedelta.
+ (1718377611, IncrementalFieldType.DateTime, 3600, 1718374011),
("abc123", IncrementalFieldType.ObjectID, 3600, "abc123"),
(None, IncrementalFieldType.Timestamp, 3600, None),
(datetime(2026, 6, 14, 15, 33, 31), None, 3600, datetime(2026, 6, 14, 15, 33, 31)),
@@ -744,6 +764,19 @@ class TestStagedIncrementalCursor:
staged = schema.sync_type_config["incremental_staged"]
assert staged == {"run_uuid": "run-1", "last_value": 42}
+ def test_stage_keeps_epoch_number_for_datetime_field(self) -> None:
+ # A datetime-typed epoch cursor must round-trip as a number, not "1718377611", so the next
+ # run's read-back doesn't feed a numeric string into dateutil and crash.
+ schema = self._make_schema(incremental_field_type=IncrementalFieldType.DateTime)
+ with patch.object(schema, "save"):
+ schema.stage_incremental_field_value("run-1", 1718377611)
+ assert schema.sync_type_config["incremental_staged"]["last_value"] == 1718377611
+
+ def test_update_incremental_field_value_keeps_epoch_number_for_datetime_field(self) -> None:
+ schema = self._make_schema(incremental_field_type=IncrementalFieldType.DateTime)
+ schema.update_incremental_field_value(1718377611, save=False)
+ assert schema.sync_type_config["incremental_field_last_value"] == 1718377611
+
def test_stage_writes_earliest_value(self) -> None:
schema = self._make_schema()
with patch.object(schema, "save"):