def METHOD_NAME(
    left_id_column: SaColumn,
    right_id_column: SaColumn,
    filter_column: SaColumn,
    filter_factory: SaColumn,
    subquery_decorator: Callable[[SaQuery], None] = None,
) -> Filter:
    filter_func = filter_factory(filter_column)
    def wrapper(
        query: SaQuery,
        criterion: Optional[criteria.BaseCriterion],
        negated: bool,
    ) -> SaQuery:
        assert criterion
        subquery = db.session.query(right_id_column.label("foreign_id"))
        if subquery_decorator:
            subquery = subquery_decorator(subquery)
        subquery = subquery.options(sa.orm.lazyload("*"))
        subquery = filter_func(subquery, criterion, False)
        subquery = subquery.subquery("t")
        expression = left_id_column.in_(subquery)
        if negated:
            expression = ~expression
        return query.filter(expression)
    return wrapper