Refs #28459 -- Improved performance of SQLCompiler.apply_converters().

This commit is contained in:
Sergey Fedoseev 2017-08-24 01:26:24 +05:00 committed by Tim Graham
parent 4302167939
commit dca67bb2c2
2 changed files with 7 additions and 7 deletions

View File

@ -119,7 +119,6 @@ class ValuesListIterable(BaseIterable):
query = queryset.query query = queryset.query
compiler = query.get_compiler(queryset.db) compiler = query.get_compiler(queryset.db)
results = compiler.results_iter()
if queryset._fields: if queryset._fields:
field_names = list(query.values_select) field_names = list(query.values_select)
extra_names = list(query.extra_select) extra_names = list(query.extra_select)
@ -133,8 +132,8 @@ class ValuesListIterable(BaseIterable):
# Reorder according to fields. # Reorder according to fields.
index_map = {name: idx for idx, name in enumerate(names)} index_map = {name: idx for idx, name in enumerate(names)}
rowfactory = operator.itemgetter(*[index_map[f] for f in fields]) rowfactory = operator.itemgetter(*[index_map[f] for f in fields])
results = map(rowfactory, results) return map(rowfactory, compiler.results_iter())
return results return compiler.results_iter(tuple_expected=True)
class FlatValuesListIterable(BaseIterable): class FlatValuesListIterable(BaseIterable):

View File

@ -942,16 +942,15 @@ class SQLCompiler:
def apply_converters(self, rows, converters): def apply_converters(self, rows, converters):
connection = self.connection connection = self.connection
converters = list(converters.items()) converters = list(converters.items())
for row in rows: for row in map(list, rows):
row = list(row)
for pos, (convs, expression) in converters: for pos, (convs, expression) in converters:
value = row[pos] value = row[pos]
for converter in convs: for converter in convs:
value = converter(value, expression, connection) value = converter(value, expression, connection)
row[pos] = value row[pos] = value
yield tuple(row) yield row
def results_iter(self, results=None): def results_iter(self, results=None, tuple_expected=False):
"""Return an iterator over the results from executing this query.""" """Return an iterator over the results from executing this query."""
if results is None: if results is None:
results = self.execute_sql(MULTI) results = self.execute_sql(MULTI)
@ -960,6 +959,8 @@ class SQLCompiler:
rows = chain.from_iterable(results) rows = chain.from_iterable(results)
if converters: if converters:
rows = self.apply_converters(rows, converters) rows = self.apply_converters(rows, converters)
if tuple_expected:
rows = map(tuple, rows)
return rows return rows
def has_results(self): def has_results(self):