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(insights): stop toDateTime re-parsing an already-parsed datetime (#58684)

Sam Pennington870e1ef943cd
diff --git a/posthog/hogql/printer/clickhouse.py b/posthog/hogql/printer/clickhouse.py
index aa62fef612bf..3b995aeec1dc 100644
--- a/posthog/hogql/printer/clickhouse.py
+++ b/posthog/hogql/printer/clickhouse.py
@@ -154,9 +154,15 @@ class ClickHousePrinter(BasePrinter):
relevant_clickhouse_name = func_meta.clickhouse_name
if func_meta.overloads:
+ # Look through aliases: an alias's declared type can be stale after a
+ # transform (e.g. the property-type swapper) rewrites its inner
+ # expression, so resolve the overload against the real expression.
+ first_arg = node.args[0] if len(node.args) > 0 else None
+ while isinstance(first_arg, ast.Alias):
+ first_arg = first_arg.expr
first_arg_constant_type = (
- node.args[0].type.resolve_constant_type(self.context)
- if len(node.args) > 0 and node.args[0].type is not None
+ first_arg.type.resolve_constant_type(self.context)
+ if first_arg is not None and first_arg.type is not None
else None
)
diff --git a/posthog/hogql/printer/test/test_printer.py b/posthog/hogql/printer/test/test_printer.py
index 0a2ee2e23017..d8ba599302b9 100644
--- a/posthog/hogql/printer/test/test_printer.py
+++ b/posthog/hogql/printer/test/test_printer.py
@@ -2107,6 +2107,25 @@ class TestPrinter(BaseTest):
)
self.assertEqual(str(error_context.exception), "Unknown timezone: 'Europe/PostHogLandia'")
+ def test_to_datetime_does_not_double_parse_datetime_property(self):
+ PropertyDefinition.objects.create(
+ team=self.team, name="dt_prop", property_type="DateTime", type=PropertyDefinition.Type.EVENT
+ )
+ printed = self._expr("toDateTime(properties.dt_prop)")
+ # The property-type swapper already wraps a DateTime property in toDateTime;
+ # an outer toDateTime must not re-parse the resulting datetime.
+ self.assertEqual(printed.count("parseDateTime64BestEffortOrNull"), 1, printed)
+
+ def test_to_datetime_does_not_double_parse_aliased_datetime_property(self):
+ PropertyDefinition.objects.create(
+ team=self.team, name="dt_prop", property_type="DateTime", type=PropertyDefinition.Type.EVENT
+ )
+ # The toDateTime arg is an Alias whose declared type is stale after the
+ # swapper rewrites the inner DateTime property; the printer must look
+ # through the Alias to resolve the already-a-datetime overload.
+ printed = self._expr("toDateTime(properties.dt_prop AS d)")
+ self.assertEqual(printed.count("parseDateTime64BestEffortOrNull"), 1, printed)
+
def test_window_functions(self):
self.assertEqual(
self._select(
diff --git a/posthog/hogql/transforms/property_types.py b/posthog/hogql/transforms/property_types.py
index a1bacd30eee8..3d8d2bc836e2 100644
--- a/posthog/hogql/transforms/property_types.py
+++ b/posthog/hogql/transforms/property_types.py
@@ -559,7 +559,20 @@ class PropertySwapper(CloningVisitor):
def _field_type_to_property_call(self, node: ast.Field, field_type: str):
if field_type == "DateTime":
- return ast.Call(name="toDateTime", args=[node])
+ # Carry the return type so an enclosing toDateTime() resolves its
+ # already-a-datetime overload instead of re-parsing this value
+ # (parseDateTime64BestEffortOrNull only accepts strings). Only
+ # return_type drives overload resolution here; arg_types is an
+ # approximation of the signature and is not re-validated.
+ return ast.Call(
+ name="toDateTime",
+ args=[node],
+ type=ast.CallType(
+ name="toDateTime",
+ arg_types=[ast.StringType(nullable=True)],
+ return_type=ast.DateTimeType(nullable=True),
+ ),
+ )
if field_type == "Float":
return ast.Call(name="toFloat", args=[node])
if field_type == "Boolean":