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

Incorrect SQL generated over 'Contains' in array having null element #3986

Closed
ademchenko opened this issue Feb 22, 2023 · 5 comments · Fixed by #3990 · May be fixed by #3991
Closed

Incorrect SQL generated over 'Contains' in array having null element #3986

ademchenko opened this issue Feb 22, 2023 · 5 comments · Fixed by #3990 · May be fixed by #3991
Assignees
Labels
status: has-pr There is active PR for issue type: bug
Milestone

Comments

@ademchenko
Copy link

ademchenko commented Feb 22, 2023

Describe your issue

Consider the following table

create table issue
(
    number        integer,
    number_string text
);

with the following data:

insert into issue(number, number_string)
VALUES (0, null), (1, 'one'), (2, 'two');

and with the following class generated:

[Table("issue", Schema = "public")]
	public partial class Issue
	{
		[Column("number"       )] public int?    Number       { get; set; } // integer
		[Column("number_string")] public string? NumberString { get; set; } // text
	}

Steps to reproduce

Consider the following test:

        [Test]
        public async Task IssueTest()
        {
            var dataConnection = new LinqToDB.Data.DataConnection();

            var strings = new[] { null, "two" };

            var result = await dataConnection.GetTable<Issue>().Where(i => i.Number == 1 && strings.Contains(i.NumberString)).ToListAsync();

            var lastQuery = dataConnection.LastQuery;

            TestContext.WriteLine(lastQuery);

            Assert.AreEqual(0, result.Count);
        }

The condition over the table 'Issue' should obviously lead to a query that returns no rows. But that's not the case, the record (0, null) is actually returned.
The bug is pretty clear, below is the SQL generated by linq2db and we can see that the necessary brackets are missed there.

SELECT
	i."number",
	i.number_string
FROM
	"public".issue i
WHERE
	i."number" = 1 AND i.number_string IN ('two') OR i.number_string IS NULL

Environment details

Linq To DB version: 4.4.1

Database (with version): Postgres 14.1

@ademchenko
Copy link
Author

@MaceWindu , this issue is quite serious, is there a way to move it to some service pack of version 4, but not to the version 5.x, which I think is far from the release?

@MaceWindu
Copy link
Contributor

5.0 release scheduled for tomorrow and I plan to look at this issue later today

@MaceWindu MaceWindu self-assigned this Feb 22, 2023
jods4 added a commit that referenced this issue Feb 22, 2023
`OR xx IS NULL` should be wrapped with parens like `xx IN () OR xx IN ()` is, as it might be part of a larger AND expression.
@jods4
Copy link
Contributor

jods4 commented Feb 22, 2023

It's a part I'm familiar with.
@MaceWindu PR #3990 for a fix.

@jods4
Copy link
Contributor

jods4 commented Feb 22, 2023

Oh wait, it's not a complete fix, it introduces a different bug. :(

-> I'm gonna write a comment with what remains to be done in the PR.

@jods4 jods4 mentioned this issue Feb 22, 2023
@jods4
Copy link
Contributor

jods4 commented Feb 22, 2023

Ok, I thought there was a bug but there is not.
It's not obvious, I'm commenting on all possible cases here: #3990 (comment)

@MaceWindu MaceWindu added the status: has-pr There is active PR for issue label Feb 22, 2023
MaceWindu added a commit that referenced this issue Feb 23, 2023
* Fix #3986

`OR xx IS NULL` should be wrapped with parens like `xx IN () OR xx IN ()` is, as it might be part of a larger AND expression.

* add tests

---------

Co-authored-by: MaceWindu <MaceWindu@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: has-pr There is active PR for issue type: bug
Development

Successfully merging a pull request may close this issue.

3 participants