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

Bad LINQ to SQL translation for filter on non-nullable boolean property #666

Open
thomaslevesque opened this issue Jan 25, 2019 · 1 comment

Comments

@thomaslevesque
Copy link

thomaslevesque commented Jan 25, 2019

Describe the bug

LINQ queries with a condition on a non-nullable boolean property are not correctly translated to SQL. The translation doesn't account for the fact that the property could be undefined in the JSON document.

To Reproduce

Assuming an object model like this:

public class Item
{
    [JsonProperty("id")]
    public string Id { get; set; }

    public bool IsDeleted { get; set; }
}

Make the following query:

var query = client.CreateDocumentQuery<Item>(collectionUri).Where(i => !i.IsDeleted);

Expected behavior

Documents where IsDeleted is not defined should be returned, because in the C# model the property is not nullable, so its absence really means that it's false.

The generated SQL should be something like this:

SELECT * FROM root WHERE (NOT IS_DEFINED(root["IsDeleted"]) OR NOT root["IsDeleted"]) 

Or maybe

SELECT * FROM root WHERE (NOT (root["IsDeleted"] ?? false))

Actual behavior

Documents where IsDeleted is not defined are not returned, because NOT (undefined) doesn't evaluate to true.

The generated SQL looks like this:

SELECT * FROM root WHERE (NOT root["IsDeleted"])

Environment summary
SDK Version: .NET Core 2.1, Microsoft.Azure.DocumentDB.Core 2.1.3
OS Version (e.g. Windows, Linux, MacOSX): Windows 10

@thomaslevesque
Copy link
Author

Up!

This issue is really annoying. It doesn't follow the semantics of C#. So instead of .Where(i => !i.IsDeleted), I have to write .Where(i => !i.IsDeleted.IsDefined() || !i.IsDeleted). For every single boolean property...

Basically, the translation for .Where(i => i.BooleanProperty) should be ... WHERE (root["BooleanProperty"] ?? false)

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

No branches or pull requests

1 participant