-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Prune SQL Server OPENJSON's WITH clause #32673
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
63 changes: 63 additions & 0 deletions
63
src/EFCore.SqlServer/Query/Internal/SqlServerSqlTreePruner.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,63 @@ | ||
// 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; | ||
using ColumnInfo = Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServerOpenJsonExpression.ColumnInfo; | ||
|
||
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 SqlServerSqlTreePruner : SqlTreePruner | ||
{ | ||
/// <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 VisitExtension(Expression node) | ||
{ | ||
switch (node) | ||
{ | ||
case SqlServerOpenJsonExpression { ColumnInfos: IReadOnlyList<ColumnInfo> columnInfos } openJson: | ||
var visitedJson = (SqlExpression)Visit(openJson.JsonExpression); | ||
|
||
#pragma warning disable EF1001 // ReferencedColumnMap is pubternal; should be made protected | ||
if (ReferencedColumnMap.TryGetValue(openJson, out var referencedAliases)) | ||
#pragma warning restore EF1001 | ||
{ | ||
List<ColumnInfo>? newColumnInfos = null; | ||
|
||
for (var i = 0; i < columnInfos.Count; i++) | ||
{ | ||
if (referencedAliases.Contains(columnInfos[i].Name)) | ||
{ | ||
newColumnInfos?.Add(columnInfos[i]); | ||
} | ||
else if (newColumnInfos is null) | ||
{ | ||
newColumnInfos = new(); | ||
for (var j = 0; j < i; j++) | ||
{ | ||
newColumnInfos.Add(columnInfos[j]); | ||
} | ||
} | ||
} | ||
|
||
// Not that if we pruned everything, the WITH clause gets removed entirely | ||
return openJson.Update(visitedJson, openJson.Path, newColumnInfos ?? openJson.ColumnInfos); | ||
} | ||
|
||
// There are no references to the OPENJSON expression; remove the WITH clause entirely | ||
return openJson.Update(visitedJson, openJson.Path); | ||
|
||
default: | ||
return base.VisitExtension(node); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -1011,16 +1011,7 @@ public override async Task Json_collection_Any_with_predicate(bool async) | |
FROM [JsonEntitiesBasic] AS [j] | ||
WHERE EXISTS ( | ||
SELECT 1 | ||
FROM OPENJSON([j].[OwnedReferenceRoot], '$.OwnedCollectionBranch') WITH ( | ||
[Date] datetime2 '$.Date', | ||
[Enum] int '$.Enum', | ||
[Enums] nvarchar(max) '$.Enums' AS JSON, | ||
[Fraction] decimal(18,2) '$.Fraction', | ||
[NullableEnum] int '$.NullableEnum', | ||
[NullableEnums] nvarchar(max) '$.NullableEnums' AS JSON, | ||
[OwnedCollectionLeaf] nvarchar(max) '$.OwnedCollectionLeaf' AS JSON, | ||
[OwnedReferenceLeaf] nvarchar(max) '$.OwnedReferenceLeaf' AS JSON | ||
) AS [o] | ||
FROM OPENJSON([j].[OwnedReferenceRoot], '$.OwnedCollectionBranch') WITH ([OwnedReferenceLeaf] nvarchar(max) '$.OwnedReferenceLeaf' AS JSON) AS [o] | ||
WHERE JSON_VALUE([o].[OwnedReferenceLeaf], '$.SomethingSomething') = N'e1_r_c1_r') | ||
"""); | ||
} | ||
|
@@ -1078,11 +1069,7 @@ SELECT JSON_VALUE([o].[OwnedReferenceLeaf], '$.SomethingSomething') AS [c], [o]. | |
FROM OPENJSON([j].[OwnedReferenceRoot], '$.OwnedCollectionBranch') WITH ( | ||
[Date] datetime2 '$.Date', | ||
[Enum] int '$.Enum', | ||
[Enums] nvarchar(max) '$.Enums' AS JSON, | ||
[Fraction] decimal(18,2) '$.Fraction', | ||
[NullableEnum] int '$.NullableEnum', | ||
[NullableEnums] nvarchar(max) '$.NullableEnums' AS JSON, | ||
[OwnedCollectionLeaf] nvarchar(max) '$.OwnedCollectionLeaf' AS JSON, | ||
[OwnedReferenceLeaf] nvarchar(max) '$.OwnedReferenceLeaf' AS JSON | ||
) AS [o] | ||
ORDER BY [o].[Date] DESC | ||
|
@@ -1130,26 +1117,10 @@ public override async Task Json_collection_within_collection_Count(bool async) | |
FROM [JsonEntitiesBasic] AS [j] | ||
WHERE EXISTS ( | ||
SELECT 1 | ||
FROM OPENJSON([j].[OwnedCollectionRoot], '$') WITH ( | ||
[Name] nvarchar(max) '$.Name', | ||
[Names] nvarchar(max) '$.Names' AS JSON, | ||
[Number] int '$.Number', | ||
[Numbers] nvarchar(max) '$.Numbers' AS JSON, | ||
[OwnedCollectionBranch] nvarchar(max) '$.OwnedCollectionBranch' AS JSON, | ||
[OwnedReferenceBranch] nvarchar(max) '$.OwnedReferenceBranch' AS JSON | ||
) AS [o] | ||
FROM OPENJSON([j].[OwnedCollectionRoot], '$') WITH ([OwnedCollectionBranch] nvarchar(max) '$.OwnedCollectionBranch' AS JSON) AS [o] | ||
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. This is a nice example: the 2nd OPENJSON down below gets its WITH clause completely trimmed away, because it's only used for COUNT(*). But its argument referenced |
||
WHERE ( | ||
SELECT COUNT(*) | ||
FROM OPENJSON([o].[OwnedCollectionBranch], '$') WITH ( | ||
[Date] datetime2 '$.Date', | ||
[Enum] int '$.Enum', | ||
[Enums] nvarchar(max) '$.Enums' AS JSON, | ||
[Fraction] decimal(18,2) '$.Fraction', | ||
[NullableEnum] int '$.NullableEnum', | ||
[NullableEnums] nvarchar(max) '$.NullableEnums' AS JSON, | ||
[OwnedCollectionLeaf] nvarchar(max) '$.OwnedCollectionLeaf' AS JSON, | ||
[OwnedReferenceLeaf] nvarchar(max) '$.OwnedReferenceLeaf' AS JSON | ||
) AS [o0]) = 2) | ||
FROM OPENJSON([o].[OwnedCollectionBranch], '$') AS [o0]) = 2) | ||
"""); | ||
} | ||
|
||
|
@@ -1161,14 +1132,7 @@ public override async Task Json_collection_in_projection_with_composition_count( | |
""" | ||
SELECT ( | ||
SELECT COUNT(*) | ||
FROM OPENJSON([j].[OwnedCollectionRoot], '$') WITH ( | ||
[Name] nvarchar(max) '$.Name', | ||
[Names] nvarchar(max) '$.Names' AS JSON, | ||
[Number] int '$.Number', | ||
[Numbers] nvarchar(max) '$.Numbers' AS JSON, | ||
[OwnedCollectionBranch] nvarchar(max) '$.OwnedCollectionBranch' AS JSON, | ||
[OwnedReferenceBranch] nvarchar(max) '$.OwnedReferenceBranch' AS JSON | ||
) AS [o]) | ||
FROM OPENJSON([j].[OwnedCollectionRoot], '$') AS [o]) | ||
FROM [JsonEntitiesBasic] AS [j] | ||
ORDER BY [j].[Id] | ||
"""); | ||
|
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.
😞
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.
I can inline and qualify with SqlServerOpenJsonExpression.ColumnInfos if you think that's better..