: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): list field traversers in information_schema columns (#130352)
Jovan Sakoviccaa44bac8e6e
diff --git a/posthog/hogql/database/schema/information_schema.py b/posthog/hogql/database/schema/information_schema.pyindex e7b06928336a..a688ced1f3a3 100644--- a/posthog/hogql/database/schema/information_schema.py+++ b/posthog/hogql/database/schema/information_schema.py@@ -575,6 +575,33 @@ class _Introspection:_capture_unexpected("information_schema: failed to resolve expression column type", e)return "Expression"+ def _traverser_target(self, table_name: str, column_name: str) -> Optional[DatabaseField]:+ """The plain field a traverser column resolves to, or None when it lands on a table, an+ expression, or nothing resolvable."""+ scope = self._table_scope(table_name)+ if scope is None:+ return None+ # Deferred: see `_table_scope`.+ from posthog.hogql.resolver import resolve_types # noqa: PLC0415++ chain: list[str | int] = [*column_name.split(".")]+ try:+ resolved_type = resolve_types(ast.Field(chain=chain), self.context, "hogql", [scope]).type+ # The table scope is a `SELECT *`, so a column comes back wrapped in its select alias.+ while isinstance(resolved_type, ast.FieldAliasType):+ resolved_type = resolved_type.type+ if not isinstance(resolved_type, ast.FieldType):+ return None+ target = resolved_type.resolve_database_field(self.context)+ except BaseHogQLError:+ return None # genuinely unresolvable chain — expected+ except Exception as e:+ _capture_unexpected("information_schema: failed to resolve field traverser target", e)+ return None+ if not isinstance(target, DatabaseField) or isinstance(target, ExpressionField):+ return None+ return target+def _row_count(self, name: str, table: Table, table_type: str) -> Optional[int]:if table_type == "data_warehouse" and table.name:return self.row_counts.get(table.name)@@ -747,6 +774,25 @@ class _Introspection:provenance_key=None,))+ target_field = self._traverser_target(table_name, qualified)+ if target_field is not None:+ column_rows.append(+ [+ table_schema,+ table_name,+ qualified,+ ordinal,+ _field_type_name(target_field),+ bool(target_field.is_nullable()),+ bool(target_field.array),+ "field_traverser",+ self._column_description(table, qualified, field),+ None,+ None,+ None,+ ]+ )+ ordinal += 1elif isinstance(field, VirtualTable):# Surface nested virtual-table columns as `parent.child` columns.column_rows.append(@@ -1719,7 +1765,10 @@ class InformationSchemaColumnsTable(InformationSchemaTable):"field_kind": _string_field("field_kind",nullable=False,- description="How the column is backed: 'column', 'expression' (computed), or 'virtual_table'.",+ description=(+ "How the column is backed: 'column', 'expression' (computed), 'field_traverser' (read "+ "through a join, typed by the field it reaches), or 'virtual_table'."+ ),),"description": _string_field("description", nullable=True, description="Human/agent-facing description of what the column holds."diff --git a/posthog/hogql/database/schema/test/test_information_schema.py b/posthog/hogql/database/schema/test/test_information_schema.pyindex fe816bf7ff7c..547f95059d30 100644--- a/posthog/hogql/database/schema/test/test_information_schema.py+++ b/posthog/hogql/database/schema/test/test_information_schema.py@@ -388,26 +388,35 @@ class TestInformationSchema(ClickhouseTestMixin, APIBaseTest):@parameterized.expand([- ("person_id", "UUID"),- ("event_issue_id", "UUID"),- ("issue_id", "UUID"),- ("issue_id_v2", "UUID"),- ("issue_first_seen", "DateTime"),- ("$virt_is_bot", "Boolean"),+ ("events", "person_id", "expression", "UUID"),+ ("events", "event_issue_id", "expression", "UUID"),+ ("events", "issue_id", "expression", "UUID"),+ ("events", "issue_id_v2", "expression", "UUID"),+ ("events", "issue_first_seen", "expression", "DateTime"),+ ("events", "$virt_is_bot", "expression", "Boolean"),+ ("system.data_modeling_endpoints", "description", "field_traverser", "String"),+ ("system.data_modeling_endpoints", "query", "field_traverser", "JSON"),+ ("system.data_modeling_endpoints", "columns", "field_traverser", "JSON"),+ ("system.data_modeling_endpoints", "data_freshness_seconds", "field_traverser", "Integer"),+ ("events", "person", None, None),])- def test_expression_columns_resolve_to_their_value_type(self, column_name: str, expected_type: str):+ def test_computed_columns_resolve_to_their_value_type(+ self, table_name: str, column_name: str, expected_kind: str | None, expected_type: str | None+ ):# Expression columns must report the type they evaluate to (like hogql autocomplete), not the# generic "Expression" — otherwise the catalog is useless for picking a cast/comparison.response = execute_hogql_query(f"SELECT data_type, field_kind FROM system.information_schema.columns "- f"WHERE table_name = 'events' AND column_name = '{column_name}'",+ f"WHERE table_name = '{table_name}' AND column_name = '{column_name}'",team=self.team,+ user=self.user,)results = response.results or []- assert len(results) == 1- assert results[0][1] == "expression"- assert results[0][0] == expected_type+ if expected_kind is None:+ assert results == []+ return+ assert results == [(expected_type, expected_kind)]@parameterized.expand([