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(hogql): add system.event_definitions and system.property_definitions (#124827)

Thomas Obermüller5c9c49a4eb6f
diff --git a/posthog/hogql/database/lazy_join_registry.py b/posthog/hogql/database/lazy_join_registry.py
index d9c41970775e..14f613222868 100644
--- a/posthog/hogql/database/lazy_join_registry.py
+++ b/posthog/hogql/database/lazy_join_registry.py
@@ -26,7 +26,7 @@ from posthog.hogql.database.schema.session_replay_events import (
from posthog.hogql.database.schema.sessions_v1 import join_events_table_to_sessions_table
from posthog.hogql.database.schema.sessions_v2 import join_events_table_to_sessions_table_v2
from posthog.hogql.database.schema.sessions_v3 import join_events_table_to_sessions_table_v3
-from posthog.hogql.database.schema.system import ticket_assignment_join, ticket_tags_join
+from posthog.hogql.database.schema.system import definition_tags_join, ticket_assignment_join, ticket_tags_join
from posthog.hogql.database.warehouse_join_resolvers import (
resolve_data_warehouse_experiments_join,
resolve_data_warehouse_join,
@@ -85,6 +85,7 @@ RESOLVERS: dict[str, LazyJoinResolver] = {
tags.ERROR_TRACKING_ISSUE_FINGERPRINT_OVERRIDES: join_with_error_tracking_issue_fingerprint_overrides_table,
tags.ERROR_TRACKING_FINGERPRINT_ISSUE_STATE: join_with_error_tracking_fingerprint_issue_state_table,
tags.TICKET_TAGS: ticket_tags_join,
+ tags.DEFINITION_TAGS: definition_tags_join,
tags.TICKET_ASSIGNMENT: ticket_assignment_join,
tags.ACCOUNT_TAGS: account_tags_join,
tags.ACCOUNT_NOTEBOOKS: account_notebooks_join,
diff --git a/posthog/hogql/database/lazy_join_tags.py b/posthog/hogql/database/lazy_join_tags.py
index 24f72553fbf8..5ad9ec7ce329 100644
--- a/posthog/hogql/database/lazy_join_tags.py
+++ b/posthog/hogql/database/lazy_join_tags.py
@@ -25,6 +25,7 @@ METRICS_TO_METRIC_SERIES = "metrics_to_metric_series"
ERROR_TRACKING_ISSUE_FINGERPRINT_OVERRIDES = "error_tracking_issue_fingerprint_overrides"
ERROR_TRACKING_FINGERPRINT_ISSUE_STATE = "error_tracking_fingerprint_issue_state"
TICKET_TAGS = "ticket_tags"
+DEFINITION_TAGS = "definition_tags"
TICKET_ASSIGNMENT = "ticket_assignment"
ACCOUNT_TAGS = "account_tags"
ACCOUNT_NOTEBOOKS = "account_notebooks"
diff --git a/posthog/hogql/database/postgres_table.py b/posthog/hogql/database/postgres_table.py
index 6037bcfbe00f..61d87fa97236 100644
--- a/posthog/hogql/database/postgres_table.py
+++ b/posthog/hogql/database/postgres_table.py
@@ -130,6 +130,10 @@ class PostgresTable(FunctionCallTable):
# read next to the team guard (see ClickHousePrinter._print_table_ref), so it prunes in Postgres
# rather than after the rows have been copied out. None means the table has no retention window.
retention_field: Optional[str] = None
+ # Keep the team guard next to the table function even when the table has predicates. A table that
+ # is too large to read in full needs this: as the right side of a LEFT JOIN its guard sits in the
+ # ON clause, which ClickHouse does not send to Postgres. The predicates then run in ClickHouse.
+ push_team_guard_with_predicates: bool = False
postgres_pushdown_values: dict[str, str | int | bool] = {}
predicates: list[Expr] = []
diff --git a/posthog/hogql/database/schema/system.py b/posthog/hogql/database/schema/system.py
index 9d78e65959b4..9ff43f5ce4c4 100644
--- a/posthog/hogql/database/schema/system.py
+++ b/posthog/hogql/database/schema/system.py
@@ -5,7 +5,7 @@ from typing import TYPE_CHECKING, Optional
from posthog.hogql import ast
from posthog.hogql.base import Expr
from posthog.hogql.context import HogQLContext
-from posthog.hogql.database.lazy_join_tags import TICKET_ASSIGNMENT, TICKET_TAGS
+from posthog.hogql.database.lazy_join_tags import DEFINITION_TAGS, TICKET_ASSIGNMENT, TICKET_TAGS
from posthog.hogql.database.models import (
BooleanDatabaseField,
DANGEROUS_NoTeamIdCheckTable,
@@ -3832,6 +3832,217 @@ inbox_report_checks: PostgresTable = PostgresTable(
)
+def _definition_tagged_items(tagged_model: str, definitions_table: str) -> TaggedItemsTable:
+ return TaggedItemsTable(
+ tagged_model=tagged_model,
+ # Built as AST: this runs at import time, and placeholder parsing would pull posthog.schema into django.setup().
+ predicates=[
+ ast.CompareOperation(
+ op=ast.CompareOperationOp.In,
+ left=ast.Field(chain=["definition_id"]),
+ right=ast.SelectQuery(
+ select=[ast.Field(chain=["id"])],
+ select_from=ast.JoinExpr(table=ast.Field(chain=["system", definitions_table])),
+ ),
+ )
+ ],
+ name=f"_{tagged_model}_tagged_items",
+ postgres_table_name="posthog_taggeditem",
+ description=f"Internal junction table (PostgreSQL `posthog_taggeditem`) of tag-to-{tagged_model} links; not for direct querying — use `system.{definitions_table}.tags`.",
+ fields={
+ "id": UUIDDatabaseField(name="id", description="Primary key of the tagged-item junction row."),
+ "tag_id": UUIDDatabaseField(
+ name="tag_id", description="Tag applied to the definition; join to `system.tags.id`."
+ ),
+ "definition_id": UUIDDatabaseField(
+ name="object_uuid",
+ nullable=True,
+ description=f"Definition the tag is applied to; join to `system.{definitions_table}.id`.",
+ ),
+ "content_type_id": IntegerDatabaseField(
+ name="content_type_id",
+ hidden=True,
+ description=f"Kind of object the tag is applied to; the table only returns {tagged_model} rows.",
+ ),
+ "team_id": IntegerDatabaseField(name="team_id", hidden=True),
+ },
+ )
+
+
+event_definition_tagged_items: TaggedItemsTable = _definition_tagged_items("event_definition", "event_definitions")
+property_definition_tagged_items: TaggedItemsTable = _definition_tagged_items(
+ "property_definition", "property_definitions"
+)
+
+
+def _definition_tags_select(tagged_items_table: str) -> ast.SelectQuery | ast.SelectSetQuery:
+ return parse_select(
+ """
+ SELECT
+ ti.definition_id AS definition_id,
+ arraySort(arrayDistinct(groupArray(t.name))) AS names
+ FROM {tagged_items} AS ti
+ INNER JOIN system.tags AS t ON t.id = ti.tag_id
+ GROUP BY ti.definition_id
+ """,
+ {"tagged_items": ast.Field(chain=["system", tagged_items_table])},
+ )
+
+
+class _DefinitionTagsTable(LazyTable):
+ tagged_items_table: str
+ description: str = "Internal aggregating table backing the `tags` column of the definition tables: the distinct, sorted tag names per definition."
+ fields: dict[str, FieldOrTable] = {
+ "definition_id": UUIDDatabaseField(name="definition_id", description="Definition these tags belong to."),
+ "names": StringArrayDatabaseField(
+ name="names", description="Distinct, sorted tag names applied to the definition."
+ ),
+ }
+
+ def lazy_select(
+ self, table_to_add: LazyTableToAdd, context: HogQLContext, node: ast.SelectQuery
+ ) -> ast.SelectQuery | ast.SelectSetQuery:
+ return _definition_tags_select(self.tagged_items_table)
+
+ def to_printed_clickhouse(self, context: HogQLContext) -> str:
+ return "definition_tags"
+
+ def to_printed_hogql(self) -> str:
+ return "definition_tags"
+
+
+def definition_tags_join(join_to_add: LazyJoinToAdd, context: HogQLContext, node: ast.SelectQuery) -> ast.JoinExpr:
+ if not join_to_add.fields_accessed:
+ raise ResolutionError(f"No fields requested from `{join_to_add.to_table}`")
+ return ast.JoinExpr(
+ alias=join_to_add.to_table,
+ table=_definition_tags_select(join_to_add.lazy_join.resolver_params["tagged_items_table"]),
+ join_type="LEFT JOIN",
+ constraint=ast.JoinConstraint(
+ constraint_type="ON",
+ expr=ast.CompareOperation(
+ op=ast.CompareOperationOp.Eq,
+ left=ast.Field(chain=[join_to_add.from_table, "id"]),
+ right=ast.Field(chain=[join_to_add.to_table, "definition_id"]),
+ ),
+ ),
+ )
+
+
+def _definition_tags_lazy_join(tagged_items_table: TaggedItemsTable) -> LazyJoin:
+ return LazyJoin(
+ from_field=["id"],
+ join_table=_DefinitionTagsTable(tagged_items_table=tagged_items_table.name),
+ resolver=DEFINITION_TAGS,
+ resolver_params={"tagged_items_table": tagged_items_table.name},
+ )
+
+
+event_definitions: PostgresTable = PostgresTable(
+ name="event_definitions",
+ postgres_table_name="posthog_eventdefinition",
+ access_scope="event_definition",
+ description="Events known to the project, from ingestion or defined ahead of time; one row per event name.",
+ fields={
+ "id": UUIDDatabaseField(name="id", description="Event definition UUID."),
+ "team_id": IntegerDatabaseField(name="team_id"),
+ "name": StringDatabaseField(name="name", description="Event name, e.g. '$pageview' or 'signed_up'."),
+ "created_at": DateTimeDatabaseField(
+ name="created_at",
+ nullable=True,
+ description="When the event was first seen or defined.",
+ ),
+ "last_seen_at": DateTimeDatabaseField(
+ name="last_seen_at",
+ nullable=True,
+ description="When the event was last ingested; NULL if it has not been seen since this was tracked.",
+ ),
+ "tags": _definition_tags_lazy_join(event_definition_tagged_items),
+ },
+)
+
+
+_PROPERTY_DEFINITION_TYPES: dict[int, str] = {1: "event", 2: "person", 3: "group", 4: "session"}
+
+
+class _PropertyDefinitionsTable(PostgresTable):
+ """Hides the properties that property-level access control denies the caller, as the REST list does.
+
+ The restrictions are loaded into the context before printing, so the predicate is built per query.
+ """
+
+ def get_predicates(self, context: Optional[HogQLContext] = None) -> list[Expr]:
+ restricted = context.restricted_properties if context is not None else None
+ if not restricted:
+ return []
+ matches: list[Expr] = []
+ for prop in sorted(restricted, key=lambda p: (p.property_type, p.group_type_index or -1, p.name)):
+ conditions: list[Expr] = [
+ parse_expr("name = {name}", {"name": ast.Constant(value=prop.name)}),
+ parse_expr("_type = {type}", {"type": ast.Constant(value=prop.property_type)}),
+ ]
+ if prop.group_type_index is not None:
+ conditions.append(
+ parse_expr("group_type_index = {index}", {"index": ast.Constant(value=prop.group_type_index)})
+ )
+ matches.append(ast.And(exprs=conditions))
+ return [ast.Not(expr=ast.Or(exprs=matches))]
+
+
+property_definitions: _PropertyDefinitionsTable = _PropertyDefinitionsTable(
+ name="property_definitions",
+ postgres_table_name="posthog_propertydefinition",
+ access_scope="property_definition",
+ push_team_guard_with_predicates=True,
+ description="Properties known to the project for events, persons, groups, and sessions; one row per property name and type.",
+ fields={
+ "id": UUIDDatabaseField(name="id", description="Property definition UUID."),
+ "team_id": IntegerDatabaseField(name="team_id"),
+ "name": StringDatabaseField(name="name", description="Property name, e.g. '$browser' or 'plan'."),
+ "_type": IntegerDatabaseField(name="type", hidden=True),
+ "type": ExpressionField(
+ name="type",
+ expr=ast.Call(
+ name="multiIf",
+ args=[
+ *(
+ arg
+ for value, label in _PROPERTY_DEFINITION_TYPES.items()
+ for arg in (
+ ast.CompareOperation(
+ op=ast.CompareOperationOp.Eq,
+ left=ast.Field(chain=["_type"]),
+ right=ast.Constant(value=value),
+ ),
+ ast.Constant(value=label),
+ )
+ ),
+ ast.Constant(value=None),
+ ],
+ ),
+ description="What the property describes: 'event', 'person', 'group', or 'session'.",
+ ),
+ "group_type_index": IntegerDatabaseField(
+ name="group_type_index",
+ nullable=True,
+ description="Group type index (0-4) for group properties; join to `system.group_type_mappings`. NULL for other types.",
+ ),
+ "property_type": StringDatabaseField(
+ name="property_type",
+ nullable=True,
+ description="Detected value type: 'String', 'Numeric', 'Boolean', 'DateTime', or 'Duration'. NULL if not detected yet.",
+ ),
+ "_is_numerical": BooleanDatabaseField(name="is_numerical", hidden=True),
+ "is_numerical": ExpressionField(
+ name="is_numerical",
+ expr=ast.Call(name="toInt", args=[ast.Field(chain=["_is_numerical"])]),
+ description="1 if the property's values can be used in numeric aggregations, 0 otherwise.",
+ ),
+ "tags": _definition_tags_lazy_join(property_definition_tagged_items),
+ },
+)
+
+
tags: PostgresTable = PostgresTable(
name="tags",
postgres_table_name="posthog_tag",
@@ -3901,6 +4112,10 @@ class SystemTables(TableNode):
"data_modeling_endpoints": TableNode(name="data_modeling_endpoints", table=endpoints),
"data_warehouse_sources": TableNode(name="data_warehouse_sources", table=data_warehouse_sources),
"data_warehouse_tables": TableNode(name="data_warehouse_tables", table=data_warehouse_tables),
+ "event_definitions": TableNode(name="event_definitions", table=event_definitions),
+ "_event_definition_tagged_items": TableNode(
+ name="_event_definition_tagged_items", table=event_definition_tagged_items, hidden=True
+ ),
"error_tracking_assignment_rules": TableNode(
name="error_tracking_assignment_rules", table=error_tracking_assignment_rules
),
@@ -3971,6 +4186,10 @@ class SystemTables(TableNode):
"review_queues": TableNode(name="review_queues", table=review_queues),
"score_definitions": TableNode(name="score_definitions", table=score_definitions),
"session_recording_playlists": TableNode(name="session_recording_playlists", table=session_recording_playlists),
+ "property_definitions": TableNode(name="property_definitions", table=property_definitions),
+ "_property_definition_tagged_items": TableNode(
+ name="_property_definition_tagged_items", table=property_definition_tagged_items, hidden=True
+ ),
"replay_scanners": TableNode(name="replay_scanners", table=replay_scanners),
"revenue_analytics_config": TableNode(name="revenue_analytics_config", table=revenue_analytics_config),
"scout_configs": TableNode(name="scout_configs", table=scout_configs),
diff --git a/posthog/hogql/database/schema/tagged_items.py b/posthog/hogql/database/schema/tagged_items.py
index b62a5e104b54..fa6eceea6032 100644
--- a/posthog/hogql/database/schema/tagged_items.py
+++ b/posthog/hogql/database/schema/tagged_items.py
@@ -11,10 +11,12 @@ from posthog.hogql.parser import parse_expr
class TaggedItemsTable(PostgresTable, DANGEROUS_NoTeamIdCheckTable):
"""`posthog_taggeditem` rows that tag one kind of object, read through the generic pointer.
- The table has no `team_id`, so each instance passes a predicate that scopes the object id
- through a team-guarded system table. The object id lives in `object_uuid` or `object_id`,
- which other models share, so rows are also limited to the content type of `tagged_model`.
- Content type ids differ per deployment, so the id is resolved when the query is built.
+ The framework adds no team guard here, so each instance passes a predicate that scopes the object
+ id through a team-guarded system table. That predicate runs in ClickHouse, so an instance can also
+ declare the row's own `team_id` field: equality on it reaches the Postgres read and keeps other
+ teams' rows out of it. The object id lives in `object_uuid` or `object_id`, which other models share,
+ so rows are also limited to the content type of `tagged_model`. Content type ids differ per
+ deployment, so the id is resolved when the query is built.
"""
tagged_model: str
@@ -25,7 +27,10 @@ class TaggedItemsTable(PostgresTable, DANGEROUS_NoTeamIdCheckTable):
from posthog.models.tagged_item_registry import content_type_id_for_legacy_field # noqa: PLC0415
content_type_id = content_type_id_for_legacy_field(self.tagged_model)
- return [
+ predicates = [
*super().get_predicates(context),
parse_expr("content_type_id = {content_type_id}", {"content_type_id": ast.Constant(value=content_type_id)}),
]
+ if "team_id" in self.fields and context is not None and context.team_id is not None:
+ predicates.append(parse_expr("team_id = {team_id}", {"team_id": ast.Constant(value=context.team_id)}))
+ return predicates
diff --git a/posthog/hogql/database/schema/test/test_system_tables.py b/posthog/hogql/database/schema/test/test_system_tables.py
index 2b7a56373b6d..7752b5d50759 100644
--- a/posthog/hogql/database/schema/test/test_system_tables.py
+++ b/posthog/hogql/database/schema/test/test_system_tables.py
@@ -1,5 +1,6 @@
import json
import uuid
+from datetime import UTC, datetime
from types import SimpleNamespace
import time_machine
@@ -18,6 +19,7 @@ from posthog.hogql.parser import parse_select
from posthog.hogql.printer import prepare_and_print_ast
from posthog.hogql.query import execute_hogql_query
+from posthog.constants import AvailableFeature
from posthog.models import (
DataDeletionRequest,
Group,
@@ -37,6 +39,8 @@ from posthog.models.team.team_revenue_analytics_config import TeamRevenueAnalyti
from posthog.persons_db import persons_db_connection
from posthog.persons_seed import insert_seed_group, insert_seed_group_type_mapping
+from products.access_control.backend.facade.contracts import PropertyAccessLevel
+from products.access_control.backend.models.property_access_control import PropertyAccessControl
from products.access_control.backend.models.role import Role
from products.actions.backend.models.action import Action
from products.aeo.backend.facade.testing import create_citation_check
@@ -94,6 +98,7 @@ from products.error_tracking.backend.facade.testing import (
create_suppression_rule,
create_symbol_set,
)
+from products.event_definitions.backend.models import EventDefinition, PropertyDefinition
from products.experiments.backend.facade.testing import create_experiment
from products.exports.backend.models.exported_asset import ExportedAsset
from products.feature_flags.backend.models.feature_flag import FeatureFlag
@@ -148,6 +153,8 @@ TEAM_ID_FILTER_PATTERNS = {
"batch_export_runs": "system__batch_exports.team_id",
# Same shape, scoped through system.support_tickets instead
"_ticket_tagged_items": "system__support_tickets.team_id",
+ "_event_definition_tagged_items": "system___event_definition_tagged_items.team_id",
+ "_property_definition_tagged_items": "system___property_definition_tagged_items.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",
@@ -191,6 +198,9 @@ class TestSystemTablesTeamScoping(BaseTest):
"_account_custom_property_values_history",
# Hidden junction backing system.support_tickets.tags; covered by TestSystemTicketTagsLazyJoin.
"_ticket_tagged_items",
+ # Hidden junctions backing the definition tables' tags; covered by TestSystemDefinitionTables.
+ "_event_definition_tagged_items",
+ "_property_definition_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.
@@ -932,6 +942,14 @@ def _create_sandbox_environment(team: Team, label: str) -> SandboxEnvironment:
return SandboxEnvironment.objects.create(team=team, name=f"env_{label}", private=False)
+def _create_event_definition(team: Team, label: str) -> EventDefinition:
+ return EventDefinition.objects.create(team=team, project_id=team.project_id, name=f"event_{label}")
+
+
+def _create_property_definition(team: Team, label: str) -> PropertyDefinition:
+ return PropertyDefinition.objects.create(team=team, project_id=team.project_id, name=f"property_{label}")
+
+
def _create_tag(team: Team, label: str) -> Tag:
return Tag.objects.create(team=team, name=f"tag_{label}")
@@ -1131,6 +1149,7 @@ SYSTEM_TABLE_FACTORIES = [
("error_tracking_symbol_sets", _create_error_tracking_symbol_set),
("error_tracking_suppression_rules", _create_error_tracking_suppression_rule),
("evaluation_directories", _create_evaluation_directory),
+ ("event_definitions", _create_event_definition),
("evaluations", _create_evaluation),
("experiments", _create_experiment),
("exports", _create_export),
@@ -1158,6 +1177,7 @@ SYSTEM_TABLE_FACTORIES = [
("inbox_reports", _create_inbox_report),
("integration_repository_cache", _create_integration_repository_cache_entry),
("logs_alerts", _create_logs_alert),
+ ("property_definitions", _create_property_definition),
("replay_scanners", _create_replay_scanner),
("revenue_analytics_config", _create_revenue_analytics_config),
("logs_views", _create_logs_view),
@@ -1825,6 +1845,112 @@ class TestSystemTicketAssignmentLazyJoin(NonAtomicBaseTest):
assert response.results == []
+class TestSystemDefinitionTables(NonAtomicBaseTest):
+ CLASS_DATA_LEVEL_SETUP = False
+
+ def setUp(self):
+ super().setUp()
+ other_org = Organization.objects.create(name="other_org")
+ other_project = Project.objects.create(id=Team.objects.increment_id_sequence(), organization=other_org)
+ self.other_team = Team.objects.create(id=other_project.id, project=other_project, organization=other_org)
+
+ def test_event_definition_columns(self):
+ EventDefinition.objects.create(
+ team=self.team,
+ project_id=self.team.project_id,
+ name="signed_up",
+ created_at=datetime(2026, 1, 2, 3, 4, 5, tzinfo=UTC),
+ last_seen_at=datetime(2026, 2, 3, 4, 5, 6, tzinfo=UTC),
+ )
+
+ response = execute_hogql_query(
+ "SELECT name, created_at, last_seen_at FROM system.event_definitions", team=self.team, user=self.user
+ )
+
+ assert response.results == [
+ ("signed_up", datetime(2026, 1, 2, 3, 4, 5, tzinfo=UTC), datetime(2026, 2, 3, 4, 5, 6, tzinfo=UTC))
+ ]
+
+ def test_property_definition_columns(self):
+ for name, prop_type, group_type_index, property_type, is_numerical in [
+ ("amount", PropertyDefinition.Type.EVENT, None, "Numeric", True),
+ ("email", PropertyDefinition.Type.PERSON, None, "String", False),
+ ("tier", PropertyDefinition.Type.GROUP, 1, None, False),
+ ("$session_duration", PropertyDefinition.Type.SESSION, None, "Duration", True),
+ ]:
+ PropertyDefinition.objects.create(
+ team=self.team,
+ project_id=self.team.project_id,
+ name=name,
+ type=prop_type,
+ group_type_index=group_type_index,
+ property_type=property_type,
+ is_numerical=is_numerical,
+ )
+
+ response = execute_hogql_query(
+ "SELECT name, type, group_type_index, property_type, is_numerical FROM system.property_definitions",
+ team=self.team,
+ user=self.user,
+ )
+
+ assert sorted(response.results) == [
+ ("$session_duration", "session", None, "Duration", 1),
+ ("amount", "event", None, "Numeric", 1),
+ ("email", "person", None, "String", 0),
+ ("tier", "group", 1, None, 0),
+ ]
+
+ @parameterized.expand(
+ [("event_definitions", _create_event_definition), ("property_definitions", _create_property_definition)]
+ )
+ def test_tags_lazy_join_returns_own_tag_names(self, table_name, factory):
+ tagged = factory(self.team, "tagged")
+ tagged.tagged_items.create(tag=Tag.objects.create(name="pii", team=self.team))
+ tagged.tagged_items.create(tag=Tag.objects.create(name="billing", team=self.team))
+ untagged = factory(self.team, "untagged")
+ theirs = factory(self.other_team, "theirs")
+ theirs.tagged_items.create(tag=Tag.objects.create(name="pii", team=self.other_team))
+
+ response = execute_hogql_query(
+ f"SELECT id, tags.names FROM system.{table_name}", team=self.team, user=self.user
+ )
+
+ assert {str(row[0]): row[1] for row in response.results} == {
+ str(tagged.pk): ["billing", "pii"],
+ str(untagged.pk): [],
+ }
+
+ def test_property_definitions_hide_restricted_properties(self):
+ self.organization.available_product_features = [
+ {"name": AvailableFeature.PROPERTY_ACCESS_CONTROL, "key": AvailableFeature.PROPERTY_ACCESS_CONTROL}
+ ]
+ self.organization.save(update_fields=["available_product_features"])
+ for name, prop_type, group_type_index, restricted in [
+ ("plan", PropertyDefinition.Type.EVENT, None, True),
+ ("plan", PropertyDefinition.Type.PERSON, None, False),
+ ("tier", PropertyDefinition.Type.GROUP, 1, True),
+ ("tier", PropertyDefinition.Type.GROUP, 0, False),
+ ]:
+ definition = PropertyDefinition.objects.create(
+ team=self.team,
+ project_id=self.team.project_id,
+ name=name,
+ type=prop_type,
+ group_type_index=group_type_index,
+ )
+ if restricted:
+ PropertyAccessControl.objects.create(
+ team=self.team, property_definition=definition, access_level=PropertyAccessLevel.NONE.value
+ )
+
+ response = execute_hogql_query(
+ "SELECT name, type, group_type_index FROM system.property_definitions", team=self.team, user=self.user
+ )
+
+ assert sorted(response.results, key=str) == [("plan", "person", None), ("tier", "group", 0)]
+
+
class TestSystemAccountsLazyJoins(NonAtomicBaseTest):
"""Verify the `accounts.tags.names` and `accounts.notebooks.count` lazy joins."""
diff --git a/posthog/hogql/database/test/__snapshots__/test_database.ambr b/posthog/hogql/database/test/__snapshots__/test_database.ambr
index 27301e946364..7d80b3d4bc6c 100644
--- a/posthog/hogql/database/test/__snapshots__/test_database.ambr
+++ b/posthog/hogql/database/test/__snapshots__/test_database.ambr
@@ -6038,6 +6038,68 @@
"row_count": null,
"type": "system"
},
+ "system.event_definitions": {
+ "certification": null,
+ "fields": {
+ "id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "id",
+ "id": null,
+ "name": "id",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "name": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "name",
+ "id": null,
+ "name": "name",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "created_at": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "created_at",
+ "id": null,
+ "name": "created_at",
+ "schema_valid": true,
+ "table": null,
+ "type": "datetime"
+ },
+ "last_seen_at": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "last_seen_at",
+ "id": null,
+ "name": "last_seen_at",
+ "schema_valid": true,
+ "table": null,
+ "type": "datetime"
+ },
+ "tags": {
+ "chain": null,
+ "fields": [
+ "definition_id",
+ "names"
+ ],
+ "hogql_value": "tags",
+ "id": "tags",
+ "name": "tags",
+ "schema_valid": true,
+ "table": "definition_tags",
+ "type": "lazy_table"
+ }
+ },
+ "id": "system.event_definitions",
+ "name": "system.event_definitions",
+ "row_count": null,
+ "type": "system"
+ },
"system.error_tracking_assignment_rules": {
"certification": null,
"fields": {
@@ -12069,6 +12131,88 @@
"row_count": null,
"type": "system"
},
+ "system.property_definitions": {
+ "certification": null,
+ "fields": {
+ "id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "id",
+ "id": null,
+ "name": "id",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "name": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "name",
+ "id": null,
+ "name": "name",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "type": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "type",
+ "id": null,
+ "name": "type",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "group_type_index": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "group_type_index",
+ "id": null,
+ "name": "group_type_index",
+ "schema_valid": true,
+ "table": null,
+ "type": "integer"
+ },
+ "property_type": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "property_type",
+ "id": null,
+ "name": "property_type",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "is_numerical": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "is_numerical",
+ "id": null,
+ "name": "is_numerical",
+ "schema_valid": true,
+ "table": null,
+ "type": "integer"
+ },
+ "tags": {
+ "chain": null,
+ "fields": [
+ "definition_id",
+ "names"
+ ],
+ "hogql_value": "tags",
+ "id": "tags",
+ "name": "tags",
+ "schema_valid": true,
+ "table": "definition_tags",
+ "type": "lazy_table"
+ }
+ },
+ "id": "system.property_definitions",
+ "name": "system.property_definitions",
+ "row_count": null,
+ "type": "system"
+ },
"system.replay_scanners": {
"certification": null,
"fields": {
@@ -20766,6 +20910,68 @@
"row_count": null,
"type": "system"
},
+ "system.event_definitions": {
+ "certification": null,
+ "fields": {
+ "id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "id",
+ "id": null,
+ "name": "id",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "name": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "name",
+ "id": null,
+ "name": "name",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "created_at": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "created_at",
+ "id": null,
+ "name": "created_at",
+ "schema_valid": true,
+ "table": null,
+ "type": "datetime"
+ },
+ "last_seen_at": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "last_seen_at",
+ "id": null,
+ "name": "last_seen_at",
+ "schema_valid": true,
+ "table": null,
+ "type": "datetime"
+ },
+ "tags": {
+ "chain": null,
+ "fields": [
+ "definition_id",
+ "names"
+ ],
+ "hogql_value": "tags",
+ "id": "tags",
+ "name": "tags",
+ "schema_valid": true,
+ "table": "definition_tags",
+ "type": "lazy_table"
+ }
+ },
+ "id": "system.event_definitions",
+ "name": "system.event_definitions",
+ "row_count": null,
+ "type": "system"
+ },
"system.error_tracking_assignment_rules": {
"certification": null,
"fields": {
@@ -26797,6 +27003,88 @@
"row_count": null,
"type": "system"
},
+ "system.property_definitions": {
+ "certification": null,
+ "fields": {
+ "id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "id",
+ "id": null,
+ "name": "id",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "name": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "name",
+ "id": null,
+ "name": "name",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "type": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "type",
+ "id": null,
+ "name": "type",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "group_type_index": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "group_type_index",
+ "id": null,
+ "name": "group_type_index",
+ "schema_valid": true,
+ "table": null,
+ "type": "integer"
+ },
+ "property_type": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "property_type",
+ "id": null,
+ "name": "property_type",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "is_numerical": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "is_numerical",
+ "id": null,
+ "name": "is_numerical",
+ "schema_valid": true,
+ "table": null,
+ "type": "integer"
+ },
+ "tags": {
+ "chain": null,
+ "fields": [
+ "definition_id",
+ "names"
+ ],
+ "hogql_value": "tags",
+ "id": "tags",
+ "name": "tags",
+ "schema_valid": true,
+ "table": "definition_tags",
+ "type": "lazy_table"
+ }
+ },
+ "id": "system.property_definitions",
+ "name": "system.property_definitions",
+ "row_count": null,
+ "type": "system"
+ },
"system.replay_scanners": {
"certification": null,
"fields": {
diff --git a/posthog/hogql/database/test/test_lazy_join_resolvers.py b/posthog/hogql/database/test/test_lazy_join_resolvers.py
index 4f86d3012524..8dfd04fd74ab 100644
--- a/posthog/hogql/database/test/test_lazy_join_resolvers.py
+++ b/posthog/hogql/database/test/test_lazy_join_resolvers.py
@@ -224,6 +224,7 @@ class TestLazyJoinManifest(SimpleTestCase):
"account_tags",
"data_warehouse",
"data_warehouse_experiments",
+ "definition_tags",
"error_tracking_fingerprint_issue_state",
"error_tracking_issue_fingerprint_overrides",
"events_to_sessions_v1",
diff --git a/posthog/hogql/database/test/test_postgres_table.py b/posthog/hogql/database/test/test_postgres_table.py
index 6c932d9ff610..ea5b4148bd34 100644
--- a/posthog/hogql/database/test/test_postgres_table.py
+++ b/posthog/hogql/database/test/test_postgres_table.py
@@ -129,6 +129,7 @@ class TestPostgresTable(BaseTest):
predicates: list[Expr] | None = None,
extra_fields: dict[str, DatabaseField] | None = None,
postgres_pushdown_values: dict[str, str | int | bool] | None = None,
+ push_team_guard_with_predicates: bool = False,
) -> None:
self.database = Database.create_for(team=self.team)
@@ -152,6 +153,7 @@ class TestPostgresTable(BaseTest):
if postgres_pushdown_values is None
else {"postgres_pushdown_values": postgres_pushdown_values}
),
+ push_team_guard_with_predicates=push_team_guard_with_predicates,
fields=fields,
),
)
@@ -164,8 +166,12 @@ class TestPostgresTable(BaseTest):
modifiers=create_default_modifiers_for_team(self.team),
)
- def _init_database_for_joins(self, *, predicates=None, extra_fields=None):
- self._init_database(predicates=predicates, extra_fields=extra_fields)
+ def _init_database_for_joins(self, *, predicates=None, extra_fields=None, push_team_guard_with_predicates=False):
+ self._init_database(
+ predicates=predicates,
+ extra_fields=extra_fields,
+ push_team_guard_with_predicates=push_team_guard_with_predicates,
+ )
self.database.tables.add_child(
TableNode(
name="other_table",
@@ -256,6 +262,19 @@ class TestPostgresTable(BaseTest):
sql,
)
+ def test_predicate_keeps_team_guard_next_to_the_read_when_opted_in(self):
+ self._init_database_for_joins(
+ predicates=[parse_expr("status != 'deleted'")],
+ extra_fields={"status": StringDatabaseField(name="status")},
+ push_team_guard_with_predicates=True,
+ )
+
+ sql = self._select(
+ "SELECT other_table.id FROM other_table LEFT JOIN postgres_table ON postgres_table.id = other_table.ref_id"
+ )
+
+ self.assertIn(f"WHERE team_id = {self.team.pk}) AS postgres_table", sql)
+
def test_predicate_combined_with_user_where(self):
self._init_database(
predicates=[parse_expr("created_at >= today() - interval 30 day")],
diff --git a/posthog/hogql/printer/clickhouse.py b/posthog/hogql/printer/clickhouse.py
index 18620b2331e0..117932ad927a 100644
--- a/posthog/hogql/printer/clickhouse.py
+++ b/posthog/hogql/printer/clickhouse.py
@@ -1305,6 +1305,7 @@ class ClickHousePrinter(BasePrinter):
and (
table.retention_field is not None
or table.postgres_pushdown_values
+ or table.push_team_guard_with_predicates
or not table.get_predicates(self.context)
)
and self.context.team_id is not None
diff --git a/posthog/hogql_queries/access_controlled_resources.py b/posthog/hogql_queries/access_controlled_resources.py
index 76b570ac33af..5dd2010cdfa3 100644
--- a/posthog/hogql_queries/access_controlled_resources.py
+++ b/posthog/hogql_queries/access_controlled_resources.py
@@ -71,6 +71,8 @@ _TRANSITIVE_SYSTEM_TABLE_SCOPES: dict[str, frozenset[str]] = {
"system._ticket_tagged_items": frozenset({"ticket"}),
"system._ticket_assignments": frozenset({"ticket"}),
"system._ticket_assignee_roles": frozenset({"ticket"}),
+ "system._event_definition_tagged_items": frozenset({"event_definition"}),
+ "system._property_definition_tagged_items": frozenset({"property_definition"}),
# Task predicates need these public channel IDs even under object-only task grants.
"system._task_public_channels": frozenset({"task"}),
"system.customer_tasks": frozenset({"account"}),