Skip to content

INTPYTHON-855 Create unique indexes in a way that can be used by the query planner#543

Draft
sophiayangDB wants to merge 11 commits into
mongodb:mainfrom
sophiayangDB:INTPYTHON-855
Draft

INTPYTHON-855 Create unique indexes in a way that can be used by the query planner#543
sophiayangDB wants to merge 11 commits into
mongodb:mainfrom
sophiayangDB:INTPYTHON-855

Conversation

@sophiayangDB
Copy link
Copy Markdown
Contributor

INTPYTHON-855

Changes in this PR

Before this change, exact lookups were generated as plain equality predicates in lookups.py. Since those queries did not include the type conditions required by partial unique indexes, MongoDB often chose a collection scan instead of using the index. This patch adds the corresponding type filters to exact lookups so the planner can select the intended partial unique index.

Test Plan

Added unit tests in tests/lookup_

Checklist

Checklist for Author

  • Did you update the changelog (if necessary)?
  • Is the intention of the code captured in relevant tests?
  • If there are new TODOs, has a related JIRA ticket been created?

Checklist for Reviewer

  • Does the title of the PR reference a JIRA Ticket?
  • Do you fully understand the implementation? (Would you be comfortable explaining how this code works to someone else?)
  • Have you checked for spelling & grammar errors?
  • Is all relevant documentation (README or docstring) updated?

Comment thread django_mongodb_backend/lookups.py Outdated
@timgraham timgraham changed the title INTPYTHON-855: Unique indexes are not being used by query planner INTPYTHON-855 Create unique indexes in a way that can be used by the query planner May 28, 2026
Comment thread django_mongodb_backend/constraints.py Outdated
Comment on lines +45 to +46
case _:
return {"$exists": True}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$exists matches if the field is null, which we don't want (that explains at least some of the test failures).

Consider this advice: "Null vs. Missing: $exists: true will return documents where a field is explicitly set to null. If you want documents where a field exists and is not null, combine it with $ne: null"

Comment thread django_mongodb_backend/constraints.py Outdated
Comment on lines +15 to +16
if db_type is None:
return {"$exists": True}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this branch used? Offhand, I can't think of why db_type would be None. That would denote a virtual field that don't exist in the database, and such fields wouldn't be indexed... (perhaps this is AI nonsense.)

Comment thread django_mongodb_backend/constraints.py Outdated
Comment on lines +41 to +42
if not field.null:
return
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conceptually, it makes sense to me that the partialFilterExpression isn't needed if the field can't contain null values. My concern is that we would also have to make AlterField modify any/drop any relevant indexes when a field changes from null to not-null (or vice versa).

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