: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 numeric properties to text as text on native events (#119411)
Vasco de Krijgerc41e73201019
diff --git a/posthog/hogql/transforms/clickhouse_property_resolution.py b/posthog/hogql/transforms/clickhouse_property_resolution.pyindex 617a535a4b83..d108525d7cf1 100644--- a/posthog/hogql/transforms/clickhouse_property_resolution.py+++ b/posthog/hogql/transforms/clickhouse_property_resolution.py@@ -52,6 +52,7 @@ from posthog.hogql.type_system import (ComparisonCompatibility,comparison_compatibility,constant_type_from_runtime_type,+ convert_literal_to_runtime_type,parse_sql_runtime_type,runtime_type_from_constant_type,)@@ -2043,6 +2044,27 @@ 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.++ 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.+ """+ single = self._single_key_property(expr)+ if single is None:+ return None+ field_type, property_name = single+ property_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":+ return None+ if all(convert_literal_to_runtime_type(value, semantic_type) is not None for value in values):+ return None+ source = resolve_materialized_property_source(field_type, property_name, self.context)+ if source is None or not _is_dynamic_json_source(source):+ return None+ return _OptimizableProperty(field_type=field_type, key=property_name, source=source)+def _materialized_property_for_op(self, expr: ast.Expr) -> _OptimizableProperty | None:"""A single-key materialized-column property (any resolved type), for IN comparisons that don't need a string."""single = self._single_key_property(expr)@@ -2417,7 +2439,9 @@ class ClickHousePropertyResolver(CloningVisitor):return Noneif self._is_boolean_conversion(property_expr):return None- prop = self._materialized_string_property(property_expr, allow_dynamic_json=True)+ prop = self._materialized_string_property(+ property_expr, allow_dynamic_json=True+ ) or self._numeric_property_to_compare_as_text(property_expr, values)if prop is None or prop.source.kind != "json_subcolumn":return Nonediff --git a/posthog/hogql/transforms/test/test_property_types.py b/posthog/hogql/transforms/test/test_property_types.pyindex 34f233f41064..2449809b1466 100644--- a/posthog/hogql/transforms/test/test_property_types.py+++ b/posthog/hogql/transforms/test/test_property_types.py@@ -41,6 +41,7 @@ from posthog.hogql.parser import parse_selectfrom posthog.hogql.printer import prepare_and_print_astfrom posthog.hogql.property import property_to_exprfrom posthog.hogql.property_access_types import RestrictedProperty+from posthog.hogql.property_metadata import PropertyMetadatafrom posthog.hogql.property_planner import (PropertyComparisonPlan,PropertyLiteralConversion,@@ -85,7 +86,11 @@ def _normalize_snapshot_sql(sql: str) -> str:class _NewEventsSchemaArraySubcolumnsHelpers:- def _context(self, use_new_events_schema: bool | None = None) -> HogQLContext:+ def _context(+ self,+ use_new_events_schema: bool | None = None,+ event_properties: dict[str, dict[str, str | None]] | None = None,+ ) -> HogQLContext:team = Team(id=1, project_id=1)context = HogQLContext(team_id=team.id,@@ -95,7 +100,9 @@ class _NewEventsSchemaArraySubcolumnsHelpers:)context.database = Database()context.restricted_properties = set()- context.property_swapper = PropertySwapper("UTC", {}, {}, {}, context, True)+ if event_properties is not None:+ context.property_metadata = PropertyMetadata(event_properties=event_properties)+ context.property_swapper = PropertySwapper("UTC", event_properties or {}, {}, {}, context, True)return contextdef _print_select(@@ -552,6 +559,39 @@ class TestNewEventsSchemaArraySubcolumnsClickhouse(_NewEventsSchemaArraySubcolum(None,) * width,]+ @parameterized.expand(+ [+ ("text_equals", "properties.step = 'template'", [1, 0, 0, 0, 0, 0]),+ ("text_not_equals", "properties.step != 'template'", [0, 1, 1, 1, 1, 1]),+ ("text_in", "properties.step IN ('template', 'domain')", [1, 1, 0, 0, None, 0]),+ ("text_and_number_in", "properties.step IN ('template', '5')", [1, 0, 1, 1, None, 0]),+ ("number_equals", "properties.step = '5'", [0, 0, 1, 1, 0, 0]),+ ("non_finite_text", "properties.step = 'nan'", [0, 0, 0, 0, 0, 1]),+ ]+ )+ def test_numeric_property_compared_to_text_matches_text(+ self, _name: str, predicate: str, expected: list[int | None]+ ) -> None:+ context = self._context(event_properties={"step": {"type": "Numeric"}})+ 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": [+ '{"step":"template"}',+ '{"step":"domain"}',+ '{"step":5}',+ '{"step":"5"}',+ "{}",+ '{"step":"nan"}',+ ],+ },+ )+ assert [row[0] for row in rows] == expected+@parameterized.expand([(name, property_name, value, escape_forward_slashes)diff --git a/posthog/hogql/transforms/type_aware_simplification.py b/posthog/hogql/transforms/type_aware_simplification.pyindex 80a6317f3b6f..74103ae0b677 100644--- a/posthog/hogql/transforms/type_aware_simplification.py+++ b/posthog/hogql/transforms/type_aware_simplification.py@@ -5,7 +5,6 @@ import mathfrom collections.abc import Callablefrom datetime import date, datetime, timedeltafrom typing import cast-from uuid import UUIDfrom posthog.hogql import astfrom posthog.hogql.base import _T_AST@@ -15,6 +14,7 @@ from posthog.hogql.observability import record_type_simplificationfrom posthog.hogql.type_system import (RuntimeType,constant_type_from_runtime_type,+ convert_literal_to_runtime_type,parse_sql_runtime_type,runtime_type_from_constant_type,)@@ -375,7 +375,7 @@ def _fold_constant_cast(source: ast.Expr, target_type: RuntimeType, allow_numeriif not allow_numeric and target_type.family in {"integer", "float", "decimal"}:return None- converted_value = _convert_literal_to_runtime_type(source.value, target_type)+ converted_value = convert_literal_to_runtime_type(source.value, target_type)if converted_value is None:return None@@ -383,69 +383,6 @@ def _fold_constant_cast(source: ast.Expr, target_type: RuntimeType, allow_numerireturn ast.Constant(value=converted_value, type=constant_type, start=source.start, end=source.end)-def _convert_literal_to_runtime_type(value: object, target_type: RuntimeType) -> object | None:- converter = _LITERAL_RUNTIME_TYPE_CONVERTERS.get(target_type.family)- if converter is None:- return None-- try:- return converter(value)- except (TypeError, ValueError, OverflowError):- return None---def _convert_integer_literal(value: object) -> int | None:- if isinstance(value, int) and not isinstance(value, bool):- return value- if isinstance(value, str):- return int(value)- return None---def _convert_float_literal(value: object) -> float | None:- if not isinstance(value, (int, float, str)) or isinstance(value, bool):- return None- result = float(value)- return result if math.isfinite(result) else None---def _convert_boolean_literal(value: object) -> bool | None:- if isinstance(value, bool):- return value- if 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"- return None---def _convert_uuid_literal(value: object) -> UUID | None:- if isinstance(value, UUID):- return value- if isinstance(value, str):- return UUID(value)- return None---def _convert_date_literal(value: object) -> date | None:- if isinstance(value, datetime):- return value.date()- if isinstance(value, date):- return value- if isinstance(value, str):- return date.fromisoformat(value)- return None---_LITERAL_RUNTIME_TYPE_CONVERTERS: dict[str, Callable[[object], object | None]] = {- "integer": _convert_integer_literal,- "float": _convert_float_literal,- "boolean": _convert_boolean_literal,- "uuid": _convert_uuid_literal,- "date": _convert_date_literal,-}--def _fold_literal_json_call(node: ast.Call, dialect: HogQLDialect) -> ast.Constant | None:normalized_name = node.name.lower()if normalized_name not in _LITERAL_JSON_FUNCTIONS or not node.args:diff --git a/posthog/hogql/type_system.py b/posthog/hogql/type_system.pyindex 40cdc0ba16e4..63ada840fe8f 100644--- a/posthog/hogql/type_system.py+++ b/posthog/hogql/type_system.py@@ -1,12 +1,15 @@from __future__ import annotationsimport re+import mathimport dataclasses-from collections.abc import Sequence+from collections.abc import Callable, Sequencefrom dataclasses import dataclass, field+from datetime import date, datetimefrom enum import StrEnumfrom functools import lru_cachefrom typing import TYPE_CHECKING, Literal, Optional, cast+from uuid import UUIDfrom posthog.hogql import astfrom posthog.hogql.constants import HogQLDialect@@ -904,6 +907,69 @@ def comparison_compatibility(return ComparisonCompatibility.INCOMPATIBLE+def _convert_integer_literal(value: object) -> int | None:+ if isinstance(value, int) and not isinstance(value, bool):+ return value+ if isinstance(value, str):+ return int(value)+ return None+++def _convert_float_literal(value: object) -> float | None:+ if not isinstance(value, (int, float, str)) or isinstance(value, bool):+ return None+ result = float(value)+ return result if math.isfinite(result) else None+++def _convert_boolean_literal(value: object) -> bool | None:+ if isinstance(value, bool):+ return value+ if 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"+ return None+++def _convert_uuid_literal(value: object) -> UUID | None:+ if isinstance(value, UUID):+ return value+ if isinstance(value, str):+ return UUID(value)+ return None+++def _convert_date_literal(value: object) -> date | None:+ if isinstance(value, datetime):+ return value.date()+ if isinstance(value, date):+ return value+ if isinstance(value, str):+ return date.fromisoformat(value)+ return None+++_LITERAL_RUNTIME_TYPE_CONVERTERS: dict[str, Callable[[object], object | None]] = {+ "integer": _convert_integer_literal,+ "float": _convert_float_literal,+ "boolean": _convert_boolean_literal,+ "uuid": _convert_uuid_literal,+ "date": _convert_date_literal,+}+++def convert_literal_to_runtime_type(value: object, target_type: RuntimeType) -> object | None:+ converter = _LITERAL_RUNTIME_TYPE_CONVERTERS.get(target_type.family)+ if converter is None:+ return None++ try:+ return converter(value)+ except (TypeError, ValueError, OverflowError):+ return None++@dataclass(frozen=True, slots=True)class FunctionTypeInference:return_type: ast.ConstantType