[Django] #33374: ExpressionWrapper for ~Q(pk__in=[]) broken

7 views
Skip to first unread message

Django

unread,
Dec 17, 2021, 4:33:06 AM12/17/21
to django-...@googlegroups.com
#33374: ExpressionWrapper for ~Q(pk__in=[]) broken
-----------------------------------------+------------------------
Reporter: StefanBrand | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 3.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
== Problem Description

I'm reducing some Q objects (similar to what is described in ticket:32554.
Everything is fine for the case where the result is
`ExpressionWrapper(Q(pk__in=[]))`. However, when I reduce to
`ExpressionWrapper(~Q(pk__in=[]))` the query breaks.

== Symptoms

=== Working for `ExpressionWrapper(Q(pk__in=[]))`
{{{#!python
print(queryset.annotate(foo=ExpressionWrapper(Q(pk__in=[]),
output_field=BooleanField())).values("foo").query)
}}}

{{{#!sql
SELECT 0 AS "foo" FROM "agri_data_declaration"
}}}

=== Not working for `ExpressionWrapper(~Q(pk__in=[]))`

{{{#!python
print(declarations.annotate(foo=ExpressionWrapper(~Q(pk__in=[]),
output_field=BooleanField())).values("foo").query)
}}}
{{{#!sql
SELECT AS "foo" FROM "agri_data_declaration"
}}}

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

Django

unread,
Dec 17, 2021, 4:33:25 AM12/17/21
to django-...@googlegroups.com
#33374: ExpressionWrapper for ~Q(pk__in=[]) broken
-------------------------------+--------------------------------------

Reporter: StefanBrand | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 3.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by StefanBrand:

Old description:

> == Problem Description
>
> I'm reducing some Q objects (similar to what is described in
> ticket:32554. Everything is fine for the case where the result is
> `ExpressionWrapper(Q(pk__in=[]))`. However, when I reduce to
> `ExpressionWrapper(~Q(pk__in=[]))` the query breaks.
>
> == Symptoms
>
> === Working for `ExpressionWrapper(Q(pk__in=[]))`
> {{{#!python
> print(queryset.annotate(foo=ExpressionWrapper(Q(pk__in=[]),
> output_field=BooleanField())).values("foo").query)
> }}}
>
> {{{#!sql
> SELECT 0 AS "foo" FROM "agri_data_declaration"
> }}}
>
> === Not working for `ExpressionWrapper(~Q(pk__in=[]))`
>
> {{{#!python
> print(declarations.annotate(foo=ExpressionWrapper(~Q(pk__in=[]),
> output_field=BooleanField())).values("foo").query)
> }}}
> {{{#!sql
> SELECT AS "foo" FROM "agri_data_declaration"
> }}}

New description:

== Problem Description

I'm reducing some Q objects (similar to what is described in ticket:32554.
Everything is fine for the case where the result is
`ExpressionWrapper(Q(pk__in=[]))`. However, when I reduce to
`ExpressionWrapper(~Q(pk__in=[]))` the query breaks.

== Symptoms

=== Working for `ExpressionWrapper(Q(pk__in=[]))`
{{{#!python
print(queryset.annotate(foo=ExpressionWrapper(Q(pk__in=[]),
output_field=BooleanField())).values("foo").query)
}}}

{{{#!sql
SELECT 0 AS "foo" FROM "agri_data_declaration"
}}}

=== Not working for `ExpressionWrapper(~Q(pk__in=[]))`

{{{#!python
print(queryset.annotate(foo=ExpressionWrapper(~Q(pk__in=[]),


output_field=BooleanField())).values("foo").query)
}}}
{{{#!sql
SELECT AS "foo" FROM "agri_data_declaration"
}}}

--

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

Django

unread,
Dec 17, 2021, 4:46:05 AM12/17/21
to django-...@googlegroups.com
#33374: ExpressionWrapper for ~Q(pk__in=[]) broken
-------------------------------+--------------------------------------
Reporter: Stefan Brand | Owner: nobody

Type: Bug | Status: new
Component: Uncategorized | Version: 3.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by Stefan Brand:

Old description:

> == Problem Description
>
> I'm reducing some Q objects (similar to what is described in
> ticket:32554. Everything is fine for the case where the result is
> `ExpressionWrapper(Q(pk__in=[]))`. However, when I reduce to
> `ExpressionWrapper(~Q(pk__in=[]))` the query breaks.
>
> == Symptoms
>
> === Working for `ExpressionWrapper(Q(pk__in=[]))`
> {{{#!python
> print(queryset.annotate(foo=ExpressionWrapper(Q(pk__in=[]),
> output_field=BooleanField())).values("foo").query)
> }}}
>
> {{{#!sql
> SELECT 0 AS "foo" FROM "agri_data_declaration"
> }}}
>
> === Not working for `ExpressionWrapper(~Q(pk__in=[]))`
>
> {{{#!python

> print(queryset.annotate(foo=ExpressionWrapper(~Q(pk__in=[]),


> output_field=BooleanField())).values("foo").query)
> }}}
> {{{#!sql
> SELECT AS "foo" FROM "agri_data_declaration"
> }}}

New description:

== Problem Description

I'm reducing some Q objects (similar to what is described in ticket:32554.
Everything is fine for the case where the result is
`ExpressionWrapper(Q(pk__in=[]))`. However, when I reduce to
`ExpressionWrapper(~Q(pk__in=[]))` the query breaks.

