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

Nullable Type comparison HasValue with boolean needs parentheses #2090

Closed
Isitar opened this issue Nov 12, 2021 · 3 comments · Fixed by #2104
Closed

Nullable Type comparison HasValue with boolean needs parentheses #2090

Isitar opened this issue Nov 12, 2021 · 3 comments · Fixed by #2104
Assignees
Labels
bug Something isn't working
Milestone

Comments

@Isitar
Copy link
Contributor

Isitar commented Nov 12, 2021

Hi

The Model looks something like this:

public class MyClass {
  public Guid Id {get;set;}
  public Guid? ParentId {get;set;}
}

then the query:

var valueFilterNegated = true;
... // some logic to set the valueFilterNegated

dbContext.MyClasses.Where(c =>valueFilter != c.ParentId.HasValue).CountAsync(cancellationToken);

the SQL Query produced is:

SELECT COUNT(*)::INT
FROM "MyClasses" AS s
WHERE ($1 <> s."ParentId" IS NOT NULL)

with $1 being either True or False, depending on the valueFilterNegated.

The Problem is, that the <> operator has a stronger binding than the IS NOT NULL and therefore it tries to compare boolean <> guid which throws: operator does not exist: boolean <> uuid at character

The solution is quite simple, put parantheses:

SELECT COUNT(*)::INT
FROM "MyClasses" AS s
WHERE ($1 <> (s."ParentId" IS NOT NULL))
@Isitar
Copy link
Contributor Author

Isitar commented Nov 12, 2021

I think I found the issue:
This Commit: 313840d
removed the parantheses (part in NpgsqlQuerySqlGenerator) which causes the bug.

@roji
Copy link
Member

roji commented Nov 12, 2021

Thanks for reporting and investigating - this is indeed because of 313840d. I did that because dotnet/efcore#23990 was supposed to fix this on the EF Core side, but it seems the fix targets boolean nullable columns only. Opened dotnet/efcore#26652 to do this for non-boolean columns as well, and submitted dotnet/efcore#26653 as a fix.

Let's see how it goes with that PR; if it ends up being serviced for EF Core 6.0.1, this will go away. Otherwise I can revert 313840d.

@roji roji added this to the 6.0.1 milestone Nov 12, 2021
@roji roji added the bug Something isn't working label Nov 12, 2021
@roji roji self-assigned this Nov 12, 2021
roji added a commit to roji/efcore.pg that referenced this issue Nov 17, 2021
roji added a commit to roji/efcore.pg that referenced this issue Nov 17, 2021
@roji
Copy link
Member

roji commented Nov 17, 2021

OK, reverting this for 6.0.1 as a fix won't be going in for EF Core 6.0.1. Opened #2105 to track removing this again once fixed on the EF Core side.

roji added a commit that referenced this issue Nov 17, 2021
roji added a commit that referenced this issue Nov 17, 2021
roji added a commit that referenced this issue Nov 17, 2021
Fixes #2090

(cherry picked from commit 78e63c4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants