-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix to #28648 - Json/Query: translate element access of a json array
Converting indexer over list/array into ElementAt, so that nav expansion understands it and can perform pushown and inject MaterializeCollectionNavigation expression where necessary. In translation phase we recognize the pattern that nav expansion creates and if the root is JsonQueryExpression, we apply collection index over it. JsonQueryExpression path segment now consists of two components - string representing JSON property name and SqlExpression representing collection index (it can be constant, parameter or any arbitrary expression that resolves to int) Deduplication is heavily restricted currently - we only de-duplicate projections whose additional path consists of JSON property accesses only (no collection indexes allowed). All queries projecting entities that need JSON array access must be set to NoTracking (for now). This is because we don't flow those collection index values into shaper. Instead, the ordinal keys are filled with dummy values, which prohibits us from doing proper change tracking. Fixes #28648
- Loading branch information
Showing
18 changed files
with
942 additions
and
28 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
59 changes: 59 additions & 0 deletions
59
...Core.Relational/Query/Internal/CollectionIndexerToElementAtConvertingExpressionVisitor.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,59 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Linq.Expressions; | ||
|
||
namespace Microsoft.EntityFrameworkCore.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> | ||
internal class CollectionIndexerToElementAtConvertingExpressionVisitor : ExpressionVisitor | ||
{ | ||
protected override Expression VisitMethodCall(MethodCallExpression methodCallExpression) | ||
{ | ||
if (methodCallExpression.Method.Name == "get_Item" | ||
&& !methodCallExpression.Method.IsStatic | ||
&& methodCallExpression.Method.DeclaringType != null | ||
&& ((methodCallExpression.Method.DeclaringType.IsGenericType | ||
&& methodCallExpression.Method.DeclaringType.GetGenericTypeDefinition() == typeof(List<>)) | ||
|| methodCallExpression.Method.DeclaringType.IsArray)) | ||
{ | ||
var source = Visit(methodCallExpression.Object!); | ||
var index = Visit(methodCallExpression.Arguments[0]); | ||
var sourceTypeArgument = source.Type.GetSequenceType(); | ||
|
||
return Expression.Call( | ||
QueryableMethods.ElementAt.MakeGenericMethod(sourceTypeArgument), | ||
Expression.Call( | ||
QueryableMethods.AsQueryable.MakeGenericMethod(sourceTypeArgument), | ||
source), | ||
index); | ||
} | ||
|
||
return base.VisitMethodCall(methodCallExpression); | ||
} | ||
|
||
protected override Expression VisitBinary(BinaryExpression binaryExpression) | ||
{ | ||
if (binaryExpression.NodeType == ExpressionType.ArrayIndex | ||
&& binaryExpression.Left.Type != typeof(byte[])) | ||
{ | ||
var source = Visit(binaryExpression.Left); | ||
var index = Visit(binaryExpression.Right); | ||
var sourceTypeArgument = source.Type.GetSequenceType(); | ||
|
||
return Expression.Call( | ||
QueryableMethods.ElementAt.MakeGenericMethod(sourceTypeArgument), | ||
Expression.Call( | ||
QueryableMethods.AsQueryable.MakeGenericMethod(sourceTypeArgument), | ||
source), | ||
index); | ||
} | ||
|
||
return base.VisitBinary(binaryExpression); | ||
} | ||
} |
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
Oops, something went wrong.