[Django] #24991: Range types not properly converted to SQL in QuerySet.query.__str__()

33 views
Skip to first unread message

Django

unread,
Jun 17, 2015, 4:44:45 AM6/17/15
to django-...@googlegroups.com
#24991: Range types not properly converted to SQL in QuerySet.query.__str__()
----------------------------------+-----------------
Reporter: unklphil | Owner:
Type: Uncategorized | Status: new
Component: contrib.postgres | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+-----------------
The string representation of the `QuerySet.query` object, which is useful
in debugging, does not convert range types to their SQL equivalents,
instead it just displays the string representation of the range type.

{{{
>>> import datetime
>>> from django.utils.timezone import utc
>>> from psycopg.extras import NumericRange, DateRange, DateTimeTZRange
>>> from myapp.models import MyModel # model defintion is irrelevant

>>> print(MyModel.objects.filter(num_rng__overlap=NumericRange(1, 5)))
SELECT [...] FROM "myapp_mymodel" WHERE ("myapp_mymodel"."num_rng" &&
NumericRange(1, 5, '[)'))

>>>
print(MyModel.objects.filter(date_rng__overlap=DateRange(datetime.date(2015,
5, 1), datetime.date(2015, 6, 1))))
SELECT [...] FROM "myapp_mymodel" WHERE ("myapp_mymodel"."date_rng" &&
DateRange(datetime.date(2015, 5, 1), datetime.date(2015, 6, 1), '[)'))

>>>
print(MyModel.objects.filter(dt_rng__overlap=DateTimeTZRange(datetime.datetime(2015,
5, 1, tzinfo=utc), datetime.datetime(2015, 6, 1, tzinfo=utc))))
SELECT [...] FROM "myapp_mymodel" WHERE ("myapp_mymodel"."dt_rng" &&
DateTimeTZRange(datetime.datetime(2015, 5, 1, 0, 0, tzinfo=<UTC>),
datetime.datetime(2015, 6, 1, 0, 0, tzinfo=<UTC>), '[)'))
}}}

The expected output is `numrange(1.0, 5.0)`, `daterange('2015-05-01',
'2015-06-01')` and `tstzrange('2015-05-01 00:00:00+00:00', '2015-06-01
00:00:00+00:00')` respectively.

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

Django

unread,
Jun 17, 2015, 6:41:59 AM6/17/15
to django-...@googlegroups.com
#24991: Range types not properly converted to SQL in QuerySet.query.__str__()
----------------------------------+------------------------------------
Reporter: unklphil | Owner:
Type: Bug | Status: new
Component: contrib.postgres | Version: 1.8
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 timgraham):

* needs_better_patch: => 0
* stage: Unreviewed => Accepted
* type: Uncategorized => Bug
* needs_tests: => 0
* needs_docs: => 0


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

Django

unread,
Aug 6, 2015, 3:27:32 AM8/6/15
to django-...@googlegroups.com
#24991: Range types not properly converted to SQL in QuerySet.query.__str__()
----------------------------------+------------------------------------
Reporter: unklphil | Owner:
Type: Bug | Status: new
Component: contrib.postgres | Version: 1.8

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
----------------------------------+------------------------------------
Description changed by unklphil:

Old description:

