Repository navigation
Prefer equality in boolean comparisons - #39797
Open
AndriySvyryd wants to merge 4 commits into
Open
AndriySvyryd wants to merge 4 commits into
AndriySvyryd wants to merge 4 commits into
Conversation
They can take advantage of indexing.
Restrict lookup rewrites and preserve converted and nullable values.
Add a process-wide AppContext switch and cover noncanonical SQLite values.
Contributor
Agent evaluation:
|
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The boolean join rewrite leaves the inner column wrapped in NOT, preventing the intended lookup; it needs to negate the left operand, update provider baselines, and add left-join regression coverage.
1 open issue
What changed in this PR
Updates EF Core’s relational query processing to prefer index-friendly equality for eligible Boolean comparisons, addressing #34164.
Changes:
- Adds guarded Boolean comparison rewrites while preserving null semantics and excluding converted operands.
- Introduces a compatibility switch and documents the risk for noncanonical SQLite Boolean values.
- Adds join, converter, and null-semantics coverage and updates provider SQL baselines.
| File | Description |
|---|---|
test/EFCore.SqlServer.FunctionalTests/Query/TemporalGearsOfWarQuerySqlServerTest.cs |
Updates temporal-query SQL baselines. |
test/EFCore.SqlServer.FunctionalTests/Query/NullSemanticsQuerySqlServerTest.cs |
Adds join and switch coverage; updates null-semantics baselines. |
test/EFCore.SqlServer.FunctionalTests/Query/NorthwindWhereQuerySqlServerTest.cs |
Updates the Boolean-expression comparison baseline. |
test/EFCore.SqlServer.FunctionalTests/Query/Inheritance/TPTGearsOfWarQuerySqlServerTest.cs |
Updates table-per-type SQL baselines. |
test/EFCore.SqlServer.FunctionalTests/Query/Inheritance/TPCGearsOfWarQuerySqlServerTest.cs |
Updates table-per-concrete-type SQL baselines. |
test/EFCore.SqlServer.FunctionalTests/Query/GearsOfWarQuerySqlServerTest.cs |
Updates Boolean filter and join baselines. |
test/EFCore.SqlServer.FunctionalTests/Query/FunkyDataQuerySqlServerTest.cs |
Updates nullable Boolean/string-predicate comparison SQL. |
test/EFCore.SqlServer.FunctionalTests/CustomConvertersSqlServerTest.cs |
Verifies converted Boolean comparisons retain inequality. |
test/EFCore.Sqlite.FunctionalTests/Query/NullSemanticsQuerySqliteTest.cs |
Adds join, switch, and noncanonical-value coverage. |
test/EFCore.Sqlite.FunctionalTests/Query/GearsOfWarQuerySqliteTest.cs |
Updates Boolean filter and join baselines. |
test/EFCore.Sqlite.FunctionalTests/CustomConvertersSqliteTest.cs |
Covers converted columns and noncanonical converter values. |
test/EFCore.Specification.Tests/CustomConvertersTestBase.cs |
Adds shared converted-Boolean comparison coverage. |
test/EFCore.Relational.Specification.Tests/Query/NullSemanticsQueryTestBase.cs |
Adds shared join and nullable-Boolean tests, including the prior GroupJoin regression. |
test/EFCore.Cosmos.FunctionalTests/CustomConvertersCosmosTest.cs |
Skips the unsupported cross-join test. |
src/EFCore.Relational/README.md |
Documents compatibility assumptions and the opt-out switch. |
src/EFCore.Relational/Query/SqlNullabilityProcessor.cs |
Implements guarded equality rewrites and switch handling. |
.agents/skills/query-pipeline/SKILL.md |
Adds guidance on converter and SQLite Boolean semantics. |
Review effort: Balanced
| || right is not ColumnExpression rightColumn | ||
| || leftColumn.TableAlias != rightColumn.TableAlias)) | ||
| { | ||
| var negateLeft = right is ColumnExpression && (left is not ColumnExpression || leftNegated || !rightNegated); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixes #34164
Replaces #34166, originally authored by Andrea Canciani. The prerequisite fix for #35393 is already on main.
Prefer equality for eligible boolean comparisons where a column can support a lookup; retain inequality for same-table columns, projected expressions, and converted operands. Cover join keys, null semantics, value converters, and provider SQL. When comparing two columns from different tables, leave the inner/right column available for lookup.
Compatibility: This optimization is enabled by default and assumes a two-valued database representation. SQLite can store noncanonical integers in boolean columns, and the rewrite can change results for those values. Set
AppContext.SetSwitch("Microsoft.EntityFrameworkCore.DisableBooleanComparisonOptimization", true)before using EF Core to disable this optimization for all relational providers and restore the previous behavior. This switch does not address pre-existing behavior for all noncanonical comparisons.Performance: In illustrative synthetic 2,000-row cross joins, SQLite used an automatic index and the equality form took a median 125 ms versus 340 ms for inequality; SQL Server LocalDB took 1,270 ms versus 1,980 ms in one aggregate run. These are not general performance guarantees. A SQLite guard for noncanonical values prevented automatic indexing and took 237 ms versus 163 ms for the original inequality in a separate synthetic run.
Validation (PowerShell, after
. .\activate.ps1):dotnet test .\test\EFCore.Sqlite.FunctionalTests\EFCore.Sqlite.FunctionalTests.csproj -- --filter-class '*NullSemanticsQuerySqliteTest' '*CustomConvertersSqliteTest' '*GearsOfWarQuerySqliteTest' '*NorthwindJoinQuerySqliteTest'— passed: 4,107; skipped: 15.dotnet test .\test\EFCore.SqlServer.FunctionalTests\EFCore.SqlServer.FunctionalTests.csproj -- --filter-class 'Microsoft.EntityFrameworkCore.Query.NullSemanticsQuerySqlServerTest' 'Microsoft.EntityFrameworkCore.CustomConvertersSqlServerTest' 'Microsoft.EntityFrameworkCore.Query.GearsOfWarQuerySqlServerTest' 'Microsoft.EntityFrameworkCore.Query.Inheritance.TPCGearsOfWarQuerySqlServerTest' 'Microsoft.EntityFrameworkCore.Query.Inheritance.TPTGearsOfWarQuerySqlServerTest' 'Microsoft.EntityFrameworkCore.Query.NorthwindJoinQuerySqlServerTest' 'Microsoft.EntityFrameworkCore.Query.NorthwindWhereQuerySqlServerTest' 'Microsoft.EntityFrameworkCore.Query.FunkyDataQuerySqlServerTest' 'Microsoft.EntityFrameworkCore.Query.ComplexNavigationsQuerySqlServerTest'— passed: 5,207; skipped: 18.dotnet test .\test\EFCore.SqlServer.FunctionalTests\EFCore.SqlServer.FunctionalTests.csproj -- --filter-class 'Microsoft.EntityFrameworkCore.Query.TemporalGearsOfWarQuerySqlServerTest'— passed: 1,192; skipped: 5.dotnet test .\test\EFCore.InMemory.FunctionalTests\EFCore.InMemory.FunctionalTests.csproj -- --filter-method '*Compare_converted_bool_columns_from_different_tables'— passed: 2.dotnet test .\test\EFCore.Cosmos.FunctionalTests\EFCore.Cosmos.FunctionalTests.csproj -- --filter-method '*Compare_converted_bool_columns_from_different_tables'— skipped: 1 (cross joins unsupported).Full test suite and CI have not yet been run.