: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(hogql): parse ISO 8601 Z-suffixed datetime filter values (#67919)
posthog[bot]4480c1be3364
diff --git a/posthog/hogql/transforms/property_types.py b/posthog/hogql/transforms/property_types.pyindex b7f75256b3d8..a5f060c038ce 100644--- a/posthog/hogql/transforms/property_types.py+++ b/posthog/hogql/transforms/property_types.py@@ -1,3 +1,4 @@+import refrom datetime import datetimefrom typing import Literal, Optional, cast@@ -31,6 +32,15 @@ from posthog.clickhouse.materialized_columns import (from posthog.models import Teamfrom posthog.models.property import PropertyName, TableColumn+# ISO 8601 datetimes carrying a `T` date/time separator followed by a `Z`/±HH:MM+# offset suffix. ClickHouse's toDateTime64 uses a strict parser that rejects these,+# so they must go through parseDateTime64BestEffortOrNull instead.+_ISO8601_WITH_OFFSET_RE = re.compile(r"\d{4}-\d{2}-\d{2}T.*(Z|[+-]\d{2}:?\d{2})")+++def _is_iso8601_with_offset(value: str) -> bool:+ return bool(_ISO8601_WITH_OFFSET_RE.fullmatch(value))+def build_property_swapper(node: ast.AST, context: HogQLContext) -> None:from posthog.models import PropertyDefinition@@ -517,10 +527,22 @@ class PropertySwapper(CloningVisitor):if isinstance(inner, ast.Constant):if isinstance(inner.value, datetime) and inner.value.tzinfo is not None:return expr- new_call = ast.Call(- name="toDateTime64",- args=[inner, ast.Constant(value=6), ast.Constant(value=tz)],- )+ # ISO 8601 strings carrying a `T` separator followed by a `Z`/offset suffix+ # (e.g. '2026-05-18T00:00:00.000Z') are preserved upstream so the offset+ # can be honored (see property.py:_resolve_date_value). toDateTime64 uses+ # a strict parser that rejects the trailing `Z`, so route these through+ # HogQL's toDateTime, which lowers a string arg to+ # parseDateTime64BestEffortOrNull(str, 6, tz) and honors the offset.+ if isinstance(inner.value, str) and _is_iso8601_with_offset(inner.value):+ new_call = ast.Call(+ name="toDateTime",+ args=[inner, ast.Constant(value=tz)],+ )+ else:+ new_call = ast.Call(+ name="toDateTime64",+ args=[inner, ast.Constant(value=6), ast.Constant(value=tz)],+ )if isinstance(expr, ast.Alias):return ast.Alias(alias=expr.alias, expr=new_call)return new_calldiff --git a/posthog/hogql/transforms/test/test_property_types.py b/posthog/hogql/transforms/test/test_property_types.pyindex 07b1f6fd40e8..e57c5a9ac8c1 100644--- a/posthog/hogql/transforms/test/test_property_types.py+++ b/posthog/hogql/transforms/test/test_property_types.py@@ -926,6 +926,18 @@ class TestTimezoneIndexPruning(ClickhouseTestMixin, BaseTest):hogql = "SELECT count() FROM events WHERE event = 'lhi_test' AND timestamp >= '2024-01-16' AND timestamp < '2024-01-17'"self._assert_correct_results(hogql, timezone="Australia/Lord_Howe", expected_count=2)+ @parameterized.expand(["UTC", "America/New_York", "Asia/Tokyo"])+ def test_iso8601_z_suffixed_filter_value_does_not_error(self, tz):+ """ISO 8601 filter values with a trailing `Z` must parse (not hit the strict+ toDateTime64 parser, which throws) and honor the UTC offset regardless of the+ team timezone — so all three timezones select the same three UTC instants."""+ hogql = (+ "SELECT count() FROM events WHERE event = '$pageview' "+ "AND timestamp >= '2024-03-12T00:00:00.000Z' "+ "AND timestamp < '2024-03-15T00:00:00.000Z'"+ )+ self._assert_correct_results(hogql, timezone=tz, expected_count=3)+def test_constant_gets_timezone_annotation(self):"""Bare string constants get wrapped with toDateTime64(..., 6, tz)."""sql, values = self._compile_hogql(