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): declare session $is_bounce nullable (#78341)

George Munyoro15f1adfe2b91
diff --git a/posthog/hogql/database/schema/sessions_v1.py b/posthog/hogql/database/schema/sessions_v1.py
index 990cdff4f9fd..a7745759ad5d 100644
--- a/posthog/hogql/database/schema/sessions_v1.py
+++ b/posthog/hogql/database/schema/sessions_v1.py
@@ -150,6 +150,7 @@ LAZY_SESSIONS_FIELDS: dict[str, FieldOrTable] = {
), # alias of $session_duration, deprecated but included for backwards compatibility
"$is_bounce": BooleanDatabaseField(
name="$is_bounce",
+ nullable=True,
description="True if the session was a bounce (single page view, short duration, no interaction).",
),
# some aliases for people reverting from v2 to v1
diff --git a/posthog/hogql/database/schema/sessions_v2.py b/posthog/hogql/database/schema/sessions_v2.py
index 0999cd92b1b2..f78e21f168a6 100644
--- a/posthog/hogql/database/schema/sessions_v2.py
+++ b/posthog/hogql/database/schema/sessions_v2.py
@@ -136,7 +136,7 @@ LAZY_SESSIONS_FIELDS: dict[str, FieldOrTable] = {
"duration": IntegerDatabaseField(
name="duration"
), # alias of $session_duration, deprecated but included for backwards compatibility
- "$is_bounce": BooleanDatabaseField(name="$is_bounce"),
+ "$is_bounce": BooleanDatabaseField(name="$is_bounce", nullable=True),
"$last_external_click_url": StringDatabaseField(name="$last_external_click_url"),
"$page_screen_autocapture_count_up_to": DatabaseField(name="$$page_screen_autocapture_count_up_to"),
# some aliases for people upgrading from v1 to v2
diff --git a/posthog/hogql/database/schema/sessions_v3.py b/posthog/hogql/database/schema/sessions_v3.py
index b0956e363976..be08cc297c6b 100644
--- a/posthog/hogql/database/schema/sessions_v3.py
+++ b/posthog/hogql/database/schema/sessions_v3.py
@@ -172,6 +172,7 @@ LAZY_SESSIONS_FIELDS: dict[str, FieldOrTable] = {
), # alias of $session_duration, deprecated but included for backwards compatibility
"$is_bounce": BooleanDatabaseField(
name="$is_bounce",
+ nullable=True,
description="True if the session was a bounce (single page view, short duration, no interaction).",
),
"$hosts": StringArrayDatabaseField(name="$hosts", description="Distinct hosts visited during the session."),
diff --git a/posthog/hogql/database/schema/test/test_sessions_v2.py b/posthog/hogql/database/schema/test/test_sessions_v2.py
index bbf8ad97348c..cc8405f264c6 100644
--- a/posthog/hogql/database/schema/test/test_sessions_v2.py
+++ b/posthog/hogql/database/schema/test/test_sessions_v2.py
@@ -1,5 +1,6 @@
import uuid
from time import time_ns
+from typing import cast
import pytest
from posthog.test.base import APIBaseTest, ClickhouseTestMixin, _create_event, _create_person
@@ -15,12 +16,15 @@ from posthog.schema import (
)
from posthog.hogql import ast
+from posthog.hogql.context import HogQLContext
+from posthog.hogql.database.database import Database
from posthog.hogql.database.schema.sessions_v2 import (
get_lazy_session_table_properties_v2,
get_lazy_session_table_values_v2,
)
from posthog.hogql.parser import parse_select
from posthog.hogql.query import execute_hogql_query
+from posthog.hogql.resolver import resolve_types
from posthog.uuidt import uuid7
@@ -753,6 +757,33 @@ class TestSessionsV2(ClickhouseTestMixin, APIBaseTest):
assert response.results == [(2,)]
+ @parameterized.expand(
+ [
+ ("v1", SessionTableVersion.V1),
+ ("v2", SessionTableVersion.V2),
+ ("v3", SessionTableVersion.V3),
+ ]
+ )
+ def test_is_bounce_resolves_as_nullable(self, _name: str, session_table_version: SessionTableVersion):
+ # Every sessions table builds $is_bounce as `if(<no pageviews>, NULL, ...)` so a session with
+ # nothing to bounce on stays out of the average. Declaring the field non-nullable makes the
+ # NULL guards around bounce rate (`coalesce(bounce.bounce_rate, 0)` in the web stats table)
+ # look redundant to type-aware rewrites; dropping them reports NULL instead of 0%.
+ modifiers = HogQLQueryModifiers(sessionTableVersion=session_table_version)
+ context = HogQLContext(
+ team_id=self.team.pk,
+ enable_select_queries=True,
+ database=Database.create_for(team=self.team, modifiers=modifiers),
+ modifiers=modifiers,
+ )
+ node = cast(
+ ast.SelectQuery,
+ resolve_types(parse_select("SELECT `$is_bounce` FROM sessions"), context, dialect="clickhouse"),
+ )
+ column_type = node.select[0].type
+ assert column_type is not None
+ assert column_type.resolve_constant_type(context).nullable is True
+
class TestGetLazySessionProperties(ClickhouseTestMixin, APIBaseTest):
def test_all(self):