Skip to content

Commit

Permalink
fix(web-analytics): Create group property filters correctly in hogql …
Browse files Browse the repository at this point in the history
…property_to_expr (#19981)
  • Loading branch information
robbie-c authored Jan 26, 2024
1 parent f22c21f commit ff17fd6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 2 additions & 0 deletions posthog/hogql/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def property_to_expr(
type=property.type,
key=property.key,
operator=property.operator,
group_type_index=property.group_type_index,
value=v,
),
team,
Expand Down Expand Up @@ -263,6 +264,7 @@ def property_to_expr(
type=property.type,
key=property.key,
operator=property.operator,
group_type_index=property.group_type_index,
value=v,
),
team,
Expand Down
6 changes: 5 additions & 1 deletion posthog/hogql/test/test_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,16 @@ def test_property_to_expr_group(self):
{"type": "group", "group_type_index": 0, "key": "a", "value": "b", "operator": "is_not_set"}
),
)
self.assertEqual(
self._property_to_expr(Property(type="group", group_type_index=0, key="a", value=["b", "c"])),
self._parse_expr("group_0.properties.a = 'b' OR group_0.properties.a = 'c'"),
)

with self.assertRaises(Exception) as e:
self._property_to_expr({"type": "group", "key": "a", "value": "b"})
self.assertEqual(
str(e.exception),
"Missing required key group_type_index for property type group with name a",
"Missing required attr group_type_index for property type group with key a",
)

def test_property_to_expr_event(self):
Expand Down
12 changes: 6 additions & 6 deletions posthog/models/property/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,14 @@ def __init__(
if self.type not in VALIDATE_PROP_TYPES.keys():
raise ValueError(f"Invalid property type: {self.type}")

for key in VALIDATE_PROP_TYPES[self.type]:
if getattr(self, key, None) is None:
raise ValueError(f"Missing required key {key} for property type {self.type} with name {self.key}")
for attr in VALIDATE_PROP_TYPES[self.type]:
if getattr(self, attr, None) is None:
raise ValueError(f"Missing required attr {attr} for property type {self.type} with key {self.key}")

if self.type == "behavioral":
for key in VALIDATE_BEHAVIORAL_PROP_TYPES[cast(BehavioralPropertyType, self.value)]:
if getattr(self, key, None) is None:
raise ValueError(f"Missing required key {key} for property type {self.type}::{self.value}")
for attr in VALIDATE_BEHAVIORAL_PROP_TYPES[cast(BehavioralPropertyType, self.value)]:
if getattr(self, attr, None) is None:
raise ValueError(f"Missing required attr {attr} for property type {self.type}::{self.value}")

def __repr__(self):
params_repr = ", ".join(f"{key}={repr(value)}" for key, value in self.to_dict().items())
Expand Down

0 comments on commit ff17fd6

Please sign in to comment.