question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 months ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
MaceWinducommented, Feb 22, 2023

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

0reactions
jods4commented, 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: https://github.com/linq2db/linq2db/pull/3990#issuecomment-1440027041

Read more comments on GitHub >

github_iconTop Results From Across the Web

sql - Array contains nulls PostgreSQL
I pass the names of the fields like one string into query (called fields) and it is the reason I do not want...
Read more >
SQL Syntax error when empty array is given into "In" method
In MySql IN () (empty IN) is a syntax error. To fix it i propose that either: improve type annotations not to allow...
Read more >
Working with SQL NULL values
The IS NULL condition is used to return rows that contain the NULL values in a column and its syntax is like below: ......
Read more >
pyspark.sql.functions.array_contains
Collection function: returns null if the array is null, true if the array contains the given value, and false otherwise. New in version...
Read more >
Array functions in GoogleSQL | Cloud Spanner
Takes an array and returns the first element in the array. Produces an error if the array is empty. Returns NULL if array_expression...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found