Warning

This document is for an in-development version of Galaxy. You can alternatively view this page in the latest release if it exists or view the top of the latest release's documentation.

Source code for galaxy.model.index_filter_util

"""Utility functions used to adapt galaxy.util.search to Galaxy model index queries."""

from sqlalchemy import (
    and_,
    func,
    or_,
    select,
)
from sqlalchemy.orm import (
    aliased,
    InstrumentedAttribute,
)
from sqlalchemy.sql.expression import BinaryExpression

from galaxy import model
from galaxy.util.search import (
    FilteredTerm,
    RawTextTerm,
)


[docs] def text_column_filter(column, term: FilteredTerm): if term.quoted: filter = column == term.text else: filter = column.ilike(f"%{term.text}%") return filter
RawTextSearchableT = BinaryExpression | InstrumentedAttribute
[docs] def raw_text_column_filter(columns: list[RawTextSearchableT], term: RawTextTerm): like_text = f"%{term.text}%" return or_(*[c.ilike(like_text) if isinstance(c, InstrumentedAttribute) else c for c in columns])
[docs] def tag_filter(assocation_model_class, term_text, quoted: bool = False): if ":" in term_text: key, value = term_text.rsplit(":", 1) if not quoted: return and_( assocation_model_class.user_tname.ilike(key), assocation_model_class.user_value.ilike(f"%{value}%"), ) else: return and_( assocation_model_class.user_tname == key, assocation_model_class.user_value == value, ) else: if not quoted: return or_( assocation_model_class.user_tname.ilike(f"%{term_text}%"), and_( assocation_model_class.user_tname.ilike("name"), assocation_model_class.user_value.ilike(f"%{term_text}%"), ), ) else: return assocation_model_class.user_tname == term_text
[docs] def append_user_filter(query, model_class, term: FilteredTerm): alias = aliased(model.User) query = query.outerjoin(model_class.user.of_type(alias)) query = query.filter(text_column_filter(alias.username, term)) return query
[docs] def tag_exists_filter( association_model_class, fk_column, parent_id_column, term_text, quoted: bool = False, tag_user_id_column=None, owner_id_column=None, ): """Correlated EXISTS subquery that matches any tag on the parent row against term_text. Prefer this over adding a per-term outer join on the tag-association table: each extra outer join multiplies rows (forcing an expensive DISTINCT) and in free-text search N whitespace-separated terms produce N such joins. Pass ``tag_user_id_column`` and ``owner_id_column`` together to consider only the owner's own tags. Anyone who can see a shared or published item may tag it, so unrestricted matching lets one user's private tag decide whether another user's item appears in someone else's search results. """ stmt = ( select(1) .select_from(association_model_class) .where(fk_column == parent_id_column) .where(tag_filter(association_model_class, term_text, quoted)) ) if tag_user_id_column is not None and owner_id_column is not None: stmt = stmt.where(tag_user_id_column == owner_id_column) return stmt.correlate_except(association_model_class).exists()
[docs] def owner_annotation_exists_filter(association_model_class, fk_column, parent_id_column, owner_id_column, term_text): """Correlated EXISTS subquery that matches the owner's annotation on the parent row against term_text. Other users' annotations are ignored for the same reason as in ``tag_exists_filter``. """ return ( select(1) .select_from(association_model_class) .where(fk_column == parent_id_column) .where(association_model_class.user_id == owner_id_column) .where(association_model_class.annotation.ilike(f"%{term_text}%")) .correlate_except(association_model_class) .exists() )
[docs] def user_exists_filter(owner_id_column, term_text: str): """Correlated EXISTS subquery that matches the owning user's username.""" return ( select(1) .select_from(model.User) .where(model.User.id == owner_id_column) .where(model.User.username.ilike(f"%{term_text}%")) .correlate_except(model.User) .exists() )
[docs] def user_in_filter(owner_id_column, usernames: list[str]): """Correlated EXISTS subquery matching the owning user against an exact, case-insensitive username allowlist. Distinct from `user_exists_filter`, which is a substring match appropriate for free-text `user:` search but not for a config-driven allowlist -- a value of `iwc` must not also match `iwc-mirror`. """ return ( select(1) .select_from(model.User) .where(model.User.id == owner_id_column) .where(func.lower(model.User.username).in_([username.lower() for username in usernames])) .correlate_except(model.User) .exists() )