Skip to content

: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.

fix(data-catalog): exclude CTEs from referenced table extraction (#76732)

Thiago Salvatoref8aba70a23bc
diff --git a/products/data_catalog/backend/logic/validation.py b/products/data_catalog/backend/logic/validation.py
index 728d4ae9734c..2be934139df8 100644
--- a/products/data_catalog/backend/logic/validation.py
+++ b/products/data_catalog/backend/logic/validation.py
@@ -45,14 +45,42 @@ _HOGQL_ALLOWED_KEYS = {"kind", "query", "values"}
class _TableReferenceCollector(TraversingVisitor):
- """Collects the identifiers used directly as FROM/JOIN targets in a parsed HogQL query."""
+ """Collects the real tables a query reads, excluding CTE aliases.
+
+ Global CTE-name subtraction (``get_table_names``) is not enough here: a CTE named after a
+ real table would erase that table even when the CTE body reads it (a non-recursive CTE body
+ does not see its own name), silently defeating the catalog's denied-table filter. So CTE
+ names are tracked per scope: each CTE body is visited under the scope of the CTEs defined
+ before it, and only single-part FROM/JOIN targets naming an in-scope CTE are skipped.
+ """
def __init__(self) -> None:
self.tables: set[str] = set()
+ self._cte_scopes: list[set[str]] = [set()]
+
+ def visit_select_query(self, node: ast.SelectQuery) -> None:
+ scope = set(self._cte_scopes[-1])
+ for cte_name, cte in (node.ctes or {}).items():
+ self._cte_scopes.append(set(scope))
+ self.visit(cte.expr)
+ self._cte_scopes.pop()
+ scope.add(cte_name)
+ self._cte_scopes.append(scope)
+ # CTEs were just visited with the right scopes; blank them out so the generic traversal
+ # of the remaining children doesn't re-visit them under this (wider) scope.
+ ctes, node.ctes = node.ctes, None
+ try:
+ super().visit_select_query(node)
+ finally:
+ node.ctes = ctes
+ self._cte_scopes.pop()
def visit_join_expr(self, node: ast.JoinExpr) -> None:
if isinstance(node.table, ast.Field):
- self.tables.add(".".join(str(part) for part in node.table.chain))
+ chain = [str(part) for part in node.table.chain]
+ if len(chain) != 1 or chain[0] not in self._cte_scopes[-1]:
+ self.tables.add(".".join(chain))
+ # The generic traversal also covers subqueries in the join target and in ON constraints.
super().visit_join_expr(node)
diff --git a/products/data_catalog/backend/tests/test_logic.py b/products/data_catalog/backend/tests/test_logic.py
index 3dd71258a901..b9d4bf958b72 100644
--- a/products/data_catalog/backend/tests/test_logic.py
+++ b/products/data_catalog/backend/tests/test_logic.py
@@ -114,11 +114,29 @@ class TestMetricUpdate(BaseTest):
class TestValidateMetricDefinition(BaseTest):
- def test_valid_hogql_extracts_referenced_tables(self) -> None:
- _, tables = validate_metric_definition(
- {"kind": "HogQLQuery", "query": "select count() from events"}, self.team, self.user
- )
- assert tables == ["events"]
+ @parameterized.expand(
+ [
+ ("plain", "select count() from events", ["events"]),
+ (
+ "cte_excluded",
+ "with monthly as (select count() as c from events) select c from monthly join persons on 1 = 1",
+ ["events", "persons"],
+ ),
+ (
+ "cte_shadowing_real_table",
+ "with events as (select uuid from events) select uuid from events",
+ ["events"],
+ ),
+ (
+ "join_constraint_subquery",
+ "select count() from persons join groups on persons.id in (select person_id from events)",
+ ["events", "groups", "persons"],
+ ),
+ ]
+ )
+ def test_valid_hogql_extracts_referenced_tables(self, _name: str, query: str, expected: list[str]) -> None:
+ _, tables = validate_metric_definition({"kind": "HogQLQuery", "query": query}, self.team, self.user)
+ assert tables == expected
def test_insight_viz_node_is_unwrapped(self) -> None:
canonical, _ = validate_metric_definition(