uranusjr commented on code in PR #76826:
URL: https://github.com/apache/airflow/pull/76826#discussion_r4585479634
##########
airflow-core/src/airflow/api_fastapi/execution_api/routes/xcoms.py:
##########
@@ -553,6 +553,36 @@ def head_xcom(
response.headers["Content-Range"] = f"map_indexes {count}"
+def _resolve_requested_map_index(
+ *,
+ dag_id: str,
+ run_id: str,
+ task_id: str,
+ params: GetXcomFilterParams,
+ session: Session,
+ dag_bag: DBDagBag,
+) -> int | None:
+ """
+ Return the map index to read, or None for every instance of a mapped task.
+
+ An omitted map index is resolved by the producing task's definition in the
+ Dag version its run uses: a mapped operator, or a task in a mapped task
+ group, is mapped however many instances it expanded to. A task that cannot
+ be found reads -1, so the read reports the XCom as not found.
+ """
+ if params.map_index is not None:
+ return params.map_index
+ if params.offset is not None:
+ return -1 # None reads a mapped task's list.
+ try:
+ task = TaskCoordinateResolver(dag_bag, session).get_task(dag_id,
run_id, task_id)
+ except AmbiguousProducerError as error:
+ raise HTTPException(status.HTTP_409_CONFLICT, str(error)) from error
+ except (TaskNotFound, ValueError):
+ return -1
+ return None if task.get_needs_expansion() else -1
Review Comment:
This is probably the only one place that defaulting to the current map index
is useful. But ultimately xcom_pull can’t be smart enough to know what the
author actually wants, and I would prefer being consistent rather than
artificial. So getting `[1, 2]` would be reasonable.
Note that using XComarg already works as expected, and is far more natural
in this case
```python
@task_group
def group(value):
@task
def a(value):
return value
@task
def b(value):
return value # First ti gets 1, second ti gets 2.
b(a(value))
group.expand(value=[1, 2])
```
--
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]