diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2026-04-18 08:53:21 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-04-18 08:53:21 +0200 |
| commit | ed79c5959add54b6e8ea589ec601e0d2e801517e (patch) | |
| tree | 0d00f241bea6de88203d1314c7de92cb262b3a3f /django/db/backends/sqlite3 | |
| parent | d687d412a9abd9c80e31945f16ce32c020512394 (diff) | |
Fixed #37028 -- Added BitAnd(), BitOr(), and BitXor() aggregates.
Diffstat (limited to 'django/db/backends/sqlite3')
| -rw-r--r-- | django/db/backends/sqlite3/_functions.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/django/db/backends/sqlite3/_functions.py b/django/db/backends/sqlite3/_functions.py index aa4100e83f..73a5f4d987 100644 --- a/django/db/backends/sqlite3/_functions.py +++ b/django/db/backends/sqlite3/_functions.py @@ -3,6 +3,7 @@ Implementations of SQL functions for SQLite. """ import functools +import operator import random import statistics import zoneinfo @@ -86,6 +87,9 @@ def register(connection): connection.create_aggregate("VAR_POP", 1, VarPop) connection.create_aggregate("VAR_SAMP", 1, VarSamp) connection.create_aggregate("ANY_VALUE", 1, AnyValue) + connection.create_aggregate("BIT_AND", 1, BitAnd) + connection.create_aggregate("BIT_OR", 1, BitOr) + connection.create_aggregate("BIT_XOR", 1, BitXor) connection.create_function("UUIDV4", 0, _sqlite_uuid4) if PY314: connection.create_function("UUIDV7", 0, _sqlite_uuid7) @@ -535,3 +539,27 @@ class VarSamp(ListAggregate): class AnyValue(ListAggregate): def finalize(self): return self[0] + + +class BitAggregate(ListAggregate): + bit_operator = None + + def finalize(self): + items = (item for item in self if item is not None) + try: + return functools.reduce(self.bit_operator, items) + except TypeError: + # items may be an empty iterator when all elements are None. + return None + + +class BitAnd(BitAggregate): + bit_operator = operator.and_ + + +class BitOr(BitAggregate): + bit_operator = operator.or_ + + +class BitXor(BitAggregate): + bit_operator = operator.xor |
