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(marketing-analytics): cast the timestamp column in the DW goal health check (#76927)

Javier Bahamondese61de7708491
diff --git a/products/marketing_analytics/backend/demo/warehouse.py b/products/marketing_analytics/backend/demo/warehouse.py
index cd6eee2c27d4..44c2ddf97efd 100644
--- a/products/marketing_analytics/backend/demo/warehouse.py
+++ b/products/marketing_analytics/backend/demo/warehouse.py
@@ -570,6 +570,10 @@ BIGQUERY_ADS_COLUMNS = {
STRIPE_INVOICES_COLUMNS = {
"id": STRING,
"distinct_id": STRING,
+ # Deliberately String, not a datetime. This is how CSV and several DLT sources
+ # actually land timestamps, and it is what caught the conversion-goal health
+ # check comparing an uncast String column against DateTime64. Don't "fix" it —
+ # a datetime here removes the only coverage of that path.
"created_at": STRING,
"amount": FLOAT,
"utm_campaign": STRING,
diff --git a/products/marketing_analytics/backend/services/conversion_goals_inspector.py b/products/marketing_analytics/backend/services/conversion_goals_inspector.py
index 4be9fdd169a3..cddf7e9c30bf 100644
--- a/products/marketing_analytics/backend/services/conversion_goals_inspector.py
+++ b/products/marketing_analytics/backend/services/conversion_goals_inspector.py
@@ -571,13 +571,19 @@ def _count_dw_goal(team: Team, goal: dict[str, Any], user: User | None = None) -
since = timezone.now() - timedelta(days=DEFAULT_LOOKBACK_DAYS)
table_chain: list[str | int] = list(str(table_name).split("."))
timestamp_chain: list[str | int] = [str(timestamp_field)]
+ # Cast both sides, exactly as `_get_where_conditions` does for the real query.
+ # Warehouse timestamp columns very often land as String (CSV and several DLT
+ # sources do it), and ClickHouse refuses to compare String with DateTime64 —
+ # so an uncast health check reports a goal as broken that queries perfectly
+ # well on the dashboard. The cast is a no-op when the column is already a
+ # datetime, which is why the real path applies it unconditionally too.
query = ast.SelectQuery(
select=[ast.Call(name="count", args=[])],
select_from=ast.JoinExpr(table=ast.Field(chain=table_chain)),
where=ast.CompareOperation(
- left=ast.Field(chain=timestamp_chain),
+ left=ast.Call(name="toDateTime", args=[ast.Field(chain=timestamp_chain)]),
op=ast.CompareOperationOp.GtEq,
- right=ast.Constant(value=since),
+ right=ast.Call(name="toDateTime", args=[ast.Constant(value=since)]),
),
)
try:
diff --git a/products/marketing_analytics/backend/services/test_conversion_goals_inspector.py b/products/marketing_analytics/backend/services/test_conversion_goals_inspector.py
index 363e29e05faa..48c3aa0b849a 100644
--- a/products/marketing_analytics/backend/services/test_conversion_goals_inspector.py
+++ b/products/marketing_analytics/backend/services/test_conversion_goals_inspector.py
@@ -348,6 +348,33 @@ class TestCountDwGoalSafety:
assert reason is not None
p_exec.assert_not_called()
+ @pytest.mark.asyncio
+ async def test_casts_the_timestamp_column_before_comparing(self):
+ # Regression: warehouse timestamp columns routinely land as String (CSV and
+ # several DLT sources do it), and ClickHouse refuses `String >= DateTime64`.
+ # An uncast health check reported "DW table or column not queryable" for a
+ # goal that the dashboard queries perfectly well — a false alarm that also
+ # blocked ROAS and cost-per-customer in the setup plan.
+ from posthog.hogql import ast
+
+ from products.marketing_analytics.backend.services.conversion_goals_inspector import _count_dw_goal
+
+ team = MagicMock()
+ goal = {"table_name": "demo_stripe_invoices", "timestamp_field": "created_at"}
+ with patch(
+ "products.marketing_analytics.backend.services.conversion_goals_inspector.execute_hogql_query",
+ ) as p_exec:
+ p_exec.return_value = MagicMock(results=[[7]])
+ count, reason = await _count_dw_goal(team, goal)
+
+ assert (count, reason) == (7, None)
+ where = p_exec.call_args.args[0].where
+ assert isinstance(where.left, ast.Call) and where.left.name == "toDateTime"
+ timestamp_field = where.left.args[0]
+ assert isinstance(timestamp_field, ast.Field) and timestamp_field.chain == ["created_at"]
+ # Both sides, or ClickHouse still has two types to reconcile.
+ assert isinstance(where.right, ast.Call) and where.right.name == "toDateTime"
+
def _make_events_goal(goal_id: str, event: str | None) -> dict:
return {