Bitwise operations in QuerySets?

866 views
Skip to first unread message

George Vilches

unread,
Oct 30, 2007, 5:03:53 PM10/30/07
to django-d...@googlegroups.com
What I want to do: Assume I have an integer column in a table that
contains a value that I want perform a bit operation on. I would like
to able to make a QuerySet that generates a query similar to this:
"SELECT * FROM table WHERE column & 4;". (example is MySQL-friendly).

I've looked around both in the current DB layer and in
Queryset-refactor, but it's possible I've missed something. If this
already exists, then give me the shaming I deserve. Otherwise...


Would anyone object to it being just another __ operation? For
instance, Poll.objects.filter(column__bitand=4). Is there a preference
whether this should be __and or __bitand?

I don't know if this makes sense for | (bitwise-OR), since you would
need to be able to specify something more like this:

"SELECT * FROM table WHERE column | 4 > 12;".
"SELECT * FROM table WHERE column | 4 = 12;".

Which may not be appropriate for a simple queryset operator in Django.
However, it would be cool if there was some way to support that with a
more complicated Q() or the like, but I could see it being out of scope
of Django, so ignore this part if so.

Bitwise-XOR, I don't know, some specific usages maybe.

I've already verified that all the DBs Django supports have some
mechanism for doing the basic bitwise operations.[1][2][3][4][5]

I don't mind doing this only against the qs-rf branch, and I can build
the tests and patch, but wanted to get the community opinion first. I'm
+1 on & (bitwise-AND), -0 on | (bitwise-OR, pending someone giving a
neat example), and +0 on bitwise-XOR/the rest, but would like to find
some way to be +1 on all of them.

Thoughts?

Thanks,
George

[1] SQLite: http://www.sqlite.org/changes.html
[2] MySQL: http://dev.mysql.com/doc/refman/5.0/en/bit-functions.html
[3] MSSQL: http://www.functionx.com/sqlserver/Lesson03.htm
[4] PostgreSQL:
http://www.postgresql.org/docs/8.1/interactive/functions-math.html
[5] Oracle: http://www.jlcomp.demon.co.uk/faq/bitwise.html

Malcolm Tredinnick

unread,
Oct 30, 2007, 10:14:09 PM10/30/07
to django-d...@googlegroups.com
On Tue, 2007-10-30 at 17:03 -0400, George Vilches wrote:
> What I want to do: Assume I have an integer column in a table that
> contains a value that I want perform a bit operation on. I would like
> to able to make a QuerySet that generates a query similar to this:
> "SELECT * FROM table WHERE column & 4;". (example is MySQL-friendly).
>
> I've looked around both in the current DB layer and in
> Queryset-refactor, but it's possible I've missed something. If this
> already exists, then give me the shaming I deserve. Otherwise...
>
>
> Would anyone object to it being just another __ operation? For
> instance, Poll.objects.filter(column__bitand=4). Is there a preference
> whether this should be __and or __bitand?

Yeah, I'd object. I don't see it as common enough to warrant inclusion.
You can achieve the same functionality either with extra(where=...) or a
custom Q-like object, so I'm inclined to leave it out of core.

By the way, in the queryset-refactor Q-like objects will have access to
the full query class, which includes the model, so you'll be able to
write something like Bit(foo__and=4) and when it comes time to produce
the SQL, your Bit class will be able to work out the right column name
for model field "foo".

Regards,
Malcolm

--
Quantum mechanics: the dreams stuff is made of.
http://www.pointy-stick.com/blog/

Charlie Hayes

unread,
Mar 16, 2015, 4:25:47 PM3/16/15
to django-d...@googlegroups.com, mal...@pointy-stick.com
How do you evaluate if something is common enough to warrant inclusion?

-Charlie

Carl Meyer

unread,
Mar 16, 2015, 4:43:03 PM3/16/15
to django-d...@googlegroups.com
Hi Charlie,

On 03/16/2015 02:13 PM, Charlie Hayes wrote:
> How do you evaluate if something is common enough to warrant inclusion?

Frequency of people asking "how do I do this?" in django-users or IRC,
popularity of third-party libraries solving the problem, etc. It's not
exactly scientific, but it's not a wild guess either.

If you're particularly interested in bitwise lookups, Django 1.7 has a
public API for registering custom lookups, making this trivial to add
yourself if you need it:
https://docs.djangoproject.com/en/1.7/howto/custom-lookups/

I'd guess there's probably already an app or two out there that does
exactly this, though I haven't needed it so I'm not familiar.

Carl
> http://www.pointy-stick.com/blog/ <http://www.pointy-stick.com/blog/>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-develop...@googlegroups.com
> <mailto:django-develop...@googlegroups.com>.
> To post to this group, send email to django-d...@googlegroups.com
> <mailto:django-d...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-developers.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/780c8a19-4b24-4adb-ad2a-964cfb4acb9e%40googlegroups.com
> <https://groups.google.com/d/msgid/django-developers/780c8a19-4b24-4adb-ad2a-964cfb4acb9e%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

signature.asc
Reply all
Reply to author
Forward
0 new messages