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): print is not null as a null check on ai columns (#115021)

posthog[bot]3761bf09e7a9
diff --git a/posthog/hogql/printer/clickhouse.py b/posthog/hogql/printer/clickhouse.py
index 1600924c6e2a..18620b2331e0 100644
--- a/posthog/hogql/printer/clickhouse.py
+++ b/posthog/hogql/printer/clickhouse.py
@@ -1054,6 +1054,15 @@ class ClickHousePrinter(BasePrinter):
if isinstance(node.left, ast.Constant) and isinstance(node.right, ast.Constant) and constant_lambda is not None:
return "1" if constant_lambda(node.left.value, node.right.value) else "0"
+ # In ClickHouse `equals(x, NULL)` is always NULL, so `IS [NOT] NULL` must print as a null check even when
+ # the shortcut below skips the ifNull wrapping (for example on the $ai_* bloom-filter columns).
+ if node.op in (ast.CompareOperationOp.Eq, ast.CompareOperationOp.NotEq) and not in_join_constraint:
+ null_check = "isNull" if node.op == ast.CompareOperationOp.Eq else "isNotNull"
+ if isinstance(node.right, ast.Constant) and node.right.value is None:
+ return f"{null_check}({left})"
+ if isinstance(node.left, ast.Constant) and node.left.value is None:
+ return f"{null_check}({right})"
+
# Special cases when we should not add any null checks
if in_join_constraint or not_nullable or in_index_hint:
return op
diff --git a/posthog/hogql/printer/test/test_printer.py b/posthog/hogql/printer/test/test_printer.py
index 63fd13f4814f..dcfe152b3ef5 100644
--- a/posthog/hogql/printer/test/test_printer.py
+++ b/posthog/hogql/printer/test/test_printer.py
@@ -3784,6 +3784,22 @@ class TestPrinter(BaseTest):
self.assertIn(f"notIn({expected_expr}, tuple(%({trace1_param_key})s, %({trace2_param_key})s))", sql)
self.assertNotIn("ifNull(notIn", sql)
+ # IS [NOT] NULL must print a null check, because equals(x, NULL) is always NULL in ClickHouse
+ context = HogQLContext(team_id=self.team.pk, enable_select_queries=True)
+ sql = self._select(
+ "SELECT countIf(properties.$ai_trace_id IS NOT NULL), countIf(properties.$ai_trace_id IS NULL) "
+ "FROM events WHERE properties.$ai_trace_id IS NOT NULL",
+ context,
+ )
+ read_expr = (
+ self._json_dynamic_property_expr("$ai_trace_id")
+ if settings.CLICKHOUSE_HOGQL_USE_NEW_EVENTS_SCHEMA
+ else expected_expr
+ )
+ self.assertIn(f"countIf(isNotNull({read_expr}))", sql)
+ self.assertIn(f"countIf(isNull({read_expr}))", sql)
+ self.assertIn(f"isNotNull({read_expr})) LIMIT", sql)
+
# Dynamic properties use JSON subcolumns under the new schema and JSON extraction under legacy.
# `other_prop` is not in the materialized-column registry, so it stays on the JSON path.
context = HogQLContext(team_id=self.team.pk, enable_select_queries=True)