: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(data-warehouse): fix read_data schema for source-backed tables (#67418)
Tom Owersd8bd9c7c5135
diff --git a/ee/hogai/tools/read_data/test/test_tool.py b/ee/hogai/tools/read_data/test/test_tool.pyindex 54570f85f319..c46518678347 100644--- a/ee/hogai/tools/read_data/test/test_tool.py+++ b/ee/hogai/tools/read_data/test/test_tool.py@@ -712,6 +712,60 @@ class TestReadDataTool(BaseTest):assert "- created_at (datetime)" in resultassert artifact is None+ async def test_table_schema_returns_source_backed_table_by_underscore_name(self):+ # A table linked to an external source serializes under its dotted key (e.g. `stripe.charges`)+ # but is surfaced/queried by its raw underscore name; reading by that name must still resolve,+ # including the source-schema description and per-column annotations keyed by the underscore name.+ credential = await DataWarehouseCredential.objects.acreate(+ access_key="test_key", access_secret="test_secret", team=self.team+ )+ source = await ExternalDataSource.objects.acreate(+ source_id="src", connection_id="conn", team=self.team, source_type="Stripe"+ )+ table = await DataWarehouseTable.objects.acreate(+ name="stripe_charges",+ format="Parquet",+ team=self.team,+ credential=credential,+ external_data_source=source,+ url_pattern="https://bucket.s3/data/*",+ columns={+ "amount": {"hogql": "IntegerDatabaseField", "clickhouse": "Nullable(Int64)", "schema_valid": True},+ "currency": {+ "hogql": "StringDatabaseField",+ "clickhouse": "Nullable(String)",+ "schema_valid": True,+ },+ },+ )+ await ExternalDataSchema.objects.acreate(+ name="stripe_charges", team=self.team, source=source, table=table, description="Stripe charges"+ )+ with team_scope(self.team.pk, canonical=True):+ await WarehouseColumnAnnotation.objects.acreate(+ team=self.team,+ table=table,+ column_name="amount",+ description="charge amount in cents",+ description_source=WarehouseColumnAnnotation.DescriptionSource.AI_GENERATED,+ )++ state = AssistantState(messages=[], root_tool_call_id=str(uuid4()))+ context_manager = MagicMock()+ context_manager.check_user_has_billing_access = AsyncMock(return_value=False)+ context_manager.check_has_audit_logs_access = AsyncMock(return_value=False)++ tool = await ReadDataTool.create_tool_class(+ team=self.team, user=self.user, state=state, context_manager=context_manager+ )++ result, _ = await tool._arun_impl({"kind": "data_warehouse_table", "table_name": "stripe_charges"})++ assert "Could not serialize" not in result+ assert "Table `stripe_charges` — Stripe charges with fields:" in result+ assert "- amount (integer) — charge amount in cents" in result+ assert "- currency (string)" in result+async def test_table_schema_returns_view_fields(self):"""Test that data_warehouse_table returns full schema for a view."""await DataWarehouseSavedQuery.objects.acreate(diff --git a/ee/hogai/tools/read_data/tool.py b/ee/hogai/tools/read_data/tool.pyindex e9791284a0ef..de893738aab6 100644--- a/ee/hogai/tools/read_data/tool.py+++ b/ee/hogai/tools/read_data/tool.py@@ -609,13 +609,25 @@ class ReadDataTool(HogQLDatabaseMixin, MaxTool):serialized = database.serialize(hogql_context, include_only={table_name})- if table_name not in serialized:+ # Warehouse tables serialize under their dotted key (`zendesk.groups`) but are also queryable by+ # their raw underscore name (`zendesk_groups`), carried in `search_aliases`. Accept either form.+ table = serialized.get(table_name)+ if table is None:+ table = next(+ (t for t in serialized.values() if table_name in (getattr(t, "search_aliases", None) or [])),+ None,+ )+ if table is None:return f"Could not serialize schema for table `{table_name}`."- table = serialized[table_name]- semantics = self._warehouse_table_semantics({table_name}).get(table_name) or {}+ # Semantics and the raw DataWarehouseTable are keyed by the raw underscore name — the+ # `search_aliases` entry when the serialized key is dotted (`zendesk.groups` → `zendesk_groups`).+ # Native tables have no alias, so this collapses to `table_name`.+ aliases = getattr(table, "search_aliases", None) or []+ db_name = aliases[0] if aliases else table_name+ semantics = self._warehouse_table_semantics({db_name}).get(db_name) or {}descriptions = self._get_table_descriptions()- raw_table = database.get_table(table_name)+ raw_table = database.get_table(db_name)raw_fields = raw_table.fields# Source-table description (warehouse only) wins; otherwise fall back to the resolver, whichdiff --git a/posthog/hogql/database/database.py b/posthog/hogql/database/database.pyindex 485bc4a5f5fe..99ba4315e802 100644--- a/posthog/hogql/database/database.py+++ b/posthog/hogql/database/database.py@@ -938,7 +938,9 @@ class Database(BaseModel):if allowed_warehouse_table_names is not None and table_key not in allowed_warehouse_table_names:continue- if include_only and table_key not in include_only:+ # Warehouse tables are queryable by their dotted key (`zendesk.groups`) or their raw+ # underscore name (`zendesk_groups`); honor either form in `include_only`.+ if include_only and table_key not in include_only and warehouse_table.name not in include_only:continuetry: