kaxil commented on code in PR #65759:
URL: https://github.com/apache/airflow/pull/65759#discussion_r3161053289


##########
airflow-core/src/airflow/state/metastore.py:
##########
@@ -0,0 +1,351 @@
+#
+# 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 TYPE_CHECKING
+
+from sqlalchemy import delete, select
+
+from airflow._shared.state import AssetScope, BaseStateBackend, StateScope, 
TaskScope
+from airflow._shared.timezones import timezone
+from airflow.models.asset_state import AssetStateModel
+from airflow.models.dagrun import DagRun
+from airflow.models.task_state import TaskStateModel
+from airflow.typing_compat import assert_never
+from airflow.utils.session import NEW_SESSION, create_session_async, 
provide_session
+from airflow.utils.sqlalchemy import get_dialect_name
+
+if TYPE_CHECKING:
+    from sqlalchemy.dialects.mysql.dml import Insert as MySQLInsert
+    from sqlalchemy.dialects.postgresql.dml import Insert as PostgreSQLInsert
+    from sqlalchemy.dialects.sqlite.dml import Insert as SQLiteInsert
+    from sqlalchemy.ext.asyncio import AsyncSession
+    from sqlalchemy.orm import Session
+
+
+def _build_upsert_stmt(
+    dialect: str | None,
+    model: type,
+    conflict_cols: list[str],
+    values: dict,
+    update_fields: dict,
+):
+    """Return a dialect-specific INSERT ... ON CONFLICT UPDATE statement."""
+    stmt: MySQLInsert | PostgreSQLInsert | SQLiteInsert
+    if dialect == "postgresql":
+        from sqlalchemy.dialects.postgresql import insert as pg_insert
+
+        stmt = pg_insert(model).values(**values)
+        stmt = stmt.on_conflict_do_update(index_elements=conflict_cols, 
set_=update_fields)
+    elif dialect == "mysql":
+        from sqlalchemy.dialects.mysql import insert as mysql_insert
+
+        stmt = mysql_insert(model).values(**values)
+        stmt = stmt.on_duplicate_key_update(**update_fields)
+    else:
+        from sqlalchemy.dialects.sqlite import insert as sqlite_insert
+
+        stmt = sqlite_insert(model).values(**values)
+        stmt = stmt.on_conflict_do_update(index_elements=conflict_cols, 
set_=update_fields)
+    return stmt
+
+
+class MetastoreStateBackend(BaseStateBackend):
+    """Default state backend for tasks and assets. Stores task and asset state 
in the Airflow metadata database."""
+
+    @provide_session
+    def get(self, scope: StateScope, key: str, *, session: Session = 
NEW_SESSION) -> str | None:
+        match scope:
+            case TaskScope():
+                return self._get_task_state(scope, key, session=session)
+            case AssetScope():
+                return self._get_asset_state(scope, key, session=session)
+            case _:
+                assert_never(scope)
+
+    @provide_session
+    def set(self, scope: StateScope, key: str, value: str, *, session: Session 
= NEW_SESSION) -> None:
+        match scope:
+            case TaskScope():
+                self._set_task_state(scope, key, value, session=session)
+            case AssetScope():
+                self._set_asset_state(scope, key, value, session=session)
+            case _:
+                assert_never(scope)
+
+    @provide_session
+    def delete(self, scope: StateScope, key: str, *, session: Session = 
NEW_SESSION) -> None:
+        match scope:
+            case TaskScope():
+                self._delete_task_state(scope, key, session=session)
+            case AssetScope():
+                self._delete_asset_state(scope, key, session=session)
+            case _:
+                assert_never(scope)
+
+    @provide_session
+    def clear(self, scope: StateScope, *, session: Session = NEW_SESSION) -> 
None:
+        match scope:
+            case TaskScope():
+                self._clear_task_state(scope, session=session)
+            case AssetScope():
+                self._clear_asset_state(scope, session=session)
+            case _:
+                assert_never(scope)
+
+    async def aget(self, scope: StateScope, key: str) -> str | None:
+        async with create_session_async() as session:
+            match scope:
+                case TaskScope():
+                    return await self._aget_task_state(scope, key, 
session=session)
+                case AssetScope():
+                    return await self._aget_asset_state(scope, key, 
session=session)
+                case _:
+                    assert_never(scope)
+
+    async def aset(self, scope: StateScope, key: str, value: str) -> None:
+        async with create_session_async() as session:
+            match scope:
+                case TaskScope():
+                    await self._aset_task_state(scope, key, value, 
session=session)
+                case AssetScope():
+                    await self._aset_asset_state(scope, key, value, 
session=session)
+                case _:
+                    assert_never(scope)
+
+    async def adelete(self, scope: StateScope, key: str) -> None:
+        async with create_session_async() as session:
+            match scope:
+                case TaskScope():
+                    await self._adelete_task_state(scope, key, session=session)
+                case AssetScope():
+                    await self._adelete_asset_state(scope, key, 
session=session)
+                case _:
+                    assert_never(scope)
+
+    async def aclear(self, scope: StateScope) -> None:
+        async with create_session_async() as session:
+            match scope:
+                case TaskScope():
+                    await self._aclear_task_state(scope, session=session)
+                case AssetScope():
+                    await self._aclear_asset_state(scope, session=session)
+                case _:
+                    assert_never(scope)
+
+    def _get_task_state(self, scope: TaskScope, key: str, *, session: Session) 
-> str | None:
+        row = session.scalar(
+            select(TaskStateModel).where(
+                TaskStateModel.dag_id == scope.dag_id,
+                TaskStateModel.run_id == scope.run_id,
+                TaskStateModel.task_id == scope.task_id,
+                TaskStateModel.map_index == scope.map_index,
+                TaskStateModel.key == key,
+            )
+        )
+        return row.value if row is not None else None
+
+    def _set_task_state(self, scope: TaskScope, key: str, value: str, *, 
session: Session) -> None:
+        dag_run_id = session.scalar(
+            select(DagRun.id).where(
+                DagRun.dag_id == scope.dag_id,
+                DagRun.run_id == scope.run_id,
+            )
+        )
+        if dag_run_id is None:
+            raise ValueError(f"No DagRun found for dag_id={scope.dag_id!r} 
run_id={scope.run_id!r}")
+        now = timezone.utcnow()
+        values = dict(
+            dag_run_id=dag_run_id,
+            dag_id=scope.dag_id,
+            run_id=scope.run_id,
+            task_id=scope.task_id,
+            map_index=scope.map_index,
+            key=key,
+            value=value,
+            updated_at=now,
+        )
+        stmt = _build_upsert_stmt(
+            get_dialect_name(session),
+            TaskStateModel,
+            ["dag_run_id", "task_id", "map_index", "key"],
+            values,
+            dict(value=value, updated_at=now),
+        )
+        session.execute(stmt)
+
+    def _delete_task_state(self, scope: TaskScope, key: str, *, session: 
Session) -> None:
+        session.execute(
+            delete(TaskStateModel).where(
+                TaskStateModel.dag_id == scope.dag_id,
+                TaskStateModel.run_id == scope.run_id,
+                TaskStateModel.task_id == scope.task_id,
+                TaskStateModel.map_index == scope.map_index,
+                TaskStateModel.key == key,
+            )
+        )
+
+    def _clear_task_state(self, scope: TaskScope, *, session: Session) -> None:
+        conditions = [
+            TaskStateModel.dag_id == scope.dag_id,
+            TaskStateModel.run_id == scope.run_id,
+            TaskStateModel.task_id == scope.task_id,
+        ]
+        if scope.map_index != -1:

Review Comment:
   `_clear_task_state` drops the `map_index` filter when `map_index == -1`, but 
`_delete_task_state` above keeps the exact-match filter. Same 
`TaskScope(map_index=-1)` value, opposite semantics: `delete` only touches the 
non-mapped row, `clear` wipes every map index. That asymmetry is a foot-gun 
even when documented. Either drop the `map_index` filter in delete too when 
`map_index == -1`, or replace the sentinel with an explicit 
`clear_all_map_indices: bool = False` flag. Same applies to 
`_aclear_task_state` below.



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