@@ -169,7 +169,7 @@ private static Expression HandleAll(HandlerContext handlerContext)
169
169
var sqlTranslatingVisitor
170
170
= handlerContext . CreateSqlTranslatingVisitor ( ) ;
171
171
172
- PrepareSelectExpressionForAggregate ( handlerContext . SelectExpression ) ;
172
+ PrepareSelectExpressionForAggregate ( handlerContext ) ;
173
173
174
174
var predicate
175
175
= sqlTranslatingVisitor . Visit (
@@ -224,7 +224,7 @@ private static Expression HandleAverage(HandlerContext handlerContext)
224
224
if ( ! handlerContext . QueryModelVisitor . RequiresClientProjection
225
225
&& handlerContext . SelectExpression . Projection . Count == 1 )
226
226
{
227
- PrepareSelectExpressionForAggregate ( handlerContext . SelectExpression ) ;
227
+ PrepareSelectExpressionForAggregate ( handlerContext ) ;
228
228
229
229
var expression = handlerContext . SelectExpression . Projection . First ( ) ;
230
230
@@ -344,7 +344,7 @@ var collectionSelectExpression
344
344
345
345
private static Expression HandleCount ( HandlerContext handlerContext )
346
346
{
347
- PrepareSelectExpressionForAggregate ( handlerContext . SelectExpression ) ;
347
+ PrepareSelectExpressionForAggregate ( handlerContext ) ;
348
348
349
349
handlerContext . SelectExpression
350
350
. SetProjectionExpression (
@@ -480,8 +480,7 @@ var sqlExpression
480
480
&& shapedQueryMethod . Method . MethodIsClosedFormOf (
481
481
handlerContext . QueryModelVisitor . QueryCompilationContext . QueryMethodProvider . ShapedQueryMethod ) )
482
482
{
483
- var selectExpression = handlerContext . SelectExpression ;
484
- PrepareSelectExpressionForAggregate ( selectExpression ) ;
483
+ PrepareSelectExpressionForAggregate ( handlerContext ) ;
485
484
486
485
// GroupBy Aggregate
487
486
// TODO: InjectParameters type Expression.
@@ -518,6 +517,8 @@ var sqlExpression
518
517
break ;
519
518
}
520
519
520
+ var selectExpression = handlerContext . SelectExpression ;
521
+
521
522
if ( key != null
522
523
|| groupResultOperator . KeySelector is ConstantExpression
523
524
|| groupResultOperator . KeySelector is ParameterExpression )
@@ -594,16 +595,14 @@ var sqlExpression
594
595
595
596
if ( sqlExpression != null )
596
597
{
597
- var selectExpression = handlerContext . SelectExpression ;
598
-
599
- PrepareSelectExpressionForAggregate ( selectExpression ) ;
598
+ PrepareSelectExpressionForAggregate ( handlerContext ) ;
600
599
601
600
sqlExpression
602
601
= sqlTranslatingExpressionVisitor . Visit ( groupResultOperator . KeySelector ) ;
603
602
604
603
var columns = ( sqlExpression as ConstantExpression ) ? . Value as Expression [ ] ?? new [ ] { sqlExpression } ;
605
604
606
- selectExpression . PrependToOrderBy ( columns . Select ( c => new Ordering ( c , OrderingDirection . Asc ) ) ) ;
605
+ handlerContext . SelectExpression . PrependToOrderBy ( columns . Select ( c => new Ordering ( c , OrderingDirection . Asc ) ) ) ;
607
606
608
607
handlerContext . QueryModelVisitor . RequiresStreamingGroupResultOperator = true ;
609
608
}
@@ -834,7 +833,7 @@ private static Expression HandleLast(HandlerContext handlerContext)
834
833
835
834
private static Expression HandleLongCount ( HandlerContext handlerContext )
836
835
{
837
- PrepareSelectExpressionForAggregate ( handlerContext . SelectExpression ) ;
836
+ PrepareSelectExpressionForAggregate ( handlerContext ) ;
838
837
839
838
handlerContext . SelectExpression
840
839
. SetProjectionExpression (
@@ -853,7 +852,7 @@ private static Expression HandleMin(HandlerContext handlerContext)
853
852
if ( ! handlerContext . QueryModelVisitor . RequiresClientProjection
854
853
&& handlerContext . SelectExpression . Projection . Count == 1 )
855
854
{
856
- PrepareSelectExpressionForAggregate ( handlerContext . SelectExpression ) ;
855
+ PrepareSelectExpressionForAggregate ( handlerContext ) ;
857
856
var expression = handlerContext . SelectExpression . Projection . First ( ) ;
858
857
859
858
if ( ! ( expression . RemoveConvert ( ) is SelectExpression ) )
@@ -882,7 +881,7 @@ private static Expression HandleMax(HandlerContext handlerContext)
882
881
if ( ! handlerContext . QueryModelVisitor . RequiresClientProjection
883
882
&& handlerContext . SelectExpression . Projection . Count == 1 )
884
883
{
885
- PrepareSelectExpressionForAggregate ( handlerContext . SelectExpression ) ;
884
+ PrepareSelectExpressionForAggregate ( handlerContext ) ;
886
885
var expression = handlerContext . SelectExpression . Projection . First ( ) ;
887
886
888
887
if ( ! ( expression . RemoveConvert ( ) is SelectExpression ) )
@@ -971,7 +970,7 @@ private static Expression HandleSum(HandlerContext handlerContext)
971
970
if ( ! handlerContext . QueryModelVisitor . RequiresClientProjection
972
971
&& handlerContext . SelectExpression . Projection . Count == 1 )
973
972
{
974
- PrepareSelectExpressionForAggregate ( handlerContext . SelectExpression ) ;
973
+ PrepareSelectExpressionForAggregate ( handlerContext ) ;
975
974
var expression = handlerContext . SelectExpression . Projection . First ( ) ;
976
975
977
976
if ( ! ( expression . RemoveConvert ( ) is SelectExpression ) )
@@ -1046,17 +1045,26 @@ private static void SetConditionAsProjection(
1046
1045
typeof ( bool ) ) ) ;
1047
1046
}
1048
1047
1049
- private static void PrepareSelectExpressionForAggregate ( SelectExpression selectExpression )
1048
+ private static void PrepareSelectExpressionForAggregate ( HandlerContext handlerContext )
1050
1049
{
1051
- if ( selectExpression . IsDistinct
1052
- || selectExpression . Limit != null
1053
- || selectExpression . Offset != null )
1050
+ var legacyBehavior12351 = AppContext . TryGetSwitch ( "Microsoft.EntityFrameworkCore.Issue12351" , out var isEnabled ) && isEnabled ;
1051
+
1052
+ if ( handlerContext . SelectExpression . IsDistinct
1053
+ || handlerContext . SelectExpression . Limit != null
1054
+ || handlerContext . SelectExpression . Offset != null
1055
+ || ( handlerContext . SelectExpression . GroupBy . Any ( )
1056
+ && ! IsGroupByAggregate ( handlerContext . QueryModel )
1057
+ && ! legacyBehavior12351 ) )
1054
1058
{
1055
- selectExpression . PushDownSubquery ( ) ;
1056
- selectExpression . ExplodeStarProjection ( ) ;
1059
+ handlerContext . SelectExpression . PushDownSubquery ( ) ;
1060
+ handlerContext . SelectExpression . ExplodeStarProjection ( ) ;
1057
1061
}
1058
1062
}
1059
1063
1064
+ private static bool IsGroupByAggregate ( QueryModel queryModel )
1065
+ => queryModel . MainFromClause . FromExpression is QuerySourceReferenceExpression mainFromClauseQsre
1066
+ && mainFromClauseQsre . ReferencedQuerySource . ItemType . IsGrouping ( ) ;
1067
+
1060
1068
private static Expression UnwrapAliasExpression ( Expression expression )
1061
1069
=> ( expression as AliasExpression ) ? . Expression ?? expression ;
1062
1070
0 commit comments