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(hogql): handle TypeCast in timestamp constant visitors (#61079)

posthog[bot]5e3a61ba0412
diff --git a/posthog/hogql/helpers/test_timestamp_visitor.py b/posthog/hogql/helpers/test_timestamp_visitor.py
index 16dcd2f72dcc..d8a42d025fea 100644
--- a/posthog/hogql/helpers/test_timestamp_visitor.py
+++ b/posthog/hogql/helpers/test_timestamp_visitor.py
@@ -89,7 +89,69 @@ class TestTimestampVisitorSelectSetQuery(unittest.TestCase):
self.assertFalse(helper_fn(node))
+class TestTimestampVisitorTypeCast(unittest.TestCase):
+ """Regression tests for visit_type_cast / visit_try_cast methods.
+
+ Postgres-style `::` casts (and CAST(...)) produce ast.TypeCast nodes that can reach these
+ visitors via the sessions where-clause extractor. Before these methods existed the visitors
+ raised NotImplementedError, crashing query execution.
+ """
+
+ @parameterized.expand(
+ [
+ (
+ "IsSimpleTimestampFieldExpressionVisitor",
+ lambda: IsSimpleTimestampFieldExpressionVisitor(_make_hogql_context(), None),
+ ),
+ ("IsTimeOrIntervalConstantVisitor", lambda: IsTimeOrIntervalConstantVisitor(None)),
+ ("IsStartOfDayConstantVisitor", lambda: IsStartOfDayConstantVisitor(None)),
+ ("IsStartOfHourConstantVisitor", lambda: IsStartOfHourConstantVisitor(None)),
+ ("IsEndOfDayConstantVisitor", lambda: IsEndOfDayConstantVisitor(None)),
+ ("IsEndOfHourConstantVisitor", lambda: IsEndOfHourConstantVisitor(None)),
+ ]
+ )
+ def test_type_cast_does_not_raise_not_implemented(self, _name: str, make_visitor) -> None:
+ type_cast = ast.TypeCast(expr=ast.Constant(value="2024-01-01"), type_name="DateTime")
+ try_cast = ast.TryCast(expr=ast.Constant(value="2024-01-01"), type_name="DateTime")
+ # Should not raise NotImplementedError
+ make_visitor().visit(type_cast)
+ make_visitor().visit(try_cast)
+
+ @parameterized.expand(
+ [
+ (
+ "type_cast_of_timestamp_field",
+ ast.TypeCast(expr=ast.Field(chain=["timestamp"]), type_name="DateTime"),
+ True,
+ ),
+ (
+ "try_cast_of_timestamp_field",
+ ast.TryCast(expr=ast.Field(chain=["timestamp"]), type_name="DateTime"),
+ True,
+ ),
+ ("type_cast_of_constant", ast.TypeCast(expr=ast.Constant(value="2024-01-01"), type_name="DateTime"), False),
+ ("try_cast_of_constant", ast.TryCast(expr=ast.Constant(value="2024-01-01"), type_name="DateTime"), False),
+ ]
+ )
+ def test_cast_preserves_simple_timestamp_field_recognition(
+ self, _name: str, node: ast.Expr, expected: bool
+ ) -> None:
+ # A cast around a timestamp field is still a timestamp field expression; a cast of a constant is not.
+ self.assertEqual(is_simple_timestamp_field_expression(node, _make_hogql_context()), expected)
+
+
class TestIsTimeOrIntervalConstant(unittest.TestCase):
+ @parameterized.expand(
+ [
+ ("type_cast_of_constant", ast.TypeCast(expr=ast.Constant(value="2024-01-01"), type_name="DateTime"), True),
+ ("type_cast_of_field", ast.TypeCast(expr=ast.Field(chain=["timestamp"]), type_name="DateTime"), False),
+ ("try_cast_of_constant", ast.TryCast(expr=ast.Constant(value="2024-01-01"), type_name="DateTime"), True),
+ ("try_cast_of_field", ast.TryCast(expr=ast.Field(chain=["timestamp"]), type_name="DateTime"), False),
+ ]
+ )
+ def test_cast_nodes(self, _name: str, node: ast.Expr, expected: bool) -> None:
+ self.assertEqual(is_time_or_interval_constant(node), expected)
+
def test_constant_returns_true(self) -> None:
self.assertTrue(is_time_or_interval_constant(ast.Constant(value="2024-01-01")))
diff --git a/posthog/hogql/helpers/timestamp_visitor.py b/posthog/hogql/helpers/timestamp_visitor.py
index 4a9b089d38c2..8c4df56406be 100644
--- a/posthog/hogql/helpers/timestamp_visitor.py
+++ b/posthog/hogql/helpers/timestamp_visitor.py
@@ -94,6 +94,13 @@ class IsSimpleTimestampFieldExpressionVisitor(Visitor[bool]):
def visit_between_expr(self, node: ast.BetweenExpr) -> bool:
return False
+ def visit_type_cast(self, node: ast.TypeCast) -> bool:
+ # a cast doesn't change whether the underlying expression is a timestamp field
+ return self.visit(node.expr)
+
+ def visit_try_cast(self, node: ast.TryCast) -> bool:
+ return self.visit(node.expr)
+
def visit_and(self, node: ast.And) -> bool:
return False
@@ -188,6 +195,13 @@ class IsTimeOrIntervalConstantVisitor(Visitor[bool]):
def visit_between_expr(self, node: ast.BetweenExpr) -> bool:
return False
+ def visit_type_cast(self, node: ast.TypeCast) -> bool:
+ # a cast of a constant is still a constant
+ return self.visit(node.expr)
+
+ def visit_try_cast(self, node: ast.TryCast) -> bool:
+ return self.visit(node.expr)
+
def visit_arithmetic_operation(self, node: ast.ArithmeticOperation) -> bool:
return self.visit(node.left) and self.visit(node.right)
@@ -276,6 +290,13 @@ class IsStartOfPeriodConstantVisitor(Visitor[bool], ABC):
def visit_between_expr(self, node: ast.BetweenExpr) -> bool:
return False
+ def visit_type_cast(self, node: ast.TypeCast) -> bool:
+ # a cast doesn't change whether the underlying expression is a start-of-period constant
+ return self.visit(node.expr)
+
+ def visit_try_cast(self, node: ast.TryCast) -> bool:
+ return self.visit(node.expr)
+
def visit_call(self, node: ast.Call) -> bool:
# some functions just return a constant
if node.name in self.constant_fns:
@@ -420,6 +441,13 @@ class IsEndOfPeriodConstantVisitor(Visitor[bool], ABC):
def visit_arithmetic_operation(self, node: ast.ArithmeticOperation) -> bool:
return False
+ def visit_type_cast(self, node: ast.TypeCast) -> bool:
+ # a cast doesn't change whether the underlying expression is an end-of-period constant
+ return self.visit(node.expr)
+
+ def visit_try_cast(self, node: ast.TryCast) -> bool:
+ return self.visit(node.expr)
+
def visit_call(self, node: ast.Call) -> bool:
# there's no toEndOfDay function, so we're just checking the constant itself
if node.name in [