From d5ea671bdb500714183a8c982108ea4f08a4eff7 Mon Sep 17 00:00:00 2001 From: Smit Patel Date: Thu, 5 Dec 2019 17:40:40 -0800 Subject: [PATCH 1/2] Query: Add regression test for #12355 --- .../NorthwindGroupByQueryInMemoryTest.cs | 8 +++++ .../Query/NorthwindGroupByQueryTestBase.cs | 36 +++++++++++++++++++ .../NorthwindGroupByQuerySqlServerTest.cs | 16 +++++++++ 3 files changed, 60 insertions(+) diff --git a/test/EFCore.InMemory.FunctionalTests/Query/NorthwindGroupByQueryInMemoryTest.cs b/test/EFCore.InMemory.FunctionalTests/Query/NorthwindGroupByQueryInMemoryTest.cs index bb5a7838c0c..af3d6fd6bce 100644 --- a/test/EFCore.InMemory.FunctionalTests/Query/NorthwindGroupByQueryInMemoryTest.cs +++ b/test/EFCore.InMemory.FunctionalTests/Query/NorthwindGroupByQueryInMemoryTest.cs @@ -1,7 +1,9 @@ // 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.Threading.Tasks; using Microsoft.EntityFrameworkCore.TestUtilities; +using Xunit; using Xunit.Abstractions; namespace Microsoft.EntityFrameworkCore.Query @@ -16,5 +18,11 @@ public NorthwindGroupByQueryInMemoryTest( { //TestLoggerFactory.TestOutputHelper = testOutputHelper; } + + [ConditionalTheory(Skip = "Issue#17536")] + public override Task Join_GroupBy_Aggregate_with_left_join(bool async) + { + return base.Join_GroupBy_Aggregate_with_left_join(async); + } } } diff --git a/test/EFCore.Specification.Tests/Query/NorthwindGroupByQueryTestBase.cs b/test/EFCore.Specification.Tests/Query/NorthwindGroupByQueryTestBase.cs index 7b058120292..71e245868c9 100644 --- a/test/EFCore.Specification.Tests/Query/NorthwindGroupByQueryTestBase.cs +++ b/test/EFCore.Specification.Tests/Query/NorthwindGroupByQueryTestBase.cs @@ -1745,6 +1745,42 @@ from g in grouping entryCount: 63); } + [ConditionalTheory] + [MemberData(nameof(IsAsyncData))] + public virtual Task Join_GroupBy_Aggregate_with_left_join(bool async) + { + return AssertQuery( + async, + ss => + from c in ss.Set().Where(c => c.CustomerID.StartsWith("A")) + join a in ss.Set().GroupBy(o => o.CustomerID) + .Where(g => g.Count() > 5) + .Select( + g => new { CustomerID = g.Key, LastOrderID = g.Max(o => o.OrderID) }) + on c.CustomerID equals a.CustomerID into grouping + from g in grouping.DefaultIfEmpty() + select new + { + c, + LastOrderID = (int?)g.LastOrderID + }, + ss => + from c in ss.Set().Where(c => c.CustomerID.StartsWith("A")) + join a in ss.Set().GroupBy(o => o.CustomerID) + .Where(g => g.Count() > 5) + .Select( + g => new { CustomerID = g.Key, LastOrderID = g.Max(o => o.OrderID) }) + on c.CustomerID equals a.CustomerID into grouping + from g in grouping.DefaultIfEmpty() + select new + { + c, + LastOrderID = g != null ? g.LastOrderID : (int?)null + }, + elementSorter: r => r.c.CustomerID, + entryCount: 4); + } + [ConditionalTheory] [MemberData(nameof(IsAsyncData))] public virtual Task Join_GroupBy_Aggregate_in_subquery(bool async) diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindGroupByQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindGroupByQuerySqlServerTest.cs index 195653a129f..7114bb61aa8 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindGroupByQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindGroupByQuerySqlServerTest.cs @@ -1334,6 +1334,22 @@ HAVING COUNT(*) > 5 INNER JOIN [Orders] AS [o0] ON [c].[CustomerID] = [o0].[CustomerID]"); } + public override async Task Join_GroupBy_Aggregate_with_left_join(bool async) + { + await base.Join_GroupBy_Aggregate_with_left_join(async); + + AssertSql( + @"SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region], [t].[c] AS [LastOrderID] +FROM [Customers] AS [c] +LEFT JOIN ( + SELECT [o].[CustomerID], MAX([o].[OrderID]) AS [c] + FROM [Orders] AS [o] + GROUP BY [o].[CustomerID] + HAVING COUNT(*) > 5 +) AS [t] ON [c].[CustomerID] = [t].[CustomerID] +WHERE [c].[CustomerID] LIKE N'A%'"); + } + public override async Task Join_GroupBy_Aggregate_in_subquery(bool async) { await base.Join_GroupBy_Aggregate_in_subquery(async); From cf17a58012e580dd8eb32cb6113f726890c024df Mon Sep 17 00:00:00 2001 From: Smit Patel Date: Thu, 5 Dec 2019 17:46:10 -0800 Subject: [PATCH 2/2] Query: Add regression test for #12437 --- .../NorthwindMiscellaneousQueryCosmosTest.cs | 6 ++++++ .../NorthwindMiscellaneousQueryTestBase.cs | 13 ++++++++++++ ...orthwindMiscellaneousQuerySqlServerTest.cs | 20 +++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindMiscellaneousQueryCosmosTest.cs b/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindMiscellaneousQueryCosmosTest.cs index 61a40efc37a..9e77b740c3b 100644 --- a/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindMiscellaneousQueryCosmosTest.cs +++ b/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindMiscellaneousQueryCosmosTest.cs @@ -3976,6 +3976,12 @@ public override Task SelectMany_correlated_subquery_hard(bool async) return base.SelectMany_correlated_subquery_hard(async); } + [ConditionalTheory(Skip = "Issue #17246")] + public override Task Subquery_DefaultIfEmpty_Any(bool async) + { + return base.Subquery_DefaultIfEmpty_Any(async); + } + private void AssertSql(params string[] expected) => Fixture.TestSqlLoggerFactory.AssertBaseline(expected); diff --git a/test/EFCore.Specification.Tests/Query/NorthwindMiscellaneousQueryTestBase.cs b/test/EFCore.Specification.Tests/Query/NorthwindMiscellaneousQueryTestBase.cs index 8ab58b4a743..45a544a2ac0 100644 --- a/test/EFCore.Specification.Tests/Query/NorthwindMiscellaneousQueryTestBase.cs +++ b/test/EFCore.Specification.Tests/Query/NorthwindMiscellaneousQueryTestBase.cs @@ -5463,5 +5463,18 @@ public virtual Task AsQueryable_in_query_server_evals(bool async) } private static Expression> ValidYear => a => a.OrderDate.Value.Year == 1998; + + [ConditionalTheory] + [MemberData(nameof(IsAsyncData))] + public virtual Task Subquery_DefaultIfEmpty_Any(bool async) + { + return AssertAny( + async, + ss => (from e in ss.Set() + .Where(e => e.EmployeeID == NonExistentID) + .Select(e => e.EmployeeID) + .DefaultIfEmpty() + select e)); + } } } diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindMiscellaneousQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindMiscellaneousQuerySqlServerTest.cs index e7047d92c54..6d84f6276b4 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindMiscellaneousQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindMiscellaneousQuerySqlServerTest.cs @@ -4691,6 +4691,26 @@ ORDER BY [o].[OrderID] ORDER BY [c].[CustomerID], [t].[OrderID]"); } + public override async Task Subquery_DefaultIfEmpty_Any(bool async) + { + await base.Subquery_DefaultIfEmpty_Any(async); + + AssertSql( + @"SELECT CASE + WHEN EXISTS ( + SELECT 1 + FROM ( + SELECT NULL AS [empty] + ) AS [empty] + LEFT JOIN ( + SELECT [e].[EmployeeID], [e].[City], [e].[Country], [e].[FirstName], [e].[ReportsTo], [e].[Title] + FROM [Employees] AS [e] + WHERE [e].[EmployeeID] = -1 + ) AS [t] ON 1 = 1) THEN CAST(1 AS bit) + ELSE CAST(0 AS bit) +END"); + } + private void AssertSql(params string[] expected) => Fixture.TestSqlLoggerFactory.AssertBaseline(expected);