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

SQL Server: Use XOR to translate more == and != expressions #34168

Merged
merged 3 commits into from
Jul 9, 2024

Conversation

ranma42
Copy link
Contributor

@ranma42 ranma42 commented Jul 5, 2024

When the parent expression is not a predicate, translate x != y to:

CAST(x ^ y AS BIT)

instead of

CASE
    WHEN x <> y THEN CAST(1 AS bit)
    ELSE CAST(0 AS bit)
END

Similarly, translate x == y to:

CAST(x ^ y AS BIT) ^ CAST(1 AS bit)

instead of

CASE
    WHEN x == y THEN CAST(1 AS bit)
    ELSE CAST(0 AS bit)
END

Contributes to #34001.

When the parent expression is not a predicate, translate `x != y` to:
```sql
CAST(x ^ y AS BIT)
```

instead of

```sql
CASE
    WHEN x <> y THEN CAST(1 AS bit)
    ELSE CAST(0 AS bit)
END
```

Similarly, translate `x == y` to:

```sql
CAST(x ^ y AS BIT) ^ CAST(1 AS bit)
```

instead of

```sql
CASE
    WHEN x == y THEN CAST(1 AS bit)
    ELSE CAST(0 AS bit)
END
```

Contributes to dotnet#34001.
@ranma42
Copy link
Contributor Author

ranma42 commented Jul 5, 2024

This extends #34124 to all integer types.

@maumar maumar merged commit 349f4bd into dotnet:main Jul 9, 2024
7 checks passed
@maumar
Copy link
Contributor

maumar commented Jul 9, 2024

Thanks again!

@roji
Copy link
Member

roji commented Jul 10, 2024

Similarly, translate x == y to:

CAST(x ^ y AS BIT) ^ CAST(1 AS bit)

Any reason not to use the simpler bitwise NOT operator here, i.e. ~CAST(x ^ y AS BIT)?

@ranma42
Copy link
Contributor Author

ranma42 commented Jul 10, 2024

Similarly, translate x == y to:
CAST(x ^ y AS BIT) ^ CAST(1 AS bit)

Any reason not to use the simpler bitwise NOT operator here, i.e. ~CAST(x ^ y AS BIT)?

You're right, it should be used both for

result = _sqlExpressionFactory.MakeBinary(
ExpressionType.ExclusiveOr,
result,
_sqlExpressionFactory.Constant(true, result.TypeMapping),
result.TypeMapping
)!;
and for
return _sqlExpressionFactory.MakeBinary(
ExpressionType.ExclusiveOr,
negatedOperand,
_sqlExpressionFactory.Constant(true, negatedOperand.TypeMapping),
negatedOperand.TypeMapping
)!;

It requires some changes that are not 100% trivial (currently "not" on boolean is always translated as NOT, ~ is only used for other types), but I will definitely work on that.

@ranma42 ranma42 deleted the sqlserver-equals branch July 10, 2024 11:55
@roji
Copy link
Member

roji commented Jul 11, 2024

Just to be sure, all this is for SQL Server only right? Do you want to open another issue to track using bitwise not?

@ranma42
Copy link
Contributor Author

ranma42 commented Jul 12, 2024

Just to be sure, all this is for SQL Server only right? Do you want to open another issue to track using bitwise not?

At least in the context of the efcore repo, yes (and in general I guess there are few providers which have a distinct notion of boolean-in-predicates vs boolean-in-values, but maybe I am wrong).

Filed #34213.

@roji roji changed the title Use XOR to translate more == and != expressions SQL Server: Use XOR to translate more == and != expressions Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants