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

AND/OR criteria should simplify #56

Open
rohanpm opened this issue Sep 23, 2019 · 0 comments
Open

AND/OR criteria should simplify #56

rohanpm opened this issue Sep 23, 2019 · 0 comments

Comments

@rohanpm
Copy link
Member

rohanpm commented Sep 23, 2019

Summary

It would be nice if Criteria.and_ / Criteria.or_ would automatically simplify for various simple cases.

This could result in cleaner mongo queries to Pulp. It could potentially result in improved performance in some cases, (although perhaps mongo already does such simplification so that this improvement is negligible.)

Issue Type

  • Feature Request

Current behavior

Criteria always translates directly into mongo queries when querying to Pulp e.g.

Criteria.and_([clause1, clause2, ...]) => {'$and': [clause1, clause2, ...]}

Desired behavior

Criteria for and_ and or_ sometimes simplify according to at least these boolean simplification rules:

# single-element AND or OR is equivalent to just the subclause
Criteria.and_([clause1]) => clause1
Criteria.or_([clause1]) => clause1

# True can be removed from any AND or OR with no effect on the result
Criteria.and_([clause1, clause2, Criteria.true()]) => Criteria.and_([clause1, clause2])
Criteria.or_([clause1, clause2, Criteria.true()]) => Criteria.or_([clause1, clause2])

# And above two simplifications should work together
Criteria.and_([clause1, Criteria.true()]) => Criteria.and_([clause1]) => clause1

Use case

The use-case is to enable a natural way of building up elements of a criteria conditionally, without having to pay the cost of mongo queries becoming uglier than needed.

For example, it should be OK for a user of this library to write this:

# start with all repos
repo_crit = Criteria.true()

# restrict by URL if given
if url_filter:
  repo_crit = Criteria.and_([repo_crit, Criteria.with_field('relative_url', Matcher.regex(url_filter))])

# restrict by product if given
if product_filter:
  repo_crit = Criteria.and_([repo_crit, Criteria.with_field('eng_product_id', Matcher.in_(product_filter))])

# ...and so on
# When we query mongo, it should result in a simple query such as:

{"$and": [{"notes.eng_product": ...}, {"notes.relative_url": ...}]}

# and NOT with nested AND, like:
{"$and": [{}, {"$and": [{"notes.eng_product": ...}, {"$and": {"notes.relative_url": ...}}]}]}

Additional Information

Noticed the use-case in release-engineering/pubtools-pulp#13 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant