Skip to content

Commit

Permalink
fix(hogql): Fix query for array of values (#18742)
Browse files Browse the repository at this point in the history
Fixes #18651
  • Loading branch information
webjunkie authored Nov 20, 2023
1 parent c04b5df commit 9a5c644
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
47 changes: 28 additions & 19 deletions posthog/hogql/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,26 +126,35 @@ def property_to_expr(
elif len(value) == 1:
value = value[0]
else:
exprs = [
property_to_expr(
Property(
type=property.type,
key=property.key,
operator=property.operator,
value=v,
),
team,
scope,
if operator in [PropertyOperator.exact, PropertyOperator.is_not]:
op = (
ast.CompareOperationOp.In
if operator == PropertyOperator.exact
else ast.CompareOperationOp.NotIn
)
for v in value
]
if (
operator == PropertyOperator.is_not
or operator == PropertyOperator.not_icontains
or operator == PropertyOperator.not_regex
):
return ast.And(exprs=exprs)
return ast.Or(exprs=exprs)

return ast.CompareOperation(
op=op,
left=ast.Field(chain=["properties", property.key]),
right=ast.Tuple(exprs=[ast.Constant(value=v) for v in value]),
)
else:
exprs = [
property_to_expr(
Property(
type=property.type,
key=property.key,
operator=property.operator,
value=v,
),
team,
scope,
)
for v in value
]
if operator == PropertyOperator.not_icontains or operator == PropertyOperator.not_regex:
return ast.And(exprs=exprs)
return ast.Or(exprs=exprs)

chain = ["person", "properties"] if property.type == "person" and scope != "person" else ["properties"]
field = ast.Field(chain=chain + [property.key])
Expand Down
4 changes: 2 additions & 2 deletions posthog/hogql/test/test_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def test_property_to_expr_event_list(self):
# positive
self.assertEqual(
self._property_to_expr({"type": "event", "key": "a", "value": ["b", "c"], "operator": "exact"}),
self._parse_expr("properties.a = 'b' or properties.a = 'c'"),
self._parse_expr("properties.a IN ('b', 'c')"),
)
self.assertEqual(
self._property_to_expr(
Expand All @@ -183,7 +183,7 @@ def test_property_to_expr_event_list(self):
# negative
self.assertEqual(
self._property_to_expr({"type": "event", "key": "a", "value": ["b", "c"], "operator": "is_not"}),
self._parse_expr("properties.a != 'b' and properties.a != 'c'"),
self._parse_expr("properties.a NOT IN ('b', 'c')"),
)
self.assertEqual(
self._property_to_expr(
Expand Down

0 comments on commit 9a5c644

Please sign in to comment.