@vlad That’s not the same though.
First you used a string with an OR
operator, and I wouldn’t swear that the OR
operator works exactly the same with strings as it does with booleans.
Second, even if it does, your example would be akin to TRUE OR NULL
. Whereas (myID IS NULL OR myId = ?4)
, when myId
is NOT NULL
but ?4
is NULL
, is actually FALSE OR NULL
. Which doesn’t have the same value.
If more explanation is necessary, here’s the full reasoning:
TRUE OR NULL
is TRUE
, but FALSE OR NULL
is NULL
. That’s because NULL
means “unknown”; TRUE OR <something>
is always true, regardless of the value of <something>
, so TRUE OR <something>
is always true; but we can’t apply the same reasoning to FALSE OR NULL
, so FALSE OR NULL
is also unknown, hence it evaluates to NULL
.
Also, still because NULL
means “unknown”, <something> = NULL
is always evaluated to NULL
. Makes sense: whether some value is equal to some unknown value is… well, unknown.
In the case of @springnotes, from what I can see, myId
is generally not NULL
, but his param may be NULL
.
So the first condition (the one that doesn’t work) I gave will evaluate to:
(myID IS NULL OR myId = ?4)
=> ('something' IS NULL OR 'something' = NULL)
=> (FALSE OR NULL)
=> NULL
The second one, on the other hand, will evaluate to:
(?4 NULL OR myId = ?4)
=> (NULL IS NULL OR 'something' = NULL)
=> (TRUE OR NULL)
=> TRUE
Anyway, you may disagree with the reasoning, but postgres cannot lie. You can try this on any psql shell:
hibernate_orm_test=# \set myParam NULL
hibernate_orm_test=# SELECT * FROM (VALUES ('foo'), ('bar')) AS vals(myId) WHERE myId IS NULL OR myId = :myParam;
myid
------
(0 rows)
hibernate_orm_test=# SELECT * FROM (VALUES ('foo'), ('bar')) AS vals(myId) WHERE :myParam IS NULL OR myId = :myParam;
myid
------
foo
bar
(2 rows)