I wrote a simple user-defined function to check for the existence of rows matching some conditions:
CREATE OR REPLACE FUNCTION is_instructor_specialized_in(eid INT, course_area VARCHAR(50))
RETURNS BOOLEAN AS $$
SELECT EXISTS(SELECT 1 FROM Specializes s WHERE s.eid = eid AND s.name = course_area);
$$ LANGUAGE sql;
I tested it with the following query:
SELECT is_instructor_specialized_in(2, 'Artificial Intelligence') as function_output,
EXISTS(SELECT 1 FROM Specializes s WHERE s.eid = 2 AND s.name = 'Artificial Intelligence') as ground_truth;
and the function gave a wrong value of true
when it is supposed to evaluate to false
(there is no such row in the Specializes
table):
image
In fact, it always gives the value of true
. I'm super confused. Is there any reason why this is happening?
Version: PostgreSQL 13.2 on x86_64-apple-darwin19.6.0, compiled by Apple clang version 11.0.3 (clang-1103.0.32.62), 64-bit