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 some == and != expressions #34124

Merged
merged 2 commits into from
Jul 3, 2024

Conversation

ranma42
Copy link
Contributor

@ranma42 ranma42 commented Jun 30, 2024

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

x ^ y

instead of

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

Similarly, translate x == y to:

x ^ y ^ CAST(1 AS bit)

instead of

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

Contributes to #34001 for simple cases (comparison of BIT expressions).

@ranma42
Copy link
Contributor Author

ranma42 commented Jun 30, 2024

This is a continuation of #34080.

Conceptually it also reduces the number of expressions that hit #34001, but in practice (because of the null rewriting) the expressions that it affects are only those that compare non-nullable BIT expressions.

This would change if there was some optimization like #34126.

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

instead of

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

Similarly, translate `x == y` to:

```sql
x ^ y ^ 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 for simple cases (comparison of BIT expressions).
@maumar maumar merged commit 0337960 into dotnet:main Jul 3, 2024
7 checks passed
@maumar
Copy link
Contributor

maumar commented Jul 3, 2024

Thanks again @ranma42 !

@ranma42 ranma42 deleted the not-equals-xor-simplify branch July 6, 2024 12:56
@roji roji changed the title Use XOR to translate some == and != expressions SQL Server: use XOR to translate some == and != expressions Sep 23, 2024
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