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): fix 500 running metrics with multi-line sql (#72439)

Thiago Salvatore4bc589873489
diff --git a/products/data_catalog/backend/logic/execution.py b/products/data_catalog/backend/logic/execution.py
index 9eaad85bdb7d..f77e54cf3f4d 100644
--- a/products/data_catalog/backend/logic/execution.py
+++ b/products/data_catalog/backend/logic/execution.py
@@ -188,16 +188,17 @@ def _markdown_envelope(metric: Metric, is_drifted: bool) -> dict:
def _deep_link(team: Team, prepared_query: dict) -> str:
+ # absolute_uri must only see the literal path: the encoded query payload contains %5C for any
+ # backslash json.dumps emits (e.g. multi-line SQL), which absolute_uri rejects as an authority
+ # bypass. The payload is appended after resolving, outside the routing part being validated.
if prepared_query.get("kind") == HOGQL_DEFINITION_KIND:
# A JSON node in open_query prefills the SQL editor with the full query, values included;
# a bare SQL string would drop the values.
node = {"kind": "DataVisualizationNode", "source": prepared_query}
- path = f"/project/{team.id}/sql?open_query={quote(json.dumps(node))}"
- else:
- # The insight scene expects the InsightVizNode wrapper insights themselves store in #q=.
- node = {"kind": "InsightVizNode", "source": prepared_query}
- path = f"/project/{team.id}/insights/new#q={quote(json.dumps(node))}"
- return absolute_uri(path)
+ return absolute_uri(f"/project/{team.id}/sql") + f"?open_query={quote(json.dumps(node))}"
+ # The insight scene expects the InsightVizNode wrapper insights themselves store in #q=.
+ node = {"kind": "InsightVizNode", "source": prepared_query}
+ return absolute_uri(f"/project/{team.id}/insights/new") + f"#q={quote(json.dumps(node))}"
def _touch_last_run(team: Team, metric: Metric) -> None:
diff --git a/products/data_catalog/backend/tests/test_execution.py b/products/data_catalog/backend/tests/test_execution.py
index 79d4e77bd140..1f0b35dcd932 100644
--- a/products/data_catalog/backend/tests/test_execution.py
+++ b/products/data_catalog/backend/tests/test_execution.py
@@ -126,7 +126,13 @@ class TestMetricRunPreparation(APIBaseTest):
assert linked["source"] == executed
def test_hogql_values_round_trip_through_deep_link(self) -> None:
- definition = {"kind": "HogQLQuery", "query": "select count() from events", "values": {"threshold": 10}}
+ # Multi-line SQL: json.dumps escapes the newline with a backslash, which quote() encodes as
+ # %5C — a byte absolute_uri rejects. The deep link must survive real, multi-line queries.
+ definition = {
+ "kind": "HogQLQuery",
+ "query": "select count()\nfrom events\nwhere event = 'purchase'",
+ "values": {"threshold": 10},
+ }
_, envelope = self._run(definition)
linked = _decoded_sql_editor_link(envelope["posthog_url"])
assert linked["kind"] == "DataVisualizationNode"