: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): keep negative resource filters negative for every operator (#118241)
Phil Haackf3eff75f5557
diff --git a/posthog/hogql/property.py b/posthog/hogql/property.pyindex dcd4d3f03eae..0d26d5e8b338 100644--- a/posthog/hogql/property.py+++ b/posthog/hogql/property.py@@ -689,6 +689,9 @@ _NEGATIVE_OPERATOR_COMPLEMENTS = {PropertyOperator.NOT_ENDS_WITH: PropertyOperator.ENDS_WITH,PropertyOperator.NOT_BETWEEN: PropertyOperator.BETWEEN,PropertyOperator.IS_CLEANED_PATH_NOT_EXACT: PropertyOperator.IS_CLEANED_PATH_EXACT,+ # The scalar semver_neq arm drops missing and non-semver values, so it is not NOT(semver_eq).+ # Callers that negate this complement instead keep those values.+ PropertyOperator.SEMVER_NEQ: PropertyOperator.SEMVER_EQ,}@@ -696,6 +699,11 @@ def operator_is_negative(operator: PropertyOperator) -> bool:return operator in _NEGATIVE_OPERATOR_COMPLEMENTS+def negative_operator_complement(operator: PropertyOperator) -> PropertyOperator:+ """Return the positive form of a negative operator, and any other operator unchanged."""+ return _NEGATIVE_OPERATOR_COMPLEMENTS.get(operator, operator)++def _array_property_filter(array: ast.Expr, element_predicate: ast.Expr, negate: bool) -> ast.Expr:"""`element_predicate` is the positive form even when `negate` is set: a negative operatornegates the whole arrayExists, so an empty or missing array is kept and an array holding any@@ -1636,7 +1644,7 @@ def property_to_expr(element_predicate = _expr_to_compare_op(expr=ast.Field(chain=["v"]),value=value,- operator=_NEGATIVE_OPERATOR_COMPLEMENTS.get(operator, operator),+ operator=negative_operator_complement(operator),team=team,property=property,is_json_field=is_json_field,diff --git a/posthog/hogql/test/test_property.py b/posthog/hogql/test/test_property.pyindex 7fab63a90275..f01404d52042 100644--- a/posthog/hogql/test/test_property.py+++ b/posthog/hogql/test/test_property.py@@ -2033,6 +2033,22 @@ class TestProperty(BaseTest):),)+ # semver_neq with a list must drop a row that equals any listed version. An OR join keeps+ # that row, because the row differs from the other listed versions.+ self.assertEqual(+ self._property_to_expr(+ {"type": "person", "key": "app_version", "operator": "semver_neq", "value": ["1.2.3", "2.0.0"]}+ ),+ ast.And(+ exprs=[+ self._property_to_expr(+ {"type": "person", "key": "app_version", "operator": "semver_neq", "value": version}+ )+ for version in ("1.2.3", "2.0.0")+ ]+ ),+ )+def test_property_to_expr_semver_validation(self):version_gate = "match(person.properties.version, '^\\\\s*v?(0|[1-9]\\\\d*)\\\\.(0|[1-9]\\\\d*)\\\\.(0|[1-9]\\\\d*)(?:[-+][^\\\\s]*)?\\\\s*$')"@@ -2248,7 +2264,7 @@ class TestProperty(BaseTest):def test_every_negative_operator_is_known(self):# A negative operator missing from operator_is_negative silently negates per element on# array properties and loses its lowercase index hint, so the name pattern pins the set.- by_name = {op for op in PropertyOperator if "not" in op.value.split("_")}+ by_name = {op for op in PropertyOperator if "not" in op.value.split("_") or op.value.endswith("_neq")}assert {op for op in PropertyOperator if operator_is_negative(op)} == by_namedef test_flag_evaluates_to_produces_neutral_expr(self):@@ -2789,7 +2805,7 @@ class TestNegativeOperatorNullParityWithData(APIBaseTest):EVENT = "purchase"SCORED_EVENT = "scored"EXCEPTION_EVENT = "exception"- ARRAYS_WITHOUT_MATCH = {"types_missing", "types_empty", "types_nonmatch_only"}+ ARRAYS_WITHOUT_MATCH = {"types_missing", "types_empty", "types_nonmatch_only", "types_versions"}ARRAYS_WITH_MATCH = {"types_match_only", "types_mixed"}@classmethod@@ -2826,6 +2842,7 @@ class TestNegativeOperatorNullParityWithData(APIBaseTest):("types_match_only", ["ReferenceError"]),("types_nonmatch_only", ["OtherError"]),("types_mixed", ["ReferenceError", "OtherError"]),+ ("types_versions", ["1.2.3", "2.0.0"]),]:_create_event(team=cls.team,@@ -2999,6 +3016,11 @@ class TestNegativeOperatorNullParityWithData(APIBaseTest):ARRAYS_WITHOUT_MATCH,),("is_not_set", {"operator": "is_not_set"}, {"types_missing", "types_empty"}),+ (+ "semver_neq",+ {"value": "1.2.3", "operator": "semver_neq"},+ (ARRAYS_WITHOUT_MATCH - {"types_versions"}) | ARRAYS_WITH_MATCH,+ ),# positive baseline: the mixed row holds a match, so it belongs on this side and only this side("icontains_multi_value",diff --git a/products/logs/backend/logs_query_runner.py b/products/logs/backend/logs_query_runner.pyindex 2ab6efb2d675..05de2b02ee26 100644--- a/products/logs/backend/logs_query_runner.py+++ b/products/logs/backend/logs_query_runner.py@@ -22,7 +22,12 @@ from posthog.hogql import astfrom posthog.hogql.constants import HogQLGlobalSettings, LimitContextfrom posthog.hogql.errors import ExposedHogQLError, QueryErrorfrom posthog.hogql.parser import parse_expr, parse_order_expr, parse_select-from posthog.hogql.property import get_lowercase_index_hint, operator_is_negative, property_to_expr+from posthog.hogql.property import (+ get_lowercase_index_hint,+ negative_operator_complement,+ operator_is_negative,+ property_to_expr,+)from posthog.clickhouse.client.connection import Workloadfrom posthog.hogql_queries.paginators import HogQLHasMorePaginator@@ -148,18 +153,8 @@ def _generate_resource_attribute_filters(for filter in resource_attribute_filters:attribute_type = "resource" if filter.type == LogPropertyFilterType.LOG_RESOURCE_ATTRIBUTE else "log"if is_negative_filter:- # invert the negative filter back to the positive equivalent- # we invert the IN logic instead- filter.operator = {- PropertyOperator.IS_NOT: PropertyOperator.EXACT,- PropertyOperator.NOT_ICONTAINS: PropertyOperator.ICONTAINS,- PropertyOperator.NOT_STARTS_WITH: PropertyOperator.STARTS_WITH,- PropertyOperator.NOT_ENDS_WITH: PropertyOperator.ENDS_WITH,- PropertyOperator.NOT_REGEX: PropertyOperator.REGEX,- PropertyOperator.IS_NOT_SET: PropertyOperator.IS_SET,- PropertyOperator.NOT_BETWEEN: PropertyOperator.BETWEEN,- PropertyOperator.NOT_IN: PropertyOperator.IN_,- }.get(filter.operator, filter.operator)+ # The subquery matches the positive form. The outer NOT IN excludes the matching resources.+ filter.operator = negative_operator_complement(filter.operator)if filter.operator == PropertyOperator.IS_SET:converted_exprs.append(diff --git a/products/logs/backend/test/test_logs_query_runner.py b/products/logs/backend/test/test_logs_query_runner.pyindex f62c39a9ad27..c897b462c5a7 100644--- a/products/logs/backend/test/test_logs_query_runner.py+++ b/products/logs/backend/test/test_logs_query_runner.py@@ -747,8 +747,16 @@ class TestLogsQueryRunner(ClickhouseTestMixin, APIBaseTest):self.assertEqual(len(response["results"]), 0)self.assertEqual(len(queries), 2)+ @parameterized.expand(+ [+ ("is_not", "envoy"),+ # The negative path flips each operator to its positive form and excludes what that form+ # matches, so an operator without a positive complement returns the opposite of the filter.+ ("not_icontains_multi", ["envoy"]),+ ]+ )@time_machine.travel("2025-12-16T10:33:00Z", tick=False)- def test_multiple_negative_resource_attribute_filters(self):+ def test_multiple_negative_resource_attribute_filters(self, container_operator, container_value):# Two negative resource attribute filters on disjoint values (no log has both an envoy# container AND the kube-system namespace). The fix exists so a resource is excluded when# it matches ANY of the negative filters, not just when it matches every one. Pre-fix this@@ -764,8 +772,8 @@ class TestLogsQueryRunner(ClickhouseTestMixin, APIBaseTest):"values": [{"key": "k8s.container.name",- "value": "envoy",- "operator": "is_not",+ "value": container_value,+ "operator": container_operator,"type": "log_resource_attribute",},{@@ -787,7 +795,7 @@ class TestLogsQueryRunner(ClickhouseTestMixin, APIBaseTest):self.assertGreater(len(results), 0)# neither excluded value appears anywhere — proves OR semanticsfor result in results:- self.assertNotEqual(result["resource_attributes"].get("k8s.container.name"), "envoy")+ self.assertNotIn("envoy", result["resource_attributes"].get("k8s.container.name", ""))self.assertNotEqual(result["resource_attributes"].get("k8s.namespace.name"), "kube-system")# both excluded groups exist in the test data, so the result count must be strictly less than# the unfiltered count. Pre-fix this would have returned every log in range (since no resource@@ -801,6 +809,59 @@ class TestLogsQueryRunner(ClickhouseTestMixin, APIBaseTest):)self.assertLess(len(results), len(unfiltered["results"]))+ @time_machine.travel("2025-11-21T00:00:00Z", tick=False)+ def test_semver_neq_resource_attribute_filter_excludes_only_the_matching_version(self):+ # The shared fixture holds no semver-shaped resource attribute, so this test inserts its own+ # resources. The class shares one team and ClickHouse keeps rows between tests, so the rows+ # sit on 2025-11-20, which is outside the date range of every other test in this class.+ versions = {"matching": "1.2.3", "other": "2.0.0", "non_semver": "latest", "absent": None}+ rows = []+ for body, version in versions.items():+ resource_attributes = {"service.name": "semver-neq-test-svc"}+ if version is not None:+ resource_attributes["service.version"] = version+ rows.append(+ {+ "uuid": str(uuid4()),+ "team_id": self.team.id,+ "timestamp": "2025-11-20 10:00:00.000000",+ "observed_timestamp": "2025-11-20 10:00:01.000000",+ "body": body,+ "severity_text": "info",+ "severity_number": 9,+ "service_name": "semver-neq-test-svc",+ "resource_attributes": resource_attributes,+ "attributes_map_str": {},+ }+ )+ payload = "\n".join(json.dumps(row) for row in rows)+ sync_execute(f"INSERT INTO logs FORMAT JSONEachRow\n{payload}")++ response = self._make_logs_api_request(+ {+ "dateRange": {"date_from": "2025-11-20T00:00:00Z", "date_to": "2025-11-21T00:00:00Z"},+ "limit": 10,+ "filterGroup": {+ "type": "AND",+ "values": [+ {+ "type": "AND",+ "values": [+ {+ "key": "service.version",+ "value": "1.2.3",+ "operator": "semver_neq",+ "type": "log_resource_attribute",+ }+ ],+ }+ ],+ },+ }+ )++ self.assertEqual({result["body"] for result in response["results"]}, {"other", "non_semver", "absent"})+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"),