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


##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -4217,30 +4217,17 @@ def _update_asset_orphanage(self, *, session: Session = 
NEW_SESSION) -> None:
 
         :seealso: :meth:`AssetModelOperation.activate_assets_if_possible`.
         """
-        # Group assets into orphaned=True and orphaned=False groups.
-        orphaned = (
-            (
-                func.count(DagScheduleAssetReference.dag_id)
-                + func.count(TaskOutletAssetReference.dag_id)
-                + func.count(TaskInletAssetReference.dag_id)
-                # An asset materialized from an ``AssetAlias`` outlet is 
referenced only through
-                # the alias association, not the schedule/outlet/inlet tables. 
Count it so it is
-                # activated (and shown in the UI) instead of being treated as 
orphaned. See #58058.
-                + func.count(alias_association_table.c.alias_id)
-            )
-            == 0
-        ).label("orphaned")
-        asset_reference_query = (
-            select(AssetModel)
-            .outerjoin(DagScheduleAssetReference)
-            .outerjoin(TaskOutletAssetReference)
-            .outerjoin(TaskInletAssetReference)
-            .outerjoin(alias_association_table, 
alias_association_table.c.asset_id == AssetModel.id)
-            .group_by(AssetModel.id)
+        referenced = or_(
+            exists().where(DagScheduleAssetReference.asset_id == 
AssetModel.id),
+            exists().where(TaskOutletAssetReference.asset_id == AssetModel.id),
+            exists().where(TaskInletAssetReference.asset_id == AssetModel.id),
+            # An asset materialized from an ``AssetAlias`` outlet is 
referenced only through
+            # the alias association, not the schedule/outlet/inlet tables. 
Count it so it is
+            # activated (and shown in the UI) instead of being treated as 
orphaned. See #58058.
+            exists().where(alias_association_table.c.asset_id == 
AssetModel.id),
         )
-
-        orphan_query = asset_reference_query.having(orphaned).cte()
-        activate_query = asset_reference_query.having(~orphaned).cte()
+        orphan_query = select(AssetModel).where(~referenced).cte()
+        activate_query = select(AssetModel).where(referenced).cte()

Review Comment:
   Both CTEs still select every asset column (`extra` JSON included, visible in 
the generated SQL posted above), but the consumers only read `.c.name` and 
`.c.uri`. This matters on the activate side since `inactive_assets_query` 
references the CTE twice. `select(AssetModel.name, AssetModel.uri)` for both 
would keep it narrow.



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