Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix group by for EmbeddedModel field #225

Merged
merged 3 commits into from
Jan 23, 2025

Conversation

WaVEV
Copy link
Collaborator

@WaVEV WaVEV commented Jan 18, 2025

fixes #221

@WaVEV WaVEV force-pushed the EMF-support-group-by branch from 8c0e138 to d7d40c3 Compare January 18, 2025 05:12
@WaVEV WaVEV requested a review from timgraham January 18, 2025 05:13
@WaVEV WaVEV changed the title support annotations, group by, and ordering EMF support annotations, group by, and ordering Jan 18, 2025
@@ -104,6 +113,46 @@ def test_nested(self):
)
self.assertCountEqual(Book.objects.filter(author__address__city="NYC"), [obj])

def truncate_ms(self, value):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helper methods like this should typically be organized at the top of a class so they don't lost among test methods, however, I suppose we should make it a function in this file so we don't have to repeat the method on every class (the move can be a separate commit in this PR that precedes the fix).

(Holder.objects.create(data=Data(integer=x)) for x in range(6)),
key=lambda x: x.data.integer,
)
query = (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather use qs = since "query" usually refers to SQL or, in our case, MQL.

"""Truncate microsends to millisecond precision as supported by MongoDB."""
return value.replace(microsecond=(value.microsecond // 1000) * 1000)

def test_ordering_by_embedded_field(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good to have, but it passes without the fix, so it should be a separate commit. (I think the fix is mostly about group by, not ordering without grouping.) I would sort these tests above test_nested since they also use the Holder model.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, and this test will be in this PR but in a separate commit, right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

tests/model_fields_/test_embedded_model.py Show resolved Hide resolved
.annotate(max_auto_now=Max("data__auto_now"))
.order_by("data__integer")
)
query_response = [{**e, "max_auto_now": self.truncate_ms(e["max_auto_now"])} for e in query]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is truncate needed here? I'd expect MongoDB to return an already truncated result.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, removed

self.assertSequenceEqual(query, expected)

def test_ordering_grouping_by_embedded_field(self):
expected = sorted(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say expected_objs since there is still some modification in calculating the expected result.

],
)

def test_ordering_grouping_by_sum(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, it's hard for me to reason about the expected result of query, so some explanatory comment would be useful. For example, the "cheat sheet" at https://docs.djangoproject.com/en/dev/topics/db/aggregation/ is easy to follow.

@timgraham timgraham changed the title EMF support annotations, group by, and ordering fix group by for EmbeddedModel field Jan 23, 2025
@timgraham timgraham force-pushed the EMF-support-group-by branch from 1ad815c to 3512586 Compare January 23, 2025 00:25
@timgraham timgraham force-pushed the EMF-support-group-by branch from 3512586 to 5156577 Compare January 23, 2025 01:00
@timgraham timgraham merged commit 5156577 into mongodb-labs:main Jan 23, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

support group by in embedded model fields
2 participants