== Symptoms

=== Working for `ExpressionWrapper(Q(pk__in=[]))`
{{{#!python
print(queryset.annotate(foo=ExpressionWrapper(Q(pk__in=[]),
output_field=BooleanField())).values("foo").query)
}}}

{{{#!sql
SELECT 0 AS "foo" FROM "table"
}}}

=== Not working for `ExpressionWrapper(~Q(pk__in=[]))`

{{{#!python
print(queryset.annotate(foo=ExpressionWrapper(~Q(pk__in=[]),
output_field=BooleanField())).values("foo").query)
}}}
{{{#!sql
SELECT AS "foo" FROM "table"
}}}

--

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

Django

unread,
Dec 17, 2021, 4:57:42 AM12/17/21
to django-...@googlegroups.com
#33374: ExpressionWrapper for ~Q(pk__in=[]) crashes.
-------------------------------------+-------------------------------------
Reporter: Stefan Brand | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* component: Uncategorized => Database layer (models, ORM)
* stage: Unreviewed => Accepted


Comment:

Good catch!

{{{#!python
>>> books =
Book.objects.annotate(selected=ExpressionWrapper(~Q(pk__in=[]),
output_field=BooleanField())).values('selected')
>>> list(books)
Traceback (most recent call last):
File "/django/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/django/django/db/backends/sqlite3/base.py", line 420, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: near "AS": syntax error
}}}

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

Django

unread,
Dec 19, 2021, 6:00:13 AM12/19/21
to django-...@googlegroups.com
#33374: ExpressionWrapper for ~Q(pk__in=[]) crashes.
-------------------------------------+-------------------------------------
Reporter: Stefan Brand | Owner: David
| Wobrock
Type: Bug | Status: assigned

Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

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

* cc: David Wobrock (added)
* owner: nobody => David Wobrock
* has_patch: 0 => 1
* status: new => assigned


Comment:

Hi all,

Opened a PR to tackle this bug https://github.com/django/django/pull/15213

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

Django

unread,
Dec 20, 2021, 7:12:22 AM12/20/21
to django-...@googlegroups.com
#33374: ExpressionWrapper for ~Q(pk__in=[]) crashes.
-------------------------------------+-------------------------------------
Reporter: Stefan Brand | Owner: David
| Wobrock
Type: Bug | Status: assigned
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/33374#comment:5>

Django

unread,
Dec 20, 2021, 7:45:34 AM12/20/21
to django-...@googlegroups.com
#33374: ExpressionWrapper for ~Q(pk__in=[]) crashes.
-------------------------------------+-------------------------------------
Reporter: Stefan Brand | Owner: David
| Wobrock
Type: Bug | Status: assigned
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

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

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/33374#comment:6>

Django

unread,
Dec 20, 2021, 2:15:45 PM12/20/21
to django-...@googlegroups.com
#33374: ExpressionWrapper for ~Q(pk__in=[]) crashes.
-------------------------------------+-------------------------------------
Reporter: Stefan Brand | Owner: David
| Wobrock
Type: Bug | Status: assigned
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | 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):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/33374#comment:7>

Django

unread,
Dec 21, 2021, 1:11:18 AM12/21/21
to django-...@googlegroups.com
#33374: ExpressionWrapper for ~Q(pk__in=[]) crashes.
-------------------------------------+-------------------------------------
Reporter: Stefan Brand | Owner: David
| Wobrock
Type: Bug | Status: closed

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

Keywords: | 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:"72b23c04d806adc8522fa9d10132e5c1d1011d5e" 72b23c04]:
{{{
#!CommitTicketReference repository=""
revision="72b23c04d806adc8522fa9d10132e5c1d1011d5e"
Fixed #33374 -- Fixed ExpressionWrapper annotations with full queryset.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33374#comment:8>

Django

unread,
Nov 7, 2022, 3:18:28 PM11/7/22
to django-...@googlegroups.com
#33374: ExpressionWrapper for ~Q(pk__in=[]) crashes.
-------------------------------------+-------------------------------------
Reporter: Stefan Brand | Owner: David
| Wobrock
Type: Bug | Status: closed
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"76e37513e22f4d9a01c7f15eee36fe44388e6670" 76e37513]:
{{{
#!CommitTicketReference repository=""
revision="76e37513e22f4d9a01c7f15eee36fe44388e6670"
Refs #33374 -- Adjusted full match condition handling.

Adjusting WhereNode.as_sql() to raise an exception when encoutering a
full match just like with empty matches ensures that all case are
explicitly handled.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33374#comment:10>

Django

unread,
Nov 7, 2022, 3:18:28 PM11/7/22
to django-...@googlegroups.com
#33374: ExpressionWrapper for ~Q(pk__in=[]) crashes.
-------------------------------------+-------------------------------------
Reporter: Stefan Brand | Owner: David
| Wobrock
Type: Bug | Status: closed
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"4b702c832cd550fe682ef37a69e93866815b9123" 4b702c8]:
{{{
#!CommitTicketReference repository=""
revision="4b702c832cd550fe682ef37a69e93866815b9123"
Refs #33374 -- Added tests for multi-table fast-deletion with filters that
match everything.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33374#comment:9>

Reply all
Reply to author
Forward
0 new messages