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

Exception when having boolean constants in the index query. #487

Closed
alafi opened this issue May 16, 2023 · 2 comments · Fixed by #488
Closed

Exception when having boolean constants in the index query. #487

alafi opened this issue May 16, 2023 · 2 comments · Fixed by #488

Comments

@alafi
Copy link

alafi commented May 16, 2023

The following exception happens when having a boolean constant in the index query. It was tested with release (3.3.0) connecting to SQL Server (this issue might be related to #476 )

Message: 
Microsoft.Data.SqlClient.SqlException : An expression of non-boolean type specified in a context where a condition is expected, near 'or'.
Stack Trace: 

I have created the following test to reproduce the issue:

[Fact]
public async Task ShouldHandleBooleanConstants()
{
    _store.RegisterIndexes<PersonWithAIndexProvider>();
    using (var session = _store.CreateSession())
    {
        session.Save(new Person { Firstname = "Alex" });
        session.Save(new Person { Firstname = "Bill" });
        session.Save(new Person { Firstname = "assan" });
        await session.SaveChangesAsync();
    }
    using (var session = _store.CreateSession())
    {
        string firstName = null;
        Assert.Null(await session.QueryIndex<PersonByBothNamesCol>(x => 
                firstName == null || 
                firstName == x.Firstname
            ).FirstOrDefaultAsync());
    }
}

I believe it is specific to SQL Server due to the fact that Boolean parameters needs an equal (=) operator to be interpreted correctly.

@hishamco
Copy link
Contributor

hishamco commented Jul 7, 2023

The produced query is

'SELECT TOP (1) * FROM [BobaFett].[tpPersonByBothNamesCol] AS [PersonByBothNamesCol_a1] WHERE (@p0 or ([PersonByBothNamesCol_a1].[Firstname] IS NULL))

This can be fixed by adding =1 for params in where clause if and only if the parameter without operator

@hishamco
Copy link
Contributor

hishamco commented Jul 7, 2023

@sebastienros assign this to me because I don't have access, I'm planning to push a PR. It's better than what I suggested earlier

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

Successfully merging a pull request may close this issue.

2 participants