Skip to content

Commit 98e5a84

Browse files
committed
INTPYTHON-972 Add support for the Sign database function
1 parent 428c1ea commit 98e5a84

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
@@ -25,7 +25,17 @@
2525
TruncDate,
2626
TruncTime,
2727
)
28-
from django.db.models.functions.math import Ceil, Cot, Degrees, Log, Power, Radians, Random, Round
28+
from django.db.models.functions.math import (
29+
Ceil,
30+
Cot,
31+
Degrees,
32+
Log,
33+
Power,
34+
Radians,
35+
Random,
36+
Round,
37+
Sign,
38+
)
2939
from django.db.models.functions.text import (
3040
MD5,
3141
SHA256,
@@ -239,6 +249,23 @@ def round_(self, compiler, connection):
239249
}
240250

241251

252+
def sign(self, compiler, connection):
253+
lhs_mql = process_lhs(self, compiler, connection, as_expr=True)
254+
return {
255+
"$cond": {
256+
"if": {"$eq": [lhs_mql, None]},
257+
"then": None, # Return null for null input.
258+
"else": {
259+
"$cond": {
260+
"if": {"$eq": [lhs_mql, 0]},
261+
"then": 0, # Return zero for zero input.
262+
"else": {"$cmp": [lhs_mql, 0]}, # Otherwise, +1 or -1.
263+
},
264+
},
265+
}
266+
}
267+
268+
242269
def str_index(self, compiler, connection):
243270
lhs = process_lhs(self, compiler, connection, as_expr=True)
244271
# StrIndex should be 0-indexed (not found) but it's -1-indexed on MongoDB.
@@ -366,6 +393,7 @@ def register_functions():
366393
Round.as_mql_expr = round_
367394
RTrim.as_mql_expr = trim("rtrim")
368395
SHA256.as_mql_expr = hash_func("sha256")
396+
Sign.as_mql_expr = sign
369397
StrIndex.as_mql_expr = str_index
370398
Substr.as_mql_expr = substr
371399
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
@@ -16,6 +16,7 @@ New features
1616
- :class:`~django.db.models.functions.MD5` (requires MongoDB 8.3+)
1717
- :class:`~django.db.models.functions.Right`
1818
- :class:`~django.db.models.functions.SHA256` (requires MongoDB 8.3+)
19+
- :class:`~django.db.models.functions.Sign`
1920

2021
- Added support for fixed-offset ``tzinfo`` in the
2122
:class:`~django.db.models.functions.Extract` database functions.

docs/topics/known-issues.rst

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

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

0 commit comments

Comments
 (0)