Skip to content

Clean up unneeded unique() calls on ORM #43068

Description

@uranusjr

Body

Similar to #43064. Maybe there are more to be had? cc @dstandish

I can find 6 with a naive text search in main, 4 in main and 2 in providers. We should review them individually.

asset_alias_models = session.scalars(
select(AssetAliasModel)
.where(AssetAliasModel.name.in_(source_alias_names))
.options(
joinedload(AssetAliasModel.consuming_dags).joinedload(DagScheduleAssetAliasReference.dag)
)
).unique()

def _find_orm_dags(dag_ids: Iterable[str], *, session: Session) -> dict[str, DagModel]:
"""Find existing DagModel objects from DAG objects."""
stmt = (
select(DagModel)
.options(joinedload(DagModel.tags, innerjoin=False))
.where(DagModel.dag_id.in_(dag_ids))
.options(joinedload(DagModel.schedule_dataset_references))
.options(joinedload(DagModel.schedule_dataset_alias_references))
.options(joinedload(DagModel.task_outlet_dataset_references))
)
stmt = with_row_locks(stmt, of=DagModel, session=session)
return {dm.dag_id: dm for dm in session.scalars(stmt).unique()}

existing_dagruns = (
session.execute(
select(DagRun.dag_id, DagRun.execution_date).where(
tuple_in_condition(
(DagRun.dag_id, DagRun.execution_date),
((dm.dag_id, dm.next_dagrun) for dm in dag_models),
),
)
)
.unique()
.all()
)

airflow/airflow/www/views.py

Lines 1000 to 1006 in e20146d

dags: list[DagModel] = (
session.scalars(
current_dags.options(joinedload(DagModel.tags)).offset(start).limit(dags_per_page)
)
.unique()
.all()
)

roles = (
session.scalars(query.order_by(direction(getattr(Role, order_param))).offset(offset).limit(limit))
.unique()
.all()
)

def _get_all_roles_with_permissions(self) -> dict[str, Role]:
"""Return a dict with a key of role name and value of role with early loaded permissions."""
return {
r.name: r
for r in self.appbuilder.get_session.scalars(
select(self.role_model).options(joinedload(self.role_model.permissions))
).unique()
}

(There is a 7th unique() match in a Jinja2 template.)

Committer

  • I acknowledge that I am a maintainer/committer of the Apache Airflow project.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions