You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by po...@apache.org on 2026/05/10 01:28:48 UTC
(airflow) branch main updated: Fix XCom prior-dates lookup for duplicate run_id across Dags (#65227)
This is an automated email from the ASF dual-hosted git repository.
potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new 76c5131df51 Fix XCom prior-dates lookup for duplicate run_id across Dags (#65227)
76c5131df51 is described below
commit 76c5131df510f91d442c28437e5749dc54e55a95
Author: Henry Chen <he...@gmail.com>
AuthorDate: Sun May 10 09:28:40 2026 +0800
Fix XCom prior-dates lookup for duplicate run_id across Dags (#65227)
---
airflow-core/src/airflow/models/xcom.py | 13 ++++---
airflow-core/tests/unit/models/test_xcom.py | 53 +++++++++++++++++++++++++----
2 files changed, 53 insertions(+), 13 deletions(-)
diff --git a/airflow-core/src/airflow/models/xcom.py b/airflow-core/src/airflow/models/xcom.py
index 60d380ae989..5b27f244bf5 100644
--- a/airflow-core/src/airflow/models/xcom.py
+++ b/airflow-core/src/airflow/models/xcom.py
@@ -314,16 +314,15 @@ class XComModel(TaskInstanceDependencies):
query = query.where(cls.map_index == map_indexes)
if include_prior_dates:
- dr = (
- select(
- func.coalesce(DagRun.logical_date, DagRun.run_after).label("logical_date_or_run_after")
- )
- .where(DagRun.run_id == run_id)
- .subquery()
+ dag_run_date_for_run_id = (
+ select(func.coalesce(DagRun.logical_date, DagRun.run_after))
+ .where(DagRun.run_id == run_id, DagRun.dag_id == cls.dag_id)
+ .correlate(cls)
+ .scalar_subquery()
)
query = query.where(
- func.coalesce(DagRun.logical_date, DagRun.run_after) <= dr.c.logical_date_or_run_after
+ func.coalesce(DagRun.logical_date, DagRun.run_after) <= dag_run_date_for_run_id
)
else:
query = query.where(cls.run_id == run_id)
diff --git a/airflow-core/tests/unit/models/test_xcom.py b/airflow-core/tests/unit/models/test_xcom.py
index f037ca7e91b..2635aac1803 100644
--- a/airflow-core/tests/unit/models/test_xcom.py
+++ b/airflow-core/tests/unit/models/test_xcom.py
@@ -64,13 +64,14 @@ def reset_db():
@pytest.fixture
def task_instance_factory(request, session: Session):
- def func(*, dag_id, task_id, logical_date, run_after=None):
+ def func(*, dag_id, task_id, logical_date, run_after=None, run_id=None):
sync_dag_to_db(DAG(dag_id=dag_id))
- run_id = DagRun.generate_run_id(
- run_type=DagRunType.SCHEDULED,
- logical_date=logical_date,
- run_after=run_after if run_after is not None else logical_date,
- )
+ if run_id is None:
+ run_id = DagRun.generate_run_id(
+ run_type=DagRunType.SCHEDULED,
+ logical_date=logical_date,
+ run_after=run_after if run_after is not None else logical_date,
+ )
interval = (logical_date, logical_date) if logical_date else None
run = DagRun(
dag_id=dag_id,
@@ -368,6 +369,46 @@ class TestXComGet:
)
assert [x.logical_date for x in stored_xcoms] == [ti2.logical_date, ti1.logical_date]
+ def test_xcom_get_many_from_prior_dates_scopes_run_id_to_dag(
+ self, session, task_instance_factory, push_simple_json_xcom
+ ):
+ shared_run_id = "manual__shared_run"
+ ti_earlier = task_instance_factory(
+ dag_id="dag_1",
+ task_id="task_1",
+ logical_date=timezone.datetime(2021, 12, 1, 4, 56),
+ )
+ ti_target = task_instance_factory(
+ dag_id="dag_1",
+ task_id="task_1",
+ logical_date=timezone.datetime(2021, 12, 3, 4, 56),
+ run_id=shared_run_id,
+ )
+ task_instance_factory(
+ dag_id="dag_2",
+ task_id="task_1",
+ logical_date=timezone.datetime(2021, 12, 2, 4, 56),
+ run_id=shared_run_id,
+ )
+
+ push_simple_json_xcom(ti=ti_earlier, key="xcom_1", value={"key": "dag_1_earlier"})
+ push_simple_json_xcom(ti=ti_target, key="xcom_1", value={"key": "dag_1_target"})
+
+ stored_xcoms = session.scalars(
+ XComModel.get_many(
+ run_id=shared_run_id,
+ key="xcom_1",
+ dag_ids="dag_1",
+ task_ids="task_1",
+ include_prior_dates=True,
+ )
+ ).all()
+
+ assert [x.value for x in stored_xcoms] == [
+ json.dumps({"key": "dag_1_target"}),
+ json.dumps({"key": "dag_1_earlier"}),
+ ]
+
def test_xcom_get_invalid_key(self, session, task_instance):
"""Test that getting an XCom with an invalid key raises a ValueError."""
with pytest.raises(ValueError, match="XCom key must be a non-empty string. Received: ''"):