Fix XOR fallback to use parity semantics for chained Q() (odd-truth) [swev-id: django__django-16901]#529
Open
rowan-stein wants to merge 2 commits intodjango__django-16901from
Open
Fix XOR fallback to use parity semantics for chained Q() (odd-truth) [swev-id: django__django-16901]#529rowan-stein wants to merge 2 commits intodjango__django-16901from
rowan-stein wants to merge 2 commits intodjango__django-16901from
Conversation
…sum, not exactly-one. [swev-id: django__django-16901]
…onditions. [swev-id: django__django-16901]
noa-lucent
approved these changes
Dec 24, 2025
noa-lucent
left a comment
There was a problem hiding this comment.
Parity fallback looks good. Thanks for adding regression coverage.
Collaborator
Author
|
Validation summary:
PR remains unmerged as requested. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On DBs without native logical XOR (e.g., PostgreSQL), Django's WhereNode fallback for chained XOR (Q(...) ^ Q(...) ^ ...) incorrectly enforced 'exactly one' semantics. Correct XOR semantics are parity: true iff an odd number of operands are true. This PR updates the fallback to compute parity via modulo of the sum of CASE WHEN predicates.
Linked Issue: #528
Task reference: swev-id: django__django-16901
Change details
(a OR b OR c ...) AND (a + b + c + ...) == 1
to
(a + b + c + ...) % 2 == 1
using Django's expression system (CASE + modulo operator). This ensures associative/parity semantics for any number of XOR operands.
Reproduction steps
Using the existing xor_lookups test app (SQLite backend exercises the fallback path as supports_logical_xor=False for non-MySQL):
Create and select a single target Number:
target = Number.objects.create(num=3)
q = Q(pk=target.pk)
Evaluate chained XORs:
Number.objects.filter(q).count() # 1
Number.objects.filter(q ^ q).count() # 0
Number.objects.filter(q ^ q ^ q).count() # Expected 1 (odd parity)
Number.objects.filter(q ^ q ^ q ^ q).count()# Expected 0
Number.objects.filter(q ^ q ^ q ^ q ^ q).count() # Expected 1
Observed failure (before this fix)
Running the new test against the previous fallback yields the following failure:
This demonstrates the incorrect 'exactly one' fallback for 3 operands; parity should return one match.
After fix (local validation)
Notes