: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(query): treat clickhouse bad_get as a user query error (#114307)
posthog[bot]7329e67e23d8
diff --git a/posthog/errors.py b/posthog/errors.pyindex ad93fa659499..c43e14e117d3 100644--- a/posthog/errors.py+++ b/posthog/errors.py@@ -86,6 +86,12 @@ def clickhouse_error_type(e: Exception) -> str:STORAGE_FILE_URI_PATTERN = re.compile(r"\(in file/uri ([^)]+)\)")+BAD_GET_TYPE_MISMATCH_PATTERN = re.compile(+ r"\bBad get: has (?P<actual>[A-Za-z][A-Za-z0-9_]{0,63}), "+ r"requested (?P<expected>[A-Za-z][A-Za-z0-9_]{0,63})(?:\.|$)"+)++BAD_GET_FALLBACK_MESSAGE = "A value in the query has the wrong type. Check function parameters and type conversions."CORRUPTED_PARQUET_METADATA_MESSAGE = ("A Parquet file backing this table has corrupted or oversized metadata and can't be read. "@@ -106,6 +112,17 @@ def _wrap_storage_file_changed_error(err: ServerException) -> "CHQueryErrorS3Fil)+def _wrap_bad_get_error(err: ServerException) -> "CHQueryErrorBadGet":+ match = BAD_GET_TYPE_MISMATCH_PATTERN.search(err.message)+ message = BAD_GET_FALLBACK_MESSAGE+ if match:+ message = (+ f"A value in the query has the wrong type: got {match.group('actual')}, "+ f"expected {match.group('expected')}. Check function parameters and type conversions."+ )+ return CHQueryErrorBadGet(message, code=err.code, code_name="bad_get")++def wrap_clickhouse_query_error(err: Exception) -> Exception:"Beautifies clickhouse client errors, using custom error classes for every code"if not isinstance(err, ServerException):@@ -171,6 +188,8 @@ def wrap_clickhouse_query_error(err: Exception) -> Exception:return CHQueryErrorQueryWasCancelled(err.message, code=err.code, code_name="query_was_cancelled")# user query errors - pass through original message with proper code_name+ elif name == "BAD_GET":+ return _wrap_bad_get_error(err)elif name == "ILLEGAL_TYPE_OF_ARGUMENT":return CHQueryErrorIllegalTypeOfArgument(err.message, code=err.code, code_name="illegal_type_of_argument")elif name == "NO_COMMON_TYPE":@@ -274,6 +293,10 @@ class CHQueryErrorIllegalTypeOfArgument(ExposedCHQueryError):pass+class CHQueryErrorBadGet(ExposedCHQueryError):+ pass++class CHQueryErrorNoCommonType(ExposedCHQueryError):pass@@ -534,7 +557,8 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {167: ErrorCodeMeta("TOO_DEEP_AST", category=QueryErrorCategory.QUERY_PERFORMANCE_ERROR),168: ErrorCodeMeta("TOO_BIG_AST", category=QueryErrorCategory.QUERY_PERFORMANCE_ERROR),169: ErrorCodeMeta("BAD_TYPE_OF_FIELD"),- 170: ErrorCodeMeta("BAD_GET"),+ # The wrapper exposes only allowlisted ClickHouse type identifiers from the raw message.+ 170: ErrorCodeMeta("BAD_GET", user_safe=True),172: ErrorCodeMeta("CANNOT_CREATE_DIRECTORY"),173: ErrorCodeMeta("CANNOT_ALLOCATE_MEMORY", category=QueryErrorCategory.QUERY_PERFORMANCE_ERROR),174: ErrorCodeMeta("CYCLIC_ALIASES", user_safe=True),diff --git a/posthog/test/test_errors.py b/posthog/test/test_errors.pyindex 79f85e4a221b..dbdb5de61ba1 100644--- a/posthog/test/test_errors.py+++ b/posthog/test/test_errors.py@@ -2,6 +2,8 @@ from clickhouse_driver.errors import ServerExceptionfrom parameterized import parameterizedfrom posthog.errors import (+ BAD_GET_FALLBACK_MESSAGE,+ CHQueryErrorBadGet,CHQueryErrorCannotParseBool,CHQueryErrorCannotParseUuid,CHQueryErrorInvalidJoinOnExpression,@@ -150,6 +152,39 @@ class TestWrapClickhouseQueryError:assert isinstance(wrapped, error_type)+ def test_bad_get_exposes_type_names_without_clickhouse_context(self) -> None:+ err = ServerException(+ "DB::Exception: Bad get: has Float64, requested UInt64. "+ "While processing private-row-value. Stack trace: private-stack-frame",+ code=170,+ nested=ServerException("fake-nested-secret", code=1000),+ )++ wrapped = wrap_clickhouse_query_error(err)++ assert isinstance(wrapped, CHQueryErrorBadGet)+ assert str(wrapped) == (+ "A value in the query has the wrong type: got Float64, expected UInt64. "+ "Check function parameters and type conversions."+ )+ assert "private" not in wrapped.message+ assert wrapped.code == 170+ assert wrapped.code_name == "bad_get"+ assert wrapped.nested is None+ assert look_up_clickhouse_error_code_meta(err).get_category() == QueryErrorCategory.USER_ERROR++ def test_bad_get_hides_unrecognized_raw_message(self) -> None:+ err = ServerException(+ "DB::Exception: Bad get: has private-row-value, requested UInt64. While processing private-query-context.",+ code=170,+ )++ wrapped = wrap_clickhouse_query_error(err)++ assert isinstance(wrapped, CHQueryErrorBadGet)+ assert str(wrapped) == BAD_GET_FALLBACK_MESSAGE+ assert "private" not in wrapped.message+@parameterized.expand([# NETWORK_ERROR (210) is a genuine server-side fault and must not be exposed.