: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(activity-logs): stop system.activity_logs failing on numeric item ids (#92270)
Yasen680666872f08
diff --git a/posthog/hogql/database/schema/activity_log_visibility.py b/posthog/hogql/database/schema/activity_log_visibility.pyindex bfd7653ce65c..e83e4923dc1f 100644--- a/posthog/hogql/database/schema/activity_log_visibility.py+++ b/posthog/hogql/database/schema/activity_log_visibility.py@@ -19,8 +19,13 @@ from posthog.hogql.parser import parse_exprCANVASES_TABLE = "system.canvases"_LOOP_ROWS_HIDDEN = "NOT (scope = 'Loop')"_CANVAS_ROWS_HIDDEN = "NOT (scope = 'Canvas')"+# `toString` on the canvas id is load-bearing. `item_id` is a String holding the id of whatever object+# the row is about, and most of those are numeric (`11510926` for an insight), while a canvas id is a+# real UUID once the federated read types it. ClickHouse coerces the left side of an IN to the set's+# type, and it does that for every row rather than only the Canvas ones, so an unconverted set makes+# any read of this table die on the first numeric id: "Cannot parse uuid 11510926"._CANVAS_ROWS_LIMITED_TO_READABLE_CANVASES = (- f"NOT (scope = 'Canvas' AND item_id NOT IN (SELECT id FROM {CANVASES_TABLE}))"+ f"NOT (scope = 'Canvas' AND item_id NOT IN (SELECT toString(id) FROM {CANVASES_TABLE}))")# Bumped whenever the compilation shape changes in a way the hashed inputs below cannot see, e.g. a rulediff --git a/posthog/hogql/database/schema/test/test_system_tables.py b/posthog/hogql/database/schema/test/test_system_tables.pyindex b791d78c5ecd..95a3f46cdb0c 100644--- a/posthog/hogql/database/schema/test/test_system_tables.py+++ b/posthog/hogql/database/schema/test/test_system_tables.py@@ -1095,6 +1095,36 @@ class TestSystemTablesCanvasDeletedExclusionIsolation(NonAtomicBaseTest):assert str(deleted_canvas.pk) not in ids+class TestSystemTablesActivityLogsCanvasIdCoercion(NonAtomicBaseTest):+ """The Canvas visibility rule compares `item_id` against canvas ids, which are UUIDs."""++ CLASS_DATA_LEVEL_SETUP = False++ def test_numeric_item_id_readable_while_canvases_exist(self):+ # One canvas is enough to make the rule's id set non-empty and UUID-typed. `item_id` is a+ # String holding whatever object the row is about, and most of those ids are numeric, so+ # ClickHouse coerced every row's item_id to UUID and the whole table failed to read.+ with team_scope(self.team.pk):+ channel = Channel.objects.create(team=self.team, name="activity-log-canvas-channel")+ Canvas.objects.create(team=self.team, channel=channel, name="live")+ ActivityLog.objects.create(+ team_id=self.team.pk,+ organization_id=self.organization.id,+ activity="created",+ scope="Insight",+ item_id="11510926",+ detail={},+ )++ response = execute_hogql_query(+ "SELECT item_id FROM system.activity_logs WHERE item_id = '11510926'",+ team=self.team,+ user=self.user,+ )++ assert [row[0] for row in response.results] == ["11510926"]++class TestSystemTablesTaskInternalExclusion(BaseTest):"""Verify the tasks system table excludes internal tasks (signals pipeline, etc.)mirroring the REST API's default filter."""diff --git a/posthog/hogql/database/test/test_activity_logs_restrictions.py b/posthog/hogql/database/test/test_activity_logs_restrictions.pyindex 84dcea6a30a8..81969af28a86 100644--- a/posthog/hogql/database/test/test_activity_logs_restrictions.py+++ b/posthog/hogql/database/test/test_activity_logs_restrictions.py@@ -137,7 +137,9 @@ class TestActivityLogsSqlRestrictions(BaseTest):hogql, _ = self._print(dialect="hogql")- self.assertIn("NOT(and(equals(scope, 'Canvas'), notIn(item_id, (SELECT id FROM canvases", hogql)+ # toString keeps the set String-typed. Canvas ids are UUIDs, item_id is a String holding+ # mostly numeric ids, and ClickHouse coerces item_id to the set's type for every row.+ self.assertIn("NOT(and(equals(scope, 'Canvas'), notIn(item_id, (SELECT toString(id) FROM canvases", hogql)# The canvases subquery keeps that table's own guards, so its access control carries over.self.assertIn("_task_public_channels", hogql)