Michael Manfre avatar Michael Manfre committed 80a2031

Added mappings for StdDev and Variance aggregates to work with MSSQL. Applied patch to django ticket #18333 to enable supports_stddev. Refs #7

Comments (0)

Files changed (4)

docs/changelog.txt

 - Fixed date part extraction for ``week_day``.
 - DatabaseWrapper reports vendor as 'microsoft'.
 - AVG function now matches core backend behaviors and will auto-cast to ``float``, instead of maintaining datatype. 
-  Set database ``OPTIONS`` setting ``disable_avg_cast`` to turn off the auto-cast behavior.
+  Set database ``OPTIONS`` setting ``disable_avg_cast`` to turn off the auto-cast behavior.
+- StdDev and Variance aggregate functions are now supported and will map to the proper MSSQL named functions. Includes work around for Django ticket `#18334`_.
+
+.. _`#18334`: https://code.djangoproject.com/ticket/18334

docs/settings.txt

 
 Default: ``False``
 
-This backend will automatically ``CAST`` fields used by the `AVG function` 
+This backend will automatically ``CAST`` fields used by the `AVG function`_ 
 as ``FLOAT`` to match the behavior of the core database backends. Set this
 to ``True`` if you need SQL server to retain the datatype of fields used
 with ``AVG``.

sqlserver_ado/compiler.py

                     # maintain multi-db support.
                     self.query.aggregate_select[alias].sql_template = \
                         '%(function)s(CAST(%(field)s AS FLOAT))'
+                # translate StdDev function names
+                elif aggregate.sql_function == 'STDDEV_SAMP':
+                    self.query.aggregate_select[alias].sql_function = 'STDEV'
+                elif aggregate.sql_function == 'STDDEV_POP':
+                    self.query.aggregate_select[alias].sql_function = 'STDEVP'
+                # translate Variance function names
+                elif aggregate.sql_function == 'VAR_SAMP':
+                    self.query.aggregate_select[alias].sql_function = 'VAR'
+                elif aggregate.sql_function == 'VAR_POP':
+                    self.query.aggregate_select[alias].sql_function = 'VARP'
 
     def as_sql(self, with_limits=True, with_col_aliases=False):
         self._fix_aggregates()

sqlserver_ado/operations.py

         MSSQL supports identifier names up to 128
         """
         return 128
+
+    def _supports_stddev(self):
+        """
+        Work around for django ticket #18334. 
+        This backend supports StdDev and the SQLCompilers will remap to 
+        the correct function names.
+        """
+        return True
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.