amoghrajesh commented on code in PR #50117:
URL: https://github.com/apache/airflow/pull/50117#discussion_r2108286057
##########
airflow-core/src/airflow/api_fastapi/execution_api/routes/xcoms.py:
##########
@@ -184,6 +188,132 @@ def get_xcom(
return XComResponse(key=key, value=result.value)
[email protected](
+ "/{dag_id}/{run_id}/{task_id}/{key}/item/{offset}",
+ description="Get a single XCom value from a mapped task by sequence index",
+)
+def get_mapped_xcom_by_index(
+ dag_id: str,
+ run_id: str,
+ task_id: str,
+ key: str,
+ offset: int,
+ session: SessionDep,
+) -> XComSequenceIndexResponse:
+ xcom_query = XComModel.get_many(
+ run_id=run_id,
+ key=key,
+ task_ids=task_id,
+ dag_ids=dag_id,
+ session=session,
+ )
+ xcom_query = xcom_query.order_by(None)
+ if offset >= 0:
+ xcom_query =
xcom_query.order_by(XComModel.map_index.asc()).offset(offset)
+ else:
+ xcom_query = xcom_query.order_by(XComModel.map_index.desc()).offset(-1
- offset)
+
+ if (result := xcom_query.limit(1).first()) is None:
+ message = (
+ f"XCom with {key=} {offset=} not found for task {task_id!r} in DAG
run {run_id!r} of {dag_id!r}"
+ )
+ raise HTTPException(
+ status_code=status.HTTP_404_NOT_FOUND,
+ detail={"reason": "not_found", "message": message},
+ )
+ return XComSequenceIndexResponse(result.value)
+
+
+class GetXComSliceFilterParams(BaseModel):
+ """Class to house slice params."""
+
+ start: int | None = None
+ stop: int | None = None
+ step: int | None = None
+
+
[email protected](
+ "/{dag_id}/{run_id}/{task_id}/{key}/slice",
+ description="Get a single XCom value from a mapped task by sequence slice",
+)
+def get_mapped_xcom_by_slice(
+ dag_id: str,
+ run_id: str,
+ task_id: str,
+ key: str,
+ params: Annotated[GetXComSliceFilterParams, Query()],
+ session: SessionDep,
+) -> XComSequenceSliceResponse:
+ orig_query = XComModel.get_many(
Review Comment:
```suggestion
xcom_query = XComModel.get_many(
```
##########
airflow-core/src/airflow/api_fastapi/execution_api/routes/xcoms.py:
##########
@@ -184,6 +188,132 @@ def get_xcom(
return XComResponse(key=key, value=result.value)
[email protected](
+ "/{dag_id}/{run_id}/{task_id}/{key}/item/{offset}",
+ description="Get a single XCom value from a mapped task by sequence index",
+)
+def get_mapped_xcom_by_index(
+ dag_id: str,
+ run_id: str,
+ task_id: str,
+ key: str,
+ offset: int,
+ session: SessionDep,
+) -> XComSequenceIndexResponse:
+ xcom_query = XComModel.get_many(
+ run_id=run_id,
+ key=key,
+ task_ids=task_id,
+ dag_ids=dag_id,
+ session=session,
+ )
+ xcom_query = xcom_query.order_by(None)
+ if offset >= 0:
+ xcom_query =
xcom_query.order_by(XComModel.map_index.asc()).offset(offset)
+ else:
+ xcom_query = xcom_query.order_by(XComModel.map_index.desc()).offset(-1
- offset)
+
+ if (result := xcom_query.limit(1).first()) is None:
+ message = (
+ f"XCom with {key=} {offset=} not found for task {task_id!r} in DAG
run {run_id!r} of {dag_id!r}"
+ )
+ raise HTTPException(
+ status_code=status.HTTP_404_NOT_FOUND,
+ detail={"reason": "not_found", "message": message},
+ )
+ return XComSequenceIndexResponse(result.value)
+
+
+class GetXComSliceFilterParams(BaseModel):
+ """Class to house slice params."""
+
+ start: int | None = None
+ stop: int | None = None
+ step: int | None = None
+
+
[email protected](
+ "/{dag_id}/{run_id}/{task_id}/{key}/slice",
+ description="Get a single XCom value from a mapped task by sequence slice",
Review Comment:
```suggestion
description="Get XCom value(s) from a mapped task by sequence slice",
```
##########
airflow-core/src/airflow/api_fastapi/execution_api/versions/v2025_05_21.py:
##########
@@ -0,0 +1,37 @@
+# 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 typing import Optional
+
+from cadwyn import VersionChange, schema
+from pydantic import Field
+
+from airflow.api_fastapi.execution_api.routes.xcoms import
GetXComSliceFilterParams
+
+
+class RemoveGetXcomFilterParamsOffset(VersionChange):
+ """Remove 'offset' from GetXcomFilterParams."""
+
+ description = __doc__
+
+ instructions_to_migrate_to_previous_version = (
+ schema(GetXComSliceFilterParams)
+ .field("offset")
+ .existed_as(type=Optional[int], info=Field(default=None)),
+ )
Review Comment:
We will also need to remove it here right?
https://github.com/apache/airflow/pull/50117/files#diff-f7c9ab9ad4675e23743cffb626e7a9c4ce28ceffef5dd0b6b6a5268fe18909c5R133
##########
airflow-core/tests/unit/api_fastapi/execution_api/versions/head/test_xcoms.py:
##########
@@ -148,12 +149,12 @@ def test_xcom_access_denied(self, client, caplog):
},
id="-4",
),
- pytest.param(-3, 200, {"key": "xcom_1", "value": "f"}, id="-3"),
- pytest.param(-2, 200, {"key": "xcom_1", "value": "o"}, id="-2"),
- pytest.param(-1, 200, {"key": "xcom_1", "value": "b"}, id="-1"),
- pytest.param(0, 200, {"key": "xcom_1", "value": "f"}, id="0"),
- pytest.param(1, 200, {"key": "xcom_1", "value": "o"}, id="1"),
- pytest.param(2, 200, {"key": "xcom_1", "value": "b"}, id="2"),
+ pytest.param(-3, 200, "f", id="-3"),
+ pytest.param(-2, 200, "o", id="-2"),
+ pytest.param(-1, 200, "b", id="-1"),
+ pytest.param(0, 200, "f", id="0"),
+ pytest.param(1, 200, "o", id="1"),
+ pytest.param(2, 200, "b", id="2"),
Review Comment:
Why this change?
##########
task-sdk/src/airflow/sdk/api/client.py:
##########
@@ -470,7 +471,27 @@ def get_sequence_item(
},
)
raise
- return XComResponse.model_validate_json(resp.read())
+ return XComSequenceIndexResponse.model_validate_json(resp.read())
+
+ def get_sequence_slice(
+ self,
+ dag_id: str,
+ run_id: str,
+ task_id: str,
+ key: str,
+ start: int | None,
+ stop: int | None,
+ step: int | None,
Review Comment:
Can we provide default values for these and handle it easier below by just
assigning?
--
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]