: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(notebooks): reject logs-cluster tables in frame materialization (#118099)
posthog[bot]1c82ed6cb878
diff --git a/products/notebooks/backend/temporal/frame_materialize.py b/products/notebooks/backend/temporal/frame_materialize.pyindex 9eb383f9cadd..5bec8408a0d3 100644--- a/products/notebooks/backend/temporal/frame_materialize.py+++ b/products/notebooks/backend/temporal/frame_materialize.py@@ -53,7 +53,7 @@ from posthog.schema import QueryStatusfrom posthog.hogql import astfrom posthog.hogql.constants import HogQLGlobalSettings, LimitContextfrom posthog.hogql.context import HogQLContext-from posthog.hogql.errors import ExposedHogQLError+from posthog.hogql.errors import ExposedHogQLError, QueryErrorfrom posthog.hogql.parser import parse_selectfrom posthog.hogql.query import HogQLQueryExecutorfrom posthog.hogql.visitor import clone_expr@@ -157,6 +157,9 @@ _RESULT_SIZE_MESSAGE = ("The materialized result is too large (over the frame size budget). ""Select fewer columns or aggregate before materializing.")+_LOGS_CLUSTER_MESSAGE = (+ "Notebooks can't load logs, trace spans, or metrics yet. Run this query in the SQL editor instead."+)_MID_STREAM_ERROR_MESSAGE = "The query failed while its result was streaming. Adjust it and re-run."# ClickHouse exception codes worth a specific user-facing message when a query dies# mid-stream: 158 TOO_MANY_ROWS, 241 MEMORY_LIMIT_EXCEEDED, 307 TOO_MANY_BYTES,@@ -432,6 +435,10 @@ def _generate_sql(context.values.clear()sql, resolved = executor.generate_clickhouse_sql()seconds = time.perf_counter() - started+ # Both write paths connect to the main cluster, and logs-cluster tables exist only on the+ # logs cluster. Fail before the DESCRIBE so the user sees why, not an unknown-table error.+ if resolved.workload == Workload.LOGS:+ raise QueryError(_LOGS_CLUSTER_MESSAGE)# The executor keeps the database it built on its own `dataclasses.replace` copy, so# without this hand-back the shared context still looks empty and the next pass builds a# second one. It resets the per-execution state it shares by reference, so a databasediff --git a/products/notebooks/backend/test/test_frame_materialize.py b/products/notebooks/backend/test/test_frame_materialize.pyindex 9e7b338ffbb2..cae85c1391e3 100644--- a/products/notebooks/backend/test/test_frame_materialize.py+++ b/products/notebooks/backend/test/test_frame_materialize.py@@ -150,6 +150,35 @@ class TestFrameMaterializeEnqueue(APIBaseTest):self.assertTrue(status.complete and status.error)self.assertIn(expected_message, status.error_message or "")+ @parameterized.expand(+ [+ ("trace_spans_streaming", "select 1 from posthog.trace_spans", False),+ ("logs_ch_writes", "select 1 from logs", True),+ ("metrics_streaming", "select 1 from posthog.metrics", False),+ ]+ )+ def test_logs_cluster_table_is_rejected_before_clickhouse(self, _name, query, ch_writes):+ # These tables exist only on the logs cluster, and neither write path connects there.+ # Without the early rejection the DESCRIBE fails with an unknown-table error.+ inputs, manager = _registered_inputs(+ self.team.id, self.notebook.short_id, self.user.id, query=query, ch_writes=ch_writes+ )++ with (+ patch.object(frame_materialize, "_materialize_slots"),+ patch.object(frame_materialize, "_describe_columns") as describe,+ patch.object(frame_materialize, "_describe_columns_pooled") as describe_pooled,+ ):+ with self.assertRaises(exceptions.ApplicationError) as caught:+ frame_materialize.materialize_frame(inputs)++ describe.assert_not_called()+ describe_pooled.assert_not_called()+ self.assertTrue(caught.exception.non_retryable)+ status = manager.get_query_status()+ self.assertTrue(status.complete and status.error)+ self.assertIn("SQL editor", status.error_message or "")+def test_mid_stream_failure_removes_the_corrupt_object_and_surfaces_the_real_error(self):# ClickHouse streams 200 before execution finishes; a mid-stream failure can close# the body cleanly with exception text appended instead of tearing the read. Without