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.

fix(hogql): restore single-arg toFloatOrDefault as toFloatOrZero (#60242)

Sam Pennington7ed3a0a1f072
diff --git a/posthog/hogql/functions/clickhouse/conversions.py b/posthog/hogql/functions/clickhouse/conversions.py
index 7e7f52834bfe..e414ab07f972 100644
--- a/posthog/hogql/functions/clickhouse/conversions.py
+++ b/posthog/hogql/functions/clickhouse/conversions.py
@@ -51,14 +51,20 @@ TYPE_CONVERSION_FUNCTIONS: dict[str, HogQLFunctionMeta] = {
# 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.
+ # The 1-arg form is degenerate (equivalent to toFloatOrZero) and is
+ # rewritten in the printer before the placeholder template renders.
"toFloat64OrDefault({0}, accurateCast({1}, 'Float64'))",
- 2,
+ 1,
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()),
+ ((IntegerType(),), FloatType()),
+ ((FloatType(),), FloatType()),
+ ((StringType(),), FloatType()),
((DecimalType(), FloatType()), FloatType()),
((DecimalType(), IntegerType()), FloatType()),
((IntegerType(), FloatType()), FloatType()),
diff --git a/posthog/hogql/printer/base.py b/posthog/hogql/printer/base.py
index 81addc50076d..7832c70b020d 100644
--- a/posthog/hogql/printer/base.py
+++ b/posthog/hogql/printer/base.py
@@ -962,6 +962,10 @@ class BasePrinter(Visitor[str]):
# Handle format strings in function names before checking function type
# HogQL preserves the macro in its original shape; SQL dialects expand it.
if func_meta.using_placeholder_arguments and self._expands_placeholder_macros():
+ # Pre-#58714 behavior: single-arg toFloatOrDefault was degenerate and
+ # equivalent to toFloatOrZero. Rewrite here so saved queries still work.
+ if node.name == "toFloatOrDefault" and len(node.args) == 1:
+ return self.visit(ast.Call(name="toFloatOrZero", args=node.args))
return self._render_placeholder_macro(
node=node,
clickhouse_name=func_meta.clickhouse_name,
diff --git a/posthog/hogql/printer/test/test_printer.py b/posthog/hogql/printer/test/test_printer.py
index 40177c3b425c..d90a5e7b6848 100644
--- a/posthog/hogql/printer/test/test_printer.py
+++ b/posthog/hogql/printer/test/test_printer.py
@@ -1190,6 +1190,8 @@ class TestPrinter(BaseTest):
self.assertEqual(
self._expr("toDecimal('3.14', 2)", context), "accurateCastOrNull(%(hogql_val_6)s, %(hogql_val_7)s)"
)
+ # Single-arg toFloatOrDefault is degenerate; rewritten to toFloatOrZero for ClickHouse.
+ self.assertEqual(self._expr("toFloatOrDefault('1.5')", context), "toFloat64OrZero(%(hogql_val_8)s)")
self.assertEqual(self._expr("quantile(0.95)( event )"), "quantile(0.95)(events.event)")
self.assertEqual(self._expr("groupArraySample(5)(event)"), "groupArraySample(5)(events.event)")
diff --git a/posthog/hogql/test/test_mapping.py b/posthog/hogql/test/test_mapping.py
index 8f917c9e7d41..c5c9ed5ec057 100644
--- a/posthog/hogql/test/test_mapping.py
+++ b/posthog/hogql/test/test_mapping.py
@@ -315,6 +315,16 @@ class TestMappings(ClickhouseTestMixin, BaseTest):
assert response.results is not None
assert response.results[0] == (0.0, 2.5, 0.0)
+ def test_to_float_or_default_single_arg(self):
+ # Pre-#58714 single-arg form is degenerate (equivalent to toFloatOrZero);
+ # preserved so saved HogQL queries written before #58714 keep working.
+ response = execute_hogql_query(
+ "SELECT toFloatOrDefault('2.5'), toFloatOrDefault('bla'), toFloatOrDefault(NULL)",
+ self.team,
+ )
+ assert response.results is not None
+ assert response.results[0] == (2.5, 0.0, None)
+
def test_map_function_with_multiple_key_value_pairs(self):
"""Test that the map function accepts multiple key-value pairs."""
response = execute_hogql_query(