Skip to content

Fix SQL templated field rendering for dynamically mapped tasks in AF2#2119

Merged
tatiana merged 3 commits into
mainfrom
fix-2018
Nov 12, 2025
Merged

Fix SQL templated field rendering for dynamically mapped tasks in AF2#2119
tatiana merged 3 commits into
mainfrom
fix-2018

Conversation

@tatiana
Copy link
Copy Markdown
Collaborator

@tatiana tatiana commented Nov 12, 2025

Fixes SQL not rendering in Airflow UI when using DbtRunLocalOperator.partial in Airflow 2.

The problem was that when using dynamically mapped tasks in Airflow 2, the _override_rtif method was not filtering by map_index when deleting old RenderedTaskInstanceFields records. This meant it could delete records for other mapped task instances, not just the current one.

I added a unittest to confirm the implementation takes into account the mapped tasks index, and I also validated by running airflow standalone with the example DAG shared by @hkc-8010 in the original bug report.

The screenshots below show the SQL being rendered in all the tasks of the Dynamic Task Group, instead of only rendering for the last task:

Screenshot 2025-11-12 at 16 32 55 Screenshot 2025-11-12 at 16 34 33 Screenshot 2025-11-12 at 16 34 23

Closes: #2018

Copilot AI review requested due to automatic review settings November 12, 2025 16:30
@netlify
Copy link
Copy Markdown

netlify Bot commented Nov 12, 2025

Deploy Preview for sunny-pastelito-5ecb04 canceled.

Name Link
🔨 Latest commit f4620d1
🔍 Latest deploy log https://app.netlify.com/projects/sunny-pastelito-5ecb04/deploys/6914b6e4522e350008918b77

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an issue where SQL templated fields were not properly rendered in the Airflow UI for dynamically mapped tasks when using DbtRunLocalOperator.partial in Airflow 2. The root cause was that the _override_rtif method was deleting RenderedTaskInstanceFields records for all mapped task instances instead of filtering by the specific map_index.

Key Changes:

  • Added map_index filter to the delete query in _override_rtif_airflow_2_x method
  • Added comprehensive test coverage to verify the fix works correctly for dynamically mapped tasks

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
cosmos/operators/local.py Added map_index filter to prevent deletion of other mapped task instances' records
tests/operators/test_local.py Added test to verify map_index filtering in the _override_rtif method for Airflow 2

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/operators/test_local.py Outdated
Copy link
Copy Markdown
Contributor

@pankajkoti pankajkoti left a comment

Choose a reason for hiding this comment

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

Nice, great fix. Thanks!

@tatiana tatiana merged commit 4994474 into main Nov 12, 2025
82 checks passed
@tatiana tatiana deleted the fix-2018 branch November 12, 2025 16:49
tatiana added a commit that referenced this pull request Nov 12, 2025
…#2119)

Fixes SQL not rendering in Airflow UI when using
`DbtRunLocalOperator.partial` in Airflow 2.

The problem was that when using dynamically mapped tasks in Airflow 2,
the `_override_rtif method` was not filtering by map_index when deleting
old RenderedTaskInstanceFields records. This meant it could delete
records for other mapped task instances, not just the current one.

I added a unittest to confirm the implementation takes into account the
mapped tasks index, and I also validated by running `airflow standalone`
with the example DAG shared by @hkc-8010 in the original bug report.

The screenshots below show the SQL being rendered in all the tasks of
the Dynamic Task Group, instead of only rendering for the last task:

<img width="4064" height="2334" alt="Screenshot 2025-11-12 at 16 32 55"
src="https://github.com/user-attachments/assets/75ce3a91-a6b5-4c2e-b37e-6369db7d15bc"
/>
<img width="4064" height="2334" alt="Screenshot 2025-11-12 at 16 34 33"
src="https://github.com/user-attachments/assets/04b8e17a-c9f7-45e4-b8ac-d3a5e6ddb9a1"
/>
<img width="4064" height="2334" alt="Screenshot 2025-11-12 at 16 34 23"
src="https://github.com/user-attachments/assets/21116ec7-d211-4afc-b0c3-8fc9ed69c28a"
/>

Closes: #2018
(cherry picked from commit 4994474)
@tatiana tatiana mentioned this pull request Nov 12, 2025
tatiana added a commit that referenced this pull request Nov 12, 2025
Bug fixes

* Fix ``ExecutionMode.WATCHER`` deadlock in Airflow 3.0 & 3.1 by
@tatiana in #2087
* Fix ``ExecutionMode.AIRFLOW_ASYNC`` ``TaskGroup`` XCom issue by
@tatiana in #2088
* Guard watcher callback exceptions to avoid hanging producer tasks by
@pankajkoti in #2101
* Fix SQL templated field rendering for dynamically mapped tasks in
Airflow 2 by @tatiana in #2119

Enhancements

* Remove usage of contextmanager in plugins for accessing connections in
Airflow >= 3.1.2 by @pankajkoti in #2073

Docs

* Improve ``ExecutionMode.AIRFLOW_ASYNC`` docs by @tatiana in #2103
* Add note about experimenting threads count for the Watcher Execution
mode by @pankajkoti in #2083
* Fix minor documentation formatting issue by @dnskrv in #2098
* Correct example YAML key from ``operator_args`` to ``operator_kwargs``
by @jx2lee in #2091

Others

* Fix broken CI due to fastapi incompatibility with cadwyn for Airflow 3
by @pankajkoti in #2076
* pre-commit autoupdate in #2078, #2104

related:
astronomer/oss-integrations-private#272
@tatiana tatiana mentioned this pull request Nov 13, 2025
tatiana added a commit that referenced this pull request Nov 13, 2025
Bug fixes

* Fix ``ExecutionMode.WATCHER`` deadlock in Airflow 3.0 & 3.1 by
@tatiana in #2087
* Fix ``ExecutionMode.AIRFLOW_ASYNC`` ``TaskGroup`` XCom issue by
@tatiana in #2088
* Guard watcher callback exceptions to avoid hanging producer tasks by
@pankajkoti in #2101
* Fix SQL templated field rendering for dynamically mapped tasks in
Airflow 2 by @tatiana in #2119

Enhancements

* Remove usage of contextmanager in plugins for accessing connections in
Airflow >= 3.1.2 by @pankajkoti in #2073

Docs

* Improve ``ExecutionMode.AIRFLOW_ASYNC`` docs by @tatiana in #2103
* Add note about experimenting threads count for the Watcher Execution
mode by @pankajkoti in #2083
* Fix minor documentation formatting issue by @dnskrv in #2098
* Correct example YAML key from ``operator_args`` to ``operator_kwargs``
by @jx2lee in #2091

Others

* Fix broken CI due to fastapi incompatibility with cadwyn for Airflow 3
by @pankajkoti in #2076
* pre-commit autoupdate in #2078, #2104

Closes: astronomer/oss-integrations-private#272
@tatiana tatiana added this to the Cosmos 1.11.1 milestone Nov 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: SQL not rendering in Airflow UI when using DbtRunLocalOperator.partial

3 participants