: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): return empty results when query compiles to empty clickhouse sql (#67470)
posthog[bot]b3697d7a1a65
diff --git a/posthog/hogql/query.py b/posthog/hogql/query.pyindex 2b95eeb2d94f..fe9abcfa290c 100644--- a/posthog/hogql/query.py+++ b/posthog/hogql/query.py@@ -610,9 +610,18 @@ class HogQLQueryExecutor:@tracer.start_as_current_span("HogQLQueryExecutor._execute_clickhouse_query")def _execute_clickhouse_query(self):- assert self.clickhouse_sql+ # A None prepared AST compiles to empty SQL: nothing to run, so return empty results.+ # execute() guards this, but direct callers (e.g. web-analytics events prefilter) don't.+ # A None (vs "") clickhouse_sql means _prepare_execution() never ran — surface that.+ if self.clickhouse_sql == "":+ self.results = []+ self.types = []+ return+ if self.clickhouse_sql is None:+ raise ValueError("Cannot execute ClickHouse query: SQL was not prepared")clickhouse_context = self.clickhouse_context- assert clickhouse_context is not None+ if clickhouse_context is None:+ raise ValueError("Cannot execute ClickHouse query: ClickHouse context was not prepared")timings_dict = self.timings.to_dict()with self.timings.measure("clickhouse_execute"):with self.timings.measure("extract_hogql_features"):diff --git a/posthog/hogql/test/test_query.py b/posthog/hogql/test/test_query.pyindex a9b6cd7e7438..778f6d33d7b9 100644--- a/posthog/hogql/test/test_query.py+++ b/posthog/hogql/test/test_query.py@@ -28,7 +28,7 @@ from posthog.hogql.direct_connection import INVALID_CONNECTION_ID_ERROR, get_dirfrom posthog.hogql.errors import ExposedHogQLError, QueryErrorfrom posthog.hogql.printer import prepare_ast_for_printing as unmocked_prepare_ast_for_printingfrom posthog.hogql.property import property_to_expr-from posthog.hogql.query import execute_hogql_query+from posthog.hogql.query import HogQLQueryExecutor, execute_hogql_queryfrom posthog.hogql.test.utils import (execute_hogql_query_with_timings,pretty_print_in_tests,@@ -374,6 +374,18 @@ class TestQuery(ClickhouseTestMixin, APIBaseTest):self.assertEqual(str(error.exception), INVALID_CONNECTION_ID_ERROR)mock_sync_execute.assert_not_called()+ @patch("posthog.hogql.query.sync_execute")+ def test_execute_clickhouse_query_short_circuits_on_empty_sql(self, mock_sync_execute):+ # Empty SQL (None prepared AST) used to trip a bare assert; direct callers need empty results.+ executor = HogQLQueryExecutor(query="select 1", team=self.team)+ executor.clickhouse_sql = ""++ executor._execute_clickhouse_query()++ self.assertEqual(executor.results, [])+ self.assertEqual(executor.types, [])+ mock_sync_execute.assert_not_called()+@pytest.mark.usefixtures("unittest_snapshot")def test_query_joins_pdi_persons(self):with freeze_time("2020-01-10"):