Skip to content

Commit

Permalink
Query: Translate GroupBy-Aggregate with condition (#21907)
Browse files Browse the repository at this point in the history
Resolves #18836
Resolves #11711
  • Loading branch information
smitpatel authored Aug 3, 2020
1 parent 180283d commit 1f34084
Show file tree
Hide file tree
Showing 9 changed files with 849 additions and 285 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,13 @@ protected override ShapedQueryExpression TranslateAverage(ShapedQueryExpression
return null;
}

var projection = _sqlTranslator.TranslateAverage(newSelector);
var translatedSelector = TranslateExpression(newSelector);
if (translatedSelector == null)
{
return null;
}

var projection = _sqlTranslator.TranslateAverage(translatedSelector);
return projection != null
? AggregateResultShaper(source, projection, throwWhenEmpty: true, resultType)
: null;
Expand Down Expand Up @@ -336,7 +342,7 @@ protected override ShapedQueryExpression TranslateCount(ShapedQueryExpression so

HandleGroupByForAggregate(selectExpression, eraseProjection: true);

var translation = _sqlTranslator.TranslateCount();
var translation = _sqlTranslator.TranslateCount(_sqlExpressionFactory.Fragment("*"));
if (translation == null)
{
return null;
Expand Down Expand Up @@ -692,7 +698,7 @@ protected override ShapedQueryExpression TranslateLongCount(ShapedQueryExpressio

HandleGroupByForAggregate(selectExpression, eraseProjection: true);

var translation = _sqlTranslator.TranslateLongCount();
var translation = _sqlTranslator.TranslateLongCount(_sqlExpressionFactory.Fragment("*"));
if (translation == null)
{
return null;
Expand Down Expand Up @@ -730,7 +736,13 @@ protected override ShapedQueryExpression TranslateMax(ShapedQueryExpression sour
return null;
}

var projection = _sqlTranslator.TranslateMax(newSelector);
var translatedSelector = TranslateExpression(newSelector);
if (translatedSelector == null)
{
return null;
}

var projection = _sqlTranslator.TranslateMax(translatedSelector);

return AggregateResultShaper(source, projection, throwWhenEmpty: true, resultType);
}
Expand All @@ -756,7 +768,13 @@ protected override ShapedQueryExpression TranslateMin(ShapedQueryExpression sour
return null;
}

var projection = _sqlTranslator.TranslateMin(newSelector);
var translatedSelector = TranslateExpression(newSelector);
if (translatedSelector == null)
{
return null;
}

var projection = _sqlTranslator.TranslateMin(translatedSelector);

return AggregateResultShaper(source, projection, throwWhenEmpty: true, resultType);
}
Expand Down Expand Up @@ -1053,7 +1071,13 @@ protected override ShapedQueryExpression TranslateSum(ShapedQueryExpression sour
return null;
}

var projection = _sqlTranslator.TranslateSum(newSelector);
var translatedSelector = TranslateExpression(newSelector);
if (translatedSelector == null)
{
return null;
}

var projection = _sqlTranslator.TranslateSum(translatedSelector);
return projection != null
? AggregateResultShaper(source, projection, throwWhenEmpty: false, resultType)
: null;
Expand Down
348 changes: 224 additions & 124 deletions src/EFCore.Relational/Query/RelationalSqlTranslatingExpressionVisitor.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,14 @@ protected override Expression VisitUnary(UnaryExpression unaryExpression)
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override SqlExpression TranslateLongCount(Expression expression = null)
public override SqlExpression TranslateLongCount(SqlExpression sqlExpression)
{
if (expression != null)
{
// TODO: Translate Count with predicate for GroupBy
return null;
}
Check.NotNull(sqlExpression, nameof(sqlExpression));

return Dependencies.SqlExpressionFactory.ApplyDefaultTypeMapping(
Dependencies.SqlExpressionFactory.Function(
"COUNT_BIG",
new[] { Dependencies.SqlExpressionFactory.Fragment("*") },
new[] { sqlExpression },
nullable: false,
argumentsPropagateNullability: new[] { false },
typeof(long)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,11 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override SqlExpression TranslateAverage(Expression expression)
public override SqlExpression TranslateAverage(SqlExpression sqlExpression)
{
Check.NotNull(expression, nameof(expression));
Check.NotNull(sqlExpression, nameof(sqlExpression));

var visitedExpression = base.TranslateAverage(expression);
var visitedExpression = base.TranslateAverage(sqlExpression);
var argumentType = GetProviderType(visitedExpression);
if (argumentType == typeof(decimal))
{
Expand All @@ -217,11 +217,11 @@ public override SqlExpression TranslateAverage(Expression expression)
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override SqlExpression TranslateMax(Expression expression)
public override SqlExpression TranslateMax(SqlExpression sqlExpression)
{
Check.NotNull(expression, nameof(expression));
Check.NotNull(sqlExpression, nameof(sqlExpression));

var visitedExpression = base.TranslateMax(expression);
var visitedExpression = base.TranslateMax(sqlExpression);
var argumentType = GetProviderType(visitedExpression);
if (argumentType == typeof(DateTimeOffset)
|| argumentType == typeof(decimal)
Expand All @@ -247,11 +247,11 @@ public override SqlExpression TranslateMax(Expression expression)
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override SqlExpression TranslateMin(Expression expression)
public override SqlExpression TranslateMin(SqlExpression sqlExpression)
{
Check.NotNull(expression, nameof(expression));
Check.NotNull(sqlExpression, nameof(sqlExpression));

var visitedExpression = base.TranslateMin(expression);
var visitedExpression = base.TranslateMin(sqlExpression);
var argumentType = GetProviderType(visitedExpression);
if (argumentType == typeof(DateTimeOffset)
|| argumentType == typeof(decimal)
Expand All @@ -271,11 +271,11 @@ public override SqlExpression TranslateMin(Expression expression)
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override SqlExpression TranslateSum(Expression expression)
public override SqlExpression TranslateSum(SqlExpression sqlExpression)
{
Check.NotNull(expression, nameof(expression));
Check.NotNull(sqlExpression, nameof(sqlExpression));

var visitedExpression = base.TranslateSum(expression);
var visitedExpression = base.TranslateSum(sqlExpression);
var argumentType = GetProviderType(visitedExpression);
if (argumentType == typeof(decimal))
{
Expand Down
4 changes: 3 additions & 1 deletion src/EFCore/Query/ShapedQueryCompilingExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ public ConstantVerifyingExpressionVisitor(ITypeMappingSource typeMappingSource)
private bool ValidConstant(ConstantExpression constantExpression)
{
return constantExpression.Value == null
|| _typeMappingSource.FindMapping(constantExpression.Type) != null;
|| _typeMappingSource.FindMapping(constantExpression.Type) != null
|| constantExpression.Value is Array array
&& array.Length == 0;
}

protected override Expression VisitConstant(ConstantExpression constantExpression)
Expand Down
Loading

0 comments on commit 1f34084

Please sign in to comment.