Skip to content

Add ability to get previous TI on RuntimeTaskInstance - #59712

Merged
kaxil merged 3 commits into
apache:mainfrom
astronomer:get-prev-ti
Dec 23, 2025
Merged

kaxil merged 3 commits into
apache:mainfrom
astronomer:get-prev-ti

Conversation

@kaxil

@kaxil kaxil commented Dec 22, 2025 •

Copy link
Copy Markdown
Member

Fixes #59609

Add get_previous_ti() method to RuntimeTaskInstance for retrieving previous task instance executions. Supports filtering by state, logical_date, and run_id.

This will help users migrating from Airflow 2.x since they were able to do prev_ti = ti.get_previous_ti(state='success') in Airflow 2 as described in the GitHub issue.

image image

^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in airflow-core/newsfragments.

@boring-cyborg boring-cyborg Bot added area:API Airflow's REST/HTTP API area:task-sdk labels Dec 22, 2025
@kaxil
kaxil requested a review from BasPH December 22, 2025 13:15
@kaxil

kaxil commented Dec 22, 2025

Copy link
Copy Markdown
Member Author

@BasPH Do you need more things in previous TI -- like hostname or anything else that doesn't exists in this PR?

@kaxil
kaxil requested review from Lee-W and uranusjr December 22, 2025 13:18
@BasPH

BasPH commented Dec 22, 2025

Copy link
Copy Markdown
Contributor

@BasPH Do you need more things in previous TI -- like hostname or anything else that doesn't exists in this PR?

No, looks good!

@gopidesupavan gopidesupavan 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.

LGTM, should we add same thing to TriggerRunnerSupervisor?

Add `get_previous_ti()` method to `RuntimeTaskInstance` for retrieving
previous task instance executions. Supports filtering by state,
logical_date, and run_id.

Fixes apache#59609
- Match RuntimeTaskInstanceProtocol signature (AwareDatetime, TaskInstanceState)
- Use TaskInstanceState enum in tests
- Add GetPreviousTI/PreviousTIResult to triggerer and dag processor exclusion lists
@kaxil
kaxil merged commit a9f3e20 into apache:main Dec 23, 2025
1 check passed
@kaxil
kaxil deleted the get-prev-ti branch December 23, 2025 16:20
@github-actions

Copy link
Copy Markdown
Contributor

Backport failed to create: v3-1-test. View the failure log Run details

Status Branch Result
❌ v3-1-test Commit Link

You can attempt to backport this manually by running:

cherry_picker a9f3e20 v3-1-test

This should apply the commit to the v3-1-test branch and leave the commit in conflict state marking
the files that need manual conflict resolution.

After you have resolved the conflicts, you can continue the backport process by running:

cherry_picker --continue

@ephraimbuddy

Copy link
Copy Markdown
Contributor

@kaxil , this failed to backport and looks more like a feature than bug fix. Do we need it in the test branch?

@kaxil

kaxil commented Jan 6, 2026

Copy link
Copy Markdown
Member Author

It depends on how you see it. #59609 was raised as a regression over 2.x so hurts migration path from Airflow 2 to 3.

@kaxil , this failed to backport and looks more like a feature than bug fix. Do we need it in the test branch?

@BasPH since you created that issue, could you wait for 3.2 -- around March/April ?

@ephraimbuddy ephraimbuddy removed this from the Airflow 3.1.6 milestone Jan 7, 2026
jhgoebbert pushed a commit to jhgoebbert/airflow_Owen-CH-Leung that referenced this pull request Feb 8, 2026
Add `get_previous_ti()` method to `RuntimeTaskInstance` for retrieving
previous task instance executions. Supports filtering by state,
logical_date, and run_id.

Fixes apache#59609


- Match RuntimeTaskInstanceProtocol signature (AwareDatetime, TaskInstanceState)
- Use TaskInstanceState enum in tests
- Add GetPreviousTI/PreviousTIResult to triggerer and dag processor exclusion lists
@estellebertrand

Copy link
Copy Markdown

Hello and thank you for adding back this feature ! Do you confirm it is still on the 3.2 roadmap, and that its release is still planned around March/April ? Looking forward to it to finalize the migration.

@kaxil

kaxil commented Feb 25, 2026

Copy link
Copy Markdown
Member Author

Hello and thank you for adding back this feature ! Do you confirm it is still on the 3.2 roadmap, and that its release is still planned around March/April ? Looking forward to it to finalize the migration.

Hey, yes it will be part of Airflow 3.2 -- planned to be released end of May-early April

@kaxil kaxil added this to the Airflow 3.2.0 milestone Feb 25, 2026
pierrejeambrun added a commit to astronomer/airflow that referenced this pull request Sep 9, 2026
Two more callers of ``session.scalars(...).first()`` /
``session.execute(select(...)).first()`` were still missing an explicit
``.limit(1)``. Because SQLAlchemy 2.0's ``Result.first()`` does not add
one (unlike the legacy ``Query.first``), each call sends every matching
row to the Python process and discards all but the first. The favorite
existence check is bounded to a single row per (dag_id, user_id) by the
schema, but the previous-TI lookup scans every earlier matching TI.

Introduced independently of apache#52325's migration: get_previous_task_instance
came in apache#59712, unfavorite_dag's existence probe in apache#51264. Same
anti-pattern as the four sites apache#72554 fixes; the tests reuse the
``capture_orm_selects`` helper that PR introduces.
pierrejeambrun added a commit that referenced this pull request Sep 9, 2026
* Bound remaining single-row lookups with .limit(1)

Two more callers of ``session.scalars(...).first()`` /
``session.execute(select(...)).first()`` were still missing an explicit
``.limit(1)``. Because SQLAlchemy 2.0's ``Result.first()`` does not add
one (unlike the legacy ``Query.first``), each call sends every matching
row to the Python process and discards all but the first. The favorite
existence check is bounded to a single row per (dag_id, user_id) by the
schema, but the previous-TI lookup scans every earlier matching TI.

Introduced independently of #52325's migration: get_previous_task_instance
came in #59712, unfavorite_dag's existence probe in #51264. Same
anti-pattern as the four sites #72554 fixes; the tests reuse the
``capture_orm_selects`` helper that PR introduces.

* Also assert the returned row on the previous-TI regression test
vatsrahul1001 added a commit that referenced this pull request Sep 10, 2026
* Bound remaining single-row lookups with .limit(1)

Two more callers of ``session.scalars(...).first()`` /
``session.execute(select(...)).first()`` were still missing an explicit
``.limit(1)``. Because SQLAlchemy 2.0's ``Result.first()`` does not add
one (unlike the legacy ``Query.first``), each call sends every matching
row to the Python process and discards all but the first. The favorite
existence check is bounded to a single row per (dag_id, user_id) by the
schema, but the previous-TI lookup scans every earlier matching TI.

Introduced independently of #52325's migration: get_previous_task_instance
came in #59712, unfavorite_dag's existence probe in #51264. Same
anti-pattern as the four sites #72554 fixes; the tests reuse the
``capture_orm_selects`` helper that PR introduces.

* Also assert the returned row on the previous-TI regression test

(cherry picked from commit ff83957)

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.

Airflow 3 equivalent for get_previous_ti

6 participants