: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): suggest to<Type> cast for unknown int/float/string calls (#65207)
posthog[bot]58b5a0783cf8
diff --git a/posthog/hogql/printer/base.py b/posthog/hogql/printer/base.pyindex 43ef15750dc1..6c474d591a6c 100644--- a/posthog/hogql/printer/base.py+++ b/posthog/hogql/printer/base.py@@ -1074,6 +1074,18 @@ class BasePrinter(Visitor[str]):if passthrough is not None:return passthrough+ # SQL/Python-style cast names (int, float, string, uuid, …) map to HogQL's+ # to<Type> functions. Prefer that hint over the lexically-nearest name — for a+ # short input like "int" the closest match is the operator "in", which sends+ # users down the wrong path instead of toward "toInt". Match case-insensitively+ # so casts with internal capitals (toUUID, toDateTime) are found and the+ # canonically-cased name is suggested.+ cast_suggestion = {name.lower(): name for name in ALL_EXPOSED_FUNCTION_NAMES}.get(f"to{node.name}".lower())+ if cast_suggestion is not None:+ raise QueryError(+ f"Unsupported function call '{node.name}(...)'. Perhaps you meant '{cast_suggestion}(...)'?"+ )+close_matches = get_close_matches(node.name, ALL_EXPOSED_FUNCTION_NAMES, 1)if len(close_matches) > 0:raise QueryError(diff --git a/posthog/hogql/printer/test/test_printer.py b/posthog/hogql/printer/test/test_printer.pyindex 24e1bb07ced9..5f8cb23c7bd6 100644--- a/posthog/hogql/printer/test/test_printer.py+++ b/posthog/hogql/printer/test/test_printer.py@@ -1298,6 +1298,22 @@ class TestPrinter(BaseTest):"tostring(event)","Unsupported function call 'tostring(...)'. Perhaps you meant 'toString(...)'?",)+ self._assert_expr_error(+ "int(event)",+ "Unsupported function call 'int(...)'. Perhaps you meant 'toInt(...)'?",+ )+ self._assert_expr_error(+ "float(event)",+ "Unsupported function call 'float(...)'. Perhaps you meant 'toFloat(...)'?",+ )+ self._assert_expr_error(+ "string(event)",+ "Unsupported function call 'string(...)'. Perhaps you meant 'toString(...)'?",+ )+ self._assert_expr_error(+ "uuid(event)",+ "Unsupported function call 'uuid(...)'. Perhaps you meant 'toUUID(...)'?",+ )self._assert_expr_error("yeet.the.cloud", "Unable to resolve field: yeet")self._assert_expr_error("chipotle", "Unable to resolve field: chipotle")self._assert_expr_error(