Bitwise Operators
Hi, It would be great to have support for bitwise operators.
In the SQL parser syntax page it seems that there is plan for it already :
| <BIT_AND: "&">
| <BIT_OR: "|">
| <XOR: "^">
But I couldn't find any issues or information about it ? Are you planning to support it ?
We would like to see bitwise operators implemented as well.
Hi guys,
Yes, the new parser already supports the syntax, but the underlying executor still lacks some components. They will be implemented right after 2.2
It's unbelievable that it's still not supported. I made a custom function myself like this:
var op = operator.toLowerCase();
if (op == "and" || op == "&") {
return left & right;
} else if (op == "or" || op == "|") {
return left | right;
} else if (op == "xor" || op == "^") {
return left ^ right;
}
throw "Unsupported bitwise operator.";
SQL statement will be:
CREATE FUNCTION bitwise "var op = operator.toLowerCase();\nif (op == \"and\" || op == \"&\") {\n return left & right;\n} else if (op == \"or\" || op == \"|\") {\n return left | right;\n} else if (op == \"xor\" || op == \"^\") {\n return left ^ right;\n}\nthrow \"Unsupported bitwise operator.\";" PARAMETERS [operator,left,right] IDEMPOTENT true LANGUAGE javascript
And usages are:
SELECT FROM Something WHERE bitwise('and', flags, 6) = 6
SELECT FROM Something WHERE bitwise('&', flags, 3) > 0
SELECT FROM Something WHERE bitwise('or', flags, 2) = 3
SELECT FROM Something WHERE bitwise('xor', flags, 1) = 2
It's inefficient obviously. Can I get another work-around maybe?
I know guys, it's in my TODO list, the parser is ready to support it, but the executor still doesn't.
@luigidellaquila hi Luigi, any news for this functionality ? Is there some version planned in which the functionality will be present ? thx for all the good works!
Hi @rdelangh
it is scheduled for 3.0
Thanks
Luigi
It's already implemented in v 3.0.0 M2 and will be released with the GA
Docs here http://orientdb.com/docs/3.0.x/sql/SQL-Syntax.html
Thanks
Luigi
I think on 3.0.9 bitwise AND seems to doesn't work when the value is return from a function.
select @rid,
mode, /* mode = 42755 */
octet2dec(mode), /* 17901 */
octet2dec(mode) & 0170000, /* is 17901 but shoud be 16384 */
$a,
$b
from Stat
let $a = octet2dec(mode),
$b = $a & 0170000
limit 10

Hi @grzegorz-aniol
I'll check it asap. I guess octet2dec() is a custom function, how did you define it?
Thanks
Luigi
Here is the body function. (x is argument)
return parseInt(x, 8);
Anyway I did my workaround and I created by custom function that calculates bitwise AND.
Hi @gregaubert
It seems the problem here is due to how JS manages numbers.
Even if you use parseInt() the result is returned as a Double, so the bitwise operation fails.
I'm trying to see if I can fix it
Thanks
Luigi
A quick work-around is the following:
octet2dec(mode).asInteger() & 0170000
Thanks
Luigi