> The string representation of the `QuerySet.query` object, which is useful
> in debugging, does not convert range types to their SQL equivalents,
> instead it just displays the string representation of the range type.
>
> {{{
> >>> import datetime
> >>> from django.utils.timezone import utc
> >>> from psycopg.extras import NumericRange, DateRange, DateTimeTZRange
> >>> from myapp.models import MyModel # model defintion is irrelevant
>
> >>> print(MyModel.objects.filter(num_rng__overlap=NumericRange(1, 5)))
> SELECT [...] FROM "myapp_mymodel" WHERE ("myapp_mymodel"."num_rng" &&
> NumericRange(1, 5, '[)'))
>
> >>>
> print(MyModel.objects.filter(date_rng__overlap=DateRange(datetime.date(2015,
> 5, 1), datetime.date(2015, 6, 1))))
> SELECT [...] FROM "myapp_mymodel" WHERE ("myapp_mymodel"."date_rng" &&
> DateRange(datetime.date(2015, 5, 1), datetime.date(2015, 6, 1), '[)'))
>
> >>>
> print(MyModel.objects.filter(dt_rng__overlap=DateTimeTZRange(datetime.datetime(2015,
> 5, 1, tzinfo=utc), datetime.datetime(2015, 6, 1, tzinfo=utc))))
> SELECT [...] FROM "myapp_mymodel" WHERE ("myapp_mymodel"."dt_rng" &&
> DateTimeTZRange(datetime.datetime(2015, 5, 1, 0, 0, tzinfo=<UTC>),
> datetime.datetime(2015, 6, 1, 0, 0, tzinfo=<UTC>), '[)'))
> }}}
>
> The expected output is `numrange(1.0, 5.0)`, `daterange('2015-05-01',
> '2015-06-01')` and `tstzrange('2015-05-01 00:00:00+00:00', '2015-06-01
> 00:00:00+00:00')` respectively.

New description:

The string representation of the `QuerySet.query` object, which is useful
in debugging, does not convert range types to their SQL equivalents,
instead it just displays the string representation of the range type.

{{{
>>> import datetime
>>> from django.utils.timezone import utc
>>> from psycopg.extras import NumericRange, DateRange, DateTimeTZRange
>>> from myapp.models import MyModel # model defintion is irrelevant

>>> print(MyModel.objects.filter(num_rng__overlap=NumericRange(1,
5)).query)


SELECT [...] FROM "myapp_mymodel" WHERE ("myapp_mymodel"."num_rng" &&
NumericRange(1, 5, '[)'))

>>>
print(MyModel.objects.filter(date_rng__overlap=DateRange(datetime.date(2015,
5, 1), datetime.date(2015, 6, 1))).query)


SELECT [...] FROM "myapp_mymodel" WHERE ("myapp_mymodel"."date_rng" &&
DateRange(datetime.date(2015, 5, 1), datetime.date(2015, 6, 1), '[)'))

>>>
print(MyModel.objects.filter(dt_rng__overlap=DateTimeTZRange(datetime.datetime(2015,
5, 1, tzinfo=utc), datetime.datetime(2015, 6, 1, tzinfo=utc))).query)


SELECT [...] FROM "myapp_mymodel" WHERE ("myapp_mymodel"."dt_rng" &&
DateTimeTZRange(datetime.datetime(2015, 5, 1, 0, 0, tzinfo=<UTC>),
datetime.datetime(2015, 6, 1, 0, 0, tzinfo=<UTC>), '[)'))
}}}

The expected output is `numrange(1.0, 5.0)`, `daterange('2015-05-01',
'2015-06-01')` and `tstzrange('2015-05-01 00:00:00+00:00', '2015-06-01
00:00:00+00:00')` respectively.

--

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

Django

unread,
Nov 7, 2015, 1:27:47 PM11/7/15
to django-...@googlegroups.com
#24991: Range types not properly converted to SQL in QuerySet.query.__str__()
----------------------------------+----------------------------------------
Reporter: unklphil | Owner: Stranger6667
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 1.8

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 Stranger6667):

* owner: => Stranger6667
* status: new => assigned


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

Django

unread,
Nov 7, 2015, 2:31:22 PM11/7/15
to django-...@googlegroups.com
#24991: Range types not properly converted to SQL in QuerySet.query.__str__()
----------------------------------+----------------------------------------
Reporter: unklphil | Owner: Stranger6667
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 1.8

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

Comment (by Stranger6667):

Based on `psycopg2.extensions.adapt` output it should be converted as:
`NumericRange(1, 5)` to `'[1,5)'`
`DateRange(datetime.date(2015, 5, 1), datetime.date(2015, 6, 1))` to
`daterange('2015-05-01'::date, '2015-06-01'::date, '[)')`
`DateTimeTZRange(datetime.datetime(2015, 5, 1, tzinfo=utc),
datetime.datetime(2015, 6, 1, tzinfo=utc))` to
`tstzrange('2015-05-01T00:00:00+00:00'::timestamptz,
'2015-06-01T00:00:00+00:00'::timestamptz, '[)')`

