Skip to content

Commit b960e5f

Browse files
committed
INTPYTHON-967 Add support for the ExtractQuarter database function
1 parent 8157383 commit b960e5f

4 files changed

Lines changed: 15 additions & 5 deletions

File tree

django_mongodb_backend/features.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -719,10 +719,6 @@ def django_test_skips(self):
719719
(NotSupportedError, "TruncDate with tzinfo (Africa/Nairobi) isn't supported on MongoDB."): {
720720
"timezones.tests.NewDatabaseTests.test_query_convert_timezones",
721721
},
722-
(NotSupportedError, "ExtractQuarter is not supported."): {
723-
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_extract_quarter_func",
724-
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_extract_quarter_func_boundaries",
725-
},
726722
(NotSupportedError, "StringAgg is not supported."): {
727723
"aggregation.tests.AggregateTestCase.test_distinct_on_stringagg",
728724
"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,
@@ -98,6 +99,9 @@ def cot(self, compiler, connection):
9899

99100
def extract(self, compiler, connection):
100101
lhs_mql = process_lhs(self, compiler, connection, as_expr=True)
102+
# ExtractQuarter lacks a built-in operator.
103+
if self.lookup_name == "quarter":
104+
return extract_quarter(self, compiler, connection)
101105
operator = EXTRACT_OPERATORS.get(self.lookup_name)
102106
if operator is None:
103107
raise NotSupportedError(f"{self.__class__.__name__} is not supported.")
@@ -106,6 +110,13 @@ def extract(self, compiler, connection):
106110
return {f"${operator}": lhs_mql}
107111

108112

113+
def extract_quarter(self, compiler, connection):
114+
lhs_mql = process_lhs(self, compiler, connection, as_expr=True)
115+
if timezone := self.get_tzname():
116+
lhs_mql = {"date": lhs_mql, "timezone": timezone}
117+
return {"$ceil": {"$divide": [{"$month": lhs_mql}, 3]}}
118+
119+
109120
def func(self, compiler, connection):
110121
lhs_mql = process_lhs(self, compiler, connection, as_expr=True)
111122
if self.function is None:
@@ -285,6 +296,7 @@ def register_functions():
285296
ConcatPair.as_mql_expr = concat_pair
286297
Cot.as_mql_expr = cot
287298
Extract.as_mql_expr = extract
299+
ExtractQuarter.as_mql_expr = extract_quarter
288300
Func.as_mql_expr = func
289301
Func.can_use_path = False
290302
JSONArray.as_mql_expr = partialmethod(process_lhs, as_expr=True)

docs/releases/6.0.x.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ New features
1212

1313
- Added support for Django's warning when performing import-time queries.
1414

15+
- Added support for the :class:`~django.db.models.functions.ExtractQuarter`
16+
database function.
17+
1518
Bug fixes
1619
---------
1720

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.MD5`

0 commit comments

Comments
 (0)