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

Fixed #29865 -- Added logical XOR support for Q() and QuerySet(). #14480

Merged
merged 1 commit into from Mar 4, 2022

Conversation

rheard
Copy link
Contributor

@rheard rheard commented Jun 2, 2021

Add logical XOR support to the ORM. MySQL will use the logical XOR, while other backends will convert

@rheard rheard marked this pull request as draft June 2, 2021 20:24
@rheard rheard marked this pull request as ready for review June 2, 2021 20:34
Copy link
Member

@ngnpope ngnpope left a comment

Choose a reason for hiding this comment

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

Here's a very quick pass. I've not looked at the tests yet.

django/db/backends/oracle/features.py Outdated Show resolved Hide resolved
django/db/backends/postgresql/features.py Outdated Show resolved Hide resolved
django/db/backends/sqlite3/features.py Outdated Show resolved Hide resolved
django/db/models/expressions.py Outdated Show resolved Hide resolved
django/db/models/expressions.py Show resolved Hide resolved
django/db/models/sql/where.py Outdated Show resolved Hide resolved
@rheard rheard force-pushed the xor_operator branch 3 times, most recently from 77c86b6 to 271e0cf Compare June 2, 2021 22:08
docs/releases/4.0.txt Outdated Show resolved Hide resolved
@rheard rheard force-pushed the xor_operator branch 3 times, most recently from 058a90f to 0fd449f Compare June 4, 2021 19:22
@rheard rheard changed the title Implement #29865 Implement #29865 - Add Logical XOR support for Q/QuerySet on MySQL Jun 4, 2021
@rheard rheard changed the title Implement #29865 - Add Logical XOR support for Q/QuerySet on MySQL Refs #29865 - Add Logical XOR support for Q/QuerySet on MySQL Jun 4, 2021
@rheard rheard changed the title Refs #29865 - Add Logical XOR support for Q/QuerySet on MySQL Refs #29865 - Add Logical XOR support for Q/QuerySet Jun 7, 2021
@rheard rheard requested a review from ngnpope June 10, 2021 03:02
Copy link
Member

@ngnpope ngnpope left a comment

Choose a reason for hiding this comment

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

Thanks for the updates.

Please update some of the existing tests.

Add the following to the end of tests in QTests.test_combine_empty_copy:

            base_q ^ Q(),
            Q() ^ base_q,

Add the following to the end of QTests.test_combine_not_q_object:

        with self.assertRaisesMessage(TypeError, str(obj)):
            q ^ obj

Add the following to the end of tests in QTests.test_combine_negated_boolean_expression:

            Q() ^ ~Exists(tagged),

It seems as though we'd want to add to QuerySetBitwiseOperationTests too to test the QuerySet behavior?

Also, I don't think that we need to create tests/xor_lookups. These tests should fit in around QuerySet-related tests for & and |.

I've marked this as patch needs improvement on ticket-29865. Please remove that flag when you're ready for another round of review. Thanks.

django/db/models/sql/where.py Outdated Show resolved Hide resolved
docs/ref/models/querysets.txt Show resolved Hide resolved
docs/ref/models/querysets.txt Show resolved Hide resolved
docs/releases/4.0.txt Outdated Show resolved Hide resolved
@rheard rheard force-pushed the xor_operator branch 2 times, most recently from bf29100 to 5e11b75 Compare July 2, 2021 20:13
@rheard
Copy link
Contributor Author

rheard commented Jul 2, 2021

Thanks for the updates.

Please update some of the existing tests.

Add the following to the end of tests in QTests.test_combine_empty_copy:

            base_q ^ Q(),
            Q() ^ base_q,

Add the following to the end of QTests.test_combine_not_q_object:

        with self.assertRaisesMessage(TypeError, str(obj)):
            q ^ obj

Add the following to the end of tests in QTests.test_combine_negated_boolean_expression:

            Q() ^ ~Exists(tagged),

It seems as though we'd want to add to QuerySetBitwiseOperationTests too to test the QuerySet behavior?

Also, I don't think that we need to create tests/xor_lookups. These tests should fit in around QuerySet-related tests for & and |.

I've marked this as patch needs improvement on ticket-29865. Please remove that flag when you're ready for another round of review. Thanks.

I had some issues around rebasing and force pushing, a nightmare, I think it has been resolved.

I've added those tests in and I added tests to QuerySetBitwiseOperationTests that emulate the "or" tests there, I'm not sure if its how you imagined though.

The tests/xor_lookups seemed appropriate as they were mostly copies of the existing tests/or_lookups. I can merge them both into tests/or_lookups perhaps? I'm just trying imagine the most logical places for them

@ngnpope
Copy link
Member

ngnpope commented Jul 2, 2021

