Skip to content

Filtering items from a mapped task is broken #49737

Closed

Description

@GeryDeKocliko

Apache Airflow version

3.0.0

If "Other Airflow 2 version" selected, which one?

No response

What happened?

Hi,

As outlined in the documentation, I’m attempting to filter out items in my dynamic task mapping by returning None within the filter task.

See : https://airflow.apache.org/docs/apache-airflow/stable/authoring-and-scheduling/dynamic-task-mapping.html#filtering-items-from-a-mapped-task

However, I’m observing that None values are still being passed to the subsequent expand, which is not the expected behavior.

What you think should happen instead?

The None value must not be passed to the next expand.

How to reproduce

from airflow.decorators import dag, task

@task
def generate_data():
    return [1, None, 3]


@task
def filter_item_from_mapped_task(item):
    if item is None:
        return None
    return item


@task
def print_item(item):
    assert item is not None
    print(item)


@dag(
    dag_id='test_bug_dag',
)
def test_bug_dag():
    data = generate_data()
    filtered_data = filter_item_from_mapped_task.expand(item=data)
    print_item.expand(item=filtered_data)

test_bug_dag()

Operating System

MacOS 14.6 (23G80)

Versions of Apache Airflow Providers

I haven’t added any additional providers

Deployment

Docker-Compose

Deployment details

No response

Anything else?

No response

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Activity

  1. added
    kind:bug
    needs-triage
    on Apr 24, 2025
  2. boring-cyborg commented on Apr 24, 2025

    @boring-cyborg

    Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.

  3. added
    priority:medium
    on Apr 24, 2025
  4. tirkarthi commented on Apr 24, 2025

    @tirkarthi
    Contributor

    Interesting, this works for [None] and [1, None] to run only one task but when None value is in the middle this error is present. I guess the map_index of 0 and 2 have xcom pushed but the underlying task seems 2 upstream tasks and tries to get map_index for 0 and 1 which causes 1 to have no XCom. I am checking on this.

  5. tirkarthi commented on Apr 24, 2025

    @tirkarthi
    Contributor

    When the xcom values map 1:1 in length then the map_index of the downstream task can directly get the xcom value from upstream value using map_index of the downstream task. But when there is a difference like upstream has 2 mapped task where None and 1 is returned for map_index=0 and map_index=1 respectively then the downstream task only has to process value of upstream task at map_index=1 but the downstream task starts with map_index=0 and goes on to again fetch xcom value for map_index=0 which is None. I guess some change has to be made at below code that gets the index to search in LazyXComSequence. But I guess this is a high priority issue for the next release since filter doesn't seem to be working on basic use cases.

    cc: @ashb for help since the dynamic task mapping was implemented by Ash and will have better idea.

    def _expand_mapped_field(self, key: str, value: Any, map_index: int, all_lengths: dict[str, int]) -> Any:
    def _find_index_for_this_field(index: int) -> int:
    # Need to use the original user input to retain argument order.
    for mapped_key in reversed(self.value):
    mapped_length = all_lengths[mapped_key]
    if mapped_length < 1:
    raise RuntimeError(f"cannot expand field mapped to length {mapped_length!r}")
    if mapped_key == key:
    return index % mapped_length
    index //= mapped_length
    return -1
    found_index = _find_index_for_this_field(map_index)
    if found_index < 0:
    return value
    if isinstance(value, Sequence):
    return value[found_index]

  6. added
    affected_version:main_branch
    affected_version:3.0
    priority:high
    and removed
    priority:medium
    needs-triage
    on Apr 24, 2025
  7. added this to the Airflow 3.0.1 milestone on Apr 24, 2025
  8. tirkarthi commented on Apr 25, 2025

    @tirkarthi
    Contributor

    I just tried it in Airflow 2 and 3 to see the difference. The index calculation is same in both Airflow 2 and 3 but the usage varies as below

    In Airflow 2, Accessing LazySequence[0] goes on to get all the upstream task id xcom values and add offset for the calculated index and limit the result to 1

    def __getitem__(self, key: int | slice) -> T | Sequence[T]:
    if isinstance(key, int):
    if key >= 0:
    stmt = self._select_asc.offset(key)
    else:
    stmt = self._select_desc.offset(-1 - key)
    if (row := self._session.execute(stmt.limit(1)).one_or_none()) is None:
    raise IndexError(key)
    return self._process_row(row)

    In Airflow 3, Accessing LazyXComSequence[0] goes on to get the upstream task id xcom values and uses the calculated index as map_index to the upstream which is different compared to Airflow 2

    def _get_item(self, index: int) -> T:
    # TODO: maybe we need to call SUPERVISOR_COMMS manually so we can handle not found here?
    return self._ti.xcom_pull(
    task_ids=self._xcom_arg.operator.task_id,
    key=self._xcom_arg.key,
    map_indexes=index,
    )

  9. tirkarthi commented on Apr 25, 2025

    @tirkarthi
    Contributor

    One approach I can think of would be to introduce offset and pass it from lazy xcom index access so that the similar operation is performed in Airflow 3 like Airflow 2 to use offset when present to get all xcom and then skip by offset to return the appropriate value.

    Ref change : tirkarthi@a6d82d0

  10. uranusjr commented on Apr 30, 2025

    @uranusjr
    Member

    I think your change is on the right track @tirkarthi. I would change the call in _get_item to call the comm directly (as mentioned in the comment) with a new request type (say GetXComSequenceItem) so we skip all the XCom interface changes. This also allows us to handle 404 better since we should raise IndexError here instead of silently defaulting to None like normal XCom requests. I’ll raise a PR for this since we want this in 3.0.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

affected_version:3.0affected_version:main_brancharea:corearea:dynamic-task-mappingkind:bugpriority:high

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions