[Django] #31396: Add support for bitwise XOR

11 views
Skip to first unread message

Django

unread,
Mar 23, 2020, 4:11:32 PM3/23/20
to django-...@googlegroups.com
#31396: Add support for bitwise XOR
-------------------------------------+-------------------------------------
Reporter: Hannes | Owner: Hannes Ljungberg
Ljungberg |
Type: New | Status: assigned
feature |
Component: Database | Version: master
layer (models, ORM) |
Severity: Normal | Keywords: bitwise xor
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
It would be nice to get support for performing bitwise XOR on expressions.
I'm aware of #29865 but from my understanding this ticket is about logical
`XOR` and not bitwise `XOR`. Let me know if that ticket should be used
instead.

My proposal is to implement this just like the other bitwise-functions,
i.e `Combinable.bitxor(other)`.

The connector defined by `Combinable` could be `#` , this is what
PostgresSQL use, see https://www.postgresql.org/docs/current/functions-
bitstring.html. I guess a more natural choice would’ve been `^` but this
connector is already in use by `Combinable.POW`:
https://github.com/django/django/blob/291539a85c8461456ab728fe6820a86de54294b6/django/db/models/expressions.py#L42

Bitwise XOR is supported by all backends except sqlite. It could be
implemented on sqlite as something like `((%(lhs)s | %(rhs)s) - (%(lhs)s &
%(rhs)s))` but the way `CombinedExpression.as_sql` is implemented makes it
a bit tricky to repeat the right-hand side binding:
`((“left_hand_side_expr" | %s) - ("left_hand_side_expr" & %s)) % (rhs,)` .

Maybe it could be OK to set
`django.db.backends.sqlite3.features.DatabaseFeatures.supports_bitwise_xor
= False`

I have a proof-of-concept ready if this would be accepted.

--
Ticket URL: <https://code.djangoproject.com/ticket/31396>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 24, 2020, 1:17:10 AM3/24/20
to django-...@googlegroups.com
#31396: Add support for bitwise XOR to expressions.
-------------------------------------+-------------------------------------
Reporter: Hannes Ljungberg | Owner: Hannes
| Ljungberg
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: bitwise xor | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* stage: Unreviewed => Accepted


Comment:

Replying to [ticket:31396 Hannes Ljungberg]:

> Bitwise XOR is supported by all backends except sqlite. It could be
implemented on sqlite as something like `((%(lhs)s | %(rhs)s) - (%(lhs)s &
%(rhs)s))` but the way `CombinedExpression.as_sql` is implemented makes it
a bit tricky to repeat the right-hand side binding:
> `((“left_hand_side_expr" | %s) - ("left_hand_side_expr" & %s)) % (rhs,)`
.

You can override a `bitxor` operator (e.g. `#`) in
`DatabaseOperations.combine_expression()` for each backend.

--
Ticket URL: <https://code.djangoproject.com/ticket/31396#comment:1>

Django

unread,
Mar 24, 2020, 4:17:25 AM3/24/20
to django-...@googlegroups.com
#31396: Add support for bitwise XOR to expressions.
-------------------------------------+-------------------------------------
Reporter: Hannes Ljungberg | Owner: Hannes
| Ljungberg
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: bitwise xor | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hannes Ljungberg):

* has_patch: 0 => 1


Comment:

PR: https://github.com/django/django/pull/12617

--
Ticket URL: <https://code.djangoproject.com/ticket/31396#comment:2>

Django

unread,
Mar 25, 2020, 5:18:19 AM3/25/20
to django-...@googlegroups.com
#31396: Add support for bitwise XOR to expressions.
-------------------------------------+-------------------------------------
Reporter: Hannes Ljungberg | Owner: Hannes
| Ljungberg
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: bitwise xor | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/31396#comment:3>

Django

unread,
Mar 25, 2020, 5:38:57 AM3/25/20
to django-...@googlegroups.com
#31396: Add support for bitwise XOR to expressions.
-------------------------------------+-------------------------------------
Reporter: Hannes Ljungberg | Owner: Hannes
| Ljungberg
Type: New feature | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: bitwise xor | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"f3da09df0f4147223ab76a00a841586ccf11005d" f3da09df]:
{{{
#!CommitTicketReference repository=""
revision="f3da09df0f4147223ab76a00a841586ccf11005d"
Fixed #31396 -- Added binary XOR operator to F expressions.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31396#comment:4>

Reply all
Reply to author
Forward
0 new messages