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

Enhancement: ExistsFilter #331

Open
riyavsinha opened this issue Jan 11, 2025 · 1 comment · May be fixed by #336
Open

Enhancement: ExistsFilter #331

riyavsinha opened this issue Jan 11, 2025 · 1 comment · May be fixed by #336
Assignees
Labels
accepted Change or enhancement is accepted for development. enhancement New feature or request

Comments

@riyavsinha
Copy link

Summary

It would be great to be able to specify something like:

variants_not_scored_by_model = await model_score_repo.list(
            CollectionFilter(models.ModelScores.variant_id, variant_ids),
            ~exists().where(
                and_(
                    models.ModelScores.model_id == model_id,
                    models.ModelScores.variant_id == models.Variant.id,
                )
            )
        )

As far as I know, there is no exists filter, and I'm not sure the above doesn't work. Essentially, if an ExistsFilter could take in a sublist of StatementFilters that would be nice.

Basic Example

variants_not_scored_by_model = await model_score_repo.list(
            CollectionFilter(models.ModelScores.variant_id, variant_ids),
            NotExistsFilter(
                    models.ModelScores.model_id == model_id,
                    models.ModelScores.variant_id == models.Variant.id,
            )
        )

Drawbacks and Impact

No response

Unresolved questions

No response

@riyavsinha riyavsinha added the enhancement New feature or request label Jan 11, 2025
@cofin cofin added the accepted Change or enhancement is accepted for development. label Jan 11, 2025
@cofin cofin self-assigned this Jan 11, 2025
@cofin
Copy link
Member

cofin commented Jan 11, 2025

@riyavsinha I think there is some value to have some filters for this. I'll try to get this added in an upcoming release.

Regarding your first question, you should be able to do something close to what your want The args of the repository will accept a StatementFilter or a ColumnElement[bool] (a where clause in SQLAlchemy)

Here's a basic example of mixing Statement Filters and ColumnExpressions into a single set of filters.

filters = [CollectionFilter(models.TeamModel.groups, country_code_groups), Team.is_deleted == False],
if not teams_service.can_view_all(current_user):
  filters.append(
     TeamModel.id.in_(
        select(TeamMemberModel.team_id).where(TeamMemberModel.user_id == current_user.id)
     )
 ) 

results, total = await teams_service.list_and_count(*filters)

You can also mix and match the kwargs and args. For the repository, all kwargs keywords are automatically converted to ColumnElement[bool] and applied to the statement:

results, total = await teams_service.list_and_count(*filters, is_active=True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Change or enhancement is accepted for development. enhancement New feature or request
Projects
None yet
2 participants