Opened 2 years ago

Closed 2 years ago

Last modified 18 months ago

#33374 closed Bug (fixed)

ExpressionWrapper for ~Q(pk__in=[]) crashes.

Reported by: Stefan Brand Owned by: David Wobrock
Component: Database layer (models, ORM) Version: 3.1
Severity: Normal Keywords:
Cc: David Wobrock Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Stefan Brand)

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=[]))

print(queryset.annotate(foo=ExpressionWrapper(Q(pk__in=[]), output_field=BooleanField())).values("foo").query)
SELECT 0 AS "foo" FROM "table"

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

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

Change History (10)

comment:1 by Stefan Brand, 2 years ago

Description: modified (diff)

comment:2 by Stefan Brand, 2 years ago

Description: modified (diff)

comment:3 by Mariusz Felisiak, 2 years ago

Component: UncategorizedDatabase layer (models, ORM)
Summary: ExpressionWrapper for ~Q(pk__in=[]) brokenExpressionWrapper for ~Q(pk__in=[]) crashes.
Triage Stage: UnreviewedAccepted

Good catch!

>>> 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

comment:4 by David Wobrock, 2 years ago

Cc: David Wobrock added
Has patch: set
Owner: changed from nobody to David Wobrock
Status: newassigned

Hi all,

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

comment:5 by Mariusz Felisiak, 2 years ago

Patch needs improvement: set

comment:6 by David Wobrock, 2 years ago

Patch needs improvement: unset

comment:7 by Mariusz Felisiak, 2 years ago

Triage Stage: AcceptedReady for checkin

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 2 years ago

Resolution: fixed
Status: assignedclosed

In 72b23c04:

Fixed #33374 -- Fixed ExpressionWrapper annotations with full queryset.

comment:9 by Mariusz Felisiak <felisiak.mariusz@…>, 18 months ago

In 4b702c8:

Refs #33374 -- Added tests for multi-table fast-deletion with filters that match everything.

comment:10 by Mariusz Felisiak <felisiak.mariusz@…>, 18 months ago

In 76e37513:

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.

Note: See TracTickets for help on using tickets.
Back to Top