From 5f6a727a6a06437c8dbafaa18817235c0ed658ca Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Fri, 10 Dec 2021 08:14:16 +0000 Subject: [PATCH] Refs #33355 -- Constructed SQLite list aggregate types once. --- django/db/backends/sqlite3/_functions.py | 36 ++++++++++++++++-------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/django/db/backends/sqlite3/_functions.py b/django/db/backends/sqlite3/_functions.py index 85a5e7c655e..3529a99dd6d 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