Skip to content
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

[release/9.0] Avoid using ^ and ~ when invalid because of value converters #35241

Merged
merged 1 commit into from
Dec 2, 2024

Conversation

roji
Copy link
Member

@roji roji commented Nov 29, 2024

Fixes #35093
Backports #35124

Description

EF 9 brings many improvements to SQL generation (list). One of these, which also improves query performance, applies bit arithmetic (XOR) to implement equality in contexts where SQL Server requires a BIT value (as opposed to a search condition, so projected SELECT values rather than WHERE predicates).

Unfortunately, the SQL optimization was implemented too widely, applying also on value-converted values, which is incorrect.

Customer impact

Queries on SQL Server which involve equality with a value-converted value sometimes generate invalid SQL and fail. For example:

_ = await context.Orders
    .Select(order => new { Paid = order.State == OrderState.Paid })
    // The following is OK (non-value-converted string)
    // .Select(order => new { IsNameFoo = order.Name == "Foo" })
    .ToListAsync();

This scenario is particularly common with value-converted enums, and so has the potential to affect many users. The fix is trivial and low-risk.

How found

Customer reported on 9.

Regression

Yes, from 8.

Testing

Tests added.

Risk

Low and quirked.

/cc @ranma42

…net#35124)

The transformation of equality/in-equality in a (negated) XOR is only possible
when the expressions are BIT or integer types on the SQL side (i.e. taking value
conversion into account).

Similarly, the Boolean negation `NOT` can be implemented as `~` only if the
underlying expression is a BIT.

Fixes dotnet#35093.

(cherry picked from commit e6abfdd)
@roji roji marked this pull request as ready for review November 30, 2024 09:17
@roji roji requested a review from a team November 30, 2024 09:17
@roji
Copy link
Member Author

roji commented Dec 2, 2024

Servicing approved via email.

@roji roji merged commit fba8789 into dotnet:release/9.0-staging Dec 2, 2024
7 checks passed
@roji roji deleted the SqlServerNegation9 branch December 2, 2024 08:45
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.

3 participants