: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(hogql): keep whole-list operators intact in warehouse filters (#118126)
Phil Haack325b9d536473
diff --git a/posthog/hogql/property.py b/posthog/hogql/property.pyindex 50b0c0990592..c679c0965a8b 100644--- a/posthog/hogql/property.py+++ b/posthog/hogql/property.py@@ -755,6 +755,21 @@ def validate_regex(value: ValueT) -> None:raise QueryError(f"Invalid regular expression: '{value}'") from err+# EXACT and IS_NOT stay out because their _expr_to_compare_op arms compare against one scalar.+_WHOLE_LIST_OPERATORS = frozenset(+ {+ PropertyOperator.IN_,+ PropertyOperator.NOT_IN,+ PropertyOperator.ICONTAINS,+ PropertyOperator.NOT_ICONTAINS,+ PropertyOperator.ICONTAINS_MULTI,+ PropertyOperator.NOT_ICONTAINS_MULTI,+ PropertyOperator.BETWEEN,+ PropertyOperator.NOT_BETWEEN,+ }+)++def _expr_to_compare_op(expr: ast.Expr, value: ValueT, operator: PropertyOperator, property: Property, is_json_field: bool, team: Team) -> ast.Expr:@@ -1436,7 +1451,10 @@ def property_to_expr(chain = split[:-1]property.key = split[-1]- if isinstance(value, list) and len(value) > 1:+ # The shared list handling below rebuilds the Property from property.key to split a list+ # per value. That key no longer holds the table chain, so this branch splits the list itself.+ # It falls through for the whole-list operators, which the shared handling never splits.+ if isinstance(value, list) and len(value) > 1 and operator not in _WHOLE_LIST_OPERATORS:field = ast.Field(chain=[*chain, property.key])exprs = [_expr_to_compare_op(@@ -1527,14 +1545,6 @@ def property_to_expr(],)- # _expr_to_compare_op combines every value of these into one multiSearchAnyCaseInsensitive- # 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,- )-if isinstance(value, list) and operator not in (PropertyOperator.BETWEEN,PropertyOperator.NOT_BETWEEN,@@ -1551,30 +1561,27 @@ def property_to_expr(# _expr_to_compare_op's ICONTAINS_MULTI/NOT_ICONTAINS_MULTI arms re-wrap a scalar# into a single-element list, so collapsing here is a no-op round trip for them too.value = value[0]- elif not combines_values_itself:- if operator in (- PropertyOperator.EXACT,- PropertyOperator.IS_NOT,- PropertyOperator.IN_,- PropertyOperator.NOT_IN,- ):- coerced = cast(list, _coerce_numeric_value_for_string_property(value, property, team))- values = ast.Tuple(exprs=[ast.Constant(value=v) for v in coerced])- negate = operator_is_negative(operator)- if array_field is not None:- return _array_property_filter(- array_field,- ast.CompareOperation(- op=ast.CompareOperationOp.In, left=ast.Field(chain=["v"]), right=values- ),- negate=negate,- )- return ast.CompareOperation(- op=ast.CompareOperationOp.NotIn if negate else ast.CompareOperationOp.In,- left=expr,- right=values,+ elif operator in (+ PropertyOperator.EXACT,+ PropertyOperator.IS_NOT,+ PropertyOperator.IN_,+ PropertyOperator.NOT_IN,+ ):+ coerced = cast(list, _coerce_numeric_value_for_string_property(value, property, team))+ values = ast.Tuple(exprs=[ast.Constant(value=v) for v in coerced])+ negate = operator_is_negative(operator)+ if array_field is not None:+ return _array_property_filter(+ array_field,+ ast.CompareOperation(op=ast.CompareOperationOp.In, left=ast.Field(chain=["v"]), right=values),+ negate=negate,)-+ return ast.CompareOperation(+ op=ast.CompareOperationOp.NotIn if negate else ast.CompareOperationOp.In,+ left=expr,+ right=values,+ )+ elif operator not in _WHOLE_LIST_OPERATORS:exprs = [property_to_expr(Property(@@ -1772,7 +1779,7 @@ def bound_property_to_expr(property: Property, expr: ast.Expr, team: Team) -> asleft=clone_expr(expr),right=ast.Tuple(exprs=[ast.Constant(value=v) for v in value]),)- elif operator not in (PropertyOperator.ICONTAINS_MULTI, PropertyOperator.NOT_ICONTAINS_MULTI):+ elif operator not in _WHOLE_LIST_OPERATORS:exprs = [bound_property_to_expr(Property(diff --git a/posthog/hogql/test/test_property.py b/posthog/hogql/test/test_property.pyindex a6792d5f22ef..c3884cbab8b3 100644--- a/posthog/hogql/test/test_property.py+++ b/posthog/hogql/test/test_property.py@@ -1312,25 +1312,43 @@ class TestProperty(BaseTest):[("exact",+ ["control", "test"],"foobars.properties.`$feature/test` = 'control' OR foobars.properties.`$feature/test` = 'test'",),+ (+ "is_not",+ ["control", "test"],+ "foobars.properties.`$feature/test` != 'control' AND foobars.properties.`$feature/test` != 'test'",+ ),("not_in",- "foobars.properties.`$feature/test` NOT IN ['control'] AND foobars.properties.`$feature/test` NOT IN ['test']",+ ["control", "test"],+ "foobars.properties.`$feature/test` NOT IN ('control', 'test')",),("not_icontains_multi",- "NOT multiSearchAnyCaseInsensitive(toString(foobars.properties.`$feature/test`), ['control']) > 0"- " AND NOT multiSearchAnyCaseInsensitive(toString(foobars.properties.`$feature/test`), ['test']) > 0",+ ["control", "test"],+ "NOT multiSearchAnyCaseInsensitive(toString(foobars.properties.`$feature/test`), ['control', 'test']) > 0",+ ),+ (+ "between",+ [1, 10],+ "foobars.properties.`$feature/test` >= 1 AND foobars.properties.`$feature/test` <= 10",+ ),+ (+ "not_between",+ [1, 10],+ "foobars.properties.`$feature/test` < 1 OR foobars.properties.`$feature/test` > 10"+ " OR isNull(foobars.properties.`$feature/test`)",),])- def test_data_warehouse_property_with_list_values(self, operator: str, expected: str):+ def test_data_warehouse_property_with_list_values(self, operator: str, value: list, expected: str):expr = self._property_to_expr({"type": "data_warehouse","key": "foobars.properties.$feature/test",- "value": ["control", "test"],+ "value": value,"operator": operator,},self.team,diff --git a/products/web_analytics/backend/hogql_queries/first_pageview_attribution.py b/products/web_analytics/backend/hogql_queries/first_pageview_attribution.pyindex 85ae4fc384ca..5edb93fb69e5 100644--- a/products/web_analytics/backend/hogql_queries/first_pageview_attribution.py+++ b/products/web_analytics/backend/hogql_queries/first_pageview_attribution.py@@ -5,7 +5,7 @@ from posthog.schema import HogQLQueryModifiers, PropertyOperator, SessionPropertfrom posthog.hogql import astfrom posthog.hogql.database.schema.channel_type import ChannelTypeExprs, create_channel_type_exprfrom posthog.hogql.parser import parse_expr, parse_select-from posthog.hogql.property import _expr_to_compare_op, property_to_expr+from posthog.hogql.property import _WHOLE_LIST_OPERATORS, _expr_to_compare_op, property_to_exprfrom posthog.hogql.timings import HogQLTimingsfrom posthog.models.property import Property, ValueT@@ -157,16 +157,7 @@ def first_pageview_filter_value_expr(def _list_aware_expr_to_compare_op(expr: ast.Expr, value: ValueT, operator: PropertyOperator, property: Property, is_json_field: bool, team: "Team") -> ast.Expr:- if not isinstance(value, list) or operator in (- PropertyOperator.BETWEEN,- PropertyOperator.NOT_BETWEEN,- PropertyOperator.ICONTAINS,- PropertyOperator.NOT_ICONTAINS,- PropertyOperator.ICONTAINS_MULTI,- PropertyOperator.NOT_ICONTAINS_MULTI,- PropertyOperator.IN_,- PropertyOperator.NOT_IN,- ):+ if not isinstance(value, list) or operator in _WHOLE_LIST_OPERATORS:return _expr_to_compare_op(expr, value, operator, property, is_json_field, team)if len(value) == 0:return ast.Constant(value=1)