: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(hogql): preserve namespaced lazy table aliases (#122576)
Marius Andra570fa98810a6
diff --git a/docs/published/handbook/engineering/databases/hogql-python.md b/docs/published/handbook/engineering/databases/hogql-python.mdindex 1148ac285084..fe603570a644 100644--- a/docs/published/handbook/engineering/databases/hogql-python.md+++ b/docs/published/handbook/engineering/databases/hogql-python.md@@ -127,6 +127,9 @@ Pass the printed SQL and `HogQLContext.values` together to the database driver.## Database schema and features+Built-in tables also accept the `posthog` namespace, such as `SELECT id FROM posthog.persons` and `SELECT count() FROM posthog.groups`.+Explicit aliases work with these names, for example `SELECT p.id FROM posthog.persons AS p`.+The HogQL database schema is in flux. You will soon be able to explore it in the [PostHog app itself](https://github.com/PostHog/posthog/pull/14591).The most up to date resource is [hogql/database.py](https://github.com/PostHog/posthog/blob/master/posthog/hogql/database.py) on Github. At the time of writing, these tables were available:diff --git a/posthog/hogql/resolver_utils.py b/posthog/hogql/resolver_utils.pyindex 951c694b5464..0588641511fc 100644--- a/posthog/hogql/resolver_utils.py+++ b/posthog/hogql/resolver_utils.py@@ -317,7 +317,7 @@ def get_long_table_name(select: ast.SelectQueryType, type: ast.Type) -> str:case ast.TableType():return select.get_alias_for_table_type(type) or ""case ast.LazyTableType(table=table):- return table.to_printed_hogql()+ return select.get_alias_for_table_type(type) or table.to_printed_hogql()case (ast.TableAliasType(alias=alias)| ast.ColumnAliasedTableType(alias=alias)diff --git a/posthog/hogql/transforms/lazy_tables.py b/posthog/hogql/transforms/lazy_tables.pyindex aeb239509307..3a5a536ccb6a 100644--- a/posthog/hogql/transforms/lazy_tables.py+++ b/posthog/hogql/transforms/lazy_tables.py@@ -333,6 +333,8 @@ class LazyTableResolver(TraversingVisitor):# Collect all the joins we need to add to the select queryjoins_to_add: dict[str, LazyJoinToAdd] = {}tables_to_add: dict[str, LazyTableToAdd] = {}+ # Field types retain the original tables after the scope replaces them with subqueries.+ original_select_type = ast.SelectQueryType(tables=dict(select_type.tables))# First properties, then fields. This way we always get the smallest units to query first.matched_properties: list[ast.PropertyType | ast.FieldType] = [@@ -725,7 +727,7 @@ class LazyTableResolver(TraversingVisitor):while isinstance(table_type, ast.VirtualTableType):table_type = table_type.table_type- table_name = get_long_table_name(select_type, table_type)+ table_name = get_long_table_name(original_select_type, table_type)try:table_type = select_type.tables[table_name]except KeyError:diff --git a/posthog/hogql/transforms/test/test_lazy_tables.py b/posthog/hogql/transforms/test/test_lazy_tables.pyindex d61ba2becacd..1dd2e0c15db5 100644--- a/posthog/hogql/transforms/test/test_lazy_tables.py+++ b/posthog/hogql/transforms/test/test_lazy_tables.py@@ -2,20 +2,64 @@ from typing import Anyimport pytestfrom posthog.test.base import BaseTest, NewEventsSchemaSnapshotExtension+from unittest.mock import patchfrom django.conf import settings-from django.test import override_settings+from django.test import SimpleTestCase, override_settings++from parameterized import parameterizedfrom posthog.schema import HogQLQueryModifiers, PersonsOnEventsModefrom posthog.hogql.context import HogQLContext+from posthog.hogql.database.database import Databasefrom posthog.hogql.parser import parse_selectfrom posthog.hogql.printer import prepare_and_print_ast+from posthog.hogql.property_metadata import PropertyMetadatafrom posthog.hogql.test.utils import pretty_print_in_tests+from posthog.models import Organization, Team+from products.data_tools.backend.models.join import DataWarehouseJoin+class TestNamespacedLazyTables(SimpleTestCase):+ @parameterized.expand(+ [+ ("persons_star", "persons", "SELECT * FROM {table} LIMIT 1"),+ ("persons_count", "persons", "SELECT count() FROM {table}"),+ ("persons_id", "persons", "SELECT id FROM {table}"),+ ("persons_property", "persons", "SELECT properties.email FROM {table}"),+ ("persons_qualified_field", "persons", "SELECT {table}.id FROM {table}"),+ ("persons_alias", "persons", "SELECT p.id, p.properties.email FROM {table} AS p"),+ ("persons_cte", "persons", "WITH people AS (SELECT id FROM {table}) SELECT id FROM people"),+ ("persons_join", "persons", "SELECT p.id FROM {table} AS p JOIN {table} AS q ON p.id = q.id"),+ ("groups_star", "groups", "SELECT * FROM {table} LIMIT 1"),+ ("groups_count", "groups", "SELECT count() FROM {table}"),+ ("groups_key", "groups", "SELECT key FROM {table}"),+ ("groups_property", "groups", "SELECT properties.name FROM {table}"),+ ("groups_alias", "groups", "SELECT g.key FROM {table} AS g"),+ ]+ )+ def test_namespaced_table_matches_unqualified_table(self, _name: str, table: str, query: str) -> None:+ expected = self._compile(query.format(table=table))+ actual = self._compile(query.format(table=f"posthog.{table}"))+ self.assertEqual(actual.replace(f"posthog__{table}", table), expected)++ def _compile(self, query: str) -> str:+ context = HogQLContext(+ team_id=1,+ team=Team(id=1, organization=Organization()),+ database=Database(),+ enable_select_queries=True,+ use_new_events_schema=False,+ apply_events_retention_floor=False,+ restricted_properties=set(),+ )+ with patch("posthog.hogql.transforms.property_types.load_property_metadata", return_value=PropertyMetadata()):+ return prepare_and_print_ast(parse_select(query), context, "clickhouse")[0]++class TestLazyJoins(BaseTest):snapshot: AnymaxDiff = None