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.

feat(conversations): resolve assignee role name in support_tickets SQL (#74246)

Christiaan Hendriksen25b10a01ae3a
diff --git a/posthog/hogql/database/schema/system.py b/posthog/hogql/database/schema/system.py
index 710f73df4d5b..ecc568401125 100644
--- a/posthog/hogql/database/schema/system.py
+++ b/posthog/hogql/database/schema/system.py
@@ -1592,6 +1592,29 @@ ticket_tags_lazy_join: LazyJoin = LazyJoin(
)
+class _TicketAssigneeRolesTable(PostgresTable, DANGEROUS_NoTeamIdCheckTable):
+ """PostgresTable variant for `ee_role`, which is organization-scoped and has no `team_id` column.
+
+ The framework's auto-injected `team_id = X` guard is skipped (the column doesn't exist); isolation
+ instead flows from the predicate, which scopes to the roles referenced by this team's ticket
+ assignments. That resolves through `system._ticket_assignments` and in turn through
+ `system.support_tickets`, whose own team_id guard the framework re-applies to the inner reference.
+ """
+
+ predicates: list[Expr] = [parse_expr("id IN (SELECT role_id FROM system._ticket_assignments)")]
+
+
+ticket_assignee_roles: _TicketAssigneeRolesTable = _TicketAssigneeRolesTable(
+ name="_ticket_assignee_roles",
+ postgres_table_name="ee_role",
+ description="Internal table (PostgreSQL `ee_role`) of roles this team's tickets are assigned to; not for direct querying — use `system.support_tickets.assignee`.",
+ fields={
+ "id": UUIDDatabaseField(name="id", description="Role UUID."),
+ "name": StringDatabaseField(name="name", description="Role name, e.g. 'Team Support'."),
+ },
+)
+
+
ticket_assignments: _TicketScopedPostgresTable = _TicketScopedPostgresTable(
name="_ticket_assignments",
postgres_table_name="posthog_conversations_ticket_assignment",
@@ -1615,10 +1638,12 @@ def _ticket_assignment_select() -> ast.SelectQuery | ast.SelectSetQuery:
return parse_select(
"""
SELECT
- ticket_id AS ticket_id,
- user_id AS user_id,
- role_id AS role_id
- FROM system._ticket_assignments
+ ta.ticket_id AS ticket_id,
+ ta.user_id AS user_id,
+ ta.role_id AS role_id,
+ nullIf(r.name, '') AS role_name
+ FROM system._ticket_assignments AS ta
+ LEFT JOIN system._ticket_assignee_roles AS r ON r.id = assumeNotNull(ta.role_id)
"""
)
@@ -1635,6 +1660,11 @@ class _TicketAssignmentTable(LazyTable):
"role_id": UUIDDatabaseField(
name="role_id", nullable=True, description="Role the ticket is assigned to, if assigned to a role."
),
+ "role_name": StringDatabaseField(
+ name="role_name",
+ nullable=True,
+ description="Name of the role the ticket is assigned to, e.g. 'Team Support'. Null when unassigned or assigned to a user.",
+ ),
}
def lazy_select(
@@ -2286,6 +2316,7 @@ class SystemTables(TableNode):
"source_sync_jobs": TableNode(name="source_sync_jobs", table=source_sync_jobs),
"_ticket_tagged_items": TableNode(name="_ticket_tagged_items", table=ticket_tagged_items, hidden=True),
"_ticket_assignments": TableNode(name="_ticket_assignments", table=ticket_assignments, hidden=True),
+ "_ticket_assignee_roles": TableNode(name="_ticket_assignee_roles", table=ticket_assignee_roles, hidden=True),
"support_tickets": TableNode(name="support_tickets", table=support_tickets),
"surveys": TableNode(name="surveys", table=surveys),
"task_runs": TableNode(name="task_runs", table=task_runs),
diff --git a/posthog/hogql/database/schema/test/test_system_tables.py b/posthog/hogql/database/schema/test/test_system_tables.py
index 58e9bcd20dc2..f7d62bfa7b77 100644
--- a/posthog/hogql/database/schema/test/test_system_tables.py
+++ b/posthog/hogql/database/schema/test/test_system_tables.py
@@ -108,6 +108,8 @@ TEAM_ID_FILTER_PATTERNS = {
# Same shape, scoped through system.support_tickets instead
"_ticket_tagged_items": "system__support_tickets.team_id",
"_ticket_assignments": "system__support_tickets.team_id",
+ # Roles scope one level deeper, through the assignments that reference them
+ "_ticket_assignee_roles": "system__support_tickets.team_id",
}
@@ -146,6 +148,8 @@ class TestSystemTablesTeamScoping(BaseTest):
"_ticket_tagged_items",
# Hidden table backing system.support_tickets.assignee; covered by TestSystemTicketAssignmentLazyJoin.
"_ticket_assignments",
+ # Hidden table backing the assignee role_name resolution; covered by TestSystemTicketAssignmentLazyJoin.
+ "_ticket_assignee_roles",
# information_schema is a namespace of virtual catalog tables (tables/columns/
# relationships/data_types) computed per-query from the caller's own Database object,
# so it has no team_id column to isolate; behaviour is covered by TestInformationSchema.
@@ -1002,6 +1006,31 @@ class TestSystemTicketAssignmentLazyJoin(NonAtomicBaseTest):
assert response.results == [(None, None)]
+ def test_assignee_role_name_resolves_for_role_assignee(self):
+ self.role = Role.objects.create(name="Team Support", organization=self.organization)
+ ticket = _create_support_ticket(self.team, "role")
+ TicketAssignment.objects.create(ticket=ticket, role=self.role)
+
+ response = execute_hogql_query(
+ f"SELECT assignee.role_name FROM system.support_tickets WHERE id = '{ticket.id}'",
+ team=self.team,
+ user=self.user,
+ )
+
+ assert response.results[0][0] == "Team Support"
+
+ def test_assignee_role_name_null_for_user_assignee(self):
+ ticket = _create_support_ticket(self.team, "user")
+ TicketAssignment.objects.create(ticket=ticket, user=self.user)
+
+ response = execute_hogql_query(
+ f"SELECT assignee.role_name FROM system.support_tickets WHERE id = '{ticket.id}'",
+ team=self.team,
+ user=self.user,
+ )
+
+ assert response.results[0][0] is None
+
def test_assignee_lazy_join_isolated_per_team(self):
other_ticket = _create_support_ticket(self.other_team, "theirs")
TicketAssignment.objects.create(ticket=other_ticket, user=self.user)
diff --git a/posthog/hogql/database/test/__snapshots__/test_database.ambr b/posthog/hogql/database/test/__snapshots__/test_database.ambr
index 35a63323cd34..9bfd649257ad 100644
--- a/posthog/hogql/database/test/__snapshots__/test_database.ambr
+++ b/posthog/hogql/database/test/__snapshots__/test_database.ambr
@@ -8825,7 +8825,8 @@
"fields": [
"ticket_id",
"user_id",
- "role_id"
+ "role_id",
+ "role_name"
],
"hogql_value": "assignee",
"id": "assignee",
@@ -17715,7 +17716,8 @@
"fields": [
"ticket_id",
"user_id",
- "role_id"
+ "role_id",
+ "role_name"
],
"hogql_value": "assignee",
"id": "assignee",