Skip to content

Bitwise operators not working properly for PostgreSQL #40484

@Arzaroth

Description

@Arzaroth
  • Laravel Version: 8.79.0
  • PHP Version: 7.4.27
  • Database Driver & Version: MariaDB 10.6.5, PostgreSQL 13.5

Description:

MariaDB supports a bitwise AND operator as follows:

SELECT 'a' WHERE (3 & 1);

PostgreSQL also does, but requires a bool cast like so:

SELECT 'a' WHERE (3 & 1):::bool;

The expression itself produces an integer, so I understand the necessity to cast in raw SQL.
However, given this operator is in the list of available ones, alongside other bitwise operators (e.g. |, ^, &~, etc...) I fail to see how it is supposed to be used.

Steps To Reproduce:

Let's pretend you have a Model named Order, with a status column. Since the status could be a combination of many, we'll use a bitmask in order to "combine" multiple status in one column.

Then, we should be able to query the table like this :

$query = Order::query();
// doing some stuffs
$query->where('status', '&', 1)->get();

This yields a PDOException.

What we currently have to do :

$query = Order::query();
// doing some stuffs
$query->where($query->raw('(status & 1)'), '!=', 0)->get();

This could cause some problems, since the column name is no longer escaped.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions