Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #10906 -- Added a check for PostgreSQL pre 8.2 when using StdDe…

…v/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
  • Loading branch information...
commit 8c2db4ab0f87643765b6bffac7c524f5e2e54d65 1 parent b97178f
@freakboy3742 freakboy3742 authored
View
9 django/db/backends/postgresql/operations.py
@@ -146,11 +146,18 @@ def prep_for_iexact_query(self, x):
def check_aggregate_support(self, 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)
under Postgres 8.2 - 8.2.4 is known to be faulty. Raise
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[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)
View
24 tests/regressiontests/aggregation_regress/models.py
@@ -1,7 +1,7 @@
# coding: utf-8
import pickle
-from django.db import models
+from django.db import connection, models
from django.conf import settings
try:
@@ -321,10 +321,26 @@ def __unicode__(self):
"""
}
-if settings.DATABASE_ENGINE != 'sqlite3':
- __test__['API_TESTS'] += """
-# Stddev and Variance are not guaranteed to be available for SQLite.
+def run_stddev_tests():
+ """Check to see if StdDev/Variance tests should be run.
+
+ 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'))
{'pages__stddev': 311.46...}
Please sign in to comment.
Something went wrong with that request. Please try again.