Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from cadwyn import VersionedAPIRouter
from fastapi import Body, HTTPException, Query, status
from pydantic import JsonValue
from sqlalchemy import func, or_, tuple_, update
from sqlalchemy import exists, func, or_, tuple_, update
from sqlalchemy.engine import CursorResult
from sqlalchemy.exc import NoResultFound, SQLAlchemyError
from sqlalchemy.orm import joinedload
Expand Down Expand Up @@ -241,12 +241,18 @@ def ti_run(
xcom_query = xcom_query.where(XComModel.map_index == map_index)

xcom_keys = list(session.scalars(xcom_query))
task_reschedule_count = (
session.scalar(
select(func.count(TaskReschedule.id)).where(TaskReschedule.ti_id == task_instance_id)

has_reschedules = session.scalar(select(exists().where(TaskReschedule.ti_id == task_instance_id)))

if has_reschedules:
task_reschedule_count = (
session.scalar(
select(func.count(TaskReschedule.id)).where(TaskReschedule.ti_id == task_instance_id)
)
or 0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This amounts to double queries. I think the right fix is adding index on the id which was done here: #61983

)
or 0
)
else:
task_reschedule_count = 0

context = TIRunContext(
dag_run=dr,
Expand Down
Loading