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

Query: Support for GroupBy entity type #29019

Merged
merged 1 commit into from
Sep 13, 2022
Merged

Query: Support for GroupBy entity type #29019

merged 1 commit into from
Sep 13, 2022

Conversation

smitpatel
Copy link
Contributor

Resolves #17653

Copy link
Member

@roji roji left a comment

Choose a reason for hiding this comment

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

LGTM, but just want us to be sure we want to group by all properties, as opposed to by the primary key properties.

.GetProjection(projectionBindingExpression);
foreach (var property in GetAllPropertiesInHierarchy(entityProjectionExpression.EntityType))
{
PopulateGroupByTerms(entityProjectionExpression.BindProperty(property), groupByTerms, groupByAliases, name: null);
Copy link
Member

Choose a reason for hiding this comment

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

So if I understand correctly, we're opting for grouping by all the entity type's properties, as opposed to grouping by its primary key properties (I can see some discussion in #17653).

Wouldn't grouping by the primary key be potentially much more efficient (I can do some quick benchmarking)? On the hand, I understand that in some databases we'd need to use a trick such as MAX() in order to project out the other properties...

BTW what happens with keyless entities, where two rows may have the same property values and therefore get grouped together? Seems like we should block that no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

EF6 did group by all properties so this implementation just aligns the behavior with that. The complexity of utilizing key in query after grouping is quite high, which questions why user is not writing group by PK in first place. Can improve it later.
For keyless entity, fine either way. 2 rows can get grouped together but change tracker has no behavior that they are same entities or different entities so neither behavior is wrong.

Copy link
Member

Choose a reason for hiding this comment

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

Re grouping by all properties, yeah, we can see about improving in the future. It's true the user can explicitly group by the primary key, but that can be said about the feature as a whole.. Specifically, if grouping by all properties is inefficient compared to grouping by primary key (will try to benchmark this), we may be doing users a disservice by providing a slower translation (and they wouldn't know about it) - a translation failure at least forces them to explicitly specify what they want to group by.

Re keyless, if our translation groups two rows together because all their properties are the same, isn't that different from the LINQ to Objects behavior which groups by the reference, and so never returns a group of two?

Copy link
Member

Choose a reason for hiding this comment

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

Comment by @smitpatel: we already compare all properties for the Distinct translation, so GroupBy is consistent with that here.

@@ -109,11 +109,18 @@ public virtual Task GroupBy_is_optimized_when_grouping_by_row_and_projecting_col
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Grouping_by_all_columns_doesnt_produce_a_groupby_statement(bool async)
Copy link
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
public virtual Task Grouping_by_all_columns_doesnt_produce_a_groupby_statement(bool async)
public virtual Task Grouping_by_all_columns_does_not_produce_a_groupby_statement(bool async)


[ConditionalTheory]
[ConditionalTheory(Skip = "Issue#29014")]
Copy link
Member

Choose a reason for hiding this comment

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

No AssertTranslationFailed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should work some day.

@smitpatel smitpatel merged commit 6c07336 into release/7.0 Sep 13, 2022
@smitpatel smitpatel deleted the smit/gbe branch September 13, 2022 00:15
@theCuriousOne
Copy link

Is it possible this to be merged into version 6 as well?

@roji
Copy link
Member

roji commented Sep 17, 2022

@theCuriousOne please see our release planning process, and specifically what qualified for patch releases. This isn't a bug fix, but rather an enhancement.

@theCuriousOne
Copy link

I understand. From my perspective this classifies as "It is a regression from a previous release" since it worked like that in EF6 (Framework). While doing an upgrade from net4.8 to .net 6, I encountered this issue, but more likely you mean regression from previous .net core version.
I managed to rewrite the queries, so I managed.

Thank you all.

@roji
Copy link
Member

roji commented Sep 18, 2022

Old EF6 is a different product, and some code that worked there won't work on EF Core; we don't consider that a regression. Though we indeed do our best to enable as many coding EF6 patterns as possible on EF Core (in fact, that's one of the main reasons this issue got implemented).

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 for GroupBy entityType
3 participants