Nima Bahrami
Nima Bahrami

Reputation: 15

How to query inside "bytea" data type column with postgresql?

I am trying to use AND operator between variables with the type of byte. the error below is bugging me.

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedFunction) operator does not exist: bytea & bytea
LINE 3: WHERE (doctors.schedule & '\x0000000000000000000000000000000...
                                ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.

let's say those two variables are:

v = b'\x00\x00\x00\x03\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

c = b'\xff\xff\xc0\x03\xff\xff\xff\xff\xfc\x03\xff\xfe\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

any idea how to solve this? I appreciate it in advance.

Upvotes: 1

Views: 467

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 246493

There is no & operator for bytea. After all, bytea stands for "byte array", not for "bit array".

There is, however, a function to test if a certain bit is set:

SELECT get_bit('\xF0'::bytea, 4);

 get_bit 
═════════
       1
(1 row)

This would get the value of the fifth bit from the lower end: 11110000

If you want to access the individual bits of a value, you should use the data type bit varying. That will also give you the bitwise operations you'd expect:

SELECT B'11110000' & B'00011000';

 ?column? 
══════════
 00010000
(1 row)

(A string literal wit preceding B is a bit varying literal.)

Upvotes: 1

Related Questions