: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): preserve debug errors on empty clickhouse sql (#60623)
posthog[bot]93678a87ac22
diff --git a/posthog/hogql/query.py b/posthog/hogql/query.pyindex 928502261400..cdffda71bf43 100644--- a/posthog/hogql/query.py+++ b/posthog/hogql/query.py@@ -885,7 +885,13 @@ class HogQLQueryExecutor:if prepared_execution.engine == "direct_postgres":self._execute_direct_postgres_query()elif self.clickhouse_sql is not None:- self._execute_clickhouse_query()+ if self.clickhouse_sql == "":+ self.results = []+ self.types = []+ if self.error is None:+ self.error = "Unknown error"+ else:+ self._execute_clickhouse_query()finally:# Side-channel: push collected warnings to the query-runner-level accumulator even on# failure. The warning is often the actual explanation for the query error (e.g. adiff --git a/posthog/hogql/test/test_query.py b/posthog/hogql/test/test_query.pyindex 2966b18716e2..8fc9ab626887 100644--- a/posthog/hogql/test/test_query.py+++ b/posthog/hogql/test/test_query.py@@ -26,6 +26,7 @@ from posthog.schema import (from posthog.hogql import astfrom posthog.hogql.direct_connection import INVALID_CONNECTION_ID_ERRORfrom posthog.hogql.errors import ExposedHogQLError, QueryError+from posthog.hogql.printer import prepare_ast_for_printing as unmocked_prepare_ast_for_printingfrom posthog.hogql.property import property_to_exprfrom posthog.hogql.query import execute_hogql_queryfrom posthog.hogql.test.utils import (@@ -2039,3 +2040,17 @@ class TestQuery(ClickhouseTestMixin, APIBaseTest):response = execute_hogql_query(query, team=self.team, modifiers=HogQLQueryModifiers(debug=True))assert response and response.metadata and response.metadata.ch_table_namesassert any("sessions" in name for name in response.metadata.ch_table_names)++ @patch("posthog.hogql.query.sync_execute")+ def test_debug_mode_preserves_prepare_error_without_executing_clickhouse(self, mock_sync_execute):+ def prepare_ast_for_printing_side_effect(*args, **kwargs):+ if kwargs.get("dialect") == "clickhouse":+ raise ExposedHogQLError("debug failure")+ return unmocked_prepare_ast_for_printing(*args, **kwargs)++ with patch("posthog.hogql.query.prepare_ast_for_printing", side_effect=prepare_ast_for_printing_side_effect):+ response = execute_hogql_query("SELECT 1", team=self.team, modifiers=HogQLQueryModifiers(debug=True))++ self.assertEqual(response.error, "debug failure")+ self.assertEqual(response.clickhouse, "")+ mock_sync_execute.assert_not_called()diff --git a/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_from_query.ambr b/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_from_query.ambrindex 4d81dc6a2021..800a3fff4b5e 100644--- a/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_from_query.ambr+++ b/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_from_query.ambr@@ -1122,7 +1122,7 @@WHERE and(equals(s.team_id, 99999), greaterOrEquals(s.min_first_timestamp, toDateTime64('2020-12-29 00:00:00.000000', 6, 'UTC')), lessOrEquals(s.min_first_timestamp, toDateTime64('today', 6, 'UTC')), globalIn(s.session_id,(SELECT nullIf(nullIf(events.`$session_id`, ''), 'null') AS session_idFROM events- WHERE and(equals(events.team_id, 99999), notEmpty(nullIf(nullIf(events.`$session_id`, ''), 'null')), lessOrEquals(events.timestamp, now64(6, 'UTC')), equals(events.mat_pp_email, 'bla'), greaterOrEquals(events.timestamp, toDateTime64('2020-12-28 00:00:00.000000', 6, 'UTC')))+ WHERE and(equals(events.team_id, 99999), notEmpty(nullIf(nullIf(events.`$session_id`, ''), 'null')), lessOrEquals(events.timestamp, now64(6, 'UTC')), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.person_properties, 'email'), ''), 'null'), '^"|"$', ''), 'bla'), 0), greaterOrEquals(events.timestamp, toDateTime64('2020-12-28 00:00:00.000000', 6, 'UTC')))GROUP BY nullIf(nullIf(events.`$session_id`, ''), 'null')HAVING 1ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESC