Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up typing with max_execution_date query builder #36958

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
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
11 changes: 5 additions & 6 deletions airflow/models/dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -3049,8 +3049,8 @@ def bulk_write_to_db(
)
query = with_row_locks(query, of=DagModel, session=session)
orm_dags: list[DagModel] = session.scalars(query).unique().all()
existing_dags = {orm_dag.dag_id: orm_dag for orm_dag in orm_dags}
missing_dag_ids = dag_ids.difference(existing_dags)
existing_dags: dict[str, DagModel] = {x.dag_id: x for x in orm_dags}
missing_dag_ids = dag_ids.difference(existing_dags.keys())

for missing_dag_id in missing_dag_ids:
orm_dag = DagModel(dag_id=missing_dag_id)
Expand All @@ -3067,7 +3067,7 @@ def bulk_write_to_db(
# Skip these queries entirely if no DAGs can be scheduled to save time.
if any(dag.timetable.can_be_scheduled for dag in dags):
# Get the latest automated dag run for each existing dag as a single query (avoid n+1 query)
query = cls._get_latest_runs_query(existing_dags, session)
query = cls._get_latest_runs_query(dags=list(existing_dags.keys()))
latest_runs = {run.dag_id: run for run in session.scalars(query)}

# Get number of active dagruns for all dags we are processing as a single query.
Expand Down Expand Up @@ -3240,16 +3240,15 @@ def bulk_write_to_db(
cls.bulk_write_to_db(dag.subdags, processor_subdir=processor_subdir, session=session)

@classmethod
def _get_latest_runs_query(cls, dags, session) -> Query:
def _get_latest_runs_query(cls, dags: list[str]) -> Query:
"""
Query the database to retrieve the last automated run for each dag.

:param dags: dags to query
:param session: sqlalchemy session object
"""
if len(dags) == 1:
# Index optimized fast path to avoid more complicated & slower groupby queryplan
existing_dag_id = list(dags)[0].dag_id
existing_dag_id = dags[0]
last_automated_runs_subq = (
select(func.max(DagRun.execution_date).label("max_execution_date"))
.where(
Expand Down
4 changes: 2 additions & 2 deletions tests/models/test_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -4140,7 +4140,7 @@ def test_validate_setup_teardown_trigger_rule(self):
def test_get_latest_runs_query_one_dag(dag_maker, session):
with dag_maker(dag_id="dag1") as dag1:
...
query = DAG._get_latest_runs_query(dags=[dag1], session=session)
query = DAG._get_latest_runs_query(dags=[dag1.dag_id])
actual = [x.strip() for x in str(query.compile()).splitlines()]
expected = [
"SELECT dag_run.id, dag_run.dag_id, dag_run.execution_date, dag_run.data_interval_start, dag_run.data_interval_end",
Expand All @@ -4157,7 +4157,7 @@ def test_get_latest_runs_query_two_dags(dag_maker, session):
...
with dag_maker(dag_id="dag2") as dag2:
...
query = DAG._get_latest_runs_query(dags=[dag1, dag2], session=session)
query = DAG._get_latest_runs_query(dags=[dag1.dag_id, dag2.dag_id])
actual = [x.strip() for x in str(query.compile()).splitlines()]
print("\n".join(actual))
expected = [
Expand Down