From 7fe11ed3d3ebd4e231403fe18bfcf90c8207196c Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Thu, 2 Nov 2023 15:40:13 +0100 Subject: [PATCH] Visit arguments in QueryableMethodNormalizingExpressionVisitor after converting List.Contains Fixes #32215 Fixes #32218 --- ...yableMethodTranslatingExpressionVisitor.cs | 8 ++++- ...yableMethodNormalizingExpressionVisitor.cs | 11 +++---- .../PrimitiveCollectionsQueryTestBase.cs | 16 +++++++++- ...imitiveCollectionsQueryOldSqlServerTest.cs | 15 ++++++++++ .../PrimitiveCollectionsQuerySqlServerTest.cs | 30 ++++++++++++++++++- .../Query/QueryBugsTest.cs | 6 +++- .../PrimitiveCollectionsQuerySqliteTest.cs | 30 ++++++++++++++++++- 7 files changed, 106 insertions(+), 10 deletions(-) diff --git a/src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.cs b/src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.cs index 7ee0350c894..f4defb8a3fb 100644 --- a/src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.cs +++ b/src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.cs @@ -288,7 +288,7 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp // Server), we need to fall back to the previous IN translation. if (method.IsGenericMethod && method.GetGenericMethodDefinition() == QueryableMethods.Contains - && methodCallExpression.Arguments[0] is ParameterQueryRootExpression parameterSource + && UnwrapAsQueryable(methodCallExpression.Arguments[0]) is ParameterQueryRootExpression parameterSource && TranslateExpression(methodCallExpression.Arguments[1]) is SqlExpression item && _sqlTranslator.Visit(parameterSource.ParameterExpression) is SqlParameterExpression sqlParameterExpression) { @@ -300,6 +300,12 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp .UpdateResultCardinality(ResultCardinality.Single); return shapedQueryExpression; } + + static Expression UnwrapAsQueryable(Expression expression) + => expression is MethodCallExpression { Method: { IsGenericMethod: true } method } methodCall + && method.GetGenericMethodDefinition() == QueryableMethods.AsQueryable + ? methodCall.Arguments[0] + : expression; } return translated; diff --git a/src/EFCore/Query/Internal/QueryableMethodNormalizingExpressionVisitor.cs b/src/EFCore/Query/Internal/QueryableMethodNormalizingExpressionVisitor.cs index f89f9733d2d..2f9fa7b4653 100644 --- a/src/EFCore/Query/Internal/QueryableMethodNormalizingExpressionVisitor.cs +++ b/src/EFCore/Query/Internal/QueryableMethodNormalizingExpressionVisitor.cs @@ -435,12 +435,13 @@ private Expression TryConvertListContainsToQueryableContains(MethodCallExpressio var sourceType = methodCallExpression.Method.DeclaringType!.GetGenericArguments()[0]; - return Expression.Call( - QueryableMethods.Contains.MakeGenericMethod(sourceType), + return VisitMethodCall( Expression.Call( - QueryableMethods.AsQueryable.MakeGenericMethod(sourceType), - methodCallExpression.Object!), - methodCallExpression.Arguments[0]); + QueryableMethods.Contains.MakeGenericMethod(sourceType), + Expression.Call( + QueryableMethods.AsQueryable.MakeGenericMethod(sourceType), + methodCallExpression.Object!), + methodCallExpression.Arguments[0])); } private static bool CanConvertEnumerableToQueryable(Type enumerableType, Type queryableType) diff --git a/test/EFCore.Specification.Tests/Query/PrimitiveCollectionsQueryTestBase.cs b/test/EFCore.Specification.Tests/Query/PrimitiveCollectionsQueryTestBase.cs index 8c9b7d8ca4d..0e302e2becf 100644 --- a/test/EFCore.Specification.Tests/Query/PrimitiveCollectionsQueryTestBase.cs +++ b/test/EFCore.Specification.Tests/Query/PrimitiveCollectionsQueryTestBase.cs @@ -807,7 +807,7 @@ public virtual Task Project_primitive_collections_element(bool async) }, assertOrder: true); - [ConditionalTheory] // #32208 + [ConditionalTheory] // #32208, #32215 [MemberData(nameof(IsAsyncData))] public virtual Task Nested_contains_with_Lists_and_no_inferred_type_mapping(bool async) { @@ -821,6 +821,20 @@ public virtual Task Nested_contains_with_Lists_and_no_inferred_type_mapping(bool ss => ss.Set().Where(e => strings.Contains(ints.Contains(e.Int) ? "one" : "two"))); } + [ConditionalTheory] // #32208, #32215 + [MemberData(nameof(IsAsyncData))] + public virtual Task Nested_contains_with_arrays_and_no_inferred_type_mapping(bool async) + { + var ints = new[] { 1, 2, 3 }; + var strings = new[] { "one", "two", "three" }; + + // Note that in this query, the outer Contains really has no type mapping, neither for its source (collection parameter), nor + // for its item (the conditional expression returns constants). The default type mapping must be applied. + return AssertQuery( + async, + ss => ss.Set().Where(e => strings.Contains(ints.Contains(e.Int) ? "one" : "two"))); + } + public abstract class PrimitiveCollectionsQueryFixtureBase : SharedStoreFixtureBase, IQueryFixtureBase { private PrimitiveArrayData? _expectedData; diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/PrimitiveCollectionsQueryOldSqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/PrimitiveCollectionsQueryOldSqlServerTest.cs index 99f28eaca16..2117994b0e9 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/PrimitiveCollectionsQueryOldSqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/PrimitiveCollectionsQueryOldSqlServerTest.cs @@ -625,6 +625,21 @@ END IN (N'one', N'two', N'three') """); } + public override async Task Nested_contains_with_arrays_and_no_inferred_type_mapping(bool async) + { + await base.Nested_contains_with_arrays_and_no_inferred_type_mapping(async); + + AssertSql( + """ +SELECT [p].[Id], [p].[Bool], [p].[Bools], [p].[DateTime], [p].[DateTimes], [p].[Enum], [p].[Enums], [p].[Int], [p].[Ints], [p].[NullableInt], [p].[NullableInts], [p].[NullableString], [p].[NullableStrings], [p].[String], [p].[Strings] +FROM [PrimitiveCollectionsEntity] AS [p] +WHERE CASE + WHEN [p].[Int] IN (1, 2, 3) THEN N'one' + ELSE N'two' +END IN (N'one', N'two', N'three') +"""); + } + [ConditionalFact] public virtual void Check_all_tests_overridden() => TestHelpers.AssertAllMethodsOverridden(GetType()); diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/PrimitiveCollectionsQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/PrimitiveCollectionsQuerySqlServerTest.cs index 5de3656afe8..970e6cbdbc9 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/PrimitiveCollectionsQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/PrimitiveCollectionsQuerySqlServerTest.cs @@ -1233,12 +1233,40 @@ public override async Task Nested_contains_with_Lists_and_no_inferred_type_mappi AssertSql( """ +@__ints_1='[1,2,3]' (Size = 4000) @__strings_0='["one","two","three"]' (Size = 4000) SELECT [p].[Id], [p].[Bool], [p].[Bools], [p].[DateTime], [p].[DateTimes], [p].[Enum], [p].[Enums], [p].[Int], [p].[Ints], [p].[NullableInt], [p].[NullableInts], [p].[NullableString], [p].[NullableStrings], [p].[String], [p].[Strings] FROM [PrimitiveCollectionsEntity] AS [p] WHERE CASE - WHEN [p].[Int] IN (1, 2, 3) THEN N'one' + WHEN [p].[Int] IN ( + SELECT [i].[value] + FROM OPENJSON(@__ints_1) WITH ([value] int '$') AS [i] + ) THEN N'one' + ELSE N'two' +END IN ( + SELECT [s].[value] + FROM OPENJSON(@__strings_0) WITH ([value] nvarchar(max) '$') AS [s] +) +"""); + } + + public override async Task Nested_contains_with_arrays_and_no_inferred_type_mapping(bool async) + { + await base.Nested_contains_with_arrays_and_no_inferred_type_mapping(async); + + AssertSql( + """ +@__ints_1='[1,2,3]' (Size = 4000) +@__strings_0='["one","two","three"]' (Size = 4000) + +SELECT [p].[Id], [p].[Bool], [p].[Bools], [p].[DateTime], [p].[DateTimes], [p].[Enum], [p].[Enums], [p].[Int], [p].[Ints], [p].[NullableInt], [p].[NullableInts], [p].[NullableString], [p].[NullableStrings], [p].[String], [p].[Strings] +FROM [PrimitiveCollectionsEntity] AS [p] +WHERE CASE + WHEN [p].[Int] IN ( + SELECT [i].[value] + FROM OPENJSON(@__ints_1) WITH ([value] int '$') AS [i] + ) THEN N'one' ELSE N'two' END IN ( SELECT [s].[value] diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs index 47dc04f4f08..e736152d913 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs @@ -3982,13 +3982,17 @@ public virtual async Task Nested_contains_with_enum() AssertSql( """ +@__todoTypes_1='[0]' (Size = 4000) @__key_2='5f221fb9-66f4-442a-92c9-d97ed5989cc7' @__keys_0='["0a47bcb7-a1cb-4345-8944-c58f82d6aac7","5f221fb9-66f4-442a-92c9-d97ed5989cc7"]' (Size = 4000) SELECT [t].[Id], [t].[Type] FROM [Todos] AS [t] WHERE CASE - WHEN [t].[Type] = 0 THEN @__key_2 + WHEN [t].[Type] IN ( + SELECT [t0].[value] + FROM OPENJSON(@__todoTypes_1) WITH ([value] int '$') AS [t0] + ) THEN @__key_2 ELSE @__key_2 END IN ( SELECT [k].[value] diff --git a/test/EFCore.Sqlite.FunctionalTests/Query/PrimitiveCollectionsQuerySqliteTest.cs b/test/EFCore.Sqlite.FunctionalTests/Query/PrimitiveCollectionsQuerySqliteTest.cs index 22f87566b86..a6ac3d00ff9 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Query/PrimitiveCollectionsQuerySqliteTest.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Query/PrimitiveCollectionsQuerySqliteTest.cs @@ -1115,12 +1115,16 @@ public override async Task Nested_contains_with_Lists_and_no_inferred_type_mappi AssertSql( """ +@__ints_1='[1,2,3]' (Size = 7) @__strings_0='["one","two","three"]' (Size = 21) SELECT "p"."Id", "p"."Bool", "p"."Bools", "p"."DateTime", "p"."DateTimes", "p"."Enum", "p"."Enums", "p"."Int", "p"."Ints", "p"."NullableInt", "p"."NullableInts", "p"."NullableString", "p"."NullableStrings", "p"."String", "p"."Strings" FROM "PrimitiveCollectionsEntity" AS "p" WHERE CASE - WHEN "p"."Int" IN (1, 2, 3) THEN 'one' + WHEN "p"."Int" IN ( + SELECT "i"."value" + FROM json_each(@__ints_1) AS "i" + ) THEN 'one' ELSE 'two' END IN ( SELECT "s"."value" @@ -1129,6 +1133,30 @@ FROM json_each(@__strings_0) AS "s" """); } + public override async Task Nested_contains_with_arrays_and_no_inferred_type_mapping(bool async) + { + await base.Nested_contains_with_arrays_and_no_inferred_type_mapping(async); + + AssertSql( + """ + @__ints_1='[1,2,3]' (Size = 7) + @__strings_0='["one","two","three"]' (Size = 21) + + SELECT "p"."Id", "p"."Bool", "p"."Bools", "p"."DateTime", "p"."DateTimes", "p"."Enum", "p"."Enums", "p"."Int", "p"."Ints", "p"."NullableInt", "p"."NullableInts", "p"."NullableString", "p"."NullableStrings", "p"."String", "p"."Strings" + FROM "PrimitiveCollectionsEntity" AS "p" + WHERE CASE + WHEN "p"."Int" IN ( + SELECT "i"."value" + FROM json_each(@__ints_1) AS "i" + ) THEN 'one' + ELSE 'two' + END IN ( + SELECT "s"."value" + FROM json_each(@__strings_0) AS "s" + ) + """); + } + [ConditionalFact] public virtual void Check_all_tests_overridden() => TestHelpers.AssertAllMethodsOverridden(GetType());