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): join current version fields into endpoint system table (#129114)

Jovan Sakovic357496a7f8ce
diff --git a/posthog/hogql/database/lazy_join_registry.py b/posthog/hogql/database/lazy_join_registry.py
index 14f613222868..a6ac7995f509 100644
--- a/posthog/hogql/database/lazy_join_registry.py
+++ b/posthog/hogql/database/lazy_join_registry.py
@@ -26,7 +26,12 @@ 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 definition_tags_join, ticket_assignment_join, ticket_tags_join
+from posthog.hogql.database.schema.system import (
+ definition_tags_join,
+ endpoint_current_version_join,
+ ticket_assignment_join,
+ ticket_tags_join,
+)
from posthog.hogql.database.warehouse_join_resolvers import (
resolve_data_warehouse_experiments_join,
resolve_data_warehouse_join,
@@ -87,6 +92,7 @@ RESOLVERS: dict[str, LazyJoinResolver] = {
tags.TICKET_TAGS: ticket_tags_join,
tags.DEFINITION_TAGS: definition_tags_join,
tags.TICKET_ASSIGNMENT: ticket_assignment_join,
+ tags.ENDPOINT_CURRENT_VERSION: endpoint_current_version_join,
tags.ACCOUNT_TAGS: account_tags_join,
tags.ACCOUNT_NOTEBOOKS: account_notebooks_join,
tags.ACCOUNT_CUSTOM_PROPERTIES: account_custom_properties_join,
diff --git a/posthog/hogql/database/lazy_join_tags.py b/posthog/hogql/database/lazy_join_tags.py
index 5ad9ec7ce329..d08c026bb5a7 100644
--- a/posthog/hogql/database/lazy_join_tags.py
+++ b/posthog/hogql/database/lazy_join_tags.py
@@ -27,6 +27,7 @@ ERROR_TRACKING_FINGERPRINT_ISSUE_STATE = "error_tracking_fingerprint_issue_state
TICKET_TAGS = "ticket_tags"
DEFINITION_TAGS = "definition_tags"
TICKET_ASSIGNMENT = "ticket_assignment"
+ENDPOINT_CURRENT_VERSION = "endpoint_current_version"
ACCOUNT_TAGS = "account_tags"
ACCOUNT_NOTEBOOKS = "account_notebooks"
ACCOUNT_CUSTOM_PROPERTIES = "account_custom_properties"
diff --git a/posthog/hogql/database/schema/system.py b/posthog/hogql/database/schema/system.py
index 2ff004bcd5cb..fe1f39049099 100644
--- a/posthog/hogql/database/schema/system.py
+++ b/posthog/hogql/database/schema/system.py
@@ -5,7 +5,12 @@ 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 DEFINITION_TAGS, TICKET_ASSIGNMENT, TICKET_TAGS
+from posthog.hogql.database.lazy_join_tags import (
+ DEFINITION_TAGS,
+ ENDPOINT_CURRENT_VERSION,
+ TICKET_ASSIGNMENT,
+ TICKET_TAGS,
+)
from posthog.hogql.database.models import (
BooleanDatabaseField,
DANGEROUS_NoTeamIdCheckTable,
@@ -14,6 +19,7 @@ from posthog.hogql.database.models import (
DecimalDatabaseField,
ExpressionField,
FieldOrTable,
+ FieldTraverser,
FloatDatabaseField,
IntegerDatabaseField,
LazyJoin,
@@ -933,7 +939,22 @@ source_sync_jobs: PostgresTable = PostgresTable(
},
)
-endpoint_versions: PostgresTable = PostgresTable(
+
+class _EndpointVersionsTable(PostgresTable):
+ def creator_owned_object_ids(
+ self, user_access_control: "UserAccessControl", team_id: Optional[int], candidate_ids: Optional[frozenset[str]]
+ ) -> frozenset[str]:
+ from products.endpoints.backend.facade.models import Endpoint # noqa: PLC0415
+
+ if team_id is None:
+ raise ValueError("system.data_modeling_endpoint_versions needs a team to resolve endpoint access")
+ endpoints = Endpoint.objects.filter(team_id=team_id, created_by_id=user_access_control.user.pk)
+ if candidate_ids is not None:
+ endpoints = endpoints.filter(id__in=candidate_ids)
+ return frozenset(str(endpoint_id) for endpoint_id in endpoints.values_list("id", flat=True))
+
+
+endpoint_versions: _EndpointVersionsTable = _EndpointVersionsTable(
name="data_modeling_endpoint_versions",
postgres_table_name="endpoints_endpointversion",
access_scope="endpoint",
@@ -963,6 +984,38 @@ endpoint_versions: PostgresTable = PostgresTable(
},
)
+
+def endpoint_current_version_join(
+ join_to_add: LazyJoinToAdd, context: HogQLContext, node: ast.SelectQuery
+) -> ast.JoinExpr:
+ fields: dict[str, list[str | int]] = {
+ **join_to_add.fields_accessed,
+ "endpoint_id": ["endpoint_id"],
+ "version": ["version"],
+ }
+ return ast.JoinExpr(
+ table=ast.SelectQuery(
+ select=[ast.Alias(alias=alias, expr=ast.Field(chain=chain)) for alias, chain in fields.items()],
+ select_from=ast.JoinExpr(table=ast.Field(chain=["system", "data_modeling_endpoint_versions"])),
+ ),
+ alias=join_to_add.to_table,
+ join_type="LEFT JOIN",
+ constraint=ast.JoinConstraint(
+ constraint_type="ON",
+ expr=ast.And(
+ exprs=[
+ ast.CompareOperation(
+ op=ast.CompareOperationOp.Eq,
+ left=ast.Field(chain=[join_to_add.from_table, source]),
+ right=ast.Field(chain=[join_to_add.to_table, target]),
+ )
+ for source, target in [("id", "endpoint_id"), ("current_version", "version")]
+ ]
+ ),
+ ),
+ )
+
+
endpoints: PostgresTable = PostgresTable(
name="data_modeling_endpoints",
postgres_table_name="endpoints_endpoint",
@@ -984,6 +1037,27 @@ endpoints: PostgresTable = PostgresTable(
name="current_version",
description="Version number currently served; joins to data_modeling_endpoint_versions.version.",
),
+ "version": LazyJoin(
+ from_field=["id"],
+ to_field=["endpoint_id"],
+ extra_from_fields=[["current_version"]],
+ join_table="system.data_modeling_endpoint_versions",
+ resolver=ENDPOINT_CURRENT_VERSION,
+ description="Current version of this endpoint. Matches the endpoint ID and current_version number.",
+ ),
+ "description": FieldTraverser(
+ chain=["version", "description"], description="Description of the current endpoint version."
+ ),
+ "query": FieldTraverser(
+ chain=["version", "query"], description="JSON HogQL query of the current endpoint version."
+ ),
+ "data_freshness_seconds": FieldTraverser(
+ chain=["version", "data_freshness_seconds"],
+ description="Maximum cache age for the current version, in seconds.",
+ ),
+ "columns": FieldTraverser(
+ chain=["version", "columns"], description="JSON schema of the current version's output columns."
+ ),
"derived_from_insight": StringDatabaseField(
name="derived_from_insight", description="Short id of the insight this endpoint was created from, if any."
),
diff --git a/posthog/hogql/database/schema/test/test_system_tables.py b/posthog/hogql/database/schema/test/test_system_tables.py
index d1872ba607ec..cd07b4324c16 100644
--- a/posthog/hogql/database/schema/test/test_system_tables.py
+++ b/posthog/hogql/database/schema/test/test_system_tables.py
@@ -1729,6 +1729,68 @@ class TestSystemTablesNotebookMarkdown(NonAtomicBaseTest):
assert rows == {"mdnote": markdown_source, "legacy": None, "empty": None}
+class TestSystemEndpointCurrentVersion(NonAtomicBaseTest):
+ CLASS_DATA_LEVEL_SETUP = False
+
+ @parameterized.expand([(1,), (2,), (99,), (1, True, True), (1, True, False)])
+ def test_current_version_fields(self, current_version, access_denied=False, reader_is_creator=True):
+ endpoint = _create_endpoint(self.team, "current")
+ if access_denied:
+ self.organization.available_product_features = [
+ {"key": AvailableFeature.ACCESS_CONTROL, "name": AvailableFeature.ACCESS_CONTROL}
+ ]
+ self.organization.save()
+ self.organization_membership.level = OrganizationMembership.Level.MEMBER
+ self.organization_membership.save()
+ endpoint.created_by = self.user if reader_is_creator else User.objects.create(email="[email protected]")
+ endpoint.save(update_fields=["created_by"])
+ AccessControl.objects.create(
+ team=self.team,
+ resource="endpoint",
+ resource_id=str(endpoint.id),
+ access_level="none",
+ organization_member=self.organization_membership,
+ )
+ endpoint.current_version = current_version
+ endpoint.save(update_fields=["current_version"])
+ for version in (1, 2):
+ EndpointVersion.objects.create(
+ endpoint=endpoint,
+ team=self.team,
+ version=version,
+ description=f"Version {version}",
+ query={"kind": "HogQLQuery", "query": f"SELECT {version}"},
+ cache_ttl_seconds=version * 60,
+ columns=[{"name": "result", "type": "integer"}],
+ created_by=self.user,
+ )
+ _create_endpoint_version(self.team, "other")
+
+ response = execute_hogql_query(
+ """
+ SELECT name, current_version, description, query.query, data_freshness_seconds,
+ columns, version.version, version.description
+ FROM system.data_modeling_endpoints
+ WHERE name = 'ep_current'
+ """,
+ team=self.team,
+ user=self.user,
+ )
+
+ if access_denied and not reader_is_creator:
+ assert response.results == []
+ return
+ assert len(response.results) == 1
+ row = response.results[0]
+ assert row[:2] == ("ep_current", current_version)
+ if current_version == 99:
+ assert row[2] == ""
+ else:
+ assert row[2:5] == (f"Version {current_version}", f"SELECT {current_version}", current_version * 60)
+ assert json.loads(row[5]) == [{"name": "result", "type": "integer"}]
+ assert row[6:] == (current_version, f"Version {current_version}")
+
+
class TestSystemTicketTagsLazyJoin(NonAtomicBaseTest):
"""Verify the `support_tickets.tags` lazy join returns per-ticket tag names and stays team-isolated."""
diff --git a/posthog/hogql/database/test/__snapshots__/test_database.ambr b/posthog/hogql/database/test/__snapshots__/test_database.ambr
index a342591aee6d..3e941711b2f6 100644
--- a/posthog/hogql/database/test/__snapshots__/test_database.ambr
+++ b/posthog/hogql/database/test/__snapshots__/test_database.ambr
@@ -5793,6 +5793,80 @@
"schema_valid": true,
"table": null,
"type": "integer"
+ },
+ "version": {
+ "chain": null,
+ "fields": [
+ "id",
+ "team_id",
+ "endpoint_id",
+ "version",
+ "description",
+ "query",
+ "data_freshness_seconds",
+ "created_at",
+ "_is_active",
+ "is_active",
+ "columns"
+ ],
+ "hogql_value": "version",
+ "id": "version",
+ "name": "version",
+ "schema_valid": true,
+ "table": "data_modeling_endpoint_versions",
+ "type": "lazy_table"
+ },
+ "description": {
+ "chain": [
+ "version",
+ "description"
+ ],
+ "fields": null,
+ "hogql_value": "description",
+ "id": null,
+ "name": "description",
+ "schema_valid": true,
+ "table": null,
+ "type": "field_traverser"
+ },
+ "query": {
+ "chain": [
+ "version",
+ "query"
+ ],
+ "fields": null,
+ "hogql_value": "query",
+ "id": null,
+ "name": "query",
+ "schema_valid": true,
+ "table": null,
+ "type": "field_traverser"
+ },
+ "data_freshness_seconds": {
+ "chain": [
+ "version",
+ "data_freshness_seconds"
+ ],
+ "fields": null,
+ "hogql_value": "data_freshness_seconds",
+ "id": null,
+ "name": "data_freshness_seconds",
+ "schema_valid": true,
+ "table": null,
+ "type": "field_traverser"
+ },
+ "columns": {
+ "chain": [
+ "version",
+ "columns"
+ ],
+ "fields": null,
+ "hogql_value": "columns",
+ "id": null,
+ "name": "columns",
+ "schema_valid": true,
+ "table": null,
+ "type": "field_traverser"
}
},
"id": "system.data_modeling_endpoints",
@@ -20675,6 +20749,80 @@
"schema_valid": true,
"table": null,
"type": "integer"
+ },
+ "version": {
+ "chain": null,
+ "fields": [
+ "id",
+ "team_id",
+ "endpoint_id",
+ "version",
+ "description",
+ "query",
+ "data_freshness_seconds",
+ "created_at",
+ "_is_active",
+ "is_active",
+ "columns"
+ ],
+ "hogql_value": "version",
+ "id": "version",
+ "name": "version",
+ "schema_valid": true,
+ "table": "data_modeling_endpoint_versions",
+ "type": "lazy_table"
+ },
+ "description": {
+ "chain": [
+ "version",
+ "description"
+ ],
+ "fields": null,
+ "hogql_value": "description",
+ "id": null,
+ "name": "description",
+ "schema_valid": true,
+ "table": null,
+ "type": "field_traverser"
+ },
+ "query": {
+ "chain": [
+ "version",
+ "query"
+ ],
+ "fields": null,
+ "hogql_value": "query",
+ "id": null,
+ "name": "query",
+ "schema_valid": true,
+ "table": null,
+ "type": "field_traverser"
+ },
+ "data_freshness_seconds": {
+ "chain": [
+ "version",
+ "data_freshness_seconds"
+ ],
+ "fields": null,
+ "hogql_value": "data_freshness_seconds",
+ "id": null,
+ "name": "data_freshness_seconds",
+ "schema_valid": true,
+ "table": null,
+ "type": "field_traverser"
+ },
+ "columns": {
+ "chain": [
+ "version",
+ "columns"
+ ],
+ "fields": null,
+ "hogql_value": "columns",
+ "id": null,
+ "name": "columns",
+ "schema_valid": true,
+ "table": null,
+ "type": "field_traverser"
}
},
"id": "system.data_modeling_endpoints",
diff --git a/posthog/hogql/database/test/test_lazy_join_resolvers.py b/posthog/hogql/database/test/test_lazy_join_resolvers.py
index 8dfd04fd74ab..8403fc94776e 100644
--- a/posthog/hogql/database/test/test_lazy_join_resolvers.py
+++ b/posthog/hogql/database/test/test_lazy_join_resolvers.py
@@ -225,6 +225,7 @@ class TestLazyJoinManifest(SimpleTestCase):
"data_warehouse",
"data_warehouse_experiments",
"definition_tags",
+ "endpoint_current_version",
"error_tracking_fingerprint_issue_state",
"error_tracking_issue_fingerprint_overrides",
"events_to_sessions_v1",