[Django] #33435: Since django 3.0 Subquery.as_sql() generates invalid SQL

27 views
Skip to first unread message

Django

unread,
Jan 13, 2022, 1:46:17 AM1/13/22
to django-...@googlegroups.com
#33435: Since django 3.0 Subquery.as_sql() generates invalid SQL
-------------------------------------+-------------------------------------
Reporter: M1ha Shvn | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: dev
layer (models, ORM) |
Severity: Normal | Keywords: subquery, as_sql
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Since
[https://github.com/django/django/commit/3a505c70e7b228bf1212c067a8f38271ca86ce09
this commit] `Subquery.as_sql(...)` method returns incorrect SQL removing
first and last symbols instead of absent breakets:


{{{
from django.db import connection
from apps.models import App

q = Subquery(App.objects.all())

print(str(q.query))

# Output SQL is valid:
# 'SELECT "apps_app"."id", "apps_app"."name" FROM "apps_app"'

print(q.as_sql(q.query.get_compiler('default'), connection))

# Outptut SQL is invalid (no S letter at the beggining and " symbol at the
end):
# ('(ELECT "apps_app"."id", "apps_app"."name" FROM "apps_app)', ())
}}}

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

Django

unread,
Jan 13, 2022, 1:59:55 AM1/13/22
to django-...@googlegroups.com
#33435: Since django 3.0 Subquery.as_sql() generates invalid SQL
-------------------------------------+-------------------------------------
Reporter: M1ha Shvn | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:

Keywords: subquery, as_sql | 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 M1ha Shvn:

Old description:

> Since
> [https://github.com/django/django/commit/3a505c70e7b228bf1212c067a8f38271ca86ce09
> this commit] `Subquery.as_sql(...)` method returns incorrect SQL removing
> first and last symbols instead of absent breakets:
>

> {{{
> from django.db import connection
> from apps.models import App
>
> q = Subquery(App.objects.all())
>
> print(str(q.query))
>
> # Output SQL is valid:
> # 'SELECT "apps_app"."id", "apps_app"."name" FROM "apps_app"'
>
> print(q.as_sql(q.query.get_compiler('default'), connection))
>
> # Outptut SQL is invalid (no S letter at the beggining and " symbol at
> the end):
> # ('(ELECT "apps_app"."id", "apps_app"."name" FROM "apps_app)', ())
> }}}

New description:

Since
[https://github.com/django/django/commit/3a505c70e7b228bf1212c067a8f38271ca86ce09
this commit] `Subquery.as_sql(...)` method returns incorrect SQL removing

first and last symbols instead of absent breakets. Adding
`Subquery().query.subquery = True` attribute fixes the problem. From my
point of view, it should be set in `Subquery` constructor.

{{{
from django.db import connection
from apps.models import App

q = Subquery(App.objects.all())

print(str(q.query))

# Output SQL is valid:
# 'SELECT "apps_app"."id", "apps_app"."name" FROM "apps_app"'

print(q.as_sql(q.query.get_compiler('default'), connection))

# Outptut SQL is invalid (no S letter at the beggining and " symbol at the
end):
# ('(ELECT "apps_app"."id", "apps_app"."name" FROM "apps_app)', ())

q.query.subquery = True
print(q.as_sql(q.query.get_compiler('default'), connection))

# Outputs correct result
('(SELECT "apps_app"."id", "apps_app"."name" FROM "apps_app")', ())
}}}

--

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

Django

unread,
Jan 13, 2022, 3:24:54 AM1/13/22
to django-...@googlegroups.com
#33435: Subquery.as_sql() generates invalid SQL.

-------------------------------------+-------------------------------------
Reporter: M1ha Shvn | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: subquery, as_sql | 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):

* cc: Simon Charette (added)
* type: Bug => Cleanup/optimization
* stage: Unreviewed => Accepted


Comment:

Sounds reasonable.

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

Django

unread,
Jan 13, 2022, 10:28:58 AM1/13/22
to django-...@googlegroups.com
#33435: Subquery.as_sql() generates invalid SQL.
-------------------------------------+-------------------------------------
Reporter: M1ha Shvn | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: subquery, as_sql | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

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

Comment (by Simon Charette):

Sounds reasonable to me as well, I'd only suggest we `.clone()` the
`query` before altering though.

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

Django

unread,
Jan 14, 2022, 6:47:09 PM1/14/22
to django-...@googlegroups.com
#33435: Subquery.as_sql() generates invalid SQL.
-------------------------------------+-------------------------------------
Reporter: M1ha Shvn | Owner: Mohamed
Type: | Nabil Rady
Cleanup/optimization | Status: assigned

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

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mohamed Nabil Rady):

* owner: nobody => Mohamed Nabil Rady
* status: new => assigned
* has_patch: 0 => 1
* easy: 0 => 1


Comment:

Pull request: https://github.com/django/django/pull/15320.

Ran back-end tests and they went fine.

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

Django

unread,
Jan 15, 2022, 7:45:20 AM1/15/22
to django-...@googlegroups.com
#33435: Subquery.as_sql() generates invalid SQL.
-------------------------------------+-------------------------------------

Reporter: M1ha Shvn | Owner: Mohamed
Type: | Nabil Rady
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: subquery, as_sql | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

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

* needs_tests: 0 => 1


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

Django

unread,
Jan 17, 2022, 3:05:00 AM1/17/22
to django-...@googlegroups.com
#33435: Subquery.as_sql() generates invalid SQL.
-------------------------------------+-------------------------------------

Reporter: M1ha Shvn | Owner: Mohamed
Type: | Nabil Rady
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: subquery, as_sql | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

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

* needs_tests: 1 => 0
* stage: Accepted => Ready for checkin


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

Django

unread,
Jan 17, 2022, 3:52:30 AM1/17/22
to django-...@googlegroups.com
#33435: Subquery.as_sql() generates invalid SQL.
-------------------------------------+-------------------------------------

Reporter: M1ha Shvn | Owner: Mohamed
Type: | Nabil Rady
Cleanup/optimization | Status: closed

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

Keywords: subquery, as_sql | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

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

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


Comment:

In [changeset:"f37face331f21cb8af70fc4ec101ec7b6be1f63e" f37face3]:
{{{
#!CommitTicketReference repository=""
revision="f37face331f21cb8af70fc4ec101ec7b6be1f63e"
Fixed #33435 -- Fixed invalid SQL generatered by Subquery.as_sql().
}}}

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

Reply all
Reply to author
Forward
0 new messages