Lee-W commented on code in PR #69849:
URL: https://github.com/apache/airflow/pull/69849#discussion_r4695585725
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -339,6 +342,53 @@ def _build_scheduled_asset_event_clause(dag_id: str) ->
ColumnElement[bool]:
)
+def _associate_asset_events_with_dag_run(
+ *, dag_run: DagRun, asset_event_ids_select: Select[tuple[int]], session:
Session
+) -> int:
+ """Associate selected asset events with a Dag run without loading them,
returning the row count."""
+ selected_event_ids = asset_event_ids_select.subquery()
+ result = session.execute(
+ insert(association_table).from_select(
+ ["dag_run_id", "event_id"], select(literal(dag_run.id),
selected_event_ids.c.event_id)
+ ),
+ # SQLAlchemy only keeps rowcount for UPDATE/DELETE by default;
PostgreSQL reports -1 for this INSERT without it.
+ execution_options={"preserve_rowcount": True},
+ )
+ # The Core insert bypasses the ORM collection, which create_dagrun left as
a loaded empty list.
+ session.expire(dag_run, ["consumed_asset_events"])
+ return cast("CursorResult", result).rowcount
+
+
+def _build_consumable_asset_event_clause(
+ dag: SerializedDAG, *, locked_records: Iterable[AssetDagRunQueue]
+) -> ColumnElement[bool]:
+ """
+ Match unconsumed events referenced by a Dag's locked ADRQ rows.
+
+ ADRQ rows are matched up to the largest event ID in *locked_records*.
+ This does not exclude every event queued after the rows were locked.
+ With catchup, events are also matched by asset regardless of ID, and
+ event IDs are not assigned in commit order. Concurrent schedulers can't
+ consume this Dag's events since the Dag's own row is locked.
+ """
+ event_predicate: ColumnElement[bool] = (
+ select(AssetDagRunQueue.asset_event_id)
+ .where(
+ AssetDagRunQueue.target_dag_id == dag.dag_id,
+ AssetDagRunQueue.asset_event_id == AssetEvent.id,
+ AssetDagRunQueue.asset_event_id <= max(record.asset_event_id for
record in locked_records),
+ )
+ .exists()
+ )
+ if dag.catchup:
+ event_predicate |= _build_scheduled_asset_event_clause(dag.dag_id)
+ return and_(event_predicate, ~build_consumed_by_dag_clause(dag.dag_id,
AssetEvent.id))
+
+
+def _has_default_asset_event_grouping(timetable: Timetable) -> bool:
+ return type(timetable).group_asset_events is Timetable.group_asset_events
Review Comment:
Yep, called it out in the docstring of `_has_default_asset_event_grouping`.
--
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]