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(hogql): scope unknown-table suggestions to the named schema (#82441)

Jovan Sakovic47fac787dd61
diff --git a/posthog/hogql/database/database.py b/posthog/hogql/database/database.py
index 9d197185bc2d..9c521ac5e100 100644
--- a/posthog/hogql/database/database.py
+++ b/posthog/hogql/database/database.py
@@ -672,6 +672,11 @@ class Database(BaseModel):
`get_all_table_names()` — the latter verifies each warehouse entry by
calling `get_table()`, which would recurse back into this helper when
a warehouse table fails to resolve.
+
+ A qualified name states which schema the author meant, so candidates
+ outside it are dropped no matter how well the leaf scores. When no such
+ schema exists, the schema itself is the likely typo, so the closest
+ known schema is searched instead.
"""
import difflib
@@ -687,6 +692,14 @@ class Database(BaseModel):
# catalog without being available on the source we actually queried.
lowered = name.casefold()
candidates = {c for c in candidates if c.casefold() != lowered}
+ prefix, dot, _ = name.rpartition(".")
+ if dot:
+ schema = prefix.casefold()
+ if not any(c.casefold().startswith(f"{schema}.") for c in candidates):
+ known = sorted({c.rpartition(".")[0].casefold() for c in candidates if "." in c})
+ nearest = difflib.get_close_matches(schema, known, n=1, cutoff=0.89)
+ schema = nearest[0] if nearest else schema
+ candidates = {c for c in candidates if c.casefold().startswith(f"{schema}.")}
if not candidates:
return []
return difflib.get_close_matches(name, sorted(candidates), n=limit, cutoff=0.7)
diff --git a/posthog/hogql/database/test/test_database.py b/posthog/hogql/database/test/test_database.py
index 5d365eccf918..9472296cdd73 100644
--- a/posthog/hogql/database/test/test_database.py
+++ b/posthog/hogql/database/test/test_database.py
@@ -199,6 +199,81 @@ class TestBuildDatabaseRootNode(TestCase):
unpickler.find_class(module, name)
+def _catalog_node(names: list[str]) -> TableNode:
+ root = TableNode(name="root", children={})
+ for name in names:
+ node = root
+ for part in name.split("."):
+ node = node.children.setdefault(part, TableNode(name=part, children={}))
+ node.table = Table(fields={"id": StringDatabaseField(name="id")})
+ return root
+
+
+class TestUnknownTableSuggestions(TestCase):
+ @parameterized.expand(
+ [
+ (
+ "cross_namespace_candidate_is_not_suggested",
+ [],
+ ["stg_customer_orders"],
+ "warehouse.customer_orders",
+ "Unknown table `warehouse.customer_orders`.",
+ ),
+ (
+ "same_namespace_typo_is_suggested",
+ ["warehouse.customer_order"],
+ [],
+ "warehouse.customer_orders",
+ "Unknown table `warehouse.customer_orders`. Did you mean: warehouse.customer_order?",
+ ),
+ (
+ "unqualified_name_still_reaches_qualified_candidate",
+ ["warehouse.customer_orders"],
+ [],
+ "customer_orders",
+ "Unknown table `customer_orders`. Did you mean: warehouse.customer_orders?",
+ ),
+ (
+ "misspelled_schema_is_resolved_to_the_closest_one",
+ ["warehouse.customer_orders"],
+ [],
+ "warehous.customer_orders",
+ "Unknown table `warehous.customer_orders`. Did you mean: warehouse.customer_orders?",
+ ),
+ (
+ "unrelated_schema_is_not_resolved_to_any_other",
+ ["warehouse.customer_orders"],
+ ["stg_customer_orders"],
+ "public.customer_orders",
+ "Unknown table `public.customer_orders`.",
+ ),
+ (
+ "sibling_schema_on_one_connection_is_not_resolved_to_the_other",
+ ["postgres.pg.customer_orders"],
+ [],
+ "postgres.ph3.customer_orders",
+ "Unknown table `postgres.ph3.customer_orders`.",
+ ),
+ ]
+ )
+ def test_suggestions_stay_within_the_namespace_the_author_named(
+ self,
+ _name: str,
+ warehouse_tables: list[str],
+ views: list[str],
+ missing_table: str,
+ expected_message: str,
+ ):
+ database = Database()
+ database._add_warehouse_tables(_catalog_node(warehouse_tables))
+ database._add_views(_catalog_node(views))
+
+ with self.assertRaises(QueryError) as error:
+ database.get_table(missing_table)
+
+ assert str(error.exception) == expected_message
+
+
class TestDatabase(BaseTest, QueryMatchingTest):
snapshot: Any
allow_dual_schema_snapshots = True