: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): compare boolean properties to text as text on native events (#124432)
Sandy Spicer142d39cd3ee8
diff --git a/docs/published/handbook/engineering/databases/hogql-python.md b/docs/published/handbook/engineering/databases/hogql-python.mdindex 7c0e5aff8714..9259e8647927 100644--- a/docs/published/handbook/engineering/databases/hogql-python.md+++ b/docs/published/handbook/engineering/databases/hogql-python.md@@ -179,7 +179,9 @@ In practice, you should avoid both and access `person.properties.$browser`, whicOn the native JSON events table, HogQL compares non-empty string constants directly with declared String paths and casts Dynamic paths to Nullable(String) for properties with String or unknown definition types.This keeps equality and membership predicates visible to the JSON skip index without failing on mixed scalar types.-Numeric, Boolean and DateTime property definitions keep their conversions when compared with strings, for both event and person properties.+Numeric and Boolean properties on the native table compare as text when a string literal cannot be converted to the property's definition type.+Boolean literals keep ClickHouse's case-insensitive spellings, including yes/no, on/off and enable/disable.+DateTime property definitions keep their conversions when compared with strings, for both event and person properties.Boolean conversion reads the native path directly because object and array text cannot match `true` or `false`; numeric conversions already read native paths directly.Constants starting with `[` or `{` keep the full property read because arrays and objects serialize differently from scalars.Single-property reads serialize sub-objects with ClickHouse's JSON formatter; whole-document reads still remove declared defaults.diff --git a/posthog/hogql/transforms/clickhouse_property_resolution.py b/posthog/hogql/transforms/clickhouse_property_resolution.pyindex e0473e3781bd..117661eda1e6 100644--- a/posthog/hogql/transforms/clickhouse_property_resolution.py+++ b/posthog/hogql/transforms/clickhouse_property_resolution.py@@ -2084,19 +2084,23 @@ class ClickHousePropertyResolver(CloningVisitor):source=source,)- def _numeric_property_to_compare_as_text(self, expr: ast.Expr, values: list[str]) -> _OptimizableProperty | None:- """A Numeric JSON property compared to non-numeric text, read as text the way the legacy table compares it.+ def _typed_property_to_compare_as_text(self, expr: ast.Expr, values: list[str]) -> _OptimizableProperty | None:+ """A Numeric or Boolean JSON property compared to text it cannot be read as, read as text the way the legacy+ table compares it.A property definition keeps the type of the first value seen, so a Numeric key can still hold text that a- Float64 read never matches.+ Float64 read never matches, and a Boolean key can hold text that the Bool read turns into NULL. Comparing that+ NULL Bool to the text fails in ClickHouse ("Cannot parse a boolean"), or counts nothing inside an aggregate."""+ if self._is_boolean_conversion(expr) and not self._is_swapper_boolean_conversion(expr):+ return Nonesingle = self._single_key_property(expr)if single is None:return Nonefield_type, property_name = singleproperty_type = ast.PropertyType(field_type=field_type, chain=[property_name])semantic_type = runtime_type_from_constant_type(property_type.resolve_constant_type(self.context))- if semantic_type.family != "float":+ if semantic_type.family not in ("float", "boolean"):return Noneif all(convert_literal_to_runtime_type(value, semantic_type) is not None for value in values):return None@@ -2477,11 +2481,13 @@ class ClickHousePropertyResolver(CloningVisitor):values = self._extract_string_constants(node.right)if not values or any(not value or value.startswith(("[", "{")) for value in values):return None- if self._is_boolean_conversion(property_expr):- return None- prop = self._materialized_string_property(- property_expr, allow_dynamic_json=True- ) or self._numeric_property_to_compare_as_text(property_expr, values)+ # A user-written `toBool(properties.x)` over a String property keeps its cast and is handled by+ # `_optimize_materialized_equals`. The swapper's Boolean cast over a Boolean-typed property is the one looked+ # through here, so that text the property cannot be read as compares as text.+ prop = None+ if not self._is_boolean_conversion(property_expr):+ prop = self._materialized_string_property(property_expr, allow_dynamic_json=True)+ prop = prop or self._typed_property_to_compare_as_text(property_expr, values)if prop is None or prop.source.kind != "json_subcolumn":return None@@ -2674,6 +2680,32 @@ class ClickHousePropertyResolver(CloningVisitor):expr = expr.exprreturn isinstance(expr, ast.Call) and expr.name.lower() == "tobool"+ @staticmethod+ def _is_swapper_boolean_conversion(expr: ast.Expr) -> bool:+ """True for the property swapper's own Boolean cast, `toBool(transform(toString(x), ['true', 'false'], [1, 0], NULL))`.++ A user-written `toBool(toString(properties.x))` unwraps to the same property but is the user's cast to keep.+ """+ while isinstance(expr, ast.Alias):+ expr = expr.expr+ match expr:+ case ast.Call(+ name="toBool",+ args=[+ ast.Call(+ name="transform",+ args=[+ ast.Call(name="toString", args=[_]),+ ast.Constant(value=["true", "false"]),+ ast.Constant(value=[1, 0]),+ ast.Constant(value=None),+ ],+ )+ ],+ ):+ return True+ return False+def _optimize_materialized_ilike(self, node: ast.CompareOperation) -> ast.Expr | None:if node.op not in (ast.CompareOperationOp.ILike, ast.CompareOperationOp.NotILike):return Nonediff --git a/posthog/hogql/transforms/test/test_property_types.py b/posthog/hogql/transforms/test/test_property_types.pyindex 1a96585cd9a2..805847821502 100644--- a/posthog/hogql/transforms/test/test_property_types.py+++ b/posthog/hogql/transforms/test/test_property_types.py@@ -208,6 +208,19 @@ class TestNewEventsSchemaArraySubcolumns(_NewEventsSchemaArraySubcolumnsHelpers,"countIf(ifNull(equals(CAST(events.properties.value, 'Nullable(String)'), %(hogql_val_0)s), 0))" in native)+ def test_boolean_text_comparison_keeps_user_written_cast(self) -> None:+ context = self._context(use_new_events_schema=True, event_properties={"opted_in": {"type": "Boolean"}})+ with patch("posthog.hogql.printer.utils.build_property_swapper"):+ printed, _ = prepare_and_print_ast(+ parse_select("SELECT toBool(toString(properties.opted_in)) = 'pending' FROM events"),+ context,+ "clickhouse",+ )+ assert "CAST(events.properties.opted_in, 'Nullable(String)')" not in printed+ assert (+ "accurateCastOrNull(toString(accurateCastOrNull(transform(toString(events.properties.opted_in)" in printed+ )+@parameterized.expand([("$active_feature_flags",), ("$exception_types",)])def test_property_comparison_planner_does_not_depend_on_json_storage_type(self, property_name: str) -> None:plan = self._plan_where_comparison(f"select count() from events where properties.{property_name} = 'TypeError'")@@ -794,6 +807,53 @@ class TestNewEventsSchemaArraySubcolumnsClickhouse(_NewEventsSchemaArraySubcolum)assert [row[0] for row in rows] == expected+ @parameterized.expand(+ [+ ("text_equals", "properties.opted_in = 'pending'", [1, 0, 0, 0, 0, 0]),+ ("text_not_equals", "properties.opted_in != 'pending'", [0, 1, 1, 1, 1, 1]),+ ("text_in", "properties.opted_in IN ('pending', 'declined')", [1, 1, 0, 0, None, 0]),+ ("text_and_boolean_in", "properties.opted_in IN ('pending', 'true')", [1, 0, 1, 1, None, 0]),+ ("boolean_text_equals", "properties.opted_in = 'true'", [0, 0, 1, 1, 0, 0]),+ ("boolean_digit_equals", "properties.opted_in = '1'", [0, 0, 1, 1, 0, 0]),+ ("boolean_yes_equals", "properties.opted_in = 'YeS'", [0, 0, 1, 1, 0, 0]),+ ("boolean_no_equals", "properties.opted_in = 'No'", [0, 0, 0, 0, 0, 1]),+ ("boolean_t_equals", "properties.opted_in = 'T'", [0, 0, 1, 1, 0, 0]),+ ("boolean_f_equals", "properties.opted_in = 'F'", [0, 0, 0, 0, 0, 1]),+ (+ "boolean_true_aliases_in",+ "countIf(properties.opted_in IN ('true', '1', 'T', 'Y', 'YeS', 'ON', 'Enable', 'Enabled'))",+ [2],+ ),+ (+ "boolean_false_aliases_in",+ "countIf(properties.opted_in IN ('false', '0', 'F', 'N', 'No', 'OFF', 'Disable', 'Disabled'))",+ [1],+ ),+ ]+ )+ def test_boolean_property_compared_to_text_matches_text(+ self, _name: str, predicate: str, expected: list[int | None]+ ) -> None:+ context = self._context(event_properties={"opted_in": {"type": "Boolean"}})+ with patch("posthog.hogql.printer.utils.build_property_swapper"):+ printed, _ = prepare_and_print_ast(parse_select(f"SELECT {predicate} FROM events"), context, "clickhouse")+ rows = sync_execute(+ "WITH events_json AS (SELECT 1 AS team_id, CAST(arrayJoin(%(documents)s), 'JSON') AS properties) "+ + printed,+ {+ **context.values,+ "documents": [+ '{"opted_in":"pending"}',+ '{"opted_in":"declined"}',+ '{"opted_in":true}',+ '{"opted_in":"true"}',+ "{}",+ '{"opted_in":false}',+ ],+ },+ )+ assert [row[0] for row in rows] == expected+@parameterized.expand([(name, property_name, value, escape_forward_slashes)diff --git a/posthog/hogql/type_system.py b/posthog/hogql/type_system.pyindex a64e46887373..87d48083626f 100644--- a/posthog/hogql/type_system.py+++ b/posthog/hogql/type_system.py@@ -927,8 +927,13 @@ def _convert_boolean_literal(value: object) -> bool | None:return valueif isinstance(value, int) and value in (0, 1):return bool(value)- if isinstance(value, str) and value.lower() in {"true", "false"}:- return value.lower() == "true"+ # Match ClickHouse's SerializationBool::tryDeserializeAllVariants so aliases keep Boolean comparisons.+ if isinstance(value, str):+ normalized = value.lower()+ if normalized in {"true", "1", "t", "y", "yes", "on", "enable", "enabled"}:+ return True+ if normalized in {"false", "0", "f", "n", "no", "off", "disable", "disabled"}:+ return Falsereturn None