Skip to content

New OpenJSON expression to SQL translation breaks aggregates on subquery with 'Cannot perform an aggregate function on an expression containing an aggregate or a subquery' exception #32374

@AlexeiScherbakov

Description

@AlexeiScherbakov

File a bug

Include your code

Steps to reproduce:

  1. Use attached example OpenJsonBug.zip
  2. Create db '''OpenJsonBugDb''' and table (no data is needed to reproduce) on local sql server
CREATE TABLE [dbo].[Sales](
	[DateID] [int] NOT NULL,
	[JOURNALID] [int] NOT NULL,
	[FiltersService] [int] NOT NULL,
	[OrdersGrossPCS] [decimal](18, 4) NULL,
	[OrdersApprovedPCS] [decimal](18, 4) NULL,
	[OrdersNetPCS] [decimal](18, 4) NULL,
	[StatusNameLine] [varchar](9) NULL
) ON [PRIMARY]
  1. TestHashSetTranslation completes normally, TestArrayTranslation fails with Microsoft.Data.SqlClient.SqlException : Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
[Test]
public void TestHashSetTranslation()
{
	using var dbContext = _webApplication.Services.GetRequiredService<SalesDbContext>();

    HashSet<string> shippedStatuses = new HashSet<string>()
    {
        "Delivered",
        "Shipped",
        "Net"
    };

    int intStart = 20230101;
    int intEnd = 20231121;

    var data = dbContext.Sales.Where(x => (x.DateId >= intStart) && (x.DateId <= intEnd) && (x.FiltersService == 0))
        .GroupBy(x => x.JournalId)
        .Select(x => new
        {
            Key = x.Key,
            NetInOrder = x.Where(x => (x.OrdersNetPCS > 0)).Count(),
            ShippedInOrder = x.Where(x =>
                   ((x.OrdersNetPCS > 0)
                   || ((x.OrdersApprovedPCS > 0) && shippedStatuses.Contains(x.StatusNameLine)))
                              ).Count(),
            ApprovedInOrder = x.Where(x => (x.OrdersApprovedPCS > 0)).Count(),
            GrossInOrder = x.Where(x => (x.OrdersGrossPCS > 0)).Count(),
        }).GroupBy(x => true)
        .Select(x => new
        {
            Net = x.Count(x => x.NetInOrder > 0),
            Shipped = x.Count(x => x.ShippedInOrder > 0),
            Approved = x.Count(x => x.ApprovedInOrder > 0),
            Gross = x.Count(x => x.GrossInOrder > 0)
        }).SingleOrDefault();
}

[Test]
public void TestArrayTranslation()
{
    using var dbContext = _webApplication.Services.GetRequiredService<SalesDbContext>();

    string[] shippedStatuses = new string[]
    {
        "Delivered",
        "Shipped",
        "Net"
    };

    int intStart = 20230101;
    int intEnd = 20231121;

    var data = dbContext.Sales.Where(x => (x.DateId >= intStart) && (x.DateId <= intEnd) && (x.FiltersService == 0))
        .GroupBy(x => x.JournalId)
        .Select(x => new
        {
            Key = x.Key,
            NetInOrder = x.Where(x => (x.OrdersNetPCS > 0)).Count(),
            ShippedInOrder = x.Where(x =>
                   ((x.OrdersNetPCS > 0)
                   || ((x.OrdersApprovedPCS > 0) && shippedStatuses.Contains(x.StatusNameLine)))
                              ).Count(),
            ApprovedInOrder = x.Where(x => (x.OrdersApprovedPCS > 0)).Count(),
            GrossInOrder = x.Where(x => (x.OrdersGrossPCS > 0)).Count(),
        }).GroupBy(x => true)
        .Select(x => new
        {
            Net = x.Count(x => x.NetInOrder > 0),
            Shipped = x.Count(x => x.ShippedInOrder > 0),
            Approved = x.Count(x => x.ApprovedInOrder > 0),
            Gross = x.Count(x => x.GrossInOrder > 0)
        }).SingleOrDefault();
}

In versions prior to 8.0.0 each query completes normally. In 8.0.0 array is translating to OpenJSON and query cannot be executed

Include provider and version information

EF Core version: 8.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
HelpLink.ProdName: Microsoft SQL Server
HelpLink.ProdVer: 15.00.2000

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions