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.
Prune SQL Server OPENJSON's WITH clause
Closes dotnet#32668
- Loading branch information
Showing
5 changed files
with
89 additions
and
46 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
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
66 changes: 66 additions & 0 deletions
66
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,66 @@ | ||
// 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); | ||
|
||
if (ReferencedColumnMap.TryGetValue(openJson, out var referencedAliases)) | ||
{ | ||
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]); | ||
} | ||
} | ||
} | ||
|
||
// If we pruned everything, replace the empty list with a null to remove the WITH clause | ||
if (newColumnInfos?.Count == 0) | ||
{ | ||
newColumnInfos = null; | ||
} | ||
|
||
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
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