summaryrefslogtreecommitdiff
path: root/django/db/backends/sqlite3/_functions.py
diff options
context:
space:
mode:
authorAdam Johnson <me@adamj.eu>2021-12-10 08:14:16 +0000
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-12-23 12:08:33 +0100
commit5f6a727a6a06437c8dbafaa18817235c0ed658ca (patch)
tree1d2b326167b8ccbe7a2617b2f53e232a7ab248bd /django/db/backends/sqlite3/_functions.py
parentec7554f1c28256fbe0e23fab452e7e9b96711230 (diff)
downloaddjango-5f6a727a6a06437c8dbafaa18817235c0ed658ca.tar.gz
Refs #33355 -- Constructed SQLite list aggregate types once.
Diffstat (limited to 'django/db/backends/sqlite3/_functions.py')
-rw-r--r--django/db/backends/sqlite3/_functions.py36
1 files changed, 24 insertions, 12 deletions
diff --git a/django/db/backends/sqlite3/_functions.py b/django/db/backends/sqlite3/_functions.py
index 85a5e7c655..3529a99dd6 100644
--- a/django/db/backends/sqlite3/_functions.py
+++ b/django/db/backends/sqlite3/_functions.py
@@ -21,14 +21,6 @@ from django.utils.crypto import md5
from django.utils.duration import duration_microseconds
-def list_aggregate(function):
- """
- Return an aggregate class that accumulates values in a list and applies
- the provided function to the data.
- """
- return type('ListAggregate', (list,), {'finalize': function, 'step': list.append})
-
-
def register(connection):
create_deterministic_function = functools.partial(
connection.create_function,
@@ -80,10 +72,10 @@ def register(connection):
# Don't use the built-in RANDOM() function because it returns a value
# in the range [-1 * 2^63, 2^63 - 1] instead of [0, 1).
connection.create_function('RAND', 0, random.random)
- connection.create_aggregate('STDDEV_POP', 1, list_aggregate(statistics.pstdev))
- connection.create_aggregate('STDDEV_SAMP', 1, list_aggregate(statistics.stdev))
- connection.create_aggregate('VAR_POP', 1, list_aggregate(statistics.pvariance))
- connection.create_aggregate('VAR_SAMP', 1, list_aggregate(statistics.variance))
+ connection.create_aggregate('STDDEV_POP', 1, StdDevPop)
+ connection.create_aggregate('STDDEV_SAMP', 1, StdDevSamp)
+ connection.create_aggregate('VAR_POP', 1, VarPop)
+ connection.create_aggregate('VAR_SAMP', 1, VarSamp)
def _sqlite_datetime_parse(dt, tzname=None, conn_tzname=None):
@@ -467,3 +459,23 @@ def _sqlite_tan(x):
if x is None:
return None
return tan(x)
+
+
+class ListAggregate(list):
+ step = list.append
+
+
+class StdDevPop(ListAggregate):
+ finalize = statistics.pstdev
+
+
+class StdDevSamp(ListAggregate):
+ finalize = statistics.stdev
+
+
+class VarPop(ListAggregate):
+ finalize = statistics.pvariance
+
+
+class VarSamp(ListAggregate):
+ finalize = statistics.variance