Skip to content

Commit 7f7da51

Browse files
committed
INTPYTHON-972 Add support for the Sign database function
1 parent 64995c8 commit 7f7da51

4 files changed

Lines changed: 28 additions & 3 deletions

File tree

django_mongodb_backend/features.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@ def django_test_expected_failures(self):
258258
"db_functions.tests.FunctionTests.test_func_transform_bilateral_multivalue",
259259
},
260260
"MongoDB does not support this database function.": {
261-
"db_functions.math.test_sign.SignTests",
262261
"db_functions.text.test_chr.ChrTests",
263262
"db_functions.text.test_md5.MD5Tests",
264263
"db_functions.text.test_ord.OrdTests",

django_mongodb_backend/functions.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,17 @@
2424
TruncDate,
2525
TruncTime,
2626
)
27-
from django.db.models.functions.math import Ceil, Cot, Degrees, Log, Power, Radians, Random, Round
27+
from django.db.models.functions.math import (
28+
Ceil,
29+
Cot,
30+
Degrees,
31+
Log,
32+
Power,
33+
Radians,
34+
Random,
35+
Round,
36+
Sign,
37+
)
2838
from django.db.models.functions.text import (
2939
Concat,
3040
ConcatPair,
@@ -175,6 +185,19 @@ def round_(self, compiler, connection):
175185
}
176186

177187

188+
def sign(self, compiler, connection):
189+
lhs_mql = process_lhs(self, compiler, connection, as_expr=True)
190+
return {
191+
"$cond": {
192+
"if": {"$eq": [lhs_mql, None]},
193+
"then": None,
194+
"else": {
195+
"$cond": {"if": {"$eq": [lhs_mql, 0]}, "then": 0, "else": {"$cmp": [lhs_mql, 0]}}
196+
},
197+
}
198+
}
199+
200+
178201
def str_index(self, compiler, connection):
179202
lhs = process_lhs(self, compiler, connection, as_expr=True)
180203
# StrIndex should be 0-indexed (not found) but it's -1-indexed on MongoDB.
@@ -298,6 +321,7 @@ def register_functions():
298321
Replace.as_mql_expr = replace
299322
Round.as_mql_expr = round_
300323
RTrim.as_mql_expr = trim("rtrim")
324+
Sign.as_mql_expr = sign
301325
StrIndex.as_mql_expr = str_index
302326
Substr.as_mql_expr = substr
303327
Trim.as_mql_expr = trim("trim")

docs/releases/6.0.x.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Django MongoDB Backend 6.0.x
1010
New features
1111
------------
1212

13+
- Added support for the :class:`~django.db.models.functions.Sign` database
14+
function.
15+
1316
- Added support for Django's warning when performing import-time queries.
1417

1518
Bug fixes

docs/topics/known-issues.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ Database functions
8989
:class:`~django.db.models.functions.SHA256`,
9090
:class:`~django.db.models.functions.SHA384`,
9191
:class:`~django.db.models.functions.SHA512`
92-
- :class:`~django.db.models.functions.Sign`
9392

9493
- The ``tzinfo`` parameter of the
9594
:class:`~django.db.models.functions.TruncDate` and

0 commit comments

Comments
 (0)