Skip to content

Reuse the explicit dag_run join on the previous-TI lookup - #72811

Merged
vatsrahul1001 merged 4 commits into
apache:mainfrom
astronomer:use-contains-eager-on-previous-ti-lookup
Sep 11, 2026
Merged

vatsrahul1001 merged 4 commits into
apache:mainfrom
astronomer:use-contains-eager-on-previous-ti-lookup

Conversation

@pierrejeambrun

Copy link
Copy Markdown
Member

Follow-up to #72699. That PR bounded the get_previous_task_instance lookup
with .limit(1); @kaxil noted in review that the same query also asks for an
eager-load that duplicates work the mapper already does.

TaskInstance.dag_run is lazy="joined" (airflow-core/src/airflow/models/taskinstance.py:727),
so the explicit joinedload(TI.dag_run) attaches a parallel JOIN dag_run AS dag_run_1
next to the explicit JOIN dag_run the query already needs for the
ORDER BY dag_run.logical_date. Both joins select the same rows over the same
(dag_id, run_id) FK pair, so every request paid for one extra join.

Switch to contains_eager(TI.dag_run) so the mapper's eager-load reuses the
explicit join. Compiled SQL drops from JOIN dag_run ... JOIN dag_run AS dag_run_1 ...
to a single JOIN dag_run; the regression test in this PR fails on the old
shape and passes on the new one.


Was generative AI tooling used to co-author this PR?
  • Yes — Claude Code (Opus 4.7)

Generated-by: Claude Code (Opus 4.7) following the guidelines

@boring-cyborg boring-cyborg Bot added area:API Airflow's REST/HTTP API area:task-sdk labels Sep 9, 2026
@pierrejeambrun
pierrejeambrun force-pushed the use-contains-eager-on-previous-ti-lookup branch from f38b16a to 1db0c22 Compare September 9, 2026 14:14
The previous-TI query in ``get_previous_task_instance`` explicitly joins
``dag_run`` for the ``ORDER BY dag_run.logical_date`` and asked for a second
``joinedload(TI.dag_run)`` for hydration. ``TaskInstance.dag_run`` is already
``lazy="joined"`` on the mapper, so the eager-load actually attached a parallel
``JOIN dag_run AS dag_run_1`` next to the explicit join. Both joins pull the
same rows over the same FK pair.

Switch to ``contains_eager(TI.dag_run)`` so the eager-load reuses the explicit
join instead of adding a second one, and add a regression that fails when the
compiled SQL references ``dag_run`` more than once in the FROM clause.
@pierrejeambrun
pierrejeambrun force-pushed the use-contains-eager-on-previous-ti-lookup branch from 1db0c22 to c70e5aa Compare September 10, 2026 15:55

@kaxil kaxil left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed at c37eb95. All three comments from the last round are addressed, and I checked each in the code rather than going by the resolved threads: contains_eager(TI.dag_run).load_only(DR.logical_date) is in at task_instances.py:1216, the join-count assert is folded into test_get_previous_ti_query_is_bounded with no duplicate test left behind, and the response now asserts logical_date. Approving.

Two notes, neither of them gating.

