Subject: [PATCH] group the case-whens into sub-chunks.
---
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
diff --git a/django/db/models/query.py b/django/db/models/query.py
a
|
b
|
|
5 | 5 | import copy |
6 | 6 | import operator |
7 | 7 | import warnings |
| 8 | from collections import defaultdict |
8 | 9 | from itertools import chain, islice |
9 | 10 | |
10 | 11 | from asgiref.sync import sync_to_async |
… |
… |
|
907 | 908 | update_kwargs = {} |
908 | 909 | for field in fields: |
909 | 910 | when_statements = [] |
| 911 | chunks = defaultdict(list) |
910 | 912 | for obj in batch_objs: |
911 | 913 | attr = getattr(obj, field.attname) |
912 | 914 | if not hasattr(attr, "resolve_expression"): |
913 | 915 | attr = Value(attr, output_field=field) |
| 916 | try: |
| 917 | if chunks is not None: |
| 918 | chunks[attr].append(obj.pk) |
| 919 | except TypeError: |
| 920 | # one value can not be hashed, so omit chunking |
| 921 | chunks = None |
914 | 922 | when_statements.append(When(pk=obj.pk, then=attr)) |
915 | | case_statement = Case(*when_statements, output_field=field) |
| 923 | if chunks is not None: # all values were hashable |
| 924 | case_statement = Case(*[When(pk__in=pks, then=val) for val, pks in chunks.items()], output_field=field) |
| 925 | else: |
| 926 | case_statement = Case(*when_statements, output_field=field) |
916 | 927 | if requires_casting: |
917 | 928 | case_statement = Cast(case_statement, output_field=field) |
918 | 929 | update_kwargs[field.attname] = case_statement |