Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the meaning of this postgres error: "ROWS is not applicable when function does not return a set"

Tags:

postgresql

I am new to postgres and getting an error that I can't seem to find on Stackoverflow (?). I am trying to write a function that returns true when a person is qualified for a job and false if they are not. This raises "ERROR: ROWS is not applicable when function does not return a set." Found some stuff in spanish--but nothing on SO. Can someone explain this one?

CREATE OR REPLACE FUNCTION "isPersonQualifiedForJob"(pid integer, jid integer)
  RETURNS bit AS
'
        IF 
        (SELECT count(*) FROM "getSkillsForJob"("jid") "j" WHERE 
        NOT EXISTS ( 
                    SELECT 1 FROM "getSkillsForPerson"("pid") "p" WHERE "j"."SkillID"="p"."SkillID" 
                   )
        )> 0 
        THEN 
           return 0;
        ELSE
           return 1;
        END IF;
'
  LANGUAGE sql VOLATILE
  COST 100
  ROWS 1000;
ALTER FUNCTION "isPersonQualifiedForJob"(integer)
  OWNER TO postgres; 
like image 345
bernie2436 Avatar asked Mar 27 '13 14:03

bernie2436


2 Answers

In this line:

ROWS 1000;

you are telling the planner you expect about 1,000 rows to return from the function but it is declared to return just a single value, a bit:

RETURNS bit AS
like image 153
Clodoaldo Neto Avatar answered Nov 28 '22 04:11

Clodoaldo Neto


The message is quite clear, and the docs :

ROWS result_rows

A positive number giving the estimated number of rows that the planner should expect the function to return. This is only allowed when the function is declared to return a set. The default assumption is 1000 rows.

Your function is not returning a set of rows, but a single boolean, hence I don't know why you are adding that line ROWS 1000 (copy-paste from other function?). Just remove it and you should be fine.

like image 27
leonbloy Avatar answered Nov 28 '22 05:11

leonbloy