: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(flags): convert typed properties at the call site on union scans (#125241)
Phil Haack093887507ce6
diff --git a/posthog/hogql/ast.py b/posthog/hogql/ast.pyindex 3a45c3b67c82..9b3466319a45 100644--- a/posthog/hogql/ast.py+++ b/posthog/hogql/ast.py@@ -499,10 +499,12 @@ class CTETableAliasType(BaseTableType):return self.cte_table_type.resolve_column_constant_type(name, context)-@dataclass(kw_only=True, slots=True)+@dataclass(kw_only=True, slots=True, frozen=False)class SelectQueryAliasType(Type):alias: strselect_query_type: SelectQueryType | SelectSetQueryType+ # The lazy table that this subquery stands for, when lazy table resolution built it.+ lazy_table: Optional[LazyTable] = field(default=None, repr=False, compare=False)def get_child(self, name: str, context: HogQLContext) -> Type:if name == "*":diff --git a/posthog/hogql/database/schema/events_with_flag_calls.py b/posthog/hogql/database/schema/events_with_flag_calls.pyindex 0439bc8ff088..a5a8452d7ae6 100644--- a/posthog/hogql/database/schema/events_with_flag_calls.py+++ b/posthog/hogql/database/schema/events_with_flag_calls.py@@ -10,6 +10,7 @@ from posthog.hogql.database.models import (LazyJoin,LazyTable,LazyTableToAdd,+ StringJSONDatabaseField,Table,VirtualTable,)@@ -19,6 +20,8 @@ from posthog.hogql.errors import QueryErrorfrom posthog.hogql.parser import parse_exprfrom posthog.hogql.visitor import GetFieldsTraverser+from posthog.dataclasses import frozen+EVENTS_WITH_FLAG_CALLS_TABLE = "events_with_flag_calls"EVENTS_WITH_FLAG_CALLS_CHAIN: list[str] = ["posthog", EVENTS_WITH_FLAG_CALLS_TABLE]@@ -51,6 +54,23 @@ def _is_expression(table: Table, chain: list[str | int]) -> bool:return isinstance(_field(table, chain)[0], ExpressionField)+@frozen+class PropertyRead:+ """A property that a chain reads: the stored JSON column, and the top-level key in it."""++ column: str+ key: str+++def property_read(table: Table, chain: list[str | int]) -> PropertyRead | None:+ """The property that `chain` reads on `table`, or None when it reads no property."""+ field, rest = _field(table, chain)+ column = _column_name(field)+ if not isinstance(field, StringJSONDatabaseField) or column is None or not rest:+ return None+ return PropertyRead(column=column, key=str(rest[0]))++def column_chains(table: Table, chain: list[str | int]) -> list[list[str | int]]:"""The chains of stored columns that `chain` reads on `table`, through the expression fields and the keys of thejoins that it reads."""@@ -195,6 +215,15 @@ class EventsWithFlagCallsTable(LazyTable):return EVENTS_WITH_FLAG_CALLS_TABLE+def union_table_of(table_type: ast.Type | None) -> "EventsWithFlagCallsTable | None":+ """The union that `table_type` scans, through aliases and virtual tables, or None when it scans another table."""+ while isinstance(table_type, (ast.TableAliasType, ast.VirtualTableType)):+ table_type = table_type.table_type+ if isinstance(table_type, ast.LazyTableType) and isinstance(table_type.table, EventsWithFlagCallsTable):+ return table_type.table+ return None++# Tables whose rows are events with the events columns. Property types, access control and time bounds treat their reads# as reads of events.EVENT_ROW_TABLES = (EventsTable, FlagEvaluationsTable, EventsWithFlagCallsTable)diff --git a/posthog/hogql/printer/utils.py b/posthog/hogql/printer/utils.pyindex 3a1b95662b63..165ae9a2ac59 100644--- a/posthog/hogql/printer/utils.py+++ b/posthog/hogql/printer/utils.py@@ -395,10 +395,11 @@ def prepare_ast_for_printing(node = PropertySwapper(timezone=context.property_swapper.timezone,group_properties=context.property_swapper.group_properties,- event_properties={},- person_properties={},+ event_properties=context.property_swapper.event_properties,+ person_properties=context.property_swapper.person_properties,context=context,setTimeZones=False,+ before_lazy_tables=True,).visit(node)with context.timings.measure("resolve_lazy_tables"):diff --git a/posthog/hogql/transforms/flag_calls_union.py b/posthog/hogql/transforms/flag_calls_union.pyindex d4ac41265349..8489556d4d76 100644--- a/posthog/hogql/transforms/flag_calls_union.py+++ b/posthog/hogql/transforms/flag_calls_union.py@@ -10,7 +10,7 @@ scan, so the rules decide which scans pay for a second table, and which shapes t"""from collections.abc import Callable-from typing import TYPE_CHECKING+from typing import TYPE_CHECKING, castfrom posthog.hogql import astfrom posthog.hogql.constants import FLAG_CALLED_EVENT@@ -25,11 +25,14 @@ from posthog.hogql.database.lazy_join_tags import (PERSON_DISTINCT_IDS,PERSONS,)+from posthog.hogql.database.models import Tablefrom posthog.hogql.database.schema.events_with_flag_calls import (EVENTS_WITH_FLAG_CALLS_CHAIN,EVENTS_WITH_FLAG_CALLS_TABLE,EventsWithFlagCallsTable,FlagEvaluationsColumns,+ property_read,+ union_table_of,)from posthog.hogql.database.schema.util.where_clause_extractor import (contains_in_cohort,@@ -37,7 +40,6 @@ from posthog.hogql.database.schema.util.where_clause_extractor import (is_nondeterministic_function,)from posthog.hogql.errors import QueryError, ResolutionError-from posthog.hogql.property_metadata import load_property_metadatafrom posthog.hogql.restricted_properties import key_read_overlaps_restriction, restricted_property_keys_for_table_typefrom posthog.hogql.transforms.events_predicate_pushdown import PREDICATE_SAFE_JOIN_TYPES, SelectAliasInlinerfrom posthog.hogql.transforms.flag_calls_from_flag_evaluations import (@@ -53,10 +55,11 @@ from posthog.hogql.transforms.flag_calls_from_flag_evaluations import (flag_key_predicate,point_scan_at,)-from posthog.hogql.transforms.property_types import STRING_INPUT_CONVERSIONS+from posthog.hogql.type_system import parse_sql_runtime_typefrom posthog.hogql.visitor import CloningVisitorfrom posthog.dataclasses import frozen+from posthog.property_columns import TableColumnif TYPE_CHECKING:from posthog.hogql.database.database import Database@@ -138,25 +141,25 @@ def union_unreadable_reason(node: ast.AST, context: HogQLContext) -> str | None:for chain in collector.chains:if columns.chain(chain) is None:return f"it reads {'.'.join(str(part) for part in chain)}, which that table doesn't have"- return _typed_property_parsed_as_string(collector.string_parsed, context)+ return _typed_materialized_property(columns.events, collector.chains, context)-def _typed_property_parsed_as_string(reads: list[tuple[str, str]], context: HogQLContext) -> str | None:- # Each branch converts a typed property before the union returns it, so a call that parses the raw string, such- # as toFloatOrZero, would get the converted value.- if not reads or context.team_id is None:+def _typed_materialized_property(union: Table, chains: list[list[str | int]], context: HogQLContext) -> str | None:+ # A raw read of a typed materialized column keeps the column's type, but the flag_evaluations branch reads a JSON+ # String, so the UNION ALL has no common type. The native events schema and disabled materialization read JSON.+ if context.uses_new_events_schema() or context.modifiers.materializationMode == "disabled":return None- metadata = load_property_metadata(- context,- event_property_names={name for kind, name in reads if kind == "event"},- person_property_names={name for kind, name in reads if kind == "person"},- group_property_names={},- )- for kind, name in reads:- types = metadata.event_properties if kind == "event" else metadata.person_properties- property_type = types.get(name, {}).get("type")- if property_type not in (None, "String"):- return f"it parses the {property_type} {kind} property {name} as a string"+ reads = {read for chain in chains if (read := property_read(union, chain)) is not None}+ if not reads:+ return None+ # Deferred: the registry reads Django models, which this module does not import.+ from posthog.clickhouse.materialized_columns import get_enabled_materialized_columns_by_table # noqa: PLC0415++ materialized = get_enabled_materialized_columns_by_table().get("events", {})+ for read in sorted(reads, key=lambda read: (read.column, read.key)):+ backing = materialized.get((read.key, cast(TableColumn, read.column)))+ if backing is not None and parse_sql_runtime_type(backing.type).family != "string":+ return f"it reads {read.key}, which events stores in a {backing.type} column"return None@@ -248,14 +251,6 @@ def _flag_key_is_restricted(database: "Database", context: HogQLContext) -> boolreturn key_read_overlaps_restriction("$feature_flag", restricted)-def _union_table_of(table_type: ast.Type | None) -> EventsWithFlagCallsTable | None:- while isinstance(table_type, (ast.TableAliasType, ast.VirtualTableType)):- table_type = table_type.table_type- if isinstance(table_type, ast.LazyTableType) and isinstance(table_type.table, EventsWithFlagCallsTable):- return table_type.table- return None--def _union_chain(field_type: ast.Type | None) -> tuple[EventsWithFlagCallsTable, list[str | int]] | None:"""The union scan that a resolved field reads directly, and its chain there, or None."""property_chain: list[str | int] = []@@ -267,7 +262,7 @@ def _union_chain(field_type: ast.Type | None) -> tuple[EventsWithFlagCallsTable,chain: list[str | int] = [field_type.name, *property_chain]if isinstance(field_type.table_type, ast.VirtualTableType):chain = [field_type.table_type.field, *chain]- union = _union_table_of(field_type.table_type)+ union = union_table_of(field_type.table_type)return (union, chain) if union is not None else None@@ -534,9 +529,6 @@ class _UnionChainCollector(SyntaxVisitor):self.exports: dict[tuple[int, str], list[str | int]] = {}self._select: ast.SelectQuery | None = Noneself.export_collision: tuple[list[str | int], list[str | int]] | None = None- # The event and person properties that a string-parsing call such as toFloatOrZero reads from the scan.- self.string_parsed: list[tuple[str, str]] = []- self._in_string_input = Falsedef visit_select_query(self, node: ast.SelectQuery) -> None:outer = self._select@@ -544,13 +536,6 @@ class _UnionChainCollector(SyntaxVisitor):super().visit_select_query(node)self._select = outer- def visit_call(self, node: ast.Call) -> None:- # The property type swapper keeps a property raw only as the direct argument of such a call.- outer = self._in_string_input- self._in_string_input = node.name in STRING_INPUT_CONVERSIONS- super().visit_call(node)- self._in_string_input = outer-def visit_field(self, node: ast.Field) -> None:read = _union_chain(node.type)if read is not None:@@ -562,8 +547,6 @@ class _UnionChainCollector(SyntaxVisitor):first = self.exports.setdefault((id(self._select), name), chain)if first != chain and self.export_collision is None:self.export_collision = (first, chain)- if self._in_string_input and chain[:-1] in (["properties"], ["poe", "properties"]):- self.string_parsed.append(("event" if chain[0] == "properties" else "person", str(chain[-1])))returnfield_type = node.type.field_type if isinstance(node.type, ast.PropertyType) else node.typeif not isinstance(field_type, ast.FieldType):@@ -576,7 +559,7 @@ class _UnionChainCollector(SyntaxVisitor):source = table_type.table_typewhile isinstance(source, ast.TableAliasType):source = source.table_type- if _union_table_of(source) is not None:+ if union_table_of(source) is not None:lazy_join = table_type.lazy_joinif isinstance(source, ast.VirtualTableType) or lazy_join.resolver not in _UNION_JOIN_RESOLVERS:self.unreadable_joins.append(table_type.field)diff --git a/posthog/hogql/transforms/lazy_tables.py b/posthog/hogql/transforms/lazy_tables.pyindex 71c54da57083..6ed3db4982c3 100644--- a/posthog/hogql/transforms/lazy_tables.py+++ b/posthog/hogql/transforms/lazy_tables.py@@ -759,7 +759,9 @@ class LazyTableResolver(TraversingVisitor):setTimeZones=False,).visit(subquery)old_table_type = select_type.tables[table_name]- select_type.tables[table_name] = ast.SelectQueryAliasType(alias=table_name, select_query_type=subquery.type)+ select_type.tables[table_name] = ast.SelectQueryAliasType(+ alias=table_name, select_query_type=subquery.type, lazy_table=table_to_add.lazy_table+ )join_ptr = node.select_fromwhile join_ptr:diff --git a/posthog/hogql/transforms/property_types.py b/posthog/hogql/transforms/property_types.pyindex 447fb1be1e85..8b99bb9fd8ba 100644--- a/posthog/hogql/transforms/property_types.py+++ b/posthog/hogql/transforms/property_types.py@@ -1,3 +1,4 @@+import dataclassesfrom datetime import datetimefrom typing import Literal, Optional@@ -7,7 +8,11 @@ from posthog.hogql.context import HogQLContextfrom posthog.hogql.database.models import BooleanDatabaseField, DateTimeDatabaseField, StringJSONDatabaseFieldfrom posthog.hogql.database.s3_table import S3Tablefrom posthog.hogql.database.schema.events import EVENTS_TABLE_TYPES, EventsGroupSubTable, EventsPersonSubTable-from posthog.hogql.database.schema.events_with_flag_calls import EVENT_ROW_TABLES+from posthog.hogql.database.schema.events_with_flag_calls import (+ EVENT_ROW_TABLES,+ EventsWithFlagCallsTable,+ union_table_of,+)from posthog.hogql.database.schema.flag_evaluations import FlagEvaluationsPersonSubTablefrom posthog.hogql.database.schema.groups import GroupsTablefrom posthog.hogql.database.schema.persons import PersonsTable, RawPersonsTable@@ -133,6 +138,8 @@ class ToTimeZoneParts:swapped: bool+_PERSON_PROPERTIES_TABLES = (*EVENTS_TABLE_TYPES, EventsWithFlagCallsTable)+# ClickHouse string-parsing conversions (toFloat64OrZero, toInt64OrZero,# toFloat64OrDefault, toInt64OrDefault) require a String first argument and raise# ILLEGAL_TYPE_OF_ARGUMENT on numeric input. When a user explicitly wraps a@@ -164,7 +171,10 @@ class PropertySwapper(CloningVisitor):group_properties: dict[str, dict[str, str | None]],context: HogQLContext,setTimeZones: bool,+ before_lazy_tables: bool = False,):+ """`before_lazy_tables` marks the pass that runs before lazy tables resolve. Of the event and person+ properties, that pass converts only the reads of a union scan, and the pass after it converts the rest."""super().__init__(clear_types=False)self.timezone = timezoneself.event_properties = event_properties@@ -175,6 +185,24 @@ class PropertySwapper(CloningVisitor):self._inside_call_depth = 0self._inside_where_depth = 0self._suppress_numeric_conversion = False+ self.before_lazy_tables = before_lazy_tables+ self._union_scan_depth = 0++ def visit_join_expr(self, node: ast.JoinExpr) -> ast.JoinExpr:+ if not (+ isinstance(node.type, ast.SelectQueryAliasType)+ and isinstance(node.type.lazy_table, EventsWithFlagCallsTable)+ ):+ return super().visit_join_expr(node)+ # Only the table is the union. The constraint and the joins after it read other tables.+ self._union_scan_depth += 1+ try:+ table = self.visit(node.table)+ finally:+ self._union_scan_depth -= 1+ joined = super().visit_join_expr(dataclasses.replace(node, table=None))+ joined.table = table+ return joineddef visit_select_query(self, node: ast.SelectQuery):# We need to track when we're inside WHERE/PREWHERE so that the@@ -742,7 +770,7 @@ class PropertySwapper(CloningVisitor):property_name = str(type.chain[0])if isinstance(type.field_type.table_type, ast.BaseTableType):resolved_table = type.field_type.table_type.resolve_database_table(self.context)- if isinstance(resolved_table, EVENTS_TABLE_TYPES):+ if isinstance(resolved_table, _PERSON_PROPERTIES_TABLES):if property_name in self.person_properties:return ("person", property_name)return None@@ -772,6 +800,8 @@ class PropertySwapper(CloningVisitor):"person": self.person_properties,"group": self.group_properties,}+ if property_type != "group" and not self._converts_event_or_person_property(node):+ return nodeprop_info = properties_by_type[property_type].get(property_name, {})field_type = "Float" if prop_info.get("type") == "Numeric" else prop_info.get("type") or "String"@@ -789,6 +819,16 @@ class PropertySwapper(CloningVisitor):# the JSON fallback.return self._field_type_to_property_call(node, field_type)+ def _converts_event_or_person_property(self, node: ast.Field) -> bool:+ # Lazy tables move a union scan's property reads into branches, away from a call such as toFloatOrZero that+ # needs the raw value. The pass before lazy tables converts each read at its call, so the branches stay raw.+ if self._union_scan_depth > 0:+ return False+ if not self.before_lazy_tables:+ return True+ assert isinstance(node.type, ast.PropertyType)+ return union_table_of(node.type.field_type.table_type) is not None+def _field_type_to_property_call(self, node: ast.Field, field_type: str):if field_type == "DateTime":# Carry the return type so an enclosing toDateTime() resolves itsdiff --git a/posthog/hogql/transforms/test/test_flag_calls_union.py b/posthog/hogql/transforms/test/test_flag_calls_union.pyindex 66963d46b273..bb5cb6af9402 100644--- a/posthog/hogql/transforms/test/test_flag_calls_union.py+++ b/posthog/hogql/transforms/test/test_flag_calls_union.py@@ -20,7 +20,7 @@ from django.test import SimpleTestCasefrom parameterized import param, parameterized-from posthog.schema import HogQLQueryModifiers, PersonsOnEventsMode+from posthog.schema import HogQLQueryModifiers, MaterializationMode, PersonsOnEventsModefrom posthog.hogql import astfrom posthog.hogql.context import HogQLContext@@ -290,16 +290,19 @@ class TestReadFlagCallsWithUnion(BaseTest):persons_on_events: PersonsOnEventsMode | None = None,new_events_schema: bool | None = None,person_id_pushdown: bool | None = None,+ modifiers: HogQLQueryModifiers | None = None,) -> str:_set_mode(self.organization, mode)with _enabled_flags(union=union):sql, context = HogQLQueryExecutor(query=query,team=self.team,- modifiers=HogQLQueryModifiers(- personsOnEventsMode=persons_on_events,- personIdPushdown=person_id_pushdown,- useNewEventsSchema=_events_schema_for(mode, new_events_schema),+ modifiers=(modifiers or HogQLQueryModifiers()).model_copy(+ update={+ "personIdPushdown": person_id_pushdown,+ "personsOnEventsMode": persons_on_events,+ "useNewEventsSchema": _events_schema_for(mode, new_events_schema),+ },),pretty=False,).generate_clickhouse_sql()@@ -354,6 +357,33 @@ class TestReadFlagCallsWithUnion(BaseTest):mode=ONLY,persons_on_events=PersonsOnEventsMode.PERSON_ID_OVERRIDE_PROPERTIES_ON_EVENTS,),+ # Mode 1 keeps a materialized property on events, so mode 2 shows which columns the union can read.+ param(+ "property_in_a_string_materialized_column_on_mode_2",+ f"SELECT properties.probe FROM events WHERE {MIXED}",+ True,+ True,+ mode=ONLY,+ column_type="Nullable(String)",+ ),+ # Each branch reads the property from JSON, so the typed column never reaches the UNION ALL.+ param(+ "typed_materialized_column_on_the_native_events_schema_on_mode_2",+ f"SELECT properties.probe FROM events WHERE {MIXED}",+ True,+ True,+ mode=ONLY,+ column_type="Nullable(Float64)",+ new_events_schema=True,+ ),+ param(+ "typed_materialized_column_with_materialization_disabled",+ f"SELECT properties.probe FROM events WHERE {MIXED}",+ True,+ True,+ column_type="Nullable(Float64)",+ modifiers=HogQLQueryModifiers(materializationMode=MaterializationMode.DISABLED),+ ),])def test_union_reads_the_scans_phase_1_leaves_on_events(@@ -363,9 +393,22 @@ class TestReadFlagCallsWithUnion(BaseTest):union: bool,expected_union: bool,mode: FlagEvaluationsMode = READ,+ column_type: str | None = None,+ new_events_schema: bool | None = None,+ modifiers: HogQLQueryModifiers | None = None,persons_on_events: PersonsOnEventsMode | None = None,):- sql = self._sql(query, union=union, mode=mode, persons_on_events=persons_on_events)+ if column_type is not None:+ self.addCleanup(cleanup_materialized_columns)+ materialize("events", "probe", is_nullable=True, column_type=column_type)+ sql = self._sql(+ query,+ union=union,+ mode=mode,+ persons_on_events=persons_on_events,+ new_events_schema=new_events_schema,+ modifiers=modifiers,+ )assert ("UNION ALL" in sql) is expected_unionif expected_union:@@ -405,16 +448,10 @@ class TestReadFlagCallsWithUnion(BaseTest):"properties and poe.properties",),param(- "string_parser_on_a_typed_event_property",- f"SELECT toFloatOrZero(properties.probe) FROM events WHERE {MIXED}",- "Numeric event property probe",- typed=PropertyDefinition.Type.EVENT,- ),- param(- "string_parser_on_a_typed_person_property",- f"SELECT toIntOrZero(person.properties.probe) FROM events WHERE {MIXED}",- "Numeric person property probe",- typed=PropertyDefinition.Type.PERSON,+ "property_in_a_typed_materialized_column",+ f"SELECT properties.probe FROM events WHERE {MIXED}",+ "probe, which events stores in a Nullable\\(Float64\\) column",+ typed_column=True,),])@@ -424,10 +461,11 @@ class TestReadFlagCallsWithUnion(BaseTest):query: str,reason: str,warehouse_join: bool = False,- typed: PropertyDefinition.Type | None = None,+ typed_column: bool = False,):- if typed is not None:- PropertyDefinition.objects.create(team=self.team, name="probe", property_type="Numeric", type=typed)+ if typed_column:+ self.addCleanup(cleanup_materialized_columns)+ materialize("events", "probe", is_nullable=True, column_type="Nullable(Float64)")if warehouse_join:# Joined on persons, it reaches events as a join that compares `person.properties.email`, not its key.DataWarehouseJoin.objects.create(@@ -440,9 +478,13 @@ class TestReadFlagCallsWithUnion(BaseTest):)persons_on_events = PersonsOnEventsMode.PERSON_ID_OVERRIDE_PROPERTIES_ON_EVENTS- assert "UNION ALL" not in self._sql(query, persons_on_events=persons_on_events)+ # The native events schema reads every property from JSON, so only the legacy schema reads a typed column.+ new_events_schema = False if typed_column else None+ assert "UNION ALL" not in self._sql(+ query, persons_on_events=persons_on_events, new_events_schema=new_events_schema+ )with pytest.raises(QueryError, match=reason):- self._sql(query, mode=ONLY, persons_on_events=persons_on_events)+ self._sql(query, mode=ONLY, persons_on_events=persons_on_events, new_events_schema=new_events_schema)@parameterized.expand([("dotted", UNION), ("quoted", f"`{UNION}`")])def test_query_cannot_name_the_union_table(self, _name: str, table: str):@@ -602,6 +644,24 @@ class TestReadFlagCallsWithUnion(BaseTest):assert sql.count("FROM person_distinct_id_overrides") == 2 # one inner, one outer for `person_id`+ def test_typed_property_converts_once_at_its_read_and_stays_raw_in_the_branches(self):+ for kind in (PropertyDefinition.Type.EVENT, PropertyDefinition.Type.PERSON):+ PropertyDefinition.objects.create(team=self.team, name="amount", property_type="Numeric", type=kind)+ sql = self._sql(+ f"SELECT toFloatOrZero(properties.amount), properties.amount FROM events "+ f"WHERE {MIXED} AND properties.amount > 9 AND person.properties.amount > 9",+ persons_on_events=PersonsOnEventsMode.PERSON_ID_OVERRIDE_PROPERTIES_JOINED,+ )++ assert "accurateCastOrNull(accurateCastOrNull" not in sql+ assert "toFloat64OrZero(events.properties___amount)" in sql+ for branch in self._branches(sql):+ projection, _, where = branch.partition(" FROM ")+ assert "accurateCastOrNull" not in projection+ assert where.count("accurateCastOrNull") == 1+ # The persons join comes after the union, so it converts its property as it does on events.+ assert "accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'amount')" in sql+class TestFlagCallsUnionRows(ClickhouseTestMixin, APIBaseTest):def setUp(self):@@ -719,6 +779,60 @@ class TestFlagCallsUnionRows(ClickhouseTestMixin, APIBaseTest):assert rows[:4] == [*history, *recent]assert [row[:3] for row in rows[4:]] == keyless+ @parameterized.expand(+ [+ (f"{kind.name.lower()}_{mode.name.lower()}", kind, mode)+ for kind in (PropertyDefinition.Type.EVENT, PropertyDefinition.Type.PERSON)+ for mode in (READ, ONLY)+ ]+ )+ def test_string_parser_reads_the_raw_value_of_a_typed_property(+ self, _name: str, kind: PropertyDefinition.Type, mode: FlagEvaluationsMode+ ):+ PropertyDefinition.objects.create(team=self.team, name="amount", property_type="Numeric", type=kind)+ now = datetime.now(UTC)+ person = _create_person(distinct_ids=["typed"], team_id=self.team.pk)+ for amount in ("9", "20"):+ _create_event(+ event="$pageview",+ distinct_id="typed",+ person_id=person.uuid,+ person_properties={"amount": amount},+ team=self.team,+ timestamp=now - timedelta(days=1),+ properties={"amount": amount},+ )+ _create_event(+ event="$feature_flag_called",+ distinct_id="typed",+ person_id=person.uuid,+ person_properties={"amount": "12.5"},+ team=self.team,+ timestamp=now - timedelta(days=1),+ properties={"$feature_flag": "typed-flag", "source": "events", "amount": "12.5"},+ )+ _create_flag_evaluations(+ self.team.pk,+ "typed-flag",+ timestamp=now - timedelta(days=1),+ distinct_id="typed",+ person_id=person.uuid,+ person_properties={"amount": "12.5"},+ properties={"source": "flag_evaluations", "amount": "12.5"},+ )+ flush_persons_and_events()+ amount = "properties.amount" if kind == PropertyDefinition.Type.EVENT else "person.properties.amount"++ # Compared as text, "20" and "12.5" sort before "9", so a raw read in any branch or above it drops them.+ response = self._run(+ f"SELECT event, properties.source, toFloatOrZero({amount}) FROM events "+ f"WHERE {MIXED} AND distinct_id = 'typed' AND {amount} > 9 ORDER BY event",+ mode,+ PersonsOnEventsMode.PERSON_ID_OVERRIDE_PROPERTIES_ON_EVENTS,+ )++ assert response.results == [("$feature_flag_called", "flag_evaluations", 12.5), ("$pageview", None, 20.0)]+def test_prewhere_reads_both_tables(self):response = self._run(f"SELECT count() FROM events PREWHERE {MIXED} WHERE timestamp >= now() - INTERVAL 120 DAY",@@ -827,7 +941,7 @@ class TestFlagCallsUnionRows(ClickhouseTestMixin, APIBaseTest):# Mode 1 keeps reading events on the native JSON table, so only mode 2 reads the union there._set_mode(self.organization, ONLY if new_schema else READ)create_group_type_mapping(team=self.team, group_type="organization", group_type_index=0)- # A typed property reads as a number, which the union keeps only when it types each branch.+ # A typed property reads as a number, which the union keeps only when it converts the read above the branches.for property_type in (PropertyDefinition.Type.EVENT, PropertyDefinition.Type.PERSON):PropertyDefinition.objects.create(team=self.team, name="probe", property_type="Numeric", type=property_type)modifiers = HogQLQueryModifiers(personsOnEventsMode=poe, useNewEventsSchema=new_schema)diff --git a/posthog/test/repo_invariants/dataclass_frozen_baseline.txt b/posthog/test/repo_invariants/dataclass_frozen_baseline.txtindex 35575354ffff..955181877299 100644--- a/posthog/test/repo_invariants/dataclass_frozen_baseline.txt+++ b/posthog/test/repo_invariants/dataclass_frozen_baseline.txt@@ -400,7 +400,7 @@9 products/signals/backend/temporal/grouping.py10 products/signals/backend/temporal/summary.py10 products/tasks/backend/temporal/process_task/activities/provision_sandbox.py-102 posthog/hogql/ast.py+101 posthog/hogql/ast.py12 ee/clickhouse/materialized_columns/columns.py14 products/marketing_analytics/backend/hogql_queries/adapters/base.py14 products/subscriptions/backend/temporal/types.py