-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Std Dev and Variance using EF.Functions (#269)
* Add Std Dev and Variance using EF.Functions
- Loading branch information
1 parent
98bdd0a
commit ec6c702
Showing
8 changed files
with
928 additions
and
385 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...Core.Jet/Query/ExpressionTranslators/Internal/JetAggregateMethodCallTranslatorProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Microsoft.EntityFrameworkCore.Query; | ||
|
||
namespace EntityFrameworkCore.Jet.Query.ExpressionTranslators.Internal; | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// 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 class JetAggregateMethodCallTranslatorProvider : RelationalAggregateMethodCallTranslatorProvider | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// 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 JetAggregateMethodCallTranslatorProvider(RelationalAggregateMethodCallTranslatorProviderDependencies dependencies) | ||
: base(dependencies) | ||
{ | ||
var sqlExpressionFactory = dependencies.SqlExpressionFactory; | ||
var typeMappingSource = dependencies.RelationalTypeMappingSource; | ||
|
||
AddTranslators( | ||
[ | ||
new JetStatisticsAggregateMethodTranslator(sqlExpressionFactory, typeMappingSource) | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...EFCore.Jet/Query/ExpressionTranslators/Internal/JetStatisticsAggregateMethodTranslator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
using Microsoft.EntityFrameworkCore.Query.SqlExpressions; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
|
||
namespace EntityFrameworkCore.Jet.Query.ExpressionTranslators.Internal; | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// 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 class JetStatisticsAggregateMethodTranslator : IAggregateMethodCallTranslator | ||
{ | ||
private readonly ISqlExpressionFactory _sqlExpressionFactory; | ||
private readonly RelationalTypeMapping _doubleTypeMapping; | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// 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 JetStatisticsAggregateMethodTranslator( | ||
ISqlExpressionFactory sqlExpressionFactory, | ||
IRelationalTypeMappingSource typeMappingSource) | ||
{ | ||
_sqlExpressionFactory = sqlExpressionFactory; | ||
_doubleTypeMapping = typeMappingSource.FindMapping(typeof(double))!; | ||
} | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// 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 virtual SqlExpression? Translate( | ||
MethodInfo method, | ||
EnumerableExpression source, | ||
IReadOnlyList<SqlExpression> arguments, | ||
IDiagnosticsLogger<DbLoggerCategory.Query> logger) | ||
{ | ||
// Docs: https://docs.microsoft.com/sql/t-sql/functions/aggregate-functions-transact-sql | ||
|
||
if (method.DeclaringType != typeof(JetDbFunctionsExtensions) | ||
|| source.Selector is not SqlExpression sqlExpression) | ||
{ | ||
return null; | ||
} | ||
|
||
var functionName = method.Name switch | ||
{ | ||
nameof(JetDbFunctionsExtensions.StandardDeviationSample) => "StDev", | ||
nameof(JetDbFunctionsExtensions.StandardDeviationPopulation) => "StDevP", | ||
nameof(JetDbFunctionsExtensions.VarianceSample) => "Var", | ||
nameof(JetDbFunctionsExtensions.VariancePopulation) => "VarP", | ||
_ => null | ||
}; | ||
|
||
if (functionName is null) | ||
{ | ||
return null; | ||
} | ||
|
||
return _sqlExpressionFactory.Function(functionName, [sqlExpression], nullable: true, argumentsPropagateNullability: [false], typeof(double), _doubleTypeMapping); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.