Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sqlite: transform CROSS APPLY to projection when possible #34375

Open
roji opened this issue Aug 7, 2024 · 0 comments
Open

Sqlite: transform CROSS APPLY to projection when possible #34375

roji opened this issue Aug 7, 2024 · 0 comments

Comments

@roji
Copy link
Member

roji commented Aug 7, 2024

Consider the following query, where Contacts is mapped to a JSON column:

_ = context.Clients
    .Select(c => new ClientDto
    {
      Id = c.Id,
      Name = c.Name,
      Contacts = c.Contacts.Select(contact => new ContactDto
      {
        FirstName = contact.FirstName,
        LastName = contact.LastName,
        Address = contact.Address,
        Email = contact.Email,
        PhoneNumber = contact.PhoneNumber
      }).ToList()
    });

EF translates the inner Select to a CROSS APPLY, with OPENJSON (on SQL Server) to extract the JSON data as relational for materialization:

SELECT [c].[Id], [c].[Name], JSON_VALUE([c0].[value], '$.FirstName'), JSON_VALUE([c0].[value], '$.LastName'), JSON_VALUE([c0].[value], '$.Address'), JSON_VALUE([c0].[value], '$.Email'), JSON_VALUE([c0].[value], '$.PhoneNumber'), [c0].[key]
FROM [Clients] AS [c]
OUTER APPLY OPENJSON([c].[Contacts], '$') AS [c0]
ORDER BY [c].[Id], CAST([c0].[key] AS int)

This fails on SQLite, because CROSS/OUTER APPLY isn't supported there.

If the CROSS APPLY is only referenced in the projection (and not e.g. in a filter), then it should be able to transform the query to replace the CROSS APPLY with a projection subquery instead. This seems particularly useful around SQLite JSON processing, as above.

Note the similarity with #34256, where we do the opposite, lifting a subquery projection to CROSS APPLY on SQL Server (where an aggregate function is involved).

Initially raised in #34367

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant