Skip to content

Commit 83826ee

Browse files
sunank200Your Name
authored andcommitted
[v3-0-test] Fix Task Instance “No Status” Filter (apache#51880)
* Support no_status alias in TaskInstance state filter for REST API * Allow 'no_status' state filter and include no_status in valid state list; skip date filters when filtering for null state * Fix NULL-state filtering in get_mapped_task_instances by coalescing date fields * Refactor datetime_range_filter_factory: coalesce only start_date and end_date filters * Add a test (cherry picked from commit c71566b) Co-authored-by: Ankit Chaurasia <[email protected]>
1 parent 8e812f2 commit 83826ee

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

airflow-core/src/airflow/api_fastapi/common/parameters.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from fastapi import Depends, HTTPException, Query, status
3838
from pendulum.parsing.exceptions import ParserError
3939
from pydantic import AfterValidator, BaseModel, NonNegativeInt
40-
from sqlalchemy import Column, and_, case, or_
40+
from sqlalchemy import Column, and_, case, func, or_
4141
from sqlalchemy.inspection import inspect
4242

4343
from airflow.api_fastapi.core_api.base import OrmClause
@@ -484,9 +484,12 @@ def depends_datetime(
484484
lower_bound: datetime | None = Query(alias=f"{filter_name}_gte", default=None),
485485
upper_bound: datetime | None = Query(alias=f"{filter_name}_lte", default=None),
486486
) -> RangeFilter:
487+
attr = getattr(model, attribute_name or filter_name)
488+
if filter_name in ("start_date", "end_date"):
489+
attr = func.coalesce(attr, func.now())
487490
return RangeFilter(
488491
Range(lower_bound=lower_bound, upper_bound=upper_bound),
489-
getattr(model, attribute_name or filter_name),
492+
attr,
490493
)
491494

492495
return depends_datetime
@@ -601,7 +604,7 @@ def _transform_ti_states(states: list[str] | None) -> list[TaskInstanceState | N
601604
return None
602605

603606
try:
604-
return [None if s in ("none", None) else TaskInstanceState(s) for s in states]
607+
return [None if s in ("no_status", "none", None) else TaskInstanceState(s) for s in states]
605608
except ValueError:
606609
raise HTTPException(
607610
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,

airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_task_instances.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,19 @@ class TestGetTaskInstances(TestTaskInstanceEndpoint):
956956
3,
957957
id="test state filter",
958958
),
959+
pytest.param(
960+
[
961+
{"state": State.RUNNING},
962+
{"state": State.QUEUED},
963+
{"state": State.SUCCESS},
964+
{"state": State.NONE},
965+
],
966+
False,
967+
("/dags/example_python_operator/dagRuns/TEST_DAG_RUN_ID/taskInstances"),
968+
{"state": ["no_status"]},
969+
1,
970+
id="test no_status state filter",
971+
),
959972
pytest.param(
960973
[
961974
{"state": State.NONE},
@@ -969,6 +982,14 @@ class TestGetTaskInstances(TestTaskInstanceEndpoint):
969982
4,
970983
id="test null states with no filter",
971984
),
985+
pytest.param(
986+
[{"start_date": None, "end_date": None}],
987+
True,
988+
"/dags/example_python_operator/dagRuns/TEST_DAG_RUN_ID/taskInstances",
989+
{"start_date_gte": DEFAULT_DATETIME_STR_1},
990+
1,
991+
id="test start_date coalesce with null",
992+
),
972993
pytest.param(
973994
[
974995
{"pool": "test_pool_1"},

0 commit comments

Comments
 (0)