Refs #28459 -- Improved performance of loading DurationField on SQLite and MySQL.

This commit is contained in:
Sergey Fedoseev 2017-12-27 22:23:08 +05:00 committed by Tim Graham
parent ef6c680f60
commit ae1baa7d1d
2 changed files with 13 additions and 4 deletions

View File

@ -7,7 +7,6 @@ from django.core.exceptions import ImproperlyConfigured
from django.db import NotSupportedError, transaction
from django.db.backends import utils
from django.utils import timezone
from django.utils.dateparse import parse_duration
from django.utils.encoding import force_text
@ -568,9 +567,7 @@ class BaseDatabaseOperations:
def convert_durationfield_value(self, value, expression, connection):
if value is not None:
value = str(decimal.Decimal(value) / decimal.Decimal(1000000))
value = parse_duration(value)
return value
return datetime.timedelta(0, 0, value)
def check_expression_support(self, expression):
"""

View File

@ -94,6 +94,12 @@ class Avg(Aggregate):
return FloatField()
return super()._resolve_output_field()
def as_mysql(self, compiler, connection):
sql, params = super().as_sql(compiler, connection)
if self.output_field.get_internal_type() == 'DurationField':
sql = 'CAST(%s as SIGNED)' % sql
return sql, params
def as_oracle(self, compiler, connection):
if self.output_field.get_internal_type() == 'DurationField':
expression = self.get_source_expressions()[0]
@ -153,6 +159,12 @@ class Sum(Aggregate):
function = 'SUM'
name = 'Sum'
def as_mysql(self, compiler, connection):
sql, params = super().as_sql(compiler, connection)
if self.output_field.get_internal_type() == 'DurationField':
sql = 'CAST(%s as SIGNED)' % sql
return sql, params
def as_oracle(self, compiler, connection):
if self.output_field.get_internal_type() == 'DurationField':
expression = self.get_source_expressions()[0]