: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(logs): force optimized property groups for attribute filters (#66617)
Christophe Eynius Tranbergc186037e91ec
diff --git a/posthog/hogql/printer/utils.py b/posthog/hogql/printer/utils.pyindex 3eb157f42d0a..4b8f3a532b02 100644--- a/posthog/hogql/printer/utils.py+++ b/posthog/hogql/printer/utils.py@@ -194,11 +194,14 @@ def prepare_ast_for_printing(collector.visit(node)context.workload = collector.get_workload()- # LOGS-cluster tables (logs, spans, metrics) store attributes in `*_map_str/_float` Map columns, not JSON blobs.- # Property reads only resolve to those Map columns under OPTIMIZED; otherwise they fall back to JSONExtract, which- # errors on a Map. Force OPTIMIZED here — after workload detection, before property resolution reads the modifier —- # so every caller (SQL panel, alerts, runners) is correct without each having to set it.- if context.workload == Workload.LOGS and context.modifiers.propertyGroupsMode is None:+ # LOGS-cluster tables (logs, spans, metrics) split attributes across typed `*_map_str/_float/_datetime` Map columns.+ # A type-suffixed attribute key (e.g. `host__str`) only resolves to its physical column via property groups, which+ # are active under OPTIMIZED. Without OPTIMIZED the read falls back to a subscript on the un-suffixed `attributes`+ # alias, where the suffixed key never matches — so `is not` filters match every row and `equals` filters match none+ # (silently wrong, not an error). OPTIMIZED is therefore required for correctness here, not merely a perf mode, so+ # force it for every logs query regardless of any non-OPTIMIZED value a caller may have set — after workload+ # detection, before property resolution reads the modifier.+ if context.workload == Workload.LOGS and context.modifiers.propertyGroupsMode != PropertyGroupsMode.OPTIMIZED:context.modifiers.propertyGroupsMode = PropertyGroupsMode.OPTIMIZEDif context.modifiers.optimizeProjections:diff --git a/products/logs/backend/test/test_logs_query_runner.py b/products/logs/backend/test/test_logs_query_runner.pyindex ecf522d6153e..e346c55de77a 100644--- a/products/logs/backend/test/test_logs_query_runner.py+++ b/products/logs/backend/test/test_logs_query_runner.py@@ -16,6 +16,7 @@ from posthog.schema import (LogsQuery,PropertyGroupFilter,PropertyGroupFilterValue,+ PropertyGroupsMode,PropertyOperator,)@@ -596,6 +597,55 @@ class TestLogsQueryRunner(ClickhouseTestMixin, APIBaseTest):)self.assertLess(len(results), len(unfiltered["results"]))+ def _run_log_attribute_filter(self, operator: PropertyOperator, mode: PropertyGroupsMode) -> list[dict]:+ query = LogsQuery(+ dateRange=DateRange(date_from="2025-12-14T00:00:00Z", date_to="2025-12-18T03:00:00Z"),+ limit=2000,+ serviceNames=[],+ severityLevels=[],+ filterGroup=PropertyGroupFilter(+ type=FilterLogicalOperator.AND_,+ values=[+ PropertyGroupFilterValue(+ type=FilterLogicalOperator.AND_,+ values=[+ LogPropertyFilter(+ key="logtag",+ operator=operator,+ type=LogPropertyFilterType.LOG_ATTRIBUTE,+ value=["F"],+ ),+ ],+ )+ ],+ ),+ kind="LogsQuery",+ )+ runner = LogsQueryRunner(query=query, team=self.team)+ # Simulate a logs query path that reaches the printer with a non-OPTIMIZED mode (the logs-cluster+ # path this regression covers). The printer must still force OPTIMIZED so the filter resolves the+ # type-suffixed key against the typed `attributes_map_str` column instead of the un-suffixed alias.+ runner.modifiers.propertyGroupsMode = mode+ return runner.calculate().results++ @parameterized.expand([(PropertyGroupsMode.OPTIMIZED,), (PropertyGroupsMode.DISABLED,)])+ @freeze_time("2025-12-18T03:00:00Z")+ def test_log_attribute_filter_resolves_regardless_of_property_groups_mode(self, mode: PropertyGroupsMode):+ # The seed data has 1000 logs with logtag="F" and 11 without. A log-attribute filter must resolve the+ # type-suffixed key (logtag__str) to its physical Map column for every property-groups mode — otherwise an+ # `is not` filter matches every row (does nothing) and an `exact` filter matches none, which is the silent+ # mis-filtering this regression guards against. DISABLED is the mode that fails without the printer's forced+ # OPTIMIZED; OPTIMIZED is the control.+ is_not_results = self._run_log_attribute_filter(PropertyOperator.IS_NOT, mode)+ # The "is not F" filter must actually exclude the 1000 logtag="F" rows — not pass them through.+ self.assertTrue(all(r["attributes"].get("logtag") != "F" for r in is_not_results))+ self.assertEqual(len(is_not_results), 11)++ exact_results = self._run_log_attribute_filter(PropertyOperator.EXACT, mode)+ # The "= F" filter must match the 1000 logtag="F" rows — not return nothing.+ self.assertTrue(all(r["attributes"].get("logtag") == "F" for r in exact_results))+ self.assertEqual(len(exact_results), 1000)+@freeze_time("2025-12-16T10:33:00Z")def test_resource_negative_attribute_filters(self):query_params = {