uranusjr commented on code in PR #37498:
URL: https://github.com/apache/airflow/pull/37498#discussion_r1547089982


##########
airflow/ti_deps/deps/mapped_task_upstream_dep.py:
##########
@@ -0,0 +1,106 @@
+#
+# 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.
+from __future__ import annotations
+
+from collections.abc import Iterator
+from typing import TYPE_CHECKING
+
+from sqlalchemy import and_, select
+
+from airflow.models.taskinstance import TaskInstance
+from airflow.ti_deps.deps.base_ti_dep import BaseTIDep
+from airflow.utils.state import State, TaskInstanceState
+
+if TYPE_CHECKING:
+    from sqlalchemy.orm import Session
+
+    from airflow.ti_deps.dep_context import DepContext
+    from airflow.ti_deps.deps.base_ti_dep import TIDepStatus
+
+
+class MappedTaskUpstreamDep(BaseTIDep):
+    """
+    Determines if the task, if mapped, is allowed to run based on its mapped 
dependencies.
+
+    In particular, check if upstream tasks that provide XComs used by this 
task for task mapping are in
+    states that allow the task instance to run.
+    """
+
+    NAME = "Mapped dependencies have succeeded"
+    IGNORABLE = True
+    IS_TASK_DEP = True
+
+    def _get_dep_statuses(
+        self,
+        ti: TaskInstance,
+        session: Session,
+        dep_context: DepContext,
+    ) -> Iterator[TIDepStatus]:
+        from airflow.models.mappedoperator import MappedOperator
+
+        if isinstance(ti.task, MappedOperator):
+            mapped_dependencies = ti.task.iter_mapped_dependencies()
+        elif ti.task is not None and (task_group := 
ti.task.get_closest_mapped_task_group()) is not None:
+            mapped_dependencies = task_group.iter_mapped_dependencies()
+        else:
+            return
+
+        # Get the tis of all mapped dependencies. In case a mapped dependency 
is itself mapped, we are
+        # only interested in it if it hasn't been expanded yet, i.e., we 
filter by map_index=-1. This is
+        # because if it has been expanded, it did not fail and was not skipped 
outright which is all we need
+        # to know for the purposes of this check.
+        mapped_dependency_tis = (
+            session.scalars(
+                select(TaskInstance).where(
+                    and_(
+                        TaskInstance.task_id.in_([operator.task_id for 
operator in mapped_dependencies]),
+                        TaskInstance.dag_id == ti.dag_id,
+                        TaskInstance.run_id == ti.run_id,
+                        TaskInstance.map_index == -1,
+                    )

Review Comment:
   ```suggestion
                       TaskInstance.task_id.in_(operator.task_id for operator 
in mapped_dependencies),
                       TaskInstance.dag_id == ti.dag_id,
                       TaskInstance.run_id == ti.run_id,
                       TaskInstance.map_index == -1,
   ```



-- 
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: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to