Skip to content

Commit 2abea1a

Browse files
author
Tim Nyborg
committed
default null ordering for window functions
1 parent f785bfa commit 2abea1a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

mssql/functions.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import json
55

66
from django import VERSION
7-
from django.db.models import BooleanField
7+
from django.db.models import BooleanField, Value
88
from django.db.models.functions import Cast
99
from django.db.models.functions.math import ATan2, Log, Ln, Mod, Round
10-
from django.db.models.expressions import Case, Exists, OrderBy, When
10+
from django.db.models.expressions import Case, Exists, OrderBy, When, Window
1111
from django.db.models.lookups import Lookup, In
1212
from django.db.models import lookups
1313

@@ -55,6 +55,13 @@ def sqlserver_round(self, compiler, connection, **extra_context):
5555
return self.as_sql(compiler, connection, template='%(function)s(%(expressions)s, 0)', **extra_context)
5656

5757

58+
def sqlserver_window(self, compiler, connection, template=None):
59+
# MSSQL window functions require an OVER clause with ORDER BY
60+
if self.order_by is None:
61+
self.order_by = Value('SELECT NULL')
62+
return self.as_sql(compiler, connection, template)
63+
64+
5865
def sqlserver_exists(self, compiler, connection, template=None, **extra_context):
5966
# MS SQL doesn't allow EXISTS() in the SELECT list, so wrap it with a
6067
# CASE WHEN expression. Change the template since the When expression
@@ -180,10 +187,12 @@ def json_HasKeyLookup(self, compiler, connection):
180187
Log.as_microsoft = sqlserver_log
181188
Mod.as_microsoft = sqlserver_mod
182189
Round.as_microsoft = sqlserver_round
190+
Window.as_microsoft = sqlserver_window
183191

184192
if DJANGO3:
185193
Lookup.as_microsoft = sqlserver_lookup
186194
else:
187195
Exists.as_microsoft = sqlserver_exists
188196

189197
OrderBy.as_microsoft = sqlserver_orderby
198+

0 commit comments

Comments
 (0)