-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
UseRelationalNulls causes subquery to include NOT IN (NULL, x) #23761
Comments
This is by design, when relational null semantics is switched on, EF Core naively translates null comparisons. There is an optimization which compacts multiple comparisons into IN/NOT IN, but the query yields the same results if the optimization is not applied, i.e. The purpose for relational null semantics is to get a bit of performance by skipping all the additional null checks/optimizations when you know that these checks are not necessary, e.g. when the database columns are nullable but they don't contain any nulls. Otherwise it is recommended to use clr null semantics to avoid data corruption errors. |
We should not combine above in InExpression if relational null semantics are on. Currently there is no way for user to write the comparison. If user writes a list.Contains where list has a null value then it is fine to generate above invalid SQL. User can explicitly check for null, if list contains it and remove the null values from list before calling Contains. (Funcletizer will take care of generate correct parameters and translating it) |
…(NULL, x) We do optimization which combines comparisons based on the same property into IN and NOT IN. Problem is that if the comparisons contain nulls, we add those into the list of IN values, generating queries like a IN (1, 2, NULL). In normal circumstances, during null semantics processing we extract these nulls and produce correct IS NULL / IS NOT NULL calls, however when useRelationalNulls is enabled, we don't run null semantics visitor and therefore don't "fix" the IN expressions. Fix is to only apply the initial optimization for c# null semantics. Fixes #23761
…(NULL, x) We do optimization which combines comparisons based on the same property into IN and NOT IN. Problem is that if the comparisons contain nulls, we add those into the list of IN values, generating queries like a IN (1, 2, NULL). In normal circumstances, during null semantics processing we extract these nulls and produce correct IS NULL / IS NOT NULL calls, however when useRelationalNulls is enabled, we don't run null semantics visitor and therefore don't "fix" the IN expressions. Fix is to only apply the initial optimization for c# null semantics. Fixes #23761
…(NULL, x) We do optimization which combines comparisons based on the same property into IN and NOT IN. Problem is that if the comparisons contain nulls, we add those into the list of IN values, generating queries like a IN (1, 2, NULL). In normal circumstances, during null semantics processing we extract these nulls and produce correct IS NULL / IS NOT NULL calls, however when useRelationalNulls is enabled, we don't run null semantics visitor and therefore don't "fix" the IN expressions. Fix is to only apply the initial optimization for c# null semantics. Fixes #23761
…(NULL, x) We do optimization which combines comparisons based on the same property into IN and NOT IN. Problem is that if the comparisons contain nulls, we add those into the list of IN values, generating queries like a IN (1, 2, NULL). In normal circumstances, during null semantics processing we extract these nulls and produce correct IS NULL / IS NOT NULL calls, however when useRelationalNulls is enabled, we don't run null semantics visitor and therefore don't "fix" the IN expressions. Fix is to only apply the initial optimization for c# null semantics. Fixes #23761
This query produces an invalid WHERE when UseRelationalNulls = true for nullable int.
Incorrect SQL producted: AND [a].[StatusCode] NOT IN (NULL, 3)
SQL when UseRelationalNulls = false.
SQL when UseRelationalNulls = true.
EF Core version: 5.0.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.8.3
The text was updated successfully, but these errors were encountered: