Skip to content

Commit 73b4d22

Browse files
committed
INTPYTHON-972 Add support for the Sign database function
1 parent 4dead9c commit 73b4d22

4 files changed

Lines changed: 30 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_ord.OrdTests",
264263
"db_functions.text.test_pad.PadTests",

django_mongodb_backend/functions.py

Lines changed: 29 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
MD5,
3040
SHA256,
@@ -219,6 +229,23 @@ def round_(self, compiler, connection):
219229
}
220230

221231

232+
def sign(self, compiler, connection):
233+
lhs_mql = process_lhs(self, compiler, connection, as_expr=True)
234+
return {
235+
"$cond": {
236+
"if": {"$eq": [lhs_mql, None]},
237+
"then": None, # Return null for null input.
238+
"else": {
239+
"$cond": {
240+
"if": {"$eq": [lhs_mql, 0]},
241+
"then": 0, # Return zero for zero input.
242+
"else": {"$cmp": [lhs_mql, 0]}, # Otherwise, +1 or -1.
243+
},
244+
},
245+
}
246+
}
247+
248+
222249
def str_index(self, compiler, connection):
223250
lhs = process_lhs(self, compiler, connection, as_expr=True)
224251
# StrIndex should be 0-indexed (not found) but it's -1-indexed on MongoDB.
@@ -345,6 +372,7 @@ def register_functions():
345372
Round.as_mql_expr = round_
346373
RTrim.as_mql_expr = trim("rtrim")
347374
SHA256.as_mql_expr = hash_func("sha256")
375+
Sign.as_mql_expr = sign
348376
StrIndex.as_mql_expr = str_index
349377
Substr.as_mql_expr = substr
350378
Trim.as_mql_expr = trim("trim")

docs/releases/6.0.x.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ New features
1515
- :class:`~django.db.models.functions.MD5` (requires MongoDB 8.3+)
1616
- :class:`~django.db.models.functions.Right`
1717
- :class:`~django.db.models.functions.SHA256` (requires MongoDB 8.3+)
18+
- :class:`~django.db.models.functions.Sign`
1819

1920
- Added support for Django's warning when performing import-time queries.
2021

docs/topics/known-issues.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ Database functions
8686
:class:`~django.db.models.functions.SHA224`,
8787
:class:`~django.db.models.functions.SHA384`,
8888
:class:`~django.db.models.functions.SHA512`
89-
- :class:`~django.db.models.functions.Sign`
9089

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

0 commit comments

Comments
 (0)