forked from dotnet/efcore
-
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.
* string.Join (SQL Server and SQLite) * string.Concat (SQL Server and SQLite) * Standard deviation and variance (SQL Server) Closes dotnet#2981 Closes dotnet#28104
- Loading branch information
Showing
19 changed files
with
1,283 additions
and
14 deletions.
There are no files selected for viewing
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
316 changes: 306 additions & 10 deletions
316
src/EFCore.SqlServer/Extensions/SqlServerDbFunctionsExtensions.cs
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
104 changes: 104 additions & 0 deletions
104
src/EFCore.SqlServer/Query/Internal/SqlServerExpression.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,104 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.EntityFrameworkCore.Query.SqlExpressions; | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.Query.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 static class SqlServerExpression | ||
{ | ||
/// <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 static SqlFunctionExpression AggregateFunction( | ||
ISqlExpressionFactory sqlExpressionFactory, | ||
string name, | ||
IEnumerable<SqlExpression> arguments, | ||
EnumerableExpression enumerableExpression, | ||
int enumerableArgumentIndex, | ||
bool nullable, | ||
IEnumerable<bool> argumentsPropagateNullability, | ||
Type returnType, | ||
RelationalTypeMapping? typeMapping = null) | ||
=> new( | ||
name, | ||
ProcessAggregateFunctionArguments(sqlExpressionFactory, arguments, enumerableExpression, enumerableArgumentIndex), | ||
nullable, | ||
argumentsPropagateNullability, | ||
returnType, | ||
typeMapping); | ||
|
||
/// <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 static SqlFunctionExpression AggregateFunctionWithOrdering( | ||
ISqlExpressionFactory sqlExpressionFactory, | ||
string name, | ||
IEnumerable<SqlExpression> arguments, | ||
EnumerableExpression enumerableExpression, | ||
int enumerableArgumentIndex, | ||
bool nullable, | ||
IEnumerable<bool> argumentsPropagateNullability, | ||
Type returnType, | ||
RelationalTypeMapping? typeMapping = null) | ||
=> enumerableExpression.Orderings.Count == 0 | ||
? AggregateFunction(sqlExpressionFactory, name, arguments, enumerableExpression, enumerableArgumentIndex, nullable, argumentsPropagateNullability, returnType, typeMapping) | ||
: new SqlServerSqlFunctionExpression( | ||
name, | ||
ProcessAggregateFunctionArguments(sqlExpressionFactory, arguments, enumerableExpression, enumerableArgumentIndex), | ||
enumerableExpression.Orderings, | ||
nullable, | ||
argumentsPropagateNullability, | ||
returnType, | ||
typeMapping); | ||
|
||
private static IReadOnlyList<SqlExpression> ProcessAggregateFunctionArguments( | ||
ISqlExpressionFactory sqlExpressionFactory, | ||
IEnumerable<SqlExpression> arguments, | ||
EnumerableExpression enumerableExpression, | ||
int enumerableArgumentIndex) | ||
{ | ||
var argIndex = 0; | ||
var typeMappedArguments = new List<SqlExpression>(); | ||
|
||
foreach (var argument in arguments) | ||
{ | ||
var modifiedArgument = sqlExpressionFactory.ApplyDefaultTypeMapping(argument); | ||
|
||
if (argIndex == enumerableArgumentIndex) | ||
{ | ||
// This is the argument representing the enumerable inputs to be aggregated. | ||
// Wrap it with a CASE/WHEN for the predicate and with DISTINCT, if necessary. | ||
if (enumerableExpression.Predicate != null) | ||
{ | ||
modifiedArgument = sqlExpressionFactory.Case( | ||
new List<CaseWhenClause> { new(enumerableExpression.Predicate, modifiedArgument) }, | ||
elseResult: null); | ||
} | ||
|
||
if (enumerableExpression.IsDistinct) | ||
{ | ||
modifiedArgument = new DistinctExpression(modifiedArgument); | ||
} | ||
} | ||
|
||
typeMappedArguments.Add(modifiedArgument); | ||
|
||
argIndex++; | ||
} | ||
|
||
return typeMappedArguments; | ||
} | ||
} |
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
165 changes: 165 additions & 0 deletions
165
src/EFCore.SqlServer/Query/Internal/SqlServerSqlFunctionExpression.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,165 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.EntityFrameworkCore.Query.SqlExpressions; | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.Query.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 SqlServerSqlFunctionExpression : SqlFunctionExpression, IEquatable<SqlServerSqlFunctionExpression> | ||
{ | ||
/// <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 SqlServerSqlFunctionExpression( | ||
string functionName, | ||
IEnumerable<SqlExpression> arguments, | ||
IReadOnlyList<OrderingExpression> aggregateOrderings, | ||
bool nullable, | ||
IEnumerable<bool> argumentsPropagateNullability, | ||
Type type, | ||
RelationalTypeMapping? typeMapping) | ||
: base(functionName, arguments, nullable, argumentsPropagateNullability, type, typeMapping) | ||
=> AggregateOrderings = aggregateOrderings; | ||
|
||
/// <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 IReadOnlyList<OrderingExpression> AggregateOrderings { get; } | ||
|
||
/// <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> | ||
protected override Expression VisitChildren(ExpressionVisitor visitor) | ||
{ | ||
var visitedBase = (SqlFunctionExpression)base.VisitChildren(visitor); | ||
|
||
OrderingExpression[]? visitedAggregateOrderings = null; | ||
|
||
for (var i = 0; i < AggregateOrderings.Count; i++) | ||
{ | ||
var visitedOrdering = (OrderingExpression)visitor.Visit(AggregateOrderings[i]); | ||
if (visitedOrdering != AggregateOrderings[i] && visitedAggregateOrderings is null) | ||
{ | ||
visitedAggregateOrderings = new OrderingExpression[AggregateOrderings.Count]; | ||
|
||
for (var j = 0; j < visitedAggregateOrderings.Length; j++) | ||
{ | ||
visitedAggregateOrderings[j] = AggregateOrderings[j]; | ||
} | ||
} | ||
|
||
if (visitedAggregateOrderings is not null) | ||
{ | ||
visitedAggregateOrderings[i] = visitedOrdering; | ||
} | ||
} | ||
|
||
return visitedBase != this || visitedAggregateOrderings is not null | ||
? new SqlServerSqlFunctionExpression( | ||
Name, | ||
visitedBase.Arguments!, | ||
visitedAggregateOrderings ?? AggregateOrderings, | ||
IsNullable, | ||
ArgumentsPropagateNullability!, | ||
Type, | ||
TypeMapping) | ||
: this; | ||
} | ||
|
||
/// <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 override SqlServerSqlFunctionExpression ApplyTypeMapping(RelationalTypeMapping? typeMapping) | ||
=> new( | ||
Name, | ||
Arguments!, | ||
AggregateOrderings, | ||
IsNullable, | ||
ArgumentsPropagateNullability!, | ||
Type, | ||
typeMapping ?? TypeMapping); | ||
|
||
/// <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 override SqlFunctionExpression Update(SqlExpression? instance, IReadOnlyList<SqlExpression>? arguments) | ||
{ | ||
Check.DebugAssert(arguments is not null, "arguments is not null"); | ||
Check.DebugAssert(instance is null, "instance not supported on SqlServerFunctionExpression"); | ||
|
||
return arguments.SequenceEqual(Arguments!) | ||
? this | ||
: new SqlServerSqlFunctionExpression( | ||
Name, arguments, AggregateOrderings, IsNullable, ArgumentsPropagateNullability!, Type, TypeMapping); | ||
} | ||
|
||
/// <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 SqlFunctionExpression UpdateAggregateOrderings(IReadOnlyList<OrderingExpression> aggregateOrderings) | ||
=> aggregateOrderings.SequenceEqual(AggregateOrderings) | ||
? this | ||
: new SqlServerSqlFunctionExpression( | ||
Name, Arguments!, aggregateOrderings, IsNullable, ArgumentsPropagateNullability!, Type, TypeMapping); | ||
|
||
/// <inheritdoc /> | ||
protected override void Print(ExpressionPrinter expressionPrinter) | ||
{ | ||
base.Print(expressionPrinter); | ||
|
||
if (AggregateOrderings.Count > 0) | ||
{ | ||
expressionPrinter.Append(" WITHIN GROUP (ORDER BY "); | ||
expressionPrinter.VisitCollection(AggregateOrderings); | ||
expressionPrinter.Append(")"); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool Equals(object? obj) | ||
=> obj is SqlServerSqlFunctionExpression sqlServerFunctionExpression && Equals(sqlServerFunctionExpression); | ||
|
||
/// <inheritdoc /> | ||
public virtual bool Equals(SqlServerSqlFunctionExpression? other) | ||
=> ReferenceEquals(this, other) | ||
|| base.Equals(other) && AggregateOrderings.SequenceEqual(other.AggregateOrderings); | ||
|
||
/// <inheritdoc /> | ||
public override int GetHashCode() | ||
{ | ||
var hash = new HashCode(); | ||
|
||
hash.Add(base.GetHashCode()); | ||
|
||
foreach (var orderingExpression in AggregateOrderings) | ||
{ | ||
hash.Add(orderingExpression); | ||
} | ||
|
||
return hash.ToHashCode(); | ||
} | ||
} |
Oops, something went wrong.