forked from npgsql/efcore.pg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement sum and average over TimeSpan (npgsql#2423)
Closes npgsql#2339
- Loading branch information
Showing
10 changed files
with
282 additions
and
1 deletion.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/EFCore.PG.NodaTime/Extensions/NpgsqlNodaTimeDbFunctionsExtensions.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,44 @@ | ||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.EntityFrameworkCore; | ||
|
||
/// <summary> | ||
/// Provides extension methods supporting NodaTime function translation for PostgreSQL. | ||
/// </summary> | ||
public static class NpgsqlNodaTimeDbFunctionsExtensions | ||
{ | ||
/// <summary> | ||
/// Computes the sum of the non-null input intervals. Corresponds to the PostgreSQL <c>sum</c> aggregate function. | ||
/// </summary> | ||
/// <param name="_">The <see cref="DbFunctions" /> instance.</param> | ||
/// <param name="input">The input values to be summed.</param> | ||
/// <seealso href="https://www.postgresql.org/docs/current/functions-aggregate.html">PostgreSQL documentation for aggregate functions.</seealso> | ||
public static Period? Sum(this DbFunctions _, IEnumerable<Period> input) | ||
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Sum))); | ||
|
||
/// <summary> | ||
/// Computes the sum of the non-null input intervals. Corresponds to the PostgreSQL <c>sum</c> aggregate function. | ||
/// </summary> | ||
/// <param name="_">The <see cref="DbFunctions" /> instance.</param> | ||
/// <param name="input">The input values to be summed.</param> | ||
/// <seealso href="https://www.postgresql.org/docs/current/functions-aggregate.html">PostgreSQL documentation for aggregate functions.</seealso> | ||
public static Duration? Sum(this DbFunctions _, IEnumerable<Duration> input) | ||
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Sum))); | ||
|
||
/// <summary> | ||
/// Computes the average (arithmetic mean) of the non-null input intervals. Corresponds to the PostgreSQL <c>avg</c> aggregate function. | ||
/// </summary> | ||
/// <param name="_">The <see cref="DbFunctions" /> instance.</param> | ||
/// <param name="input">The input values to be computed into an average.</param> | ||
/// <seealso href="https://www.postgresql.org/docs/current/functions-aggregate.html">PostgreSQL documentation for aggregate functions.</seealso> | ||
public static Period? Average(this DbFunctions _, IEnumerable<Period> input) | ||
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Average))); | ||
|
||
/// <summary> | ||
/// Computes the average (arithmetic mean) of the non-null input intervals. Corresponds to the PostgreSQL <c>avg</c> aggregate function. | ||
/// </summary> | ||
/// <param name="_">The <see cref="DbFunctions" /> instance.</param> | ||
/// <param name="input">The input values to be computed into an average.</param> | ||
/// <seealso href="https://www.postgresql.org/docs/current/functions-aggregate.html">PostgreSQL documentation for aggregate functions.</seealso> | ||
public static Duration? Average(this DbFunctions _, IEnumerable<Duration> input) | ||
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Average))); | ||
} |
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
69 changes: 69 additions & 0 deletions
69
src/EFCore.PG.NodaTime/Query/Internal/NpgsqlNodaTimeAggregateMethodCallTranslatorPlugin.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,69 @@ | ||
using Npgsql.EntityFrameworkCore.PostgreSQL.Query; | ||
|
||
namespace Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime.Query.Internal; | ||
|
||
public class NpgsqlNodaTimeAggregateMethodCallTranslatorPlugin : IAggregateMethodCallTranslatorPlugin | ||
{ | ||
public NpgsqlNodaTimeAggregateMethodCallTranslatorPlugin(ISqlExpressionFactory sqlExpressionFactory) | ||
{ | ||
if (sqlExpressionFactory is not NpgsqlSqlExpressionFactory npgsqlSqlExpressionFactory) | ||
{ | ||
throw new ArgumentException($"Must be an {nameof(NpgsqlSqlExpressionFactory)}", nameof(sqlExpressionFactory)); | ||
} | ||
|
||
Translators = new IAggregateMethodCallTranslator[] | ||
{ | ||
new NpgsqlNodaTimeAggregateMethodTranslator(npgsqlSqlExpressionFactory) | ||
}; | ||
} | ||
|
||
public virtual IEnumerable<IAggregateMethodCallTranslator> Translators { get; } | ||
} | ||
|
||
public class NpgsqlNodaTimeAggregateMethodTranslator : IAggregateMethodCallTranslator | ||
{ | ||
private static readonly bool[][] FalseArrays = { Array.Empty<bool>(), new[] { false } }; | ||
|
||
private readonly NpgsqlSqlExpressionFactory _sqlExpressionFactory; | ||
|
||
public NpgsqlNodaTimeAggregateMethodTranslator(NpgsqlSqlExpressionFactory sqlExpressionFactory) | ||
=> _sqlExpressionFactory = sqlExpressionFactory; | ||
|
||
public virtual SqlExpression? Translate( | ||
MethodInfo method, | ||
EnumerableExpression source, | ||
IReadOnlyList<SqlExpression> arguments, | ||
IDiagnosticsLogger<DbLoggerCategory.Query> logger) | ||
{ | ||
if (source.Selector is not SqlExpression sqlExpression || method.DeclaringType != typeof(NpgsqlNodaTimeDbFunctionsExtensions)) | ||
{ | ||
return null; | ||
} | ||
|
||
switch (method.Name) | ||
{ | ||
case nameof(NpgsqlNodaTimeDbFunctionsExtensions.Sum): | ||
return _sqlExpressionFactory.AggregateFunction( | ||
"sum", | ||
new[] { sqlExpression }, | ||
source, | ||
nullable: true, | ||
argumentsPropagateNullability: FalseArrays[1], | ||
returnType: sqlExpression.Type, | ||
sqlExpression.TypeMapping); | ||
|
||
case nameof(NpgsqlNodaTimeDbFunctionsExtensions.Average): | ||
return _sqlExpressionFactory.AggregateFunction( | ||
"avg", | ||
new[] { sqlExpression }, | ||
source, | ||
nullable: true, | ||
argumentsPropagateNullability: FalseArrays[1], | ||
returnType: sqlExpression.Type, | ||
sqlExpression.TypeMapping); | ||
|
||
default: | ||
return null; | ||
} | ||
} | ||
} |
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
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
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
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