orientdb icon indicating copy to clipboard operation
orientdb copied to clipboard

Bitwise Operators

Open gregaubert opened this issue 11 years ago • 12 comments

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 ?

gregaubert avatar Aug 03 '15 16:08 gregaubert

We would like to see bitwise operators implemented as well.

nethertek avatar Oct 30 '15 23:10 nethertek

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

luigidellaquila avatar Nov 02 '15 15:11 luigidellaquila

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?

gongdo avatar May 25 '16 06:05 gongdo

I know guys, it's in my TODO list, the parser is ready to support it, but the executor still doesn't.

luigidellaquila avatar May 25 '16 07:05 luigidellaquila

@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!

langhha avatar Oct 17 '16 08:10 langhha

Hi @rdelangh

it is scheduled for 3.0

Thanks

Luigi

luigidellaquila avatar Oct 17 '16 08:10 luigidellaquila

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

luigidellaquila avatar Jul 31 '17 13:07 luigidellaquila

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

image

grzegorz-aniol avatar Nov 15 '18 09:11 grzegorz-aniol

Hi @grzegorz-aniol

I'll check it asap. I guess octet2dec() is a custom function, how did you define it?

Thanks

Luigi

luigidellaquila avatar Nov 15 '18 11:11 luigidellaquila

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.

grzegorz-aniol avatar Nov 15 '18 11:11 grzegorz-aniol

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

luigidellaquila avatar Nov 16 '18 10:11 luigidellaquila

A quick work-around is the following:

octet2dec(mode).asInteger() & 0170000

Thanks

Luigi

luigidellaquila avatar Nov 16 '18 10:11 luigidellaquila