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): compare delta timestamp bounds in one offset shape (#108279)

Tom Owers218eed6656ff
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py
index 309992967259..667d682c4358 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py
@@ -20,7 +20,7 @@ import json
import uuid
import dataclasses
from collections.abc import Callable, Iterable
-from datetime import date, datetime, timedelta
+from datetime import UTC, date, datetime, timedelta
from decimal import Decimal, localcontext
from typing import Any
@@ -271,6 +271,19 @@ class _UnparseableValue(Exception):
"""
+def _normalize_timestamp(delta_type: str, value: datetime) -> datetime:
+ """Give a Delta timestamp one offset shape per type, whatever shape its spelling carried.
+
+ A `timestamp` is a UTC instant and a `timestamp_ntz` is a wall clock, but writers disagree on
+ whether to spell the offset out: a commit log can carry a `timestamp` bound with no offset while
+ the stored bound came from the Add-action scan's UTC-aware value, or the reverse. The fold
+ compares the two, and comparing a naive datetime with an aware one raises TypeError.
+ """
+ if delta_type == "timestamp_ntz":
+ return value.replace(tzinfo=None)
+ return value.astimezone(UTC) if value.tzinfo is not None else value.replace(tzinfo=UTC)
+
+
def _parse_stored_value(delta_type: Any, text: str) -> Any:
"""Parse a stored min/max back into the Python value `str()` produced from the Add-action scan.
@@ -294,7 +307,7 @@ def _parse_stored_value(delta_type: Any, text: str) -> Any:
if delta_type == "date":
return date.fromisoformat(text)
if delta_type in ("timestamp", "timestamp_ntz"):
- return datetime.fromisoformat(text)
+ return _normalize_timestamp(delta_type, datetime.fromisoformat(text))
if delta_type.startswith("decimal("):
return Decimal(text)
except (ValueError, ArithmeticError):
@@ -326,7 +339,7 @@ def _parse_log_value(delta_type: Any, value: Any) -> Any:
if delta_type == "date":
return date.fromisoformat(value)
if delta_type in ("timestamp", "timestamp_ntz"):
- return datetime.fromisoformat(value)
+ return _normalize_timestamp(delta_type, datetime.fromisoformat(value))
if delta_type.startswith("decimal("):
precision, scale = (int(p) for p in delta_type[len("decimal(") : -1].split(","))
# The default context (28 significant digits) is narrower than Delta allows (up to 38),
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py
index f996eec28cb9..351d9dad61bc 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py
@@ -371,6 +371,26 @@ class TestComputeTableStatisticsSync:
"2023-12-31 23:00:00+00:00",
"2024-06-01 12:30:00.000123+00:00",
),
+ (
+ "timestamp_logged_without_an_offset",
+ "timestamp",
+ "2024-01-01 00:00:00+00:00",
+ "2024-02-01 00:00:00+00:00",
+ "2023-12-31T23:00:00",
+ "2024-06-01T12:30:00.000123",
+ "2023-12-31 23:00:00+00:00",
+ "2024-06-01 12:30:00.000123+00:00",
+ ),
+ (
+ "timestamp_ntz_logged_with_an_offset",
+ "timestamp_ntz",
+ "2024-01-01 00:00:00",
+ "2024-02-01 00:00:00",
+ "2023-12-31T23:00:00Z",
+ "2024-06-01T12:30:00Z",
+ "2023-12-31 23:00:00",
+ "2024-06-01 12:30:00",
+ ),
("decimal", "decimal(10,2)", "1.50", "9.99", 0.5, 12.5, "0.50", "12.50"),
]
)
@@ -388,6 +408,8 @@ class TestComputeTableStatisticsSync:
# The stored bound is `str()` of the typed value the Add-action scan yields; the commit log
# carries the same value in JSON form. A fold that compared the two as text, or stored the
# log's spelling, would drift from what the next full scan writes.
+ # A timestamp is the one type whose two sides can disagree on spelling the offset, which
+ # made the fold compare a naive datetime with an aware one and abandon itself.
team = self._team()
schema, table, _ = self._schema_table_job(team)
self._stored(team, table, min_value=stored_min, max_value=stored_max)