: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): show data warehouse joins on system tables in SQL editor (#61456)
Arthur Moreira de Deus4125b4dde999
diff --git a/posthog/hogql/database/database.py b/posthog/hogql/database/database.pyindex 43a3054a95e5..329c75e36f07 100644--- a/posthog/hogql/database/database.py+++ b/posthog/hogql/database/database.py@@ -664,10 +664,8 @@ class Database(BaseModel):field_input: dict[str, Any] = {}table = self.get_table(table_name)- if isinstance(table, FunctionCallTable):- field_input = table.get_asterisk()- elif isinstance(table, Table):- field_input = table.fields+ if isinstance(table, Table):+ field_input = _schema_field_input(table)fields = serialize_fields(field_input, context, table_name.split("."), table_type="posthog")fields_dict = {field.name: field for field in fields}@@ -681,10 +679,8 @@ class Database(BaseModel):system_field_input: dict[str, Any] = {}table = self.get_table(table_key)- if isinstance(table, FunctionCallTable):- system_field_input = table.get_asterisk()- elif isinstance(table, Table):- system_field_input = table.fields+ if isinstance(table, Table):+ system_field_input = _schema_field_input(table)fields = serialize_fields(system_field_input, context, table_key.split("."), table_type="posthog")fields_dict = {field.name: field for field in fields}@@ -1770,6 +1766,25 @@ def _should_include_connection_table(return not schemas or any(schema.should_sync for schema in schemas)+def _schema_field_input(table: Table) -> dict[str, Any]:+ """Fields to surface in the serialized schema (SQL editor sidebar, autocomplete).++ `get_asterisk()` exists for `SELECT *` expansion, so it drops lazy joins, virtual tables,+ and field traversers — but those are exactly the relational fields users need to see in the+ schema. Add them back while preserving `get_asterisk`'s column filtering (`avoid_asterisk_fields`+ and hidden columns), so data warehouse joins show up on `FunctionCallTable`-backed tables like+ the `system.*` Postgres tables.+ """+ if not isinstance(table, FunctionCallTable):+ return table.fields++ field_input = table.get_asterisk()+ for key, field in table.fields.items():+ if key not in field_input and isinstance(field, (LazyJoin, Table, FieldTraverser)):+ field_input[key] = field+ return field_input++def serialize_fields(field_input,context: HogQLContext,diff --git a/posthog/hogql/database/test/__snapshots__/test_database.ambr b/posthog/hogql/database/test/__snapshots__/test_database.ambrindex 295efa3a251d..f65bfe3825f9 100644--- a/posthog/hogql/database/test/__snapshots__/test_database.ambr+++ b/posthog/hogql/database/test/__snapshots__/test_database.ambr@@ -2498,6 +2498,32 @@"schema_valid": true,"table": null,"type": "datetime"+ },+ "tags": {+ "chain": null,+ "fields": [+ "account_id",+ "names"+ ],+ "hogql_value": "tags",+ "id": "tags",+ "name": "tags",+ "schema_valid": true,+ "table": "account_tags",+ "type": "lazy_table"+ },+ "notebooks": {+ "chain": null,+ "fields": [+ "account_id",+ "count"+ ],+ "hogql_value": "notebooks",+ "id": "notebooks",+ "name": "notebooks",+ "schema_valid": true,+ "table": "account_notebooks",+ "type": "lazy_table"}},"id": "system.accounts",@@ -10479,6 +10505,32 @@"schema_valid": true,"table": null,"type": "datetime"+ },+ "tags": {+ "chain": null,+ "fields": [+ "account_id",+ "names"+ ],+ "hogql_value": "tags",+ "id": "tags",+ "name": "tags",+ "schema_valid": true,+ "table": "account_tags",+ "type": "lazy_table"+ },+ "notebooks": {+ "chain": null,+ "fields": [+ "account_id",+ "count"+ ],+ "hogql_value": "notebooks",+ "id": "notebooks",+ "name": "notebooks",+ "schema_valid": true,+ "table": "account_notebooks",+ "type": "lazy_table"}},"id": "system.accounts",diff --git a/posthog/hogql/database/test/test_database.py b/posthog/hogql/database/test/test_database.pyindex 3ad08cf4ee81..c46c4d9e0b91 100644--- a/posthog/hogql/database/test/test_database.py+++ b/posthog/hogql/database/test/test_database.py@@ -612,6 +612,23 @@ class TestDatabase(BaseTest, QueryMatchingTest):sql = "select some_field.key from events"prepare_and_print_ast(parse_select(sql), context, dialect="clickhouse")+ def test_database_warehouse_joins_on_system_table_are_serialized(self):+ DataWarehouseJoin.objects.create(+ team=self.team,+ source_table_name="system.accounts",+ source_table_key="external_id",+ joining_table_name="groups",+ joining_table_key="key",+ field_name="my_join_field",+ )++ db = Database.create_for(team=self.team)+ context = HogQLContext(team_id=self.team.pk, database=db)+ serialized = db.serialize(context, include_only={"system.accounts"})++ assert "system.accounts" in serialized+ assert "my_join_field" in serialized["system.accounts"].fields+def test_database_warehouse_joins_deleted_join(self):DataWarehouseJoin.objects.create(team=self.team,