New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cosmos: Fix reference nested owned entities in queries #16276
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -4,12 +4,14 @@ | |
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Conventions; | ||
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
using Microsoft.EntityFrameworkCore.Internal; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
|
@@ -47,13 +49,12 @@ public class CosmosShapedQueryCompilingExpressionVisitor : ShapedQueryCompilingE | |
|
||
protected override Expression VisitShapedQueryExpression(ShapedQueryExpression shapedQueryExpression) | ||
{ | ||
var shaperBody = InjectEntityMaterializer(shapedQueryExpression.ShaperExpression); | ||
var selectExpression = (SelectExpression)shapedQueryExpression.QueryExpression; | ||
selectExpression.ApplyProjection(); | ||
var jObjectParameter = Expression.Parameter(typeof(JObject), "jObject"); | ||
|
||
shaperBody = new CosmosProjectionBindingRemovingExpressionVisitor(selectExpression, jObjectParameter) | ||
.Visit(shaperBody); | ||
var jObjectParameter = Expression.Parameter(typeof(JObject), "jObject"); | ||
var shaperBody = new CosmosProjectionBindingRemovingExpressionVisitor(selectExpression, jObjectParameter, this) | ||
.Visit(shapedQueryExpression.ShaperExpression); | ||
|
||
var shaperLambda = Expression.Lambda( | ||
shaperBody, | ||
|
@@ -75,8 +76,6 @@ protected override Expression VisitShapedQueryExpression(ShapedQueryExpression s | |
|
||
private class CosmosProjectionBindingRemovingExpressionVisitor : ExpressionVisitor | ||
{ | ||
private SelectExpression _selectExpression; | ||
private readonly ParameterExpression _jObjectParameter; | ||
private static readonly MethodInfo _getItemMethodInfo | ||
= typeof(JObject).GetTypeInfo().GetRuntimeProperties() | ||
.Single(pi => pi.Name == "Item" && pi.GetIndexParameters()[0].ParameterType == typeof(string)) | ||
|
@@ -88,14 +87,24 @@ private class CosmosProjectionBindingRemovingExpressionVisitor : ExpressionVisit | |
= typeof(CosmosProjectionBindingRemovingExpressionVisitor).GetTypeInfo().GetRuntimeMethods() | ||
.Single(mi => mi.Name == nameof(IsNull)); | ||
|
||
private readonly SelectExpression _selectExpression; | ||
private ParameterExpression _jObjectParameter; | ||
private readonly CosmosShapedQueryCompilingExpressionVisitor _shapedQueryCompilingExpressionVisitor; | ||
|
||
private readonly IDictionary<ProjectionBindingExpression, Expression> _valueBufferBindings | ||
= new Dictionary<ProjectionBindingExpression, Expression>(); | ||
private readonly IDictionary<ParameterExpression, Expression> _materializationContextBindings | ||
= new Dictionary<ParameterExpression, Expression>(); | ||
private int _currentEntityIndex; | ||
|
||
public CosmosProjectionBindingRemovingExpressionVisitor( | ||
SelectExpression selectExpression, ParameterExpression jObjectParameter) | ||
SelectExpression selectExpression, | ||
ParameterExpression jObjectParameter, | ||
CosmosShapedQueryCompilingExpressionVisitor shapedQueryCompilingExpressionVisitor) | ||
{ | ||
_selectExpression = selectExpression; | ||
_jObjectParameter = jObjectParameter; | ||
_shapedQueryCompilingExpressionVisitor = shapedQueryCompilingExpressionVisitor; | ||
} | ||
|
||
protected override Expression VisitBinary(BinaryExpression binaryExpression) | ||
|
@@ -105,13 +114,8 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression) | |
&& parameterExpression.Type == typeof(MaterializationContext)) | ||
{ | ||
var newExpression = (NewExpression)binaryExpression.Right; | ||
var projectionBindingExpression = (ProjectionBindingExpression)newExpression.Arguments[0]; | ||
var projectionIndex = (int)GetProjectionIndex(projectionBindingExpression); | ||
var projection = _selectExpression.Projection[projectionIndex]; | ||
|
||
_materializationContextBindings[parameterExpression] = Expression.Convert( | ||
CreateReadJTokenExpression(_jObjectParameter, projection.Alias), | ||
typeof(JObject)); | ||
_materializationContextBindings[parameterExpression] = _jObjectParameter; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this work when you have projected out multiple entities in projection There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should. Is there a test for that? |
||
|
||
var updatedExpression = Expression.New(newExpression.Constructor, | ||
Expression.Constant(ValueBuffer.Empty), | ||
|
@@ -128,7 +132,6 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression) | |
return memberExpression.Assign(Visit(binaryExpression.Right)); | ||
} | ||
|
||
|
||
return base.VisitBinary(binaryExpression); | ||
} | ||
|
||
|
@@ -181,9 +184,100 @@ protected override Expression VisitExtension(Expression extensionExpression) | |
projectionBindingExpression.Type); | ||
} | ||
|
||
if (extensionExpression is EntityShaperExpression shaperExpression) | ||
{ | ||
_currentEntityIndex++; | ||
|
||
var jObjectVariable = Expression.Variable(typeof(JObject), | ||
"jObject" + _currentEntityIndex); | ||
var variables = new List<ParameterExpression> { jObjectVariable }; | ||
|
||
var expressions = new List<Expression>(); | ||
|
||
if (shaperExpression.ParentNavigation == null) | ||
{ | ||
var projectionIndex = (int)GetProjectionIndex((ProjectionBindingExpression)shaperExpression.ValueBufferExpression); | ||
var projection = _selectExpression.Projection[projectionIndex]; | ||
|
||
expressions.Add( | ||
Expression.Assign( | ||
jObjectVariable, | ||
Expression.TypeAs( | ||
CreateReadJTokenExpression(_jObjectParameter, projection.Alias), | ||
typeof(JObject)))); | ||
|
||
shaperExpression = shaperExpression.Update( | ||
shaperExpression.ValueBufferExpression, | ||
GetNestedShapers(shaperExpression.EntityType, shaperExpression.ValueBufferExpression)); | ||
} | ||
else | ||
{ | ||
var methodCallExpression = (MethodCallExpression)shaperExpression.ValueBufferExpression; | ||
Debug.Assert(methodCallExpression.Method.IsGenericMethod | ||
&& methodCallExpression.Method.GetGenericMethodDefinition() == EntityMaterializerSource.TryReadValueMethod); | ||
|
||
var navigation = (INavigation)((ConstantExpression)methodCallExpression.Arguments[2]).Value; | ||
|
||
expressions.Add( | ||
Expression.Assign( | ||
jObjectVariable, | ||
Expression.TypeAs( | ||
CreateReadJTokenExpression(_jObjectParameter, navigation.GetTargetType().GetCosmosContainingPropertyName()), | ||
typeof(JObject)))); | ||
} | ||
|
||
var parentJObject = _jObjectParameter; | ||
_jObjectParameter = jObjectVariable; | ||
expressions.Add(Expression.Condition( | ||
Expression.Equal(jObjectVariable, Expression.Constant(null, jObjectVariable.Type)), | ||
Expression.Constant(null, shaperExpression.Type), | ||
Visit(_shapedQueryCompilingExpressionVisitor.InjectEntityMaterializer(shaperExpression)))); | ||
_jObjectParameter = parentJObject; | ||
|
||
return Expression.Block( | ||
shaperExpression.Type, | ||
variables, | ||
expressions); | ||
} | ||
|
||
if (extensionExpression is CollectionShaperExpression collectionShaperExpression) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
return base.VisitExtension(extensionExpression); | ||
} | ||
|
||
private List<EntityShaperExpression> GetNestedShapers(IEntityType entityType, Expression valueBufferExpression) | ||
{ | ||
var nestedEntities = new List<EntityShaperExpression>(); | ||
foreach (var ownedNavigation in entityType.GetNavigations().Concat(entityType.GetDerivedNavigations())) | ||
{ | ||
var fk = ownedNavigation.ForeignKey; | ||
if (!fk.IsOwnership | ||
|| ownedNavigation.IsDependentToPrincipal() | ||
|| fk.DeclaringEntityType.IsDocumentRoot()) | ||
{ | ||
continue; | ||
} | ||
|
||
var targetType = ownedNavigation.GetTargetType(); | ||
|
||
var nestedValueBufferExpression = _shapedQueryCompilingExpressionVisitor.CreateReadValueExpression( | ||
valueBufferExpression, | ||
valueBufferExpression.Type, | ||
ownedNavigation.GetIndex(), | ||
ownedNavigation); | ||
|
||
var nestedShaper = new EntityShaperExpression( | ||
targetType, nestedValueBufferExpression, nullable: true, ownedNavigation, | ||
GetNestedShapers(targetType, nestedValueBufferExpression)); | ||
nestedEntities.Add(nestedShaper); | ||
} | ||
|
||
return nestedEntities.Count == 0 ? null : nestedEntities; | ||
} | ||
|
||
private object GetProjectionIndex(ProjectionBindingExpression projectionBindingExpression) | ||
{ | ||
return projectionBindingExpression.ProjectionMember != null | ||
|
@@ -216,7 +310,7 @@ private static Expression CreateReadJTokenExpression(Expression jObjectExpressio | |
return CreateGetStoreValueExpression(jObjectExpression, storeName, property.GetTypeMapping(), property.ClrType); | ||
} | ||
|
||
public static Expression CreateGetStoreValueExpression( | ||
private static Expression CreateGetStoreValueExpression( | ||
Expression jObjectExpression, | ||
string storeName, | ||
CoreTypeMapping typeMapping, | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we keep injecting materializer and binding removal separate?
We can have another visitor which inject nested entityshapers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was my original design, but we need to add the jObject variable before each materializer is injected.