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.

perf(hogql): remove raw-query shadow translation (#83286)

Bill Guowei Yang7c8f29162bf3
diff --git a/posthog/hogql/query.py b/posthog/hogql/query.py
index fe45164f9b48..c53826ca0f1b 100644
--- a/posthog/hogql/query.py
+++ b/posthog/hogql/query.py
@@ -62,7 +62,6 @@ from posthog.clickhouse.client import sync_execute
from posthog.clickhouse.client.connection import ClickHouseUser, Workload
from posthog.clickhouse.query_tagging import tag_queries
from posthog.errors import CHQueryErrorS3Error, CHQueryErrorS3FileChangedDuringRead, ExposedCHQueryError
-from posthog.exceptions_capture import capture_exception
from posthog.models.team import Team
from posthog.models.user import User
from posthog.rbac.user_access_control import UserAccessControl
@@ -73,7 +72,7 @@ tracer = trace.get_tracer(__name__)
TRANSIENT_S3_ERROR_RETRY_DELAY_SECONDS = 1.0
[email protected](frozen=False)
class HogQLQueryExecutor:
query: Union[str, ast.SelectQuery, ast.SelectSetQuery] | None
team: Team
@@ -612,48 +611,6 @@ class HogQLQueryExecutor:
self.direct_sql = adapter.prepare_raw_sql(str(self.query))
self._execute_direct_sql_query()
- def _capture_send_raw_query_translation_error(self) -> None:
- """Try a post-success HogQL translation for raw queries.
-
- On success, this stores the translated HogQL in ``self.hogql`` for the response.
- On failure, it records the exception for telemetry and leaves ``self.hogql`` unset.
-
- This runs synchronously after the raw query succeeds, so it adds the cost of
- ``_prepare_execution()`` to raw-query responses.
- """
- if not isinstance(self.query, str) or self.connection_id is None:
- return
-
- try:
- shadow_executor = HogQLQueryExecutor(
- query=str(self.query),
- team=self.team,
- query_type=self.query_type,
- filters=self.filters,
- placeholders=self.placeholders,
- variables=self.variables,
- workload=self.workload,
- settings=self.settings,
- modifiers=self.modifiers,
- limit_context=self.limit_context,
- pretty=self.pretty,
- connection_id=self.connection_id,
- user=self.user,
- )
- shadow_executor._prepare_execution()
- self.hogql = shadow_executor.hogql
- except Exception as error:
- capture_exception(
- error,
- {
- "component": "send_raw_query_parse_and_print",
- "send_raw_query": True,
- "team_id": self.team.pk,
- "connection_id": self.connection_id,
- "query_type": self.query_type,
- },
- )
-
@tracer.start_as_current_span("HogQLQueryExecutor._execute_clickhouse_query")
def _execute_clickhouse_query(self):
# A None prepared AST compiles to empty SQL: nothing to run, so return empty results.
@@ -755,7 +712,6 @@ class HogQLQueryExecutor:
try:
if self.send_raw_query and self.connection_id is not None:
self._execute_raw_direct_query()
- self._capture_send_raw_query_translation_error()
else:
prepared_execution = self._prepare_execution()
diff --git a/posthog/hogql/test/test_direct_motherduck_query.py b/posthog/hogql/test/test_direct_motherduck_query.py
index 00766117bdcf..11fc3759c1b9 100644
--- a/posthog/hogql/test/test_direct_motherduck_query.py
+++ b/posthog/hogql/test/test_direct_motherduck_query.py
@@ -111,8 +111,7 @@ class TestDirectMotherDuckQuery(APIBaseTest):
send_raw_query=True,
)
with patch(_TRANSPORT_CONNECT, side_effect=_local_duckdb):
- with patch.object(HogQLQueryExecutor, "_capture_send_raw_query_translation_error"):
- response = executor.execute()
+ response = executor.execute()
self.assertEqual(response.results, [(3, "ny")])
self.assertEqual(response.types, [("n", "Int64"), ("c", "String")])
diff --git a/posthog/hogql/test/test_direct_mysql_query.py b/posthog/hogql/test/test_direct_mysql_query.py
index 2041c59a1fd5..75d8b7287d63 100644
--- a/posthog/hogql/test/test_direct_mysql_query.py
+++ b/posthog/hogql/test/test_direct_mysql_query.py
@@ -309,8 +309,7 @@ class TestDirectMySQLQuery(APIBaseTest):
"posthog.hogql.direct_sql.mysql_adapter.MySQLAdapter.validate_source_config",
return_value=(implementation, MagicMock()),
):
- with patch.object(HogQLQueryExecutor, "_capture_send_raw_query_translation_error"):
- response = executor.execute()
+ response = executor.execute()
self.assertEqual(executor.direct_dialect, "mysql")
self.assertEqual(executor.direct_sql, "SELECT 1")
@@ -349,8 +348,7 @@ class TestDirectMySQLQuery(APIBaseTest):
"posthog.hogql.direct_sql.mysql_adapter.MySQLAdapter.validate_source_config",
return_value=(implementation, MagicMock()),
):
- with patch.object(HogQLQueryExecutor, "_capture_send_raw_query_translation_error"):
- response = executor.execute()
+ response = executor.execute()
self.assertEqual(executor.direct_sql, query)
self.assertEqual(response.results, [(1,)])
diff --git a/posthog/hogql/test/test_direct_postgres_query.py b/posthog/hogql/test/test_direct_postgres_query.py
index e8c27a97d659..2ccc83f18565 100644
--- a/posthog/hogql/test/test_direct_postgres_query.py
+++ b/posthog/hogql/test/test_direct_postgres_query.py
@@ -1053,11 +1053,8 @@ class TestDirectPostgresQuery(APIBaseTest):
f"USE {escape_postgres_identifier(source.job_inputs['schema'])}"
)
- @patch("posthog.hogql.query.capture_exception")
@patch("posthog.hogql.direct_sql.postgres_adapter.psycopg.connect")
- def test_send_raw_query_executes_raw_query_and_preserves_hogql_when_printable(
- self, mock_connect, mock_capture_exception
- ):
+ def test_send_raw_query_executes_raw_query_without_hogql_preparation(self, mock_connect):
source = ExternalDataSource.objects.create(
team=self.team,
source_id="source_id",
@@ -1092,21 +1089,21 @@ class TestDirectPostgresQuery(APIBaseTest):
send_raw_query=True,
)
- response = executor.execute()
+ with patch.object(HogQLQueryExecutor, "_prepare_execution") as mock_prepare_execution:
+ response = executor.execute()
self.assertEqual(response.results, [(1,)])
self.assertEqual(response.clickhouse, "SELECT 1 AS value")
self.assertEqual(response.columns, ["value"])
- self.assertIsNotNone(response.hogql)
+ self.assertIsNone(response.hogql)
+ mock_prepare_execution.assert_not_called()
mocked_connection.execute.assert_called_once_with(
f"SET search_path TO {escape_postgres_identifier(source.job_inputs['schema'])}"
)
mocked_cursor.execute.assert_called_once_with("SELECT 1 AS value", None)
- mock_capture_exception.assert_not_called()
- @patch("posthog.hogql.query.capture_exception")
@patch("posthog.hogql.direct_sql.postgres_adapter.psycopg.connect")
- def test_send_raw_query_captures_parse_failures_after_success(self, mock_connect, mock_capture_exception):
+ def test_send_raw_query_unsupported_by_hogql_succeeds_without_hogql(self, mock_connect):
source = ExternalDataSource.objects.create(
team=self.team,
source_id="source_id",
@@ -1148,11 +1145,9 @@ class TestDirectPostgresQuery(APIBaseTest):
self.assertEqual(response.columns, ["value"])
self.assertIsNone(response.hogql)
mocked_cursor.execute.assert_called_once_with("SELECT 1 IS TRUE AS value", None)
- mock_capture_exception.assert_called_once()
- @patch("posthog.hogql.query.capture_exception")
@patch("posthog.hogql.direct_sql.postgres_adapter.psycopg.connect")
- def test_send_raw_query_uses_catalog_for_duckdb_without_schema(self, mock_connect, mock_capture_exception):
+ def test_send_raw_query_uses_catalog_for_duckdb_without_schema(self, mock_connect):
source = ExternalDataSource.objects.create(
team=self.team,
source_id="source_id",
@@ -1193,7 +1188,6 @@ class TestDirectPostgresQuery(APIBaseTest):
self.assertEqual(response.results, [(1,)])
mocked_connection.execute.assert_called_once_with("USE ducklake")
mocked_cursor.execute.assert_called_once_with("SELECT 1 AS value", None)
- mock_capture_exception.assert_not_called()
@patch("posthog.hogql.direct_sql.postgres_adapter.psycopg.connect")
def test_send_raw_query_skips_session_setup_when_schema_is_blank(self, mock_connect):
diff --git a/posthog/hogql/test/test_direct_redshift_query.py b/posthog/hogql/test/test_direct_redshift_query.py
index 0bcce5bee169..6d75bb43b2a0 100644
--- a/posthog/hogql/test/test_direct_redshift_query.py
+++ b/posthog/hogql/test/test_direct_redshift_query.py
@@ -170,9 +170,8 @@ class TestDirectRedshiftQuery(APIBaseTest):
"posthog.hogql.direct_sql.redshift_adapter.RedshiftAdapter.validate_source_config",
return_value=(implementation, MagicMock()),
):
- with patch.object(HogQLQueryExecutor, "_capture_send_raw_query_translation_error"):
- with self.assertRaisesRegex(ExposedHogQLError, "Add a LIMIT clause"):
- executor.execute()
+ with self.assertRaisesRegex(ExposedHogQLError, "Add a LIMIT clause"):
+ executor.execute()
def test_execute_raises_exposed_error_when_ssh_tunnel_fails(self):
source = self._create_source()
@@ -191,9 +190,8 @@ class TestDirectRedshiftQuery(APIBaseTest):
"posthog.hogql.direct_sql.redshift_adapter.RedshiftAdapter.validate_source_config",
return_value=(implementation, MagicMock()),
):
- with patch.object(HogQLQueryExecutor, "_capture_send_raw_query_translation_error"):
- with self.assertRaises(ExposedHogQLError) as error:
- executor.execute()
+ with self.assertRaises(ExposedHogQLError) as error:
+ executor.execute()
self.assertEqual(str(error.exception), "Could not establish session to SSH gateway")
diff --git a/posthog/hogql/test/test_direct_snowflake_query.py b/posthog/hogql/test/test_direct_snowflake_query.py
index 5007ea5fc853..55fef9d91f66 100644
--- a/posthog/hogql/test/test_direct_snowflake_query.py
+++ b/posthog/hogql/test/test_direct_snowflake_query.py
@@ -185,8 +185,7 @@ class TestDirectSnowflakeQuery(APIBaseTest):
"posthog.hogql.direct_sql.snowflake_adapter.SnowflakeAdapter.validate_source_config",
return_value=(implementation, MagicMock()),
):
- with patch.object(HogQLQueryExecutor, "_capture_send_raw_query_translation_error"):
- response = executor.execute()
+ response = executor.execute()
executed_statements = [call.args[0] for call in cursor.execute.call_args_list]
# Read-only is enforced our side: pin single-statement and the statement timeout
@@ -220,9 +219,8 @@ class TestDirectSnowflakeQuery(APIBaseTest):
"posthog.hogql.direct_sql.snowflake_adapter.SnowflakeAdapter.validate_source_config",
return_value=(implementation, MagicMock()),
):
- with patch.object(HogQLQueryExecutor, "_capture_send_raw_query_translation_error"):
- with self.assertRaisesRegex(ExposedHogQLError, "Add a LIMIT clause"):
- executor.execute()
+ with self.assertRaisesRegex(ExposedHogQLError, "Add a LIMIT clause"):
+ executor.execute()
@parameterized.expand(
[
@@ -304,8 +302,7 @@ class TestDirectSnowflakeQuery(APIBaseTest):
"posthog.hogql.direct_sql.snowflake_adapter.SnowflakeAdapter.validate_source_config",
return_value=(implementation, MagicMock()),
):
- with patch.object(HogQLQueryExecutor, "_capture_send_raw_query_translation_error"):
- executor.execute()
+ executor.execute()
# The statement passed the read-only gate and reached execution unchanged.
self.assertEqual(executor.direct_sql, query)
diff --git a/posthog/test/dataclass_frozen_baseline.txt b/posthog/test/dataclass_frozen_baseline.txt
index b5a6abd6450c..91a0fd5ad5a2 100644
--- a/posthog/test/dataclass_frozen_baseline.txt
+++ b/posthog/test/dataclass_frozen_baseline.txt
@@ -30,7 +30,6 @@
1 posthog/hogql/functions/core.py
1 posthog/hogql/observability.py
1 posthog/hogql/printer/types.py
-1 posthog/hogql/query.py
1 posthog/hogql/scripts/_diagnostic_common.py
1 posthog/hogql/snowflake_connection_cache.py
1 posthog/hogql/transforms/federated_join_merge.py