Skip to content

CSHARP-5529: Optimize grouping.First().X to not retrieve the entire $$ROOT #1653

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rstam
Copy link
Contributor

@rstam rstam commented Mar 28, 2025

No description provided.

@rstam rstam requested review from sanych-sun and adelinowona March 28, 2025 18:22
@rstam rstam requested a review from a team as a code owner March 28, 2025 18:22
Copy link
Contributor

@adelinowona adelinowona left a comment

Choose a reason for hiding this comment

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

Looks good but you should update the LINQ2 tests as well since the optimization changes the output. Evergreen is failing because of that.

@rstam
Copy link
Contributor Author

rstam commented Mar 31, 2025

Looks good but you should update the LINQ2 tests as well since the optimization changes the output. Evergreen is failing because of that.

I was wondering if that would be the case.

Updating now.

@rstam rstam requested a review from adelinowona April 1, 2025 15:18
Copy link
Contributor

@adelinowona adelinowona left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@sanych-sun sanych-sun left a comment

Choose a reason for hiding this comment

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

LGTM + minor change suggested.

@@ -19,6 +19,9 @@ namespace MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions
{
internal static class AstExpressionExtensions
{
public static bool IsConstant(this AstExpression expression, BsonValue value)
Copy link
Member

Choose a reason for hiding this comment

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

Should we rename it to IsConstantEqualsTo or make it return the constant's value instead:
public static bool IsConstant(this AstExpression expression, out BsonValue value)

@@ -484,9 +484,9 @@ public void GroupBy_where_select_anonymous_type_with_duplicate_accumulators_meth

Assert(query,
1,
"{ $group: { _id: '$A', __agg0: { $first: '$$ROOT'} } }",
"{ $group: { _id: '$A', __agg0: { $first: '$$ROOT'}, __agg1 : { $first : '$B' } } }",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

You could argue that __agg1 is redundant because we've already fetched the entire first $$ROOT so we could compute { $first : "$B" } by calling __agg0.B client-side.

Not sure how easy this would be. Do you think it is worth working on?

@@ -511,9 +511,9 @@ where g.First().B == "Balloon"

Assert(query,
1,
"{ $group: { _id: '$A', __agg0: { $first: '$$ROOT'} } }",
"{ $group: { _id: '$A', __agg0: { $first: '$$ROOT' }, __agg1 : { $first : '$B' } } }",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same comment as above about __agg1.

"{ $match: { '__agg0.B' : 'Balloon' } }",
"{ $project: { Key: '$_id', FirstB: '$__agg0.B', _id: 0 } }");
"{ $project: { Key: '$_id', FirstB: '$__agg1', _id: 0 } }");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same comment as above about __agg1.

@rstam
Copy link
Contributor Author

rstam commented Apr 24, 2025

I've rebased on main now that CSHARP-5563 has been merged to main.

See the comments for a question about whether some further work is called for.

Copy link
Member

@sanych-sun sanych-sun left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants