Skip to content

: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.

fix(hogql): order override join before dependent lazy joins on same table (#69644)

posthog[bot]7101c4a5600c
diff --git a/posthog/hogql/transforms/lazy_tables.py b/posthog/hogql/transforms/lazy_tables.py
index e0d7edab4293..ccbae2c5b0dd 100644
--- a/posthog/hogql/transforms/lazy_tables.py
+++ b/posthog/hogql/transforms/lazy_tables.py
@@ -673,14 +673,21 @@ class LazyTableResolver(TraversingVisitor):
if join_scope.from_table == join_ptr.alias or (
isinstance(join_ptr.table, ast.Field) and join_scope.from_table == join_ptr.table.chain[0]
):
- # If the `join_to_add` is reliant on the existing `next_join`, then just append after instead of before
- if join_ptr.next_join and join_ptr.next_join.alias in constraint_tables:
- if join_ptr.next_join.next_join:
- join_to_add.next_join = join_ptr.next_join.next_join
- join_ptr.next_join.next_join = join_to_add
- else:
- join_to_add.next_join = join_ptr.next_join
- join_ptr.next_join = join_to_add
+ # The join's ON constraint may depend on other joins (`constraint_tables`); the
+ # ones that matter here are those earlier in this source table's join chain,
+ # which ClickHouse resolves left-to-right and so must appear before this join.
+ # Scan the whole remaining chain — not just the immediate next join — and insert
+ # after the last such dependency, or right after the source table when none are
+ # present. Looking only one join ahead placed this join before a dependency that
+ # a non-dependent join had been inserted in front of.
+ insert_after = join_ptr
+ scan = join_ptr.next_join
+ while scan is not None:
+ if scan.alias in constraint_tables:
+ insert_after = scan
+ scan = scan.next_join
+ join_to_add.next_join = insert_after.next_join
+ insert_after.next_join = join_to_add
added = True
break
if join_ptr.next_join:
diff --git a/posthog/hogql/transforms/test/test_lazy_tables.py b/posthog/hogql/transforms/test/test_lazy_tables.py
index 8851df122019..b4fcd77d5563 100644
--- a/posthog/hogql/transforms/test/test_lazy_tables.py
+++ b/posthog/hogql/transforms/test/test_lazy_tables.py
@@ -171,6 +171,44 @@ class TestLazyJoins(BaseTest):
)
assert printed == self.snapshot
+ def test_person_override_join_ordered_before_dependent_warehouse_joins(self):
+ # A data-warehouse join keyed on `person_id` produces an ON constraint that references the
+ # person-overrides join (`e__override`). When another warehouse join that does *not* depend
+ # on the override is emitted between the events table and the override join, the override
+ # join must still come before the dependent one: ClickHouse resolves joins left-to-right, so
+ # a forward reference to `e__override` throws "Unknown identifier". Regression test for an
+ # aliased events actors query that produced exactly that broken ordering.
+ DataWarehouseJoin(
+ team=self.team,
+ source_table_name="events",
+ source_table_key="event",
+ joining_table_name="persons",
+ joining_table_key="id",
+ field_name="event_link",
+ ).save()
+ DataWarehouseJoin(
+ team=self.team,
+ source_table_name="events",
+ source_table_key="person_id",
+ joining_table_name="persons",
+ joining_table_key="id",
+ field_name="person_link",
+ ).save()
+
+ printed = self._print_select(
+ "SELECT e.person_id AS actor_id, e.event_link.id AS a, e.person_link.id AS b FROM events AS e",
+ HogQLQueryModifiers(personsOnEventsMode=PersonsOnEventsMode.PERSON_ID_OVERRIDE_PROPERTIES_ON_EVENTS),
+ )
+
+ flat = " ".join(printed.split())
+ override_pos = flat.find("AS e__override ON")
+ dependent_pos = flat.find("AS e__person_link ON")
+ assert override_pos != -1, "person-overrides join was not emitted"
+ assert dependent_pos != -1, "person_id-keyed warehouse join was not emitted"
+ # Guard against a vacuous assertion: the dependent join's ON must actually reference the override.
+ assert "e__override." in flat[dependent_pos : dependent_pos + 300]
+ assert override_pos < dependent_pos, "override join must be emitted before the join that references it"
+
@pytest.mark.usefixtures("unittest_snapshot")
def test_events_session_join_with_timestamp_filter(self):
# Documents SQL generation for events with session access and a timestamp filter.