The tests/xor_lookups seemed appropriate as they were mostly copies of the existing tests/or_lookups. I can merge them both into tests/or_lookups perhaps? I'm just trying imagine the most logical places for them

My mistake. That'll teach me for making this point based on the non-existent test/and_lookups and missing that test/or_lookups existed! 😆

Copy link
Member

@ngnpope ngnpope left a comment

Choose a reason for hiding this comment

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

I've suggested a few more tweaks in the documentation.

Please can you change the commit message and PR title to the following:

Fixed #29865 -- Added logical XOR support for Q() and QuerySet().

I've also noticed that the ticket was triaged and "Someday/Maybe" and not "Accepted". I'm sorry I didn't notice this before. However, based on the fact that we have what seems to be a working implementation here I'll change it to "Accepted". This will put it on the review queue for the fellows to follow up on.

docs/ref/models/querysets.txt Outdated Show resolved Hide resolved
docs/ref/models/querysets.txt Show resolved Hide resolved
docs/releases/4.0.txt Outdated Show resolved Hide resolved
@ngnpope
Copy link
Member

ngnpope commented Jul 14, 2021

buildbot, test on oracle.

@rheard rheard changed the title Refs #29865 - Add Logical XOR support for Q/QuerySet Fixed #29865 -- Added logical XOR support for Q() and QuerySet(). Jul 14, 2021
@rheard rheard requested a review from ngnpope August 4, 2021 22:21
Copy link
Member

@felixxm felixxm left a comment

Choose a reason for hiding this comment

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

@rheard Thanks for this patch 👍

docs/ref/models/querysets.txt Outdated Show resolved Hide resolved
django/db/models/sql/where.py Outdated Show resolved Hide resolved
docs/ref/models/querysets.txt Outdated Show resolved Hide resolved
docs/ref/models/querysets.txt Outdated Show resolved Hide resolved
docs/ref/models/querysets.txt Outdated Show resolved Hide resolved
docs/releases/4.0.txt Outdated Show resolved Hide resolved
tests/queries/tests.py Outdated Show resolved Hide resolved
tests/queries/tests.py Outdated Show resolved Hide resolved
tests/xor_lookups/models.py Outdated Show resolved Hide resolved
tests/xor_lookups/tests.py Show resolved Hide resolved
@rheard rheard force-pushed the xor_operator branch 6 times, most recently from 4659059 to dadea92 Compare September 24, 2021 00:07
django/db/models/sql/where.py Outdated Show resolved Hide resolved
docs/releases/4.1.txt Show resolved Hide resolved
tests/queries/tests.py Outdated Show resolved Hide resolved
@felixxm
Copy link
Member

felixxm commented Sep 24, 2021

@rheard Thanks for updates 👍

@rheard rheard force-pushed the xor_operator branch 2 times, most recently from 317e173 to dcd3ed0 Compare September 24, 2021 21:39
Copy link
Member

@felixxm felixxm left a comment

Choose a reason for hiding this comment

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

@rheard I pushed small edits to tests and docs. Unfortunately, I found an issue in chaining multiple conditions. Can you take a look?

Also, please don't rebase this patch and push edits in separate commits. I will squash everything before merging.

docs/ref/models/querysets.txt Outdated Show resolved Hide resolved
django/db/models/sql/where.py Outdated Show resolved Hide resolved
@felixxm felixxm force-pushed the xor_operator branch 2 times, most recently from d9fac68 to 05942a5 Compare September 30, 2021 07:35
@felixxm
Copy link
Member

felixxm commented Sep 30, 2021

It looks like the ordering was removed from the Meta though and broke some tests :/

I changed to assertSequenceEqual() and assertCountEqual() so Meta.ordering is not necessary anymore. Only the regression test (xor_lookups.tests.XorLookupsTests.test_filter_negated) for many conditions is broken on SQLite and PostgreSQL (as expected) .

@rheard
Copy link
Contributor Author

rheard commented Sep 30, 2021

It looks like the ordering was removed from the Meta though and broke some tests :/

I changed to assertSequenceEqual() and assertCountEqual() so Meta.ordering is not necessary anymore. Only the regression test (xor_lookups.tests.XorLookupsTests.test_filter_negated) for many conditions is broken on SQLite and PostgreSQL (as expected) .

Sorry, quickly realized that. Again, will implement my proposed solution when I have time. Thanks!

@rheard rheard requested a review from felixxm September 30, 2021 23:12
@felixxm
Copy link
Member

felixxm commented Mar 4, 2022

I rebased from the main branch, resolved conflicts, and applied black.

Copy link
Member

@felixxm felixxm left a comment

Choose a reason for hiding this comment

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

@rheard Thanks for all your efforts 👍 Welcome aboard ⛵

I pushed final edits and squashed commits.

@felixxm felixxm merged commit c6b4d62 into django:main Mar 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants