: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): exclude direct sources from warehouse metadata (#103771)
Paweł Szczur9da33d0cad84
diff --git a/docs/internal/hogql-language-service.md b/docs/internal/hogql-language-service.mdindex 7bdb28974d54..fc27b82b985b 100644--- a/docs/internal/hogql-language-service.md+++ b/docs/internal/hogql-language-service.md@@ -32,6 +32,9 @@ Django remains authoritative for authentication, team membership, entitlements,resolution. The Go service does not read PostHog permission tables or accept browser-selected identity without anauthenticated internal request.+The shared catalog excludes direct-connection table rows because those rows belong only to an explicit `connectionId` database.+This keeps a direct table's raw dotted name from replacing a synced table that resolves to the same catalog key.+Django adds resolver-confirmed `tableAliases` to the same permission-filtered catalog snapshot.For example, a catalog can map `demo_postgres_orders` to `postgres.demo.orders` when both names resolve to the same visible warehouse table.Aliases from another project or hidden tables are not published.diff --git a/posthog/hogql/database/database.py b/posthog/hogql/database/database.pyindex 4236fab7c3ed..cda04ac98b37 100644--- a/posthog/hogql/database/database.py+++ b/posthog/hogql/database/database.py@@ -1382,7 +1382,9 @@ class Database(BaseModel):if self._is_direct_query():warehouse_tables_query = warehouse_tables_query.filter(external_data_source_id=self._connection_id)elif warehouse_table_names:- warehouse_tables_query = warehouse_tables_query.filter(name__in=warehouse_table_names)+ warehouse_tables_query = warehouse_tables_query.filter(name__in=warehouse_table_names).exclude(+ external_data_source__access_method=ExternalDataSourceAccessMethod.DIRECT+ )else:warehouse_tables_query = warehouse_tables_query.none()diff --git a/posthog/hogql/database/test/test_database.py b/posthog/hogql/database/test/test_database.pyindex 6612785d5b9e..5928184d66da 100644--- a/posthog/hogql/database/test/test_database.py+++ b/posthog/hogql/database/test/test_database.py@@ -821,12 +821,14 @@ class TestDatabase(BaseTest, QueryMatchingTest):# Guards the deleted-source exclusion against the Django exclude()-with-NULL gotcha:# a self-managed table (no source) must still resolve.credential = DataWarehouseCredential.objects.create(team=self.team, access_key="k", access_secret="s")- self._create_warehouse_table(name="self_managed", url_pattern="s3://self/*", credential=credential)+ table = self._create_warehouse_table(name="self_managed", url_pattern="s3://self/*", credential=credential)database = Database.create_for(team=self.team)+ serialized = database.serialize(HogQLContext(team_id=self.team.pk, database=database))assert database.has_table("self_managed")assert cast(HogQLDataWarehouseTable, database.get_table("self_managed")).url == "s3://self/*"+ assert serialized["self_managed"].id == str(table.id)def test_create_hogql_database_resolves_duplicate_live_table_names_to_newest(self):# Two live tables share a name (e.g. a re-sync produced a duplicate): newest wins.@@ -3626,21 +3628,38 @@ class TestDatabase(BaseTest, QueryMatchingTest):credentials = DataWarehouseCredential.objects.create(access_key="test_key", access_secret="test_secret", team=self.team)- source = ExternalDataSource.objects.create(+ synced_source = ExternalDataSource.objects.create(team=self.team,- source_id="source_id",- connection_id="connection_id",+ source_id="synced_source",+ connection_id="synced_connection",+ status=ExternalDataSource.Status.COMPLETED,+ source_type=ExternalDataSourceType.POSTGRES,+ prefix="",+ )+ synced_table = DataWarehouseTable.objects.create(+ name="orders",+ format="Parquet",+ team=self.team,+ credential=credentials,+ external_data_source=synced_source,+ url_pattern="s3://synthetic/orders/*",+ columns={"id": {"hogql": "StringDatabaseField", "clickhouse": "Nullable(String)", "schema_valid": True}},+ )+ direct_source = ExternalDataSource.objects.create(+ team=self.team,+ source_id="direct_source",+ connection_id="direct_connection",status=ExternalDataSource.Status.COMPLETED,source_type=ExternalDataSourceType.POSTGRES,access_method=ExternalDataSource.AccessMethod.DIRECT,- prefix="ph3",+ prefix="managed",)DataWarehouseTable.objects.create(- name="analytics_platform_preaggregationjob",+ name="postgres.orders",format="Parquet",team=self.team,credential=credentials,- external_data_source=source,+ external_data_source=direct_source,url_pattern="direct://postgres",columns={"id": {"hogql": "StringDatabaseField", "clickhouse": "Nullable(String)", "schema_valid": True}},)@@ -3648,9 +3667,9 @@ class TestDatabase(BaseTest, QueryMatchingTest):database = Database.create_for(team=self.team)serialized = database.serialize(HogQLContext(team_id=self.team.pk, database=database))- assert not database.has_table("analytics_platform_preaggregationjob")- assert "analytics_platform_preaggregationjob" not in database.get_warehouse_table_names()- assert "analytics_platform_preaggregationjob" not in serialized+ assert database.has_table("postgres.orders")+ assert "postgres.orders" in database.get_warehouse_table_names()+ assert cast(DatabaseSchemaDataWarehouseTable, serialized["postgres.orders"]).id == str(synced_table.id)def test_serialize_direct_postgres_table_uses_table_name_in_direct_mode(self) -> None:credentials = DataWarehouseCredential.objects.create(