Skip to content

Commit

Permalink
Directly modify test account filters in TrendsQueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbachhuber committed Dec 18, 2024
1 parent 3169809 commit d1c62b9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
9 changes: 0 additions & 9 deletions posthog/hogql/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
from posthog.hogql.database.schema.person_distinct_ids import (
PersonDistinctIdsTable,
RawPersonDistinctIdsTable,
join_data_warehouse_experiment_table_with_person_distinct_ids_table,
)
from posthog.hogql.database.schema.persons import (
PersonsTable,
Expand Down Expand Up @@ -458,14 +457,6 @@ def define_mappings(warehouse: dict[str, Table], get_table: Callable):
),
)

if "events" in join.joining_table_name and join.configuration.get("experiments_optimized"):
source_table.fields["pdi"] = LazyJoin(
from_field=from_field,
join_table=PersonDistinctIdsTable(),
join_function=join_data_warehouse_experiment_table_with_person_distinct_ids_table,
)
source_table.fields["person"] = FieldTraverser(chain=["pdi", "person"])

if join.source_table_name == "persons":
person_field = database.events.fields["person"]
if isinstance(person_field, ast.FieldTraverser):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,13 @@ def test_query_runner_with_data_warehouse_series_no_end_date_and_nested_id(self)
"value": "@posthog.com",
"operator": "not_icontains",
"type": "person",
}
},
{
"key": "$host",
"type": "event",
"value": "^(localhost|127\\.0\\.0\\.1)($|:)",
"operator": "not_regex",
},
]
self.team.save()
count_query = TrendsQuery(
Expand Down Expand Up @@ -938,7 +944,7 @@ def test_query_runner_with_data_warehouse_series_no_end_date_and_nested_id(self)
)

# Assert the expected join condition in the clickhouse SQL
expected_join_condition = f"and(equals(events.team_id, {query_runner.count_query_runner.team.id}), equals(event, %(hogql_val_11)s), greaterOrEquals(timestamp, assumeNotNull(parseDateTime64BestEffortOrNull(%(hogql_val_12)s, 6, %(hogql_val_13)s))), lessOrEquals(timestamp, assumeNotNull(parseDateTime64BestEffortOrNull(%(hogql_val_14)s, 6, %(hogql_val_15)s))))) AS e__events ON"
expected_join_condition = f"and(equals(events.team_id, {query_runner.count_query_runner.team.id}), equals(event, %(hogql_val_9)s), greaterOrEquals(timestamp, assumeNotNull(parseDateTime64BestEffortOrNull(%(hogql_val_10)s, 6, %(hogql_val_11)s))), lessOrEquals(timestamp, assumeNotNull(parseDateTime64BestEffortOrNull(%(hogql_val_12)s, 6, %(hogql_val_13)s))))) AS e__events ON"
self.assertIn(
expected_join_condition,
str(response.clickhouse),
Expand Down
11 changes: 10 additions & 1 deletion posthog/hogql_queries/insights/trends/trends_query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,16 @@ def _events_filter(
and len(self.team.test_account_filters) > 0
):
for property in self.team.test_account_filters:
filters.append(property_to_expr(property, self.team))
if is_data_warehouse_series:
if property["type"] in ("event", "person"):
if property["type"] == "event":
property["key"] = f"events.properties.{property['key']}"
elif property["type"] == "person":
property["key"] = f"events.person.properties.{property['key']}"
property["type"] = "data_warehouse"
filters.append(property_to_expr(property, self.team))
else:
filters.append(property_to_expr(property, self.team))

# Properties
if self.query.properties is not None and self.query.properties != []:
Expand Down

0 comments on commit d1c62b9

Please sign in to comment.