The title names the half that does nothing on its own. TaskInstance.dag_run is declared lazy="joined", innerjoin=True (taskinstance.py#L727), so the mapper contributes its own aliased JOIN dag_run whether or not joinedload(TI.dag_run) is spelled out. Removing that joinedload in isolation leaves both the explicit .join(DR, ...) and the mapper's join in place. What collapses them to one is contains_eager, telling the mapper the explicit join already populates the relationship. The commit message ("Reuse the explicit dag_run join on the previous-TI lookup") describes it correctly, and that is the line that survives a squash merge.

On the new docstring line, I was wrong last round: I said asserting logical_date would cover the load_only half, and it does not, so "eager-loaded" claims more than the test checks. capture_orm_selects keeps only statements matching \bFROM task_instance\b (asserts.py#L202). If logical_date were deferred rather than eager-loaded, it would arrive through a separate SELECT dag_run.logical_date ... FROM dag_run WHERE dag_run.id = ?, which that filter drops, so the loop never inspects it and data["logical_date"] still comes back correct. The assert proves the value is right, not that it came from the join. A query count around the request would pin the eager-load claim; short of that, the docstring is the thing to soften.

@pierrejeambrun pierrejeambrun changed the title Drop the redundant joinedload on the previous-TI lookup Reuse the explicit dag_run join on the previous-TI lookup Sep 10, 2026
@pierrejeambrun

pierrejeambrun commented Sep 10, 2026 •

Copy link
Copy Markdown
Member Author

On the new docstring line, I was wrong last round: I said asserting logical_date would cover the load_only half, and it does not, so "eager-loaded" claims more than the test checks. capture_orm_selects keeps only statements matching \bFROM task_instance\b (asserts.py#L202). If logical_date were deferred rather than eager-loaded, it would arrive through a separate SELECT dag_run.logical_date ... FROM dag_run WHERE dag_run.id = ?, which that filter drops, so the loop never inspects it and data["logical_date"] still comes back correct. The assert proves the value is right, not that it came from the join. A query count around the request would pin the eager-load claim; short of that, the docstring is the thing to soften.

Updated.

@pierrejeambrun pierrejeambrun added this to the Airflow 3.3.2 milestone Sep 11, 2026
@vatsrahul1001
vatsrahul1001 merged commit d076a57 into apache:main Sep 11, 2026
155 checks passed
@vatsrahul1001
vatsrahul1001 deleted the use-contains-eager-on-previous-ti-lookup branch September 11, 2026 10:56
@github-actions

Copy link
Copy Markdown
Contributor

Backport successfully created: v3-3-test

Note: As of Merging PRs targeted for Airflow 3.X
the committer who merges the PR is responsible for backporting the PRs that are bug fixes (generally speaking) to the maintenance branches.

In matter of doubt please ask in #release-management Slack channel.

Status Branch Result
✅ v3-3-test PR Link

vatsrahul1001 pushed a commit that referenced this pull request Sep 11, 2026
…#72811) (#72944)

* Reuse the explicit dag_run join on the previous-TI lookup

The previous-TI query in ``get_previous_task_instance`` explicitly joins
``dag_run`` for the ``ORDER BY dag_run.logical_date`` and asked for a second
``joinedload(TI.dag_run)`` for hydration. ``TaskInstance.dag_run`` is already
``lazy="joined"`` on the mapper, so the eager-load actually attached a parallel
``JOIN dag_run AS dag_run_1`` next to the explicit join. Both joins pull the
same rows over the same FK pair.

Switch to ``contains_eager(TI.dag_run)`` so the eager-load reuses the explicit
join instead of adding a second one, and add a regression that fails when the
compiled SQL references ``dag_run`` more than once in the FROM clause.

* Drop unused load_only import

* Assert the eager-load claim by watching for stray dag_run SELECTs

* Swap the stray-select capture for a request-level query count
(cherry picked from commit d076a57)

Co-authored-by: Pierre Jeambrun <[email protected]>
github-actions Bot pushed a commit to aws-mwaa/upstream-to-airflow that referenced this pull request Sep 11, 2026
…apache#72811)

* Reuse the explicit dag_run join on the previous-TI lookup

The previous-TI query in ``get_previous_task_instance`` explicitly joins
``dag_run`` for the ``ORDER BY dag_run.logical_date`` and asked for a second
``joinedload(TI.dag_run)`` for hydration. ``TaskInstance.dag_run`` is already
``lazy="joined"`` on the mapper, so the eager-load actually attached a parallel
``JOIN dag_run AS dag_run_1`` next to the explicit join. Both joins pull the
same rows over the same FK pair.

Switch to ``contains_eager(TI.dag_run)`` so the eager-load reuses the explicit
join instead of adding a second one, and add a regression that fails when the
compiled SQL references ``dag_run`` more than once in the FROM clause.

* Drop unused load_only import

* Assert the eager-load claim by watching for stray dag_run SELECTs

* Swap the stray-select capture for a request-level query count
(cherry picked from commit d076a57)

Co-authored-by: Pierre Jeambrun <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:API Airflow's REST/HTTP API area:task-sdk

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants