Incorrect SQL generated over 'Contains' in array having null element
See original GitHub issueDescribe 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:
- Created 7 months ago
- Comments:5 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
5.0 release scheduled for tomorrow and I plan to look at this issue later today
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