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


##########
airflow-core/src/airflow/serialization/definitions/dag.py:
##########
@@ -1104,6 +1124,106 @@ def apply_state_filter(query):
         else:
             tis_full = apply_state_filter(tis_full)
 
+        if include_dependent_dags:
+            # Recursively find external tasks indicated by ExternalTaskMarker
+            from airflow.providers.standard.sensors.external_task import 
ExternalTaskMarker
+
+            # Build a full-object query for identifying ExternalTaskMarker TIs 
in the current set
+            if as_pk_tuple:
+                all_ti_rows = session.execute(tis_pk).all()
+                condition = TaskInstance.filter_for_tis(
+                    TaskInstanceKey(**cols._mapping) for cols in all_ti_rows
+                )
+                marker_query = select(TaskInstance).where(condition) if 
condition is not None else None
+            else:
+                marker_query = tis_full
+
+            if marker_query is not None:
+                if visited_external_tis is None:
+                    visited_external_tis = set()
+
+                external_marker_tis = session.scalars(
+                    marker_query.where(TaskInstance.operator == 
ExternalTaskMarker.__name__)
+                )
+
+                for ti in external_marker_tis:
+                    if (ti_key := ti.key.primary) in visited_external_tis:
+                        continue
+
+                    visited_external_tis.add(ti_key)
+                    task: ExternalTaskMarker = cast("ExternalTaskMarker", 
self.get_task(ti.task_id))

Review Comment:
   In the same vein, we shouldn’t cast an operator like this. (Also I believe 
this is technically incorrect; the object is not actually an 
ExternalTaskMarker, just a SerializedBaseOperator with the same 
`recursion_depth` defined.



##########
airflow-core/src/airflow/serialization/definitions/dag.py:
##########
@@ -1104,6 +1124,106 @@ def apply_state_filter(query):
         else:
             tis_full = apply_state_filter(tis_full)
 
+        if include_dependent_dags:
+            # Recursively find external tasks indicated by ExternalTaskMarker
+            from airflow.providers.standard.sensors.external_task import 
ExternalTaskMarker
+
+            # Build a full-object query for identifying ExternalTaskMarker TIs 
in the current set
+            if as_pk_tuple:
+                all_ti_rows = session.execute(tis_pk).all()
+                condition = TaskInstance.filter_for_tis(
+                    TaskInstanceKey(**cols._mapping) for cols in all_ti_rows
+                )
+                marker_query = select(TaskInstance).where(condition) if 
condition is not None else None
+            else:
+                marker_query = tis_full
+
+            if marker_query is not None:
+                if visited_external_tis is None:
+                    visited_external_tis = set()
+
+                external_marker_tis = session.scalars(
+                    marker_query.where(TaskInstance.operator == 
ExternalTaskMarker.__name__)
+                )
+
+                for ti in external_marker_tis:
+                    if (ti_key := ti.key.primary) in visited_external_tis:
+                        continue
+
+                    visited_external_tis.add(ti_key)
+                    task: ExternalTaskMarker = cast("ExternalTaskMarker", 
self.get_task(ti.task_id))

Review Comment:
   In the same vein, we shouldn’t cast an operator like this. Also I believe 
this is technically incorrect; the object is not actually an 
ExternalTaskMarker, just a SerializedBaseOperator with the same 
`recursion_depth` defined.



-- 
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]

Reply via email to