Skip to content

Commit 09e30a0

Browse files
committed
INTPYTHON-967 Add support for the ExtractQuarter database function
1 parent 4dead9c commit 09e30a0

4 files changed

Lines changed: 13 additions & 5 deletions

File tree

django_mongodb_backend/features.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -725,10 +725,6 @@ def django_test_skips(self):
725725
(NotSupportedError, "TruncDate with tzinfo (Africa/Nairobi) isn't supported on MongoDB."): {
726726
"timezones.tests.NewDatabaseTests.test_query_convert_timezones",
727727
},
728-
(NotSupportedError, "ExtractQuarter is not supported."): {
729-
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_extract_quarter_func",
730-
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_extract_quarter_func_boundaries",
731-
},
732728
(NotSupportedError, "StringAgg is not supported."): {
733729
"aggregation.tests.AggregateTestCase.test_distinct_on_stringagg",
734730
"aggregation.tests.AggregateTestCase.test_string_agg_escapes_delimiter",

django_mongodb_backend/functions.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
ExtractIsoYear,
1616
ExtractMinute,
1717
ExtractMonth,
18+
ExtractQuarter,
1819
ExtractSecond,
1920
ExtractWeek,
2021
ExtractWeekDay,
@@ -101,6 +102,9 @@ def cot(self, compiler, connection):
101102

102103
def extract(self, compiler, connection):
103104
lhs_mql = process_lhs(self, compiler, connection, as_expr=True)
105+
# ExtractQuarter lacks a built-in operator.
106+
if self.lookup_name == "quarter":
107+
return extract_quarter(self, compiler, connection)
104108
operator = EXTRACT_OPERATORS.get(self.lookup_name)
105109
if operator is None:
106110
raise NotSupportedError(f"{self.__class__.__name__} is not supported.")
@@ -109,6 +113,13 @@ def extract(self, compiler, connection):
109113
return {f"${operator}": lhs_mql}
110114

111115

116+
def extract_quarter(self, compiler, connection):
117+
lhs_mql = process_lhs(self, compiler, connection, as_expr=True)
118+
if timezone := self.get_tzname():
119+
lhs_mql = {"date": lhs_mql, "timezone": timezone}
120+
return {"$ceil": {"$divide": [{"$month": lhs_mql}, 3]}}
121+
122+
112123
def func(self, compiler, connection):
113124
lhs_mql = process_lhs(self, compiler, connection, as_expr=True)
114125
if self.function is None:
@@ -329,6 +340,7 @@ def register_functions():
329340
ConcatPair.as_mql_expr = concat_pair
330341
Cot.as_mql_expr = cot
331342
Extract.as_mql_expr = extract
343+
ExtractQuarter.as_mql_expr = extract_quarter
332344
Func.as_mql_expr = func
333345
Func.can_use_path = False
334346
JSONArray.as_mql_expr = partialmethod(process_lhs, as_expr=True)

docs/releases/6.0.x.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ New features
1212

1313
- Added support for the following database functions:
1414

15+
- :class:`~django.db.models.functions.ExtractQuarter`
1516
- :class:`~django.db.models.functions.MD5` (requires MongoDB 8.3+)
1617
- :class:`~django.db.models.functions.Right`
1718
- :class:`~django.db.models.functions.SHA256` (requires MongoDB 8.3+)

docs/topics/known-issues.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ Database functions
7676
- Some of Django's built-in database functions aren't supported by MongoDB:
7777

7878
- :class:`~django.db.models.functions.Chr`
79-
- :class:`~django.db.models.functions.ExtractQuarter`
8079
- :class:`~django.db.models.functions.LPad`,
8180
:class:`~django.db.models.functions.RPad`
8281
- :class:`~django.db.models.functions.Ord`

0 commit comments

Comments
 (0)