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

Coalesce with not equal unexpected SQL translation #16955

Closed
ickers opened this issue Aug 5, 2019 · 2 comments · Fixed by #18768
Closed

Coalesce with not equal unexpected SQL translation #16955

ickers opened this issue Aug 5, 2019 · 2 comments · Fixed by #18768
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@ickers
Copy link

ickers commented Aug 5, 2019

Evaluation of simple LINQ expression involving null coalescing operator and subsequent primitive not-equals comparison within predicate returns an unexpected result set.

Given data saved:

context.Data.Add( new Data { Name = "HasTestId", TestId = 123 } );
context.Data.Add( new Data { Name = "ZeroTestId", TestId = 0 } );
context.Data.Add( new Data { Name = "NullTestId", TestId = null } );
context.SaveChanges( );

the LINQ

context.Data.Where( o => (o.TestId ?? 0) != 0 ).Select( o => o.Name )

evaluates to the result [ "HasTestId", "NullTestId" ] instead of the expected [ "HasTestId" ]. This is because the above LINQ is compiled by EF Core to the SQL:

SELECT "o"."Name"
FROM "Data" AS "o"
WHERE (COALESCE("o"."TestId", 0) <> 0) OR "o"."TestId" IS NULL

The extraneous OR "o"."TestId" IS NULL added to the WHERE clause seems incorrect IMHO. Interestingly, by switching the predicate from != to == the LINQ evaluates to the expected result [ "ZeroTestId", "NullTestId" ].

Steps to reproduce

Clone repro project at ickers/ef-core-coalesce-not-equal-query@6d5af16 (multi-targeted project for netcoreapp2.0/ef-core-2.0.3, netcoreapp2.1/ef-core-2.1.11, netcoreapp2.2/ef-core-2.2.6 - issue is reproducible in all three runtime/EF Core versions).

Further technical details

EF Core version: 2.2.6, 2.1.11, 2.0.3
Database Provider: Microsoft.EntityFrameworkCore.Sqlite (originally observed with Microsoft.EntityFrameworkCore.SqlServer)
Operating system: Linux Mint 18
IDE: VS Code 1.36 (originally observed with Visual Studio 2017 15.4)

@ickers ickers changed the title Coalesce with not equal SQL translation Coalesce with not equal unexpected SQL translation Aug 5, 2019
@maumar maumar self-assigned this Aug 5, 2019
@maumar
Copy link
Contributor

maumar commented Aug 5, 2019

Looks like Bug in null semantics

@ajcvickers ajcvickers added this to the Backlog milestone Aug 5, 2019
@maumar
Copy link
Contributor

maumar commented Nov 5, 2019

fixed in dd92ba3

@maumar maumar added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Nov 5, 2019
@maumar maumar modified the milestones: Backlog, 3.1.0 Nov 5, 2019
maumar added a commit that referenced this issue Nov 5, 2019
maumar added a commit that referenced this issue Nov 5, 2019
@ajcvickers ajcvickers modified the milestones: 3.1.0-preview3, 3.1.0 Dec 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants