Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#31839 closed New feature (wontfix)

Add additional database functions.

Reported by: Nick Pope Owned by: Nick Pope
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: functions, random, truncate, log2, log10, bit length, octet length, hyperbolic
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Nick Pope)

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.

Change History (6)

comment:1 by Nick Pope, 4 years ago

Has patch: set

comment:2 by Mariusz Felisiak, 4 years ago

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 Random sound reasonable.

Last edited 4 years ago by Mariusz Felisiak (previous) (diff)

comment:3 by Nick Pope, 4 years ago

Description: modified (diff)

comment:4 by Mariusz Felisiak, 4 years ago

Resolution: wontfix
Status: assignedclosed

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.

comment:5 by Nick Pope, 4 years ago

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

comment:6 by Mariusz Felisiak, 4 years ago

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.

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