Fixed #10906 -- Added a check for PostgreSQL pre 8.2 when using StdDev/Variance aggregates. Thanks to Richard Davies for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10731 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2009-05-10 09:22:53 +00:00
parent b97178f7ec
commit 8c2db4ab0f
2 changed files with 28 additions and 5 deletions

View File

@ -146,11 +146,18 @@ class DatabaseOperations(BaseDatabaseOperations):
def check_aggregate_support(self, aggregate): def check_aggregate_support(self, aggregate):
"""Check that the backend fully supports the provided aggregate. """Check that the backend fully supports the provided aggregate.
The population and sample statistics (STDDEV_POP, STDDEV_SAMP,
VAR_POP, VAR_SAMP) were first implemented in Postgres 8.2.
The implementation of population statistics (STDDEV_POP and VAR_POP) The implementation of population statistics (STDDEV_POP and VAR_POP)
under Postgres 8.2 - 8.2.4 is known to be faulty. Raise under Postgres 8.2 - 8.2.4 is known to be faulty. Raise
NotImplementedError if this is the database in use. NotImplementedError if this is the database in use.
""" """
if aggregate.sql_function == 'STDDEV_POP' or aggregate.sql_function == 'VAR_POP': if aggregate.sql_function in ('STDDEV_POP', 'STDDEV_SAMP', 'VAR_POP', 'VAR_SAMP'):
if self.postgres_version[0:2] < [8,2]:
raise NotImplementedError('PostgreSQL does not support %s prior to version 8.2. Please upgrade your version of PostgreSQL.' % aggregate.sql_function)
if aggregate.sql_function in ('STDDEV_POP', 'VAR_POP'):
if self.postgres_version[0:2] == [8,2]: if self.postgres_version[0:2] == [8,2]:
if self.postgres_version[2] is None or self.postgres_version[2] <= 4: if self.postgres_version[2] is None or self.postgres_version[2] <= 4:
raise NotImplementedError('PostgreSQL 8.2 to 8.2.4 is known to have a faulty implementation of %s. Please upgrade your version of PostgreSQL.' % aggregate.sql_function) raise NotImplementedError('PostgreSQL 8.2 to 8.2.4 is known to have a faulty implementation of %s. Please upgrade your version of PostgreSQL.' % aggregate.sql_function)

View File

@ -1,7 +1,7 @@
# coding: utf-8 # coding: utf-8
import pickle import pickle
from django.db import models from django.db import connection, models
from django.conf import settings from django.conf import settings
try: try:
@ -321,10 +321,26 @@ FieldError: Cannot compute Avg('mean_age'): 'mean_age' is an aggregate
""" """
} }
if settings.DATABASE_ENGINE != 'sqlite3': def run_stddev_tests():
__test__['API_TESTS'] += """ """Check to see if StdDev/Variance tests should be run.
# Stddev and Variance are not guaranteed to be available for SQLite.
Stddev and Variance are not guaranteed to be available for SQLite, and
are not available for PostgreSQL before 8.2.
"""
if settings.DATABASE_ENGINE == 'sqlite3':
return False
class StdDevPop(object):
sql_function = 'STDDEV_POP'
try:
connection.ops.check_aggregate_support(StdDevPop())
except:
return False
return True
if run_stddev_tests():
__test__['API_TESTS'] += """
>>> Book.objects.aggregate(StdDev('pages')) >>> Book.objects.aggregate(StdDev('pages'))
{'pages__stddev': 311.46...} {'pages__stddev': 311.46...}