Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
timgl committed Oct 25, 2024
1 parent 2ddfb27 commit 7b08365
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 59 deletions.
6 changes: 5 additions & 1 deletion posthog/hogql/database/schema/persons.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ def select_from_persons_table(
and_conditions.append(filter)

# For now, only do this optimization for directly querying the persons table (without joins or as part of a subquery) to avoid knock-on effects to insight queries
if hasattr(node.select_from.type, "table") and isinstance(node.select_from.type.table, PersonsTable):
if (
node.select_from
and getattr(node.select_from.type, "table", False)
and isinstance(node.select_from.type.table, PersonsTable)

Check failure on line 70 in posthog/hogql/database/schema/persons.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Item "BaseTableType" of "BaseTableType | SelectUnionQueryType | SelectQueryType | SelectQueryAliasType | SelectViewType | None" has no attribute "table"

Check failure on line 70 in posthog/hogql/database/schema/persons.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Item "SelectUnionQueryType" of "BaseTableType | SelectUnionQueryType | SelectQueryType | SelectQueryAliasType | SelectViewType | None" has no attribute "table"

Check failure on line 70 in posthog/hogql/database/schema/persons.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Item "SelectQueryType" of "BaseTableType | SelectUnionQueryType | SelectQueryType | SelectQueryAliasType | SelectViewType | None" has no attribute "table"

Check failure on line 70 in posthog/hogql/database/schema/persons.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Item "SelectQueryAliasType" of "BaseTableType | SelectUnionQueryType | SelectQueryType | SelectQueryAliasType | SelectViewType | None" has no attribute "table"

Check failure on line 70 in posthog/hogql/database/schema/persons.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Item "SelectViewType" of "BaseTableType | SelectUnionQueryType | SelectQueryType | SelectQueryAliasType | SelectViewType | None" has no attribute "table"

Check failure on line 70 in posthog/hogql/database/schema/persons.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Item "None" of "BaseTableType | SelectUnionQueryType | SelectQueryType | SelectQueryAliasType | SelectViewType | None" has no attribute "table"
):
extractor = WhereClauseExtractor(context)
extractor.add_local_tables(join_or_table)
where = extractor.get_inner_where(node)
Expand Down
53 changes: 0 additions & 53 deletions posthog/hogql/database/schema/test/__snapshots__/test_persons.ambr
Original file line number Diff line number Diff line change
@@ -1,57 +1,4 @@
# serializer version: 1
# name: TestPersonOptimization.test_funnel_whatever
'''
SELECT persons.id AS id,
persons.created_at AS created_at,
source.event_count AS event_count
FROM
(SELECT actor_id AS actor_id,
count() AS event_count
FROM
(SELECT e__pdi.person_id AS actor_id,
toTimeZone(e.timestamp, 'UTC') AS timestamp,
e.uuid AS uuid
FROM events AS e
INNER JOIN
(SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id,
person_distinct_id2.distinct_id AS distinct_id
FROM person_distinct_id2
WHERE equals(person_distinct_id2.team_id, 2)
GROUP BY person_distinct_id2.distinct_id
HAVING ifNull(equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0), 0) SETTINGS optimize_aggregation_in_order=1) AS e__pdi ON equals(e.distinct_id, e__pdi.distinct_id)
WHERE and(equals(e.team_id, 2), greaterOrEquals(timestamp, toDateTime64('2024-01-01 00:00:00.000000', 6, 'UTC')), less(timestamp, toDateTime64('2024-01-02 00:00:00.000000', 6, 'UTC')), equals(e.event, '$pageview')))
GROUP BY actor_id) AS source
INNER JOIN
(SELECT argMax(toTimeZone(person.created_at, 'UTC'), person.version) AS created_at,
person.id AS id
FROM person
WHERE and(equals(person.team_id, 2), in(id,
(SELECT source.actor_id AS actor_id
FROM
(SELECT actor_id AS actor_id, count() AS event_count
FROM
(SELECT e__pdi.person_id AS actor_id, toTimeZone(e.timestamp, 'UTC') AS timestamp, e.uuid AS uuid
FROM events AS e
INNER JOIN
(SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id, person_distinct_id2.distinct_id AS distinct_id
FROM person_distinct_id2
WHERE equals(person_distinct_id2.team_id, 2)
GROUP BY person_distinct_id2.distinct_id
HAVING ifNull(equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0), 0) SETTINGS optimize_aggregation_in_order=1) AS e__pdi ON equals(e.distinct_id, e__pdi.distinct_id)
WHERE and(equals(e.team_id, 2), greaterOrEquals(timestamp, toDateTime64('2024-01-01 00:00:00.000000', 6, 'UTC')), less(timestamp, toDateTime64('2024-01-02 00:00:00.000000', 6, 'UTC')), equals(e.event, '$pageview')))
GROUP BY actor_id) AS source)))
GROUP BY person.id
HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))), 0)) SETTINGS optimize_aggregation_in_order=1) AS persons ON equals(persons.id, source.actor_id)
ORDER BY source.event_count DESC
LIMIT 100 SETTINGS readonly=2,
max_execution_time=60,
allow_experimental_object_type=1,
format_csv_allow_double_quotes=0,
max_ast_elements=4000000,
max_expanded_ast_elements=4000000,
max_bytes_before_external_group_by=0
'''
# ---
# name: TestPersonOptimization.test_joins_are_left_alone_for_now
'''
SELECT events.uuid AS uuid
Expand Down
10 changes: 5 additions & 5 deletions posthog/hogql/database/schema/test/test_persons.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def test_simple_filter(self):
modifiers=self.modifiers,
)
assert len(response.results) == 2
assert "where_optimization" in response.clickhouse
assert "in(tuple(person.id, person.version)" not in response.clickhouse
self.assertIn("where_optimization", response.clickhouse)

Check failure on line 86 in posthog/hogql/database/schema/test/test_persons.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Argument 2 to "assertIn" of "TestCase" has incompatible type "str | None"; expected "Iterable[Any] | Container[Any]"
self.assertNotIn("in(tuple(person.id, person.version)", response.clickhouse)

Check failure on line 87 in posthog/hogql/database/schema/test/test_persons.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Argument 2 to "assertNotIn" of "TestCase" has incompatible type "str | None"; expected "Iterable[Any] | Container[Any]"

@snapshot_clickhouse_queries
def test_joins_are_left_alone_for_now(self):
Expand All @@ -94,8 +94,8 @@ def test_joins_are_left_alone_for_now(self):
modifiers=self.modifiers,
)
assert len(response.results) == 2
assert "in(tuple(person.id, person.version)" in response.clickhouse
assert "where_optimization" not in response.clickhouse
self.assertIn("in(tuple(person.id, person.version)", response.clickhouse)

Check failure on line 97 in posthog/hogql/database/schema/test/test_persons.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Argument 2 to "assertIn" of "TestCase" has incompatible type "str | None"; expected "Iterable[Any] | Container[Any]"
self.assertNotIn("where_optimization", response.clickhouse)

Check failure on line 98 in posthog/hogql/database/schema/test/test_persons.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Argument 2 to "assertNotIn" of "TestCase" has incompatible type "str | None"; expected "Iterable[Any] | Container[Any]"

def test_person_modal_not_optimized_yet(self):
source_query = TrendsQuery(
Expand All @@ -122,4 +122,4 @@ def test_person_modal_not_optimized_yet(self):
)
query_runner = ActorsQueryRunner(query=actors_query, team=self.team)
response = execute_hogql_query(query_runner.to_query(), self.team, modifiers=self.modifiers)
assert "where_optimization" not in response.clickhouse
self.assertNotIn("where_optimization", response.clickhouse)

0 comments on commit 7b08365

Please sign in to comment.