Skip to content

Prefer equality in boolean comparisons - #39797

Open
AndriySvyryd wants to merge 4 commits into
dotnet:mainfrom
AndriySvyryd:andriysvyryd-rebase-pr-34166
Open

AndriySvyryd wants to merge 4 commits into
dotnet:mainfrom
AndriySvyryd:andriysvyryd-rebase-pr-34166

Conversation

@AndriySvyryd

Copy link
Copy Markdown
Member
  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

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).
  • Switch-enabled tests, executed in isolated processes with the switch set in a temporary runtime configuration: SQLite passed 4, SQL Server passed 2. These conditional tests are skipped in ordinary runs. Exact commands:
$roots=@('EFCore.Sqlite.FunctionalTests','EFCore.SqlServer.FunctionalTests')
foreach ($root in $roots) {
    $dir=Join-Path (Get-Location) "artifacts\bin\$root\Debug\net12.0"
    $base="Microsoft.EntityFrameworkCore.$($root -replace '^EFCore\.', '')"
    $cfg=Get-Content "$dir\$base.runtimeconfig.json" -Raw | ConvertFrom-Json
    $cfg.runtimeOptions.configProperties | Add-Member -NotePropertyName 'Microsoft.EntityFrameworkCore.DisableBooleanComparisonOptimization' -NotePropertyValue $true
    $runtime=Join-Path $env:TEMP "$root.switch.runtimeconfig.json"
    $cfg | ConvertTo-Json -Depth 10 | Set-Content $runtime
    dotnet exec --runtimeconfig $runtime --depsfile "$dir\$base.deps.json" "$dir\$base.dll" --filter-method '*Boolean_comparison_optimization_can_be_disabled' '*Compare_boolean_columns_with_noncanonical_provider_value'
}

Full test suite and CI have not yet been run.

ranma42 and others added 4 commits November 24, 2026 16:43
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.
@AndriySvyryd
AndriySvyryd requested review from a team and wtgodbe as code owners November 25, 2026 00:49
Copilot AI balanced review requested due to automatic review settings November 25, 2026 00:49
@github-actions

Copy link
Copy Markdown
Contributor

Agent evaluation: query-pipeline

Status: running for 67d289056f13c8a18755768e976d7de92ba1a1ec

View workflow run

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prefer equality when comparing values

3 participants