[Django] #31839: Add additional database functions.

30 views
Skip to first unread message

Django

unread,
Jul 28, 2020, 3:50:20 PM7/28/20
to django-...@googlegroups.com
#31839: Add additional database functions.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: New | Status: assigned
feature |
Component: Database | Version: master
layer (models, ORM) | Keywords: functions, random,
Severity: Normal | truncate, log2, log10, bit length,
Triage Stage: | octet length, hyperbolic
Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I propose adding some additional database functions:

- `Random` -- generate random numbers in the range `[0.0, 0.1)`.
- `Truncate` -- round number towards zero. (SQL function is `TRUNC`, but
`Trunc` is used for date truncation in Django.)
- `Log2` and `Log10` -- simple logarithm functions without needing to
provide the base.
- `ACosh`, `ASinh`, `ATanh`, `Cosh`, `Sinh`, and `Tanh` -- hyperbolic
functions.
- `BitLength` and `OctetLength` -- length of string in bits and bytes,
complement to `Length` which is character length.

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

Django

unread,
Jul 28, 2020, 3:54:03 PM7/28/20
to django-...@googlegroups.com
#31839: Add additional database functions.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: functions, random, | Triage Stage:
truncate, log2, log10, bit | Unreviewed
length, octet length, hyperbolic |
Has patch: 1 | Needs documentation: 0

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

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/13252 PR]

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

Django

unread,
Jul 29, 2020, 1:12:31 AM7/29/20
to django-...@googlegroups.com
#31839: Add additional database functions.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: functions, random, | Triage Stage:
truncate, log2, log10, bit | Unreviewed
length, octet length, hyperbolic |
Has patch: 1 | Needs documentation: 0

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

Comment (by felixxm):

I'm not sure if it's worth adding extra database functions:

- `Log2` and `Log10` can be achieved with proving base to `Log` so I'm
against them,
- hyperbolic functions are supported only on PostgreSQL 12+, we need to
emulate them on other DBs, it's not worth, IMO,
- `BitLength` and `OctetLength` will probably not work on Oracle.


`Truncate` and `Round` sound reasonable.

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

Django

unread,
Jul 29, 2020, 6:57:37 PM7/29/20
to django-...@googlegroups.com
#31839: Add additional database functions.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: functions, random, | Triage Stage:
truncate, log2, log10, bit | Unreviewed
length, octet length, hyperbolic |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Nick Pope:

Old description:

> I propose adding some additional database functions:
>
> - `Random` -- generate random numbers in the range `[0.0, 0.1)`.
> - `Truncate` -- round number towards zero. (SQL function is `TRUNC`, but
> `Trunc` is used for date truncation in Django.)
> - `Log2` and `Log10` -- simple logarithm functions without needing to
> provide the base.
> - `ACosh`, `ASinh`, `ATanh`, `Cosh`, `Sinh`, and `Tanh` -- hyperbolic
> functions.
> - `BitLength` and `OctetLength` -- length of string in bits and bytes,
> complement to `Length` which is character length.

New description:

I propose adding some additional database functions:

- `Random` -- generate random numbers in the range `[0.0, 0.1)`.
- `Truncate` -- round number towards zero. (SQL function is `TRUNC`, but
`Trunc` is used for date truncation in Django.)
- `Log2` and `Log10` -- simple logarithm functions without needing to
provide the base.
- `ACosh`, `ASinh`, `ATanh`, `Cosh`, `Sinh`, and `Tanh` -- hyperbolic
functions.

- `BitLength` and `ByteLength` -- length of string in bits and bytes,


complement to `Length` which is character length.

--

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

Django

unread,
Jul 30, 2020, 2:34:23 AM7/30/20
to django-...@googlegroups.com
#31839: Add additional database functions.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: New feature | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: wontfix

Keywords: functions, random, | Triage Stage:
truncate, log2, log10, bit | Unreviewed
length, octet length, hyperbolic |
Has patch: 1 | Needs documentation: 0

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

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


Comment:

I think we need to decide at what point we say enough is enough here, and
recommend users create their own functions like we did with template tags.
For me hyperbolic functions cross that line. You can start a discussion on
DevelopersMailingList if you don't agree. Personally I've never used any
of proposed database functions.

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

Django

unread,
Jul 30, 2020, 2:51:33 AM7/30/20
to django-...@googlegroups.com
#31839: Add additional database functions.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: New feature | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: functions, random, | Triage Stage:
truncate, log2, log10, bit | Unreviewed
length, octet length, hyperbolic |
Has patch: 1 | Needs documentation: 0

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

Comment (by Nick Pope):

Will you still take `Random` and `Truncate`?

In the case of `Random` there is also a nice bit of simplification:

https://github.com/django/django/commit/c8913d41e74859747893dc21356a1fb6906c56e9

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

Django

unread,
Jul 30, 2020, 3:19:47 AM7/30/20
to django-...@googlegroups.com
#31839: Add additional database functions.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: New feature | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: functions, random, | Triage Stage:
truncate, log2, log10, bit | Unreviewed
length, octet length, hyperbolic |
Has patch: 1 | Needs documentation: 0

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

Comment (by felixxm):

`Random()` is already there (as an expression) so we can change it to a
function and promote in docs, I'm just not sure why we change its
behavior.

`Truncate()` can be confused with `Trunc()` for dates/datetimes, and we
can achieve the same by using `Cast(..., models.IntegerField())` so I
don't think it's necessary.

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

Reply all
Reply to author
Forward
0 new messages