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: support bitwise NOT on boolean values #34213

Closed
ranma42 opened this issue Jul 12, 2024 · 3 comments · Fixed by #34303
Closed

SQL Server: support bitwise NOT on boolean values #34213

ranma42 opened this issue Jul 12, 2024 · 3 comments · Fixed by #34303
Assignees
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Milestone

Comments

@ranma42
Copy link
Contributor

ranma42 commented Jul 12, 2024

For boolean expressions, bitwise ~ and logical NOT are generally the same, but in SqlServer the first can be applied on BITs, while the second one is for boolean predicates.

Since #34080 the SqlServer provider avoids converting to a predicate when it just needs to negate a BIT, but it does so using ^ 1.
A better translation could be emitted by translating to a ~.

@roji
Copy link
Member

roji commented Jul 15, 2024

Just noting that bitwise NOT is useful also for non-boolean types, so this would be useful beyond the specific SQL Server scenario we've been focusing on (and beyond SQL Server in general).

@ranma42
Copy link
Contributor Author

ranma42 commented Jul 15, 2024

Non-boolean types can already use the bitwise not:

case ExpressionType.Not:
{
_relationalCommandBuilder.Append("~");

The boolean case is explicitly treated as special:

case ExpressionType.Not
when sqlUnaryExpression.Type == typeof(bool):
{
switch (sqlUnaryExpression.Operand)
{
case InExpression inExpression:
GenerateIn(inExpression, negated: true);
break;
case ExistsExpression existsExpression:
GenerateExists(existsExpression, negated: true);
break;
case LikeExpression likeExpression:
GenerateLike(likeExpression, negated: true);
break;
default:
_relationalCommandBuilder.Append("NOT (");

The current encoding does not differentiate between bitwise and logical not (well, on boolean expressions it is always the logical not, on integer expressions it is always the bitwise not).

@cincuranet cincuranet added this to the 9.0.0 milestone Jul 19, 2024
@cincuranet cincuranet assigned roji and unassigned roji and cincuranet Jul 19, 2024
@ranma42
Copy link
Contributor Author

ranma42 commented Jul 22, 2024

I am thinking about working on this. My approach would be to extend SqlUnaryExpression to also allow OnesComplement.
In the first iteration, I would probably only use it in SqlServer to encode this specific not (changing other parts might needlessly affect other providers).

@maumar maumar closed this as completed in 1e828fd Jul 29, 2024
@maumar maumar added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jul 29, 2024
@ajcvickers ajcvickers modified the milestones: 9.0.0, 9.0.0-rc1 Aug 21, 2024
@roji roji changed the title Support bitwise NOT on boolean values SQL Server: support bitwise NOT on boolean values Sep 23, 2024
@roji roji modified the milestones: 9.0.0-rc1, 9.0.0 Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants