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

Query: composing projection on top of FromSql doesn't get translated correctly #16079

Closed
maumar opened this issue Jun 13, 2019 · 3 comments · Fixed by #20366
Closed

Query: composing projection on top of FromSql doesn't get translated correctly #16079

maumar opened this issue Jun 13, 2019 · 3 comments · Fixed by #20366
Assignees
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. poachable punted-for-3.0 type-bug
Milestone

Comments

@maumar
Copy link
Contributor

maumar commented Jun 13, 2019

test:
FromSqlRaw_queryable_simple_projection_composed

context.Set<Product>().FromSqlRaw(
                        NormalizeDelimetersInRawString(
                            @"SELECT *
FROM [Products]
WHERE [Discontinued] <> " + boolMapping.GenerateSqlLiteral(true) + @"
AND (([UnitsInStock] + [UnitsOnOrder]) < [ReorderLevel])"))
                    .Select(p => p.ProductName)
                    .ToArray();

expected sql:

SELECT [p].[ProductName]
FROM (
    SELECT *
    FROM ""Products""
    WHERE ""Discontinued"" <> CAST(1 AS bit)
    AND ((""UnitsInStock"" + ""UnitsOnOrder"") < ""ReorderLevel"")
) AS [p]

actual sql:

SELECT *
FROM ""Products""
WHERE ""Discontinued"" <> CAST(1 AS bit)
AND ((""UnitsInStock"" + ""UnitsOnOrder"") < ""ReorderLevel"")
@smitpatel
Copy link
Member

For the record it is correct translation, just gets more data because we lifted subquery.

@PawelGerr
Copy link

PawelGerr commented Oct 22, 2019

I'm not sure I understand the answer. Is the current behavior going to be changed?

If not, can you explain why the following queries should generate different queries:

// Generates unexpected SQL: 
//      SELECT * FROM Products

Context.Products.FromSqlRaw("SELECT * FROM Products")
            .Select(p => p.Id)
            .ToList()

// Generates correct SQL: 
//      SELECT [p].[Id] FROM [Products] AS [p]

Context.Products
            .Select(p => p.Id)
            .ToList()

Interesting enough, FromSqlRaw + First/FirstOrDefault work correctly

// Generates correct SQL:
//       SELECT TOP(1) [p].[Id]
//       FROM (
//              SELECT * FROM Products
//       ) AS [p]

Context.Products
            .FromSqlRaw("SELECT * FROM Products")
            .Select(p => p.Id)
            .FirstOrDefault();

Fetching unnecessary columns lowers performance and the current behavior feels like a bug because it occurs "randomly", at least from the point of view of the developer using the EF.

Tested with

  • Microsoft.EntityFrameworkCore.SqlServer: 3.0.0 and 3.1.0-preview1

@smitpatel
Copy link
Member

I may just do it as tiny project.

@smitpatel smitpatel modified the milestones: Backlog, 5.0.0 Mar 20, 2020
smitpatel added a commit that referenced this issue Mar 20, 2020
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Mar 20, 2020
smitpatel added a commit that referenced this issue Mar 20, 2020
smitpatel added a commit that referenced this issue Mar 21, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0, 5.0.0-preview3 Mar 31, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0-preview3, 5.0.0 Nov 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. poachable punted-for-3.0 type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants