: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): fold joined table names only when the table folds case (#126961)
Phil Haack05b2f7351559
diff --git a/posthog/hogql/resolver_utils.py b/posthog/hogql/resolver_utils.pyindex 0588641511fc..5371f1b7540f 100644--- a/posthog/hogql/resolver_utils.py+++ b/posthog/hogql/resolver_utils.py@@ -289,19 +289,20 @@ def lookup_table_by_nested_name(return match+def table_folds_identifier_case(table: Table, alias: Optional[str] = None) -> bool:+ """Whether the resolver matches a qualifier to `table` in any case. An explicit `alias` keeps the user's exact+ spelling, so only an alias that re-spells the table's own name counts."""+ if not getattr(table, "case_insensitive_identifiers", False):+ return False+ return alias is None or (table.name is not None and table.name.lower() == alias.lower())++def _folds_identifier_case(table_type: ast.TableOrSelectType) -> bool:if isinstance(table_type, ast.TableType):- return getattr(table_type.table, "case_insensitive_identifiers", False)+ return table_folds_identifier_case(table_type.table)# A table referenced with different casing than discovery registers as an implicit alias.- # Fold only that re-spelling; an explicit alias keeps the user's exact name.- if isinstance(table_type, ast.TableAliasType):- inner = table_type.table_type- return (- isinstance(inner, ast.TableType)- and getattr(inner.table, "case_insensitive_identifiers", False)- and inner.table.name is not None- and inner.table.name.lower() == table_type.alias.lower()- )+ if isinstance(table_type, ast.TableAliasType) and isinstance(table_type.table_type, ast.TableType):+ return table_folds_identifier_case(table_type.table_type.table, table_type.alias)return Falsediff --git a/posthog/hogql/transforms/flag_calls_from_flag_evaluations.py b/posthog/hogql/transforms/flag_calls_from_flag_evaluations.pyindex 125eb627c34e..1a2ac95408e9 100644--- a/posthog/hogql/transforms/flag_calls_from_flag_evaluations.py+++ b/posthog/hogql/transforms/flag_calls_from_flag_evaluations.py@@ -23,7 +23,7 @@ from posthog.hogql import astfrom posthog.hogql.base import _T_ASTfrom posthog.hogql.constants import FLAG_CALLED_EVENTfrom posthog.hogql.context import HogQLContext-from posthog.hogql.database.models import DatabaseField+from posthog.hogql.database.models import DatabaseField, Tablefrom posthog.hogql.database.schema.events import EventsTablefrom posthog.hogql.database.schema.events_with_flag_calls import EVENT_ROW_TABLESfrom posthog.hogql.database.schema.flag_evaluations import (@@ -41,6 +41,7 @@ from posthog.hogql.errors import BaseHogQLError, QueryErrorfrom posthog.hogql.functions.mapping import HOGQL_COMPARISON_MAPPING, find_hogql_functionfrom posthog.hogql.join_sources import JoinSourcesfrom posthog.hogql.property_metadata import PropertyMetadata, load_property_metadata+from posthog.hogql.resolver_utils import table_folds_identifier_casefrom posthog.hogql.utils import is_json_verbatimfrom posthog.hogql.visitor import TraversingVisitor, clone_expr@@ -122,6 +123,7 @@ def can_read_flag_evaluations(*,only_in_flag_evaluations: bool = False,flag_key_compares_as_string: bool = True,+ database: "Database | None" = None,) -> bool:"""Whether this query's `events` scan selects only rows that `flag_evaluations` holds.@@ -153,7 +155,7 @@ def can_read_flag_evaluations(`resolve_flag_calls_from_flag_evaluations` checks the rest after resolution: every name in the swapped querybinds as it did before, and every field that starts with `posthog.events` reads the scan."""- scan = EventsScan.of(node, lambda: flag_key_compares_as_string, frozenset(node.ctes or ()))+ scan = EventsScan.of(node, lambda: flag_key_compares_as_string, frozenset(node.ctes or ()), database)return scan is not None and _qualifies(scan, node, filter_conjuncts(node), now, timezone, only_in_flag_evaluations=only_in_flag_evaluations)@@ -265,6 +267,7 @@ def _resolve_with_swaps(timezone=ZoneInfo(database.get_timezone()),only_in_flag_evaluations=only_in_flag_evaluations,flag_key_compares_as_string=compares_as_string,+ database=database,)swapper.visit(rewritten)union = read_flag_calls_with_union(rewritten, context, compares_as_string)@@ -378,10 +381,12 @@ class EventsScan:other_tables: frozenset[str],flag_key_compares_as_string: Callable[[], bool],qualified_only: bool = False,+ folded_tables: frozenset[str] = frozenset(),):self.join = joinself.alias = aliasself.other_tables = other_tables+ self.folded_tables = folded_tablesself.qualified_only = qualified_onlyself.flag_key_compares_as_string = flag_key_compares_as_stringself.shadowed: frozenset[str] = frozenset()@@ -399,10 +404,15 @@ class EventsScan:@classmethoddef of(- cls, node: ast.SelectQuery, flag_key_compares_as_string: Callable[[], bool], ctes: frozenset[str]+ cls,+ node: ast.SelectQuery,+ flag_key_compares_as_string: Callable[[], bool],+ ctes: frozenset[str],+ database: "Database | None" = None,) -> "EventsScan | None":"""The scan of `node`, or None when `node` does not read `events`. `ctes` holds the CTE names in scope. The- resolver reads a CTE when one has the table's dotted name."""+ resolver reads a CTE when one has the table's dotted name. Without `database`, no joined table counts as one+ that folds identifier case."""join = node.select_fromif join is None or not isinstance(join.table, ast.Field):return None@@ -413,16 +423,16 @@ class EventsScan:scan = cls(join=join,alias=join.alias or "__".join(parts),- # The resolver reads the head of a chain of two or more parts as one of these tables first. Some warehouse- # tables match a name in any case.- other_tables=frozenset(name.lower() for name in ctes.union(_join_names(join.next_join))),+ # The resolver reads the head of a chain of two or more parts as one of these tables first.+ other_tables=ctes.union(_join_names(join.next_join)),+ folded_tables=frozenset(_folded_join_names(join.next_join, database)),flag_key_compares_as_string=flag_key_compares_as_string,)collector = _AliasCollector(node)collector.visit(node)# The resolver tries a joined table's name, a CTE and a select alias before it reads a qualifier such as# `posthog.events` as the name of a table.- scan.qualifier_shadows = ctes.union(_join_names(join.next_join), (name for name, _ in collector.aliases))+ scan.qualifier_shadows = scan.other_tables.union(name for name, _ in collector.aliases)# An alias such as `e.timestamp AS timestamp` reads the column it names, so it changes no read.scan.shadowed = frozenset(name for name, expr in collector.aliases if name in _SCAN_COLUMNS and scan.column(expr) != [name]@@ -444,7 +454,7 @@ class EventsScan:if self.qualified_only:return Nonehead = str(expr.chain[0])- if len(expr.chain) > 1 and head.lower() in self.other_tables:+ if len(expr.chain) > 1 and (head in self.other_tables or head.lower() in self.folded_tables):return None# The resolver rejects a name that two sources in FROM have as ambiguous. Events has every scan column, so a# name that no select alias shadows reads the scan in any query that resolves.@@ -580,6 +590,20 @@ def _join_names(join: ast.JoinExpr | None) -> Iterator[str]:join = join.next_join+def _folded_join_names(join: ast.JoinExpr | None, database: "Database | None") -> Iterator[str]:+ """The lowercased name of each joined table that the resolver matches in any case."""+ while database is not None and join is not None:+ if isinstance(join.table, ast.Field):+ chain = [str(part) for part in join.table.chain]+ try:+ table = database.get_table(chain) if database.has_table(chain) else None+ except BaseHogQLError:+ table = None+ if isinstance(table, Table) and table_folds_identifier_case(table, join.alias):+ yield (join.alias or "__".join(chain)).lower()+ join = join.next_join++def _below_posthog_events(chain: list[str | int]) -> list[str | int] | None:"""`chain` without a leading `posthog.events` or `` `posthog.events` ``, or None when it has neither."""if chain[0] == "posthog.events" and len(chain) > 1:@@ -940,8 +964,10 @@ class _EventsScanSwapper(ScanVisitor):timezone: ZoneInfo,only_in_flag_evaluations: bool,flag_key_compares_as_string: Callable[[], bool],+ database: "Database",) -> None:super().__init__()+ self.database = databaseself.now = nowself.timezone = timezoneself.only_in_flag_evaluations = only_in_flag_evaluations@@ -952,7 +978,7 @@ class _EventsScanSwapper(ScanVisitor):self.refusals: list[str] = []def visit_scan_query(self, node: ast.SelectQuery) -> None:- scan = EventsScan.of(node, self.flag_key_compares_as_string, self.ctes)+ scan = EventsScan.of(node, self.flag_key_compares_as_string, self.ctes, self.database)if scan is None:returnconjuncts = filter_conjuncts(node)diff --git a/posthog/hogql/transforms/test/test_flag_calls_from_flag_evaluations.py b/posthog/hogql/transforms/test/test_flag_calls_from_flag_evaluations.pyindex 9f10ae474b25..9a3629205e90 100644--- a/posthog/hogql/transforms/test/test_flag_calls_from_flag_evaluations.py+++ b/posthog/hogql/transforms/test/test_flag_calls_from_flag_evaluations.py@@ -3,18 +3,25 @@ from zoneinfo import ZoneInfoimport pytestimport time_machine+from posthog.test.base import BaseTestfrom django.test import SimpleTestCasefrom parameterized import parameterizedfrom posthog.hogql import ast+from posthog.hogql.context import HogQLContext+from posthog.hogql.database.database import Database+from posthog.hogql.database.direct_trino_table import DirectTrinoTable+from posthog.hogql.database.models import StringDatabaseField, Table, TableNodefrom posthog.hogql.database.schema.flag_evaluations import FLAG_EVALUATIONS_COMPLETE_DAYSfrom posthog.hogql.parser import parse_select+from posthog.hogql.resolver import resolve_typesfrom posthog.hogql.transforms.flag_calls_from_flag_evaluations import (EventsScan,can_read_flag_evaluations,filter_conjuncts,+ swap_flag_calls,)from posthog.models.flag_evaluations.sql import FLAG_EVALUATIONS_TTL_DAYS@@ -95,11 +102,16 @@ class TestCanReadFlagEvaluations(SimpleTestCase):timezone: str = "UTC",*,only_in_flag_evaluations: bool = False,+ database: Database | None = None,) -> bool:select = parse_select(query, placeholders=placeholders)assert isinstance(select, ast.SelectQuery)return can_read_flag_evaluations(- select, datetime.now(UTC), ZoneInfo(timezone), only_in_flag_evaluations=only_in_flag_evaluations+ select,+ datetime.now(UTC),+ ZoneInfo(timezone),+ only_in_flag_evaluations=only_in_flag_evaluations,+ database=database,)@parameterized.expand(@@ -520,6 +532,42 @@ class TestCanReadFlagEvaluations(SimpleTestCase):def test_refuses_a_query_that_breaks_a_condition(self, _name: str, query: str):assert self._can_read(query) is False+ @parameterized.expand(+ [+ ("regular_table_spelled_in_another_case", "Properties", "Properties", False, True),+ ("regular_table_spelled_the_same", "properties", "properties", False, False),+ ("case_folding_table_spelled_in_another_case", "properties", "Properties", True, False),+ (+ "case_folding_table_with_an_alias_that_respells_it",+ "properties",+ "properties AS Properties",+ True,+ False,+ ),+ ("case_folding_table_with_another_alias", "orders", "orders AS Properties", True, True),+ ]+ )+ def test_joined_table_shadows_a_column_in_another_case_only_when_it_folds_case(+ self, _name: str, registered: str, spelled: str, folds_case: bool, expected: bool+ ):+ fields = {"distinct_id": StringDatabaseField(name="distinct_id")}+ table: Table = (+ DirectTrinoTable(+ name=registered,+ fields=fields,+ external_data_source_id="source-id",+ trino_catalog="c",+ trino_schema="s",+ trino_table_name=registered,+ )+ if folds_case+ else Table(fields=fields)+ )+ database = Database(include_posthog_tables=False)+ database.tables.add_child(TableNode(name=registered, table=table, case_insensitive=folds_case))+ query = _beside(f"JOIN {spelled} ON e.distinct_id = {spelled.split()[-1]}.distinct_id")+ assert self._can_read(query, database=database) is expected+@parameterized.expand([("no_lower_bound", _where(FLAG_CALLED, FLAG_KEY), True),@@ -601,3 +649,46 @@ class TestCanReadFlagEvaluations(SimpleTestCase):):with time_machine.travel(now, tick=False):assert self._can_read(_bounded_by(bound), timezone=timezone) is False+++class TestSwapFlagCallsBesideJoinedTable(BaseTest):+ @parameterized.expand(+ [+ ("regular_table_spelled_in_another_case", "Properties", "Properties", False, True),+ ("case_folding_table_spelled_in_another_case", "properties", "Properties", True, False),+ ]+ )+ def test_swaps_the_scan_unless_a_folding_table_shadows_the_flag_key(+ self, _name: str, registered: str, spelled: str, folds_case: bool, expected: bool+ ):+ fields = {+ "distinct_id": StringDatabaseField(name="distinct_id"),+ "$feature_flag": StringDatabaseField(name="$feature_flag"),+ }+ table: Table = (+ DirectTrinoTable(+ name=registered,+ fields=fields,+ external_data_source_id="source-id",+ trino_catalog="c",+ trino_schema="s",+ trino_table_name=registered,+ )+ if folds_case+ else Table(fields=fields)+ )+ database = Database.create_for(team=self.team, include_flag_evaluations_table=True)+ database._read_flag_calls_from_flag_evaluations = True+ database.tables.add_child(TableNode(name=registered, table=table, case_insensitive=folds_case))+ context = HogQLContext(+ team_id=self.team.pk, database=database, enable_select_queries=True, use_new_events_schema=False+ )+ query = _beside(f"JOIN {spelled} ON e.distinct_id = {spelled}.distinct_id")++ swap = swap_flag_calls(+ parse_select(query),+ context,+ lambda node, **kwargs: resolve_types(node, context, "clickhouse", **kwargs),+ )++ assert ("flag_evaluations" in swap.source.to_hogql()) is expected