Repository navigation
Reuse the explicit dag_run join on the previous-TI lookup - #72811
vatsrahul1001 merged 4 commits into
Conversation
f38b16a to
1db0c22
Compare
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.
1db0c22 to
c70e5aa
Compare
kaxil
left a comment
There was a problem hiding this comment.
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.
Updated. |
Backport successfully created: v3-3-testNote: As of Merging PRs targeted for Airflow 3.X In matter of doubt please ask in #release-management Slack channel.
|
…#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]>
…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]>
Follow-up to #72699. That PR bounded the
get_previous_task_instancelookupwith
.limit(1); @kaxil noted in review that the same query also asks for aneager-load that duplicates work the mapper already does.
TaskInstance.dag_runislazy="joined"(airflow-core/src/airflow/models/taskinstance.py:727),so the explicit
joinedload(TI.dag_run)attaches a parallelJOIN dag_run AS dag_run_1next to the explicit
JOIN dag_runthe query already needs for theORDER 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 theexplicit 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 oldshape and passes on the new one.
Was generative AI tooling used to co-author this PR?
Generated-by: Claude Code (Opus 4.7) following the guidelines