Skip to content

Commit

Permalink
Extract SelectExpression construction out to RelationalQueryableMetho…
Browse files Browse the repository at this point in the history
…dTranslatingEV (#32877)
  • Loading branch information
roji committed Jan 22, 2024
1 parent e59d055 commit 4380d3e
Show file tree
Hide file tree
Showing 10 changed files with 898 additions and 890 deletions.
20 changes: 8 additions & 12 deletions src/EFCore.InMemory/Query/Internal/InMemoryQueryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public virtual void ReplaceProjection(IReadOnlyDictionary<ProjectionMember, Expr
EntityProjectionExpression AddEntityProjection(EntityProjectionExpression entityProjectionExpression)
{
var readExpressionMap = new Dictionary<IProperty, MethodCallExpression>();
foreach (var property in GetAllPropertiesInHierarchy(entityProjectionExpression.EntityType))
foreach (var property in entityProjectionExpression.EntityType.GetAllPropertiesInHierarchy())
{
var expression = entityProjectionExpression.BindProperty(property);
selectorExpressions.Add(expression);
Expand Down Expand Up @@ -266,7 +266,7 @@ public virtual void ApplyProjection()
case EntityProjectionExpression entityProjectionExpression:
{
var indexMap = new Dictionary<IProperty, int>();
foreach (var property in GetAllPropertiesInHierarchy(entityProjectionExpression.EntityType))
foreach (var property in entityProjectionExpression.EntityType.GetAllPropertiesInHierarchy())
{
selectorExpressions.Add(entityProjectionExpression.BindProperty(property));
indexMap[property] = selectorExpressions.Count - 1;
Expand Down Expand Up @@ -307,7 +307,7 @@ public virtual void ApplyProjection()
if (expression is EntityProjectionExpression entityProjectionExpression)
{
var indexMap = new Dictionary<IProperty, int>();
foreach (var property in GetAllPropertiesInHierarchy(entityProjectionExpression.EntityType))
foreach (var property in entityProjectionExpression.EntityType.GetAllPropertiesInHierarchy())
{
selectorExpressions.Add(entityProjectionExpression.BindProperty(property));
indexMap[property] = selectorExpressions.Count - 1;
Expand Down Expand Up @@ -384,7 +384,7 @@ public virtual void ApplySetOperation(MethodInfo setOperationMethodInfo, InMemor
&& value2 is EntityProjectionExpression entityProjection2)
{
var map = new Dictionary<IProperty, MethodCallExpression>();
foreach (var property in GetAllPropertiesInHierarchy(entityProjection1.EntityType))
foreach (var property in entityProjection1.EntityType.GetAllPropertiesInHierarchy())
{
var expressionToAdd1 = entityProjection1.BindProperty(property);
var expressionToAdd2 = entityProjection2.BindProperty(property);
Expand Down Expand Up @@ -674,7 +674,7 @@ public virtual StructuralTypeShaperExpression AddNavigationToWeakEntityType(
var outerIndex = selectorExpressions.Count;
var innerEntityProjection = (EntityProjectionExpression)innerQueryExpression._projectionMapping[new ProjectionMember()];
var innerReadExpressionMap = new Dictionary<IProperty, MethodCallExpression>();
foreach (var property in GetAllPropertiesInHierarchy(innerEntityProjection.EntityType))
foreach (var property in innerEntityProjection.EntityType.GetAllPropertiesInHierarchy())
{
var propertyExpression = innerEntityProjection.BindProperty(property);
propertyExpression = MakeReadValueNullable(propertyExpression);
Expand Down Expand Up @@ -874,7 +874,7 @@ private static Expression GetGroupingKey(Expression key, List<Expression> groupi
(EntityProjectionExpression)((InMemoryQueryExpression)projectionBindingExpression.QueryExpression)
.GetProjection(projectionBindingExpression);
var readExpressions = new Dictionary<IProperty, MethodCallExpression>();
foreach (var property in GetAllPropertiesInHierarchy(entityProjectionExpression.EntityType))
foreach (var property in entityProjectionExpression.EntityType.GetAllPropertiesInHierarchy())
{
readExpressions[property] = (MethodCallExpression)GetGroupingKey(
entityProjectionExpression.BindProperty(property),
Expand Down Expand Up @@ -1156,10 +1156,6 @@ private void ConvertToEnumerable()
private MethodCallExpression CreateReadValueExpression(Type type, int index, IPropertyBase? property)
=> (MethodCallExpression)_valueBufferParameter.CreateValueBufferReadValueExpression(type, index, property);

private static IEnumerable<IProperty> GetAllPropertiesInHierarchy(IEntityType entityType)
=> entityType.GetAllBaseTypes().Concat(entityType.GetDerivedTypesInclusive())
.SelectMany(t => t.GetDeclaredProperties());

private static IPropertyBase? InferPropertyFromInner(Expression expression)
=> expression is MethodCallExpression { Method.IsGenericMethod: true } methodCallExpression
&& methodCallExpression.Method.GetGenericMethodDefinition() == ExpressionExtensions.ValueBufferTryReadValueMethod
Expand All @@ -1169,7 +1165,7 @@ private static IEnumerable<IProperty> GetAllPropertiesInHierarchy(IEntityType en
private static EntityProjectionExpression MakeEntityProjectionNullable(EntityProjectionExpression entityProjectionExpression)
{
var readExpressionMap = new Dictionary<IProperty, MethodCallExpression>();
foreach (var property in GetAllPropertiesInHierarchy(entityProjectionExpression.EntityType))
foreach (var property in entityProjectionExpression.EntityType.GetAllPropertiesInHierarchy())
{
readExpressionMap[property] = MakeReadValueNullable(entityProjectionExpression.BindProperty(property));
}
Expand Down Expand Up @@ -1277,7 +1273,7 @@ private EntityProjectionExpression TraverseEntityProjection(
bool makeNullable)
{
var readExpressionMap = new Dictionary<IProperty, MethodCallExpression>();
foreach (var property in GetAllPropertiesInHierarchy(entityProjectionExpression.EntityType))
foreach (var property in entityProjectionExpression.EntityType.GetAllPropertiesInHierarchy())
{
var expression = entityProjectionExpression.BindProperty(property);
if (makeNullable)
Expand Down
29 changes: 2 additions & 27 deletions src/EFCore.Relational/Query/ISqlExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;

namespace Microsoft.EntityFrameworkCore.Query;
Expand Down Expand Up @@ -459,33 +461,6 @@ SqlFunctionExpression NiladicFunction(
/// <returns>An expression representing a SQL token.</returns>
SqlFragmentExpression Fragment(string sql);

/// <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>
[EntityFrameworkInternal]
SelectExpression Select(SqlExpression? projection, SqlAliasManager sqlAliasManager);

/// <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>
[EntityFrameworkInternal]
SelectExpression Select(IEntityType entityType, SqlAliasManager sqlAliasManager);

/// <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>
[EntityFrameworkInternal]
SelectExpression Select(IEntityType entityType, TableExpressionBase tableExpressionBase, SqlAliasManager sqlAliasManager);

/// <summary>
/// Attempts to creates a new expression that returns the smallest value from a list of expressions, e.g. an invocation of the
/// <c>LEAST</c> SQL function.
Expand Down
Loading

0 comments on commit 4380d3e

Please sign in to comment.