amoghrajesh opened a new pull request, #47141:
URL: https://github.com/apache/airflow/pull/47141
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
Thank you for contributing! Please make sure that your code changes
are covered with tests. And in case of new features or big changes
remember to adjust the documentation.
Feel free to ping committers for the review!
In case of an existing issue, reference it using one of the following:
closes: #ISSUE
related: #ISSUE
How to write a good git commit message:
http://chris.beams.io/posts/git-commit/
-->
closes: https://github.com/apache/airflow/issues/46580
This PR fixes a bug that doesn't allow dynamic task mapping to be used with
XCOM values returned from another task.
Two issues:
- The issue was that we were not calling the "SetXcom" utility with
mapped_length argument at all.
- Second issue was that on unmapping, we were not calling with the right
map_index due to which it was falling back to the "current task" map_index
leading to wrong behaviour and crashes.
## Testing
### 1. DAG mentioned in the issue:
```
from airflow import DAG
from airflow.decorators import task
from datetime import datetime
@task
def a_list():
return [3, 6, 9]
@task
def ref_context(num, **context):
return num + context["ti"].map_index * 5
@task
def assert_sum(nums, expect):
print(nums)
print(f"expecting sum: {' + '.join(map(str,nums))} == {expect}")
print(sum(nums))
assert sum(nums) == expect
with DAG(
dag_id="context_ref",
start_date=datetime(1970, 1, 1),
schedule=None,
tags=["taskmap"]
) as dag:
assert_sum(ref_context.expand(num=a_list()), 33)
```
Success:
<img width="1719" alt="image"
src="https://github.com/user-attachments/assets/2c11430a-fbd7-4b37-a504-a2b495570a7a"
/>
Task 1 xcom:
<img width="1719" alt="image"
src="https://github.com/user-attachments/assets/3b3f31df-cbcb-4098-81d1-41ff0723ead6"
/>
Task 2 mapped xcoms:
map_index = 0
<img width="1719" alt="image"
src="https://github.com/user-attachments/assets/00ba0750-4d90-4459-ae1f-e909e1eb40dd"
/>
map_index = 1
<img width="1719" alt="image"
src="https://github.com/user-attachments/assets/2d53a26c-3d52-4c30-8ad9-51b5bc00361a"
/>
map_index = 2
<img width="1719" alt="image"
src="https://github.com/user-attachments/assets/e3d17e23-2b7b-49cd-83d9-4adc125da235"
/>
Assert task
```
{"timestamp":"2025-02-27T10:15:54.946777Z","level":"info","event":"expecting
sum: 3 + 11 + 19 == 33","chan":"stdout","logger":"task"}
```
<img width="1719" alt="image"
src="https://github.com/user-attachments/assets/bf864173-864e-4fc3-bfbe-2df4f5b135af"
/>
### DAG2: example_dynamic_task_mapping
```
from datetime import datetime
from airflow.decorators import task
from airflow.models.dag import DAG
with DAG(dag_id="example_dynamic_task_mapping", schedule=None,
start_date=datetime(2022, 3, 4)) as dag:
@task
def add_one(x: int):
return x + 1
@task
def sum_it(values):
total = sum(values)
print(f"Total was {total}")
added_values = add_one.expand(x=[1, 2, 3])
sum_it(added_values)
```
Success:
<img width="1719" alt="image"
src="https://github.com/user-attachments/assets/6b2a1d32-c726-4fb5-877e-532cf2709c20"
/>
### DAG3: cross product
DAG:
```
from datetime import datetime, timedelta
from airflow import DAG
from airflow.decorators import task
from airflow.models.taskinstance import TaskInstance
delays = [30, 60, 90]
l = [5, 10]
@task
def get_delays():
return delays
@task
def get_delays2():
return l
@task
def get_wakes(delay, delay2, **context):
"Wake {delay} seconds after the task starts"
ti: TaskInstance = context["ti"]
return [delay, delay2]
with DAG(
dag_id="mapping_with_xcom",
start_date=datetime(1970, 1, 1),
schedule=None,
tags=["taskmap"]
) as dag:
wake_times = get_wakes.expand(delay=get_delays(), delay2=get_delays2())
```
Task1:
<img width="1719" alt="image"
src="https://github.com/user-attachments/assets/330137cc-b5e2-4d06-a6c4-1e073aa79061"
/>
Task2:
<img width="1719" alt="image"
src="https://github.com/user-attachments/assets/dd61594a-92cd-4d2d-9338-949103080845"
/>
map_index = 0 for Task3:
<img width="1719" alt="image"
src="https://github.com/user-attachments/assets/a44610ab-1bf7-44ed-99d5-d943242d935b"
/>
Cross product value from the table:
```
5,get_wakes,2,return_value,mapping_with_xcom,manual__2025-02-27T10:08:45.638526+00:00_9SZchT5K,"""[60,
5]"""
5,get_wakes,1,return_value,mapping_with_xcom,manual__2025-02-27T10:08:45.638526+00:00_9SZchT5K,"""[30,
10]"""
5,get_wakes,3,return_value,mapping_with_xcom,manual__2025-02-27T10:08:45.638526+00:00_9SZchT5K,"""[60,
10]"""
5,get_wakes,5,return_value,mapping_with_xcom,manual__2025-02-27T10:08:45.638526+00:00_9SZchT5K,"""[90,
10]"""
5,get_wakes,0,return_value,mapping_with_xcom,manual__2025-02-27T10:08:45.638526+00:00_9SZchT5K,"""[30,
5]"""
5,get_wakes,4,return_value,mapping_with_xcom,manual__2025-02-27T10:08:45.638526+00:00_9SZchT5K,"""[90,
5]"""
```
<!-- Please keep an empty line above the dashes. -->
---
**^ Add meaningful description above**
Read the **[Pull Request
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
for more information.
In case of fundamental code changes, an Airflow Improvement Proposal
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
is needed.
In case of a new dependency, check compliance with the [ASF 3rd Party
License Policy](https://www.apache.org/legal/resolved.html#category-x).
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
[newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]