Please correct me if I'm wrong. If it is true, then I'll try to provide PR
for that.

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

Django

unread,
Nov 9, 2015, 3:24:10 AM11/9/15
to django-...@googlegroups.com
#24991: Range types not properly converted to SQL in QuerySet.query.__str__()
----------------------------------+----------------------------------------
Reporter: unklphil | Owner: Stranger6667
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 1.8

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

Comment (by unklphil):

Replying to [comment:4 Stranger6667]:


> Based on `psycopg2.extensions.adapt` output it should be converted as:
> `NumericRange(1, 5)` to `'[1,5)'`
> `DateRange(datetime.date(2015, 5, 1), datetime.date(2015, 6, 1))` to
`daterange('2015-05-01'::date, '2015-06-01'::date, '[)')`
> `DateTimeTZRange(datetime.datetime(2015, 5, 1, tzinfo=utc),
datetime.datetime(2015, 6, 1, tzinfo=utc))` to
`tstzrange('2015-05-01T00:00:00+00:00'::timestamptz,
'2015-06-01T00:00:00+00:00'::timestamptz, '[)')`
>
> Please correct me if I'm wrong. If it is true, then I'll try to provide
PR for that.

That looks right. The output should ideally be exactly what gets executed
in the database, to make debugging easier.

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

Django

unread,
Nov 13, 2015, 12:28:15 PM11/13/15
to django-...@googlegroups.com
#24991: Range types not properly converted to SQL in QuerySet.query.__str__()
----------------------------------+----------------------------------------
Reporter: unklphil | Owner: Stranger6667
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 1.8

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 timgraham):

* has_patch: 0 => 1


Comment:

Looks like #25705 provides a patch for this issue.

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

Django

unread,
Dec 22, 2015, 4:25:56 PM12/22/15
to django-...@googlegroups.com
#24991: Range types not properly converted to SQL in QuerySet.query.__str__()
----------------------------------+----------------------------------------
Reporter: unklphil | Owner: Stranger6667
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 1.8

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 timgraham):

* needs_better_patch: 0 => 1


Comment:

Created [https://github.com/django/django/pull/5848 a pull request] with
the patch from #25705 but there are some test failures.

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

Django

unread,
Nov 28, 2019, 4:02:49 PM11/28/19
to django-...@googlegroups.com
#24991: Range types not properly converted to SQL in QuerySet.query.__str__()
-------------------------------------+-------------------------------------
Reporter: Villiers Strauss | Owner: Dmitry
| Dygalo
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 1.8

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 Claude Paroz):

* needs_better_patch: 1 => 0


Comment:

I tried to fix errors in [https://github.com/django/django/pull/12156 that
PR]. I'm not very proud of those regex testing, but well, it works...

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

Django

unread,
Dec 5, 2019, 6:15:58 AM12/5/19
to django-...@googlegroups.com
#24991: Range types not properly converted to SQL in QuerySet.query.__str__()
-------------------------------------+-------------------------------------
Reporter: Villiers Strauss | Owner: Dmitry
| Dygalo
Type: Bug | Status: assigned
Component: contrib.postgres | Version: master

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 felixxm):

* needs_better_patch: 0 => 1

* version: 1.8 => master


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

Django

unread,
Dec 31, 2021, 5:33:37 AM12/31/21
to django-...@googlegroups.com
#24991: Range types not properly converted to SQL in QuerySet.query.__str__()
-------------------------------------+-------------------------------------
Reporter: Villiers Strauss | Owner: Dmitry
| Dygalo
Type: Bug | Status: closed
Component: contrib.postgres | Version: dev
Severity: Normal | Resolution: duplicate

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

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


Comment:

Duplicate of #25705.

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

Reply all
Reply to author
Forward
0 new messages