Skip to content

: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.

fix(hogql): exclude every value in multi-value negative filters (#118050)

Phil Haack18f29351e532
diff --git a/posthog/hogql/property.py b/posthog/hogql/property.py
index 415231378b43..50b0c0990592 100644
--- a/posthog/hogql/property.py
+++ b/posthog/hogql/property.py
@@ -1449,13 +1449,7 @@ def property_to_expr(
)
for v in value
]
- if (
- operator == PropertyOperator.NOT_ICONTAINS
- or operator == PropertyOperator.NOT_REGEX
- or operator == PropertyOperator.IS_NOT
- or operator == PropertyOperator.NOT_STARTS_WITH
- or operator == PropertyOperator.NOT_ENDS_WITH
- ):
+ if operator_is_negative(operator):
return ast.And(exprs=exprs)
return ast.Or(exprs=exprs)
elif property.type == "data_warehouse_person_property":
@@ -1534,8 +1528,8 @@ def property_to_expr(
)
# _expr_to_compare_op combines every value of these into one multiSearchAnyCaseInsensitive
- # call, so they must reach it with the list intact. Expanding them per value would OR
- # separate negations together and keep a row that matches only one needle.
+ # call per 255 needles, so they must reach it with the list intact. Expanding them per
+ # value would build one call per needle.
combines_values_itself = operator in (
PropertyOperator.ICONTAINS_MULTI,
PropertyOperator.NOT_ICONTAINS_MULTI,
@@ -1596,13 +1590,7 @@ def property_to_expr(
)
for v in value
]
- if (
- operator == PropertyOperator.NOT_ICONTAINS
- or operator == PropertyOperator.NOT_REGEX
- or operator == PropertyOperator.IS_NOT
- or operator == PropertyOperator.NOT_STARTS_WITH
- or operator == PropertyOperator.NOT_ENDS_WITH
- ):
+ if operator_is_negative(operator):
return ast.And(exprs=exprs)
return ast.Or(exprs=exprs)
@@ -1658,13 +1646,7 @@ def property_to_expr(
)
for v in value
]
- if (
- operator == PropertyOperator.IS_NOT
- or operator == PropertyOperator.NOT_ICONTAINS
- or operator == PropertyOperator.NOT_REGEX
- or operator == PropertyOperator.NOT_STARTS_WITH
- or operator == PropertyOperator.NOT_ENDS_WITH
- ):
+ if operator_is_negative(operator):
return ast.And(exprs=exprs)
return ast.Or(exprs=exprs)
@@ -1790,7 +1772,7 @@ def bound_property_to_expr(property: Property, expr: ast.Expr, team: Team) -> as
left=clone_expr(expr),
right=ast.Tuple(exprs=[ast.Constant(value=v) for v in value]),
)
- else:
+ elif operator not in (PropertyOperator.ICONTAINS_MULTI, PropertyOperator.NOT_ICONTAINS_MULTI):
exprs = [
bound_property_to_expr(
Property(
@@ -1805,13 +1787,7 @@ def bound_property_to_expr(property: Property, expr: ast.Expr, team: Team) -> as
)
for v in value
]
- if (
- operator == PropertyOperator.IS_NOT
- or operator == PropertyOperator.NOT_ICONTAINS
- or operator == PropertyOperator.NOT_REGEX
- or operator == PropertyOperator.NOT_STARTS_WITH
- or operator == PropertyOperator.NOT_ENDS_WITH
- ):
+ if operator_is_negative(operator):
return ast.And(exprs=exprs)
return ast.Or(exprs=exprs)
diff --git a/posthog/hogql/test/test_filters.py b/posthog/hogql/test/test_filters.py
index 76dbf9b4af72..47cbb58c888c 100644
--- a/posthog/hogql/test/test_filters.py
+++ b/posthog/hogql/test/test_filters.py
@@ -940,6 +940,11 @@ class TestFilters(BaseTest):
EventPropertyFilter(key="plan", operator="is_not", value=["hobby", "scale"], type="event"),
"notIn(properties.plan, tuple('hobby', 'scale'))",
),
+ (
+ "multi_value_not_icontains_multi",
+ EventPropertyFilter(key="plan", operator="not_icontains_multi", value=["hobby", "scale"], type="event"),
+ "not(greater(multiSearchAnyCaseInsensitive(toString(properties.plan), ['hobby', 'scale']), 0))",
+ ),
(
"person_property_binds_by_key",
PersonPropertyFilter(key="plan", operator="icontains", value="hobby", type="person"),
diff --git a/posthog/hogql/test/test_property.py b/posthog/hogql/test/test_property.py
index b168cd197cf1..a6792d5f22ef 100644
--- a/posthog/hogql/test/test_property.py
+++ b/posthog/hogql/test/test_property.py
@@ -842,6 +842,17 @@ class TestProperty(BaseTest):
),
self._parse_expr("toString(elements_chain_href) ilike '%href-text.%'"),
)
+ self.assertEqual(
+ self._property_to_expr(
+ {
+ "type": "element",
+ "key": "href",
+ "value": ["a", "b"],
+ "operator": "not_in",
+ }
+ ),
+ self._parse_expr("elements_chain_href NOT IN ['a'] AND elements_chain_href NOT IN ['b']"),
+ )
for text_key in ("text", "$el_text"):
self.assertEqual(
self._property_to_expr(
@@ -1297,47 +1308,35 @@ class TestProperty(BaseTest):
self._parse_expr("person.extended_properties.bool_prop = true"),
)
- def test_data_warehouse_property_with_list_values(self):
- credential = DataWarehouseCredential.objects.create(
- team=self.team, access_key="_accesskey", access_secret="_secret"
- )
- DataWarehouseTable.objects.create(
- team=self.team,
- name="foobars",
- columns={
- "event": {"hogql": "StringDatabaseField", "clickhouse": "String"},
- "properties": {"hogql": "JSONField", "clickhouse": "String"},
- },
- credential=credential,
- url_pattern="",
- )
-
- expr = self._property_to_expr(
- Property(
- type="data_warehouse",
- key="foobars.properties.$feature/test",
- value=["control", "test"],
- operator="exact",
+ @parameterized.expand(
+ [
+ (
+ "exact",
+ "foobars.properties.`$feature/test` = 'control' OR foobars.properties.`$feature/test` = 'test'",
+ ),
+ (
+ "not_in",
+ "foobars.properties.`$feature/test` NOT IN ['control'] AND foobars.properties.`$feature/test` NOT IN ['test']",
+ ),
+ (
+ "not_icontains_multi",
+ "NOT multiSearchAnyCaseInsensitive(toString(foobars.properties.`$feature/test`), ['control']) > 0"
+ " AND NOT multiSearchAnyCaseInsensitive(toString(foobars.properties.`$feature/test`), ['test']) > 0",
),
+ ]
+ )
+ def test_data_warehouse_property_with_list_values(self, operator: str, expected: str):
+ expr = self._property_to_expr(
+ {
+ "type": "data_warehouse",
+ "key": "foobars.properties.$feature/test",
+ "value": ["control", "test"],
+ "operator": operator,
+ },
self.team,
)
- self.assertIsInstance(expr, ast.Or)
- self.assertEqual(len(expr.exprs), 2)
-
- # First expression
- compare_op_1 = expr.exprs[0]
- self.assertIsInstance(compare_op_1, ast.CompareOperation)
- self.assertIsInstance(compare_op_1.left, ast.Field)
- self.assertEqual(compare_op_1.left.chain, ["foobars", "properties", "$feature/test"])
- self.assertEqual(compare_op_1.right.value, "control")
-
- # Second expression
- compare_op_2 = expr.exprs[1]
- self.assertIsInstance(compare_op_2, ast.CompareOperation)
- self.assertIsInstance(compare_op_2.left, ast.Field)
- self.assertEqual(compare_op_2.left.chain, ["foobars", "properties", "$feature/test"])
- self.assertEqual(compare_op_2.right.value, "test")
+ self.assertEqual(expr, self._parse_expr(expected))
def test_revenue_analytics_property(self):
self.assertEqual(