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

Handle In(key, []) and NotIn(key, []) correctly. #746

Merged
merged 4 commits into from
May 21, 2024

Conversation

danielballan
Copy link
Member

@danielballan danielballan commented May 21, 2024

This fixes an important bug in queries on the SQL-backed catalog. This bug is important particularly given queries' role in implementing authorization. The issue is that that In("color", ["red", "green"]) issues a query WHERE color="red" OR color="green" via the SQLAlchemy construction or_(...) where ... is a list comprehension of the possible values. But if said list is empty the effect is no restriction at all, this results in not additional filter on the results! When in fact the desired result is no rows returned: In("color", []) is a condition that no results satisfy.

This seems to be a recognized easy mistake to make; SQLAlchemy has deprecated the usage:

SADeprecationWarning: Invoking or_() without arguments is deprecated, and will be disallowed in a future release.   For an empty or_() construct, use 'or_(false(), *args)' or 'or_(False, *args)'.

This will be easier to review commit-by-commit:

  1. Tested added that exercises the bug, and fails
  2. Function refactored from conditional branching to dispatch, in a pattern that @padraic-shafer has taught me to reach for more often.
  3. Bug fixed, passing test

Checklist

  • Add a Changelog entry
  • Add the ticket number which this PR closes to the comment section

@padraic-shafer
Copy link
Contributor

Out of curiosity how should one query a key whose value is None?
...Is In(key, [None]) a valid usage, or must a Key have a non-None value?

@padraic-shafer padraic-shafer merged commit 6ca698c into bluesky:main May 21, 2024
9 checks passed
@danielballan
Copy link
Member Author

Any JSON-realizable value is valid, so In(key, [None]) would be a valid query. More testing on this with a range of types would be good for the future.

@danielballan danielballan deleted the fix-in-empty-list branch May 21, 2024 21:38
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

Successfully merging this pull request may close these issues.

2 participants