: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(insights): cast toFloatOrDefault default value to float (#58714)
Sam Pennington3c032f4dce93
diff --git a/posthog/hogql/functions/clickhouse/conversions.py b/posthog/hogql/functions/clickhouse/conversions.pyindex 41c6edae9fbf..7e7f52834bfe 100644--- a/posthog/hogql/functions/clickhouse/conversions.py+++ b/posthog/hogql/functions/clickhouse/conversions.py@@ -48,14 +48,25 @@ TYPE_CONVERSION_FUNCTIONS: dict[str, HogQLFunctionMeta] = {"toFloat": HogQLFunctionMeta("accurateCastOrNull", 1, 1, suffix_args=[ast.Constant(value="Float64")]),"toFloatOrZero": HogQLFunctionMeta("toFloat64OrZero", 1, 1, signatures=[((StringType(),), FloatType())]),"toFloatOrDefault": HogQLFunctionMeta(- "toFloat64OrDefault",- 1,+ # ClickHouse's toFloat64OrDefault requires the default value to already be+ # Float64 — passing e.g. an integer 0 raises "Default value type should be+ # same as cast type". Cast the default so any numeric/string literal works.+ "toFloat64OrDefault({0}, accurateCast({1}, 'Float64'))",+ 2,2,+ using_placeholder_arguments=True,+ using_positional_arguments=True,+ # The default arg (second) may be an integer or float literal — the+ # template casts it to Float64 either way, so both must resolve.signatures=[((DecimalType(), FloatType()), FloatType()),+ ((DecimalType(), IntegerType()), FloatType()),((IntegerType(), FloatType()), FloatType()),+ ((IntegerType(), IntegerType()), FloatType()),((FloatType(), FloatType()), FloatType()),+ ((FloatType(), IntegerType()), FloatType()),((StringType(), FloatType()), FloatType()),+ ((StringType(), IntegerType()), FloatType()),],),"toDecimal": HogQLFunctionMeta(diff --git a/posthog/hogql/printer/postgres_functions.py b/posthog/hogql/printer/postgres_functions.pyindex ac8a0f5771cf..08193c31da48 100644--- a/posthog/hogql/printer/postgres_functions.py+++ b/posthog/hogql/printer/postgres_functions.py@@ -289,6 +289,8 @@ POSTGRES_FUNCTION_HANDLERS: dict[str, Callable[[list[str]], str]] = {# More type conversions"toDecimal": _make_cast_handler("DECIMAL"),"toDateTime64": _make_cast_handler("TIMESTAMP"),+ # Postgres backend intentionally drops the default arg — _make_cast_handler+ # only uses args[0], so this is a plain CAST and the fallback is unsupported."toFloatOrDefault": _make_cast_handler("DOUBLE PRECISION"),# More JSON"JSONExtractUInt": _make_json_cast_handler("INTEGER"),diff --git a/posthog/hogql/printer/test/test_printer.py b/posthog/hogql/printer/test/test_printer.pyindex 52644fedbea5..cde8ad3b2dd0 100644--- a/posthog/hogql/printer/test/test_printer.py+++ b/posthog/hogql/printer/test/test_printer.py@@ -5988,7 +5988,7 @@ class TestPostgresPrinter(BaseTest):("toInt", "toInt(3.14)", "CAST(3.14 AS BIGINT)"),("toFloat", "toFloat(1)", "CAST(1 AS DOUBLE PRECISION)"),("toFloatOrZero", "toFloatOrZero('1.5')", "CAST(%(hogql_val_0)s AS DOUBLE PRECISION)"),- ("toFloatOrDefault", "toFloatOrDefault('1.5')", "CAST(%(hogql_val_0)s AS DOUBLE PRECISION)"),+ ("toFloatOrDefault", "toFloatOrDefault('1.5', 0)", "CAST(%(hogql_val_0)s AS DOUBLE PRECISION)"),("toIntOrZero", "toIntOrZero('42')", "CAST(%(hogql_val_0)s AS BIGINT)"),("toBool", "toBool(1)", "CAST(1 AS BOOLEAN)"),("toUUID", "toUUID('abc')", "CAST(%(hogql_val_0)s AS UUID)"),diff --git a/posthog/hogql/test/test_mapping.py b/posthog/hogql/test/test_mapping.pyindex b852a310a420..8f917c9e7d41 100644--- a/posthog/hogql/test/test_mapping.py+++ b/posthog/hogql/test/test_mapping.py@@ -303,6 +303,18 @@ class TestMappings(ClickhouseTestMixin, BaseTest):assert response.columns is not Noneassert response.results[0] == (3.14, None, 3.0, 3.14, 3.14, 3.14, 0.0, 7.0, 0.0, 7.0, None, 7.0)+ def test_to_float_or_default_with_integer_default(self):+ # ClickHouse's toFloat64OrDefault requires the default to be Float64;+ # an integer default literal must still work. The wrapping coalesce+ # also confirms the call resolves to a known (Float) type — an integer+ # default that fell through to UnknownType would break nesting.+ response = execute_hogql_query(+ "SELECT toFloatOrDefault('bla', 0), toFloatOrDefault('2.5', 1), coalesce(toFloatOrDefault('bla', 0), 1)",+ self.team,+ )+ assert response.results is not None+ assert response.results[0] == (0.0, 2.5, 0.0)+def test_map_function_with_multiple_key_value_pairs(self):"""Test that the map function accepts multiple key-value pairs."""response = execute_hogql_query(