-
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 #18147 - Where bool column needs to convert to equality when v…
…alue converter is applied Added a postprocessor step that identifies bool columns with value converters inside a predicate and modifies them accordingly. Fixes #18147
- Loading branch information
Showing
14 changed files
with
553 additions
and
13 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
43 changes: 43 additions & 0 deletions
43
src/EFCore.Cosmos/Query/CosmosQueryTranslationPostprocessor.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,43 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Linq.Expressions; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Cosmos.Query.Internal; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Cosmos.Query | ||
{ | ||
/// <inheritdoc /> | ||
public class CosmosQueryTranslationPostprocessor : QueryTranslationPostprocessor | ||
{ | ||
private readonly ISqlExpressionFactory _sqlExpressionFactory; | ||
|
||
/// <summary> | ||
/// Creates a new instance of the <see cref="CosmosQueryTranslationPostprocessor" /> class. | ||
/// </summary> | ||
/// <param name="dependencies"> Parameter object containing dependencies for this class. </param> | ||
/// <param name="sqlExpressionFactory"> The SqlExpressionFactory object to use. </param> | ||
/// <param name="queryCompilationContext"> The query compilation context object to use. </param> | ||
public CosmosQueryTranslationPostprocessor( | ||
[NotNull] QueryTranslationPostprocessorDependencies dependencies, | ||
[NotNull] ISqlExpressionFactory sqlExpressionFactory, | ||
[NotNull] QueryCompilationContext queryCompilationContext) | ||
: base(dependencies, queryCompilationContext) | ||
{ | ||
Check.NotNull(sqlExpressionFactory, nameof(sqlExpressionFactory)); | ||
|
||
_sqlExpressionFactory = sqlExpressionFactory; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override Expression Process(Expression query) | ||
{ | ||
query = base.Process(query); | ||
query = new CosmosValueConverterCompensatingExpressionVisitor(_sqlExpressionFactory).Visit(query); | ||
|
||
return query; | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/EFCore.Cosmos/Query/Internal/CosmosQueryTranslationPostprocessorFactory.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 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal | ||
{ | ||
/// <summary> | ||
/// <para> | ||
/// 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. | ||
/// </para> | ||
/// <para> | ||
/// The service lifetime is <see cref="ServiceLifetime.Singleton" />. This means a single instance | ||
/// is used by many <see cref="DbContext" /> instances. The implementation must be thread-safe. | ||
/// This service cannot depend on services registered as <see cref="ServiceLifetime.Scoped" />. | ||
/// </para> | ||
/// </summary> | ||
public class CosmosQueryTranslationPostprocessorFactory : IQueryTranslationPostprocessorFactory | ||
{ | ||
private readonly QueryTranslationPostprocessorDependencies _dependencies; | ||
private readonly ISqlExpressionFactory _sqlExpressionFactory; | ||
|
||
/// <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 CosmosQueryTranslationPostprocessorFactory( | ||
[NotNull] QueryTranslationPostprocessorDependencies dependencies, | ||
[NotNull] ISqlExpressionFactory sqlExpressionFactory) | ||
{ | ||
_dependencies = dependencies; | ||
_sqlExpressionFactory = sqlExpressionFactory; | ||
} | ||
|
||
/// <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 virtual QueryTranslationPostprocessor Create(QueryCompilationContext queryCompilationContext) | ||
{ | ||
Check.NotNull(queryCompilationContext, nameof(queryCompilationContext)); | ||
|
||
return new CosmosQueryTranslationPostprocessor( | ||
_dependencies, | ||
_sqlExpressionFactory, | ||
queryCompilationContext); | ||
} | ||
} | ||
} |
134 changes: 134 additions & 0 deletions
134
src/EFCore.Cosmos/Query/Internal/CosmosValueConverterCompensatingExpressionVisitor.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,134 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq.Expressions; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Cosmos.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 CosmosValueConverterCompensatingExpressionVisitor : ExpressionVisitor | ||
{ | ||
private readonly ISqlExpressionFactory _sqlExpressionFactory; | ||
|
||
/// <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 CosmosValueConverterCompensatingExpressionVisitor( | ||
[NotNull] ISqlExpressionFactory sqlExpressionFactory) | ||
{ | ||
_sqlExpressionFactory = sqlExpressionFactory; | ||
} | ||
|
||
/// <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 extensionExpression) | ||
{ | ||
return extensionExpression switch | ||
{ | ||
ShapedQueryExpression shapedQueryExpression => VisitShapedQueryExpression(shapedQueryExpression), | ||
ReadItemExpression readItemExpression => readItemExpression, | ||
SelectExpression selectExpression => VisitSelect(selectExpression), | ||
SqlConditionalExpression sqlConditionalExpression => VisitSqlConditional(sqlConditionalExpression), | ||
_ => base.VisitExtension(extensionExpression), | ||
}; | ||
} | ||
|
||
private Expression VisitShapedQueryExpression(ShapedQueryExpression shapedQueryExpression) | ||
{ | ||
return shapedQueryExpression.Update( | ||
Visit(shapedQueryExpression.QueryExpression), shapedQueryExpression.ShaperExpression); | ||
} | ||
|
||
private Expression VisitSelect(SelectExpression selectExpression) | ||
{ | ||
Check.NotNull(selectExpression, nameof(selectExpression)); | ||
|
||
var changed = false; | ||
|
||
var projections = new List<ProjectionExpression>(); | ||
foreach (var item in selectExpression.Projection) | ||
{ | ||
var updatedProjection = (ProjectionExpression)Visit(item); | ||
projections.Add(updatedProjection); | ||
changed |= updatedProjection != item; | ||
} | ||
|
||
var fromExpression = (RootReferenceExpression)Visit(selectExpression.FromExpression); | ||
changed |= fromExpression != selectExpression.FromExpression; | ||
|
||
var predicate = TryCompensateForBoolWithValueConverter(selectExpression.Predicate); | ||
changed |= predicate != selectExpression.Predicate; | ||
|
||
var orderings = new List<OrderingExpression>(); | ||
foreach (var ordering in selectExpression.Orderings) | ||
{ | ||
var orderingExpression = (SqlExpression)Visit(ordering.Expression); | ||
changed |= orderingExpression != ordering.Expression; | ||
orderings.Add(ordering.Update(orderingExpression)); | ||
} | ||
|
||
var limit = (SqlExpression)Visit(selectExpression.Limit); | ||
var offset = (SqlExpression)Visit(selectExpression.Offset); | ||
|
||
return changed | ||
? selectExpression.Update(projections, fromExpression, predicate, orderings, limit, offset) | ||
: selectExpression; | ||
} | ||
|
||
private Expression VisitSqlConditional(SqlConditionalExpression sqlConditionalExpression) | ||
{ | ||
Check.NotNull(sqlConditionalExpression, nameof(sqlConditionalExpression)); | ||
|
||
var test = TryCompensateForBoolWithValueConverter(sqlConditionalExpression.Test); | ||
var ifTrue = (SqlExpression)Visit(sqlConditionalExpression.IfTrue); | ||
var ifFalse = (SqlExpression)Visit(sqlConditionalExpression.IfFalse); | ||
|
||
return sqlConditionalExpression.Update(test, ifTrue, ifFalse); | ||
} | ||
|
||
private SqlExpression TryCompensateForBoolWithValueConverter(SqlExpression sqlExpression) | ||
{ | ||
if (sqlExpression is KeyAccessExpression keyAccessExpression | ||
&& keyAccessExpression.TypeMapping.ClrType == typeof(bool) | ||
&& keyAccessExpression.TypeMapping.Converter != null) | ||
{ | ||
return _sqlExpressionFactory.Equal( | ||
sqlExpression, | ||
_sqlExpressionFactory.Constant(true, sqlExpression.TypeMapping)); | ||
} | ||
|
||
if (sqlExpression is SqlUnaryExpression sqlUnaryExpression) | ||
{ | ||
return sqlUnaryExpression.Update( | ||
TryCompensateForBoolWithValueConverter(sqlUnaryExpression.Operand)); | ||
} | ||
|
||
if (sqlExpression is SqlBinaryExpression sqlBinaryExpression | ||
&& (sqlBinaryExpression.OperatorType == ExpressionType.AndAlso | ||
|| sqlBinaryExpression.OperatorType == ExpressionType.OrElse)) | ||
{ | ||
return sqlBinaryExpression.Update( | ||
TryCompensateForBoolWithValueConverter(sqlBinaryExpression.Left), | ||
TryCompensateForBoolWithValueConverter(sqlBinaryExpression.Right)); | ||
} | ||
|
||
return sqlExpression; | ||
} | ||
} | ||
} |
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.