0

I am running a simple postgres function to return the count of rows. I am able to run the same query outside function with the output of raise option , but the function doesn't return any rows. I have tried different ways to produce results but unable to. Please find my function below,

CREATE OR REPLACE FUNCTION my_schema.usp_spellcheck3(SearchORItems_WithPipe varchar, site varchar, lan varchar, rows_display integer)
  RETURNS TABLE (docmnt int) AS $BODY$
DECLARE
  arrSearchTerms text[];
  NewTerm varchar;
  i integer;
  AltSearch_withComma varchar;
  AltSearch_withPipe varchar;
  strDidYouMean varchar;
  dpDidYouMean double precision;
  txtDidYouMean Text;
  SearchORItems_withComma  varchar;
  SearchORItems  varchar;
  SearchORItem  varchar;
  ws varchar;
  arrSearchORItems_withComma varchar[];
BEGIN
  strDidYouMean = 'DidYouMeanRow';
  dpDidYouMean = 0.0;
  txtDidYouMean = 'DidYouMeanRow';

  ws = '''' || '%' || site || '%' || '''' ;
  RAISE NOTICE '%', ws;

  SearchORItems = REPLACE(SearchORItems_WithPipe, '|', ',');
  SELECT regexp_split_to_array(SearchORItems, ',') INTO arrSearchORItems_withComma;
  RAISE NOTICE '%', SearchORItems;

  FOR i IN 1 .. coalesce(array_upper(arrSearchORItems_withComma, 1), 1) LOOP
    IF (i = 1) THEN
      SearchORItems_withComma = '''' || arrSearchORItems_withComma[i] || '''';
    ELSE
      SearchORItems_withComma = SearchORItems_withComma||','||'''' || arrSearchORItems_withComma[i] || '''';
    END IF;
  END LOOP;
  RAISE NOTICE '%',SearchORItems_withComma;

  SELECT COUNT(*) INTO res_count 
  FROM ( 
    SELECT 1 FROM my_schema.features f , my_schema.documents d 
    WHERE term IN (SearchORItems_withComma) 
      AND d.docid = f.docid
      AND d.url LIKE ws
    GROUP BY f.docid, d.url) t; 

  RAISE NOTICE '%', res_count;

  SearchORItem = 'SELECT COUNT(*) INTO res_count
    FROM (SELECT 1 FROM my_schema.features f , my_schema.documents d 
    WHERE term IN ('||SearchORItems_withComma||') 
    AND d.docid = f.docid AND d.url LIKE ' || ws ||'
    GROUP BY f.docid, d.url) t';

  RAISE NOTICE '%',SearchORItem;
END;
$BODY$ LANGUAGE SQL VOLATILE; 

this is my query output : NOTICE: '%uni%' NOTICE: daniel,data NOTICE: 'daniel','data' NOTICE: 0 NOTICE: select count(*) into res_count from ( select 1 from my_schema.features f , my_schema.documents d where term in ('daniel','data') and d.docid=f.docid and d.url like '%uni%' group by f.docid,d.url)t

Total query runtime: 16 ms. 0 rows retrieved.

I dont know where I'm going wrong, any help would be appreciated .. Thanks..

1 Answer 1

0

The simple reason that nothing is returned is that you have no RETURN statements in your code. When a function RETURNS TABLE you need to explicitly put one or more RETURN NEXT or RETURN QUERY statements in the body of your code, with a final RETURN statement to indicate the end of the function. See the documentation here: http://www.postgresql.org/docs/9.3/static/plpgsql-control-structures.html#PLPGSQL-STATEMENTS-RETURNING. What exactly you want to return is not clear but likely candidates are res_count and d.docid.

Other than that, your code could use a real clean-up reducing clutter like:

ws := '''%' || site || '%''' ;

instead of:

ws = '''' || '%' || site || '%' || '''' ;

and:

SELECT COUNT(*) INTO res_count 
FROM my_schema.features f,
JOIN my_schema.documents d ON d.docid = f.docid
WHERE term IN (SearchORItems_withComma) 
  AND d.url LIKE ws
GROUP BY f.docid, d.url;

instead of:

SELECT COUNT(*) INTO res_count 
FROM ( 
  SELECT 1 FROM my_schema.features f , my_schema.documents d 
  WHERE term IN (SearchORItems_withComma) 
    AND d.docid = f.docid
    AND d.url LIKE ws
  GROUP BY f.docid, d.url) t;

And you should use the assignment operator (:=) instead of the equality operator in any plpgsql statement that is not a SQL statement.

Sign up to request clarification or add additional context in comments.

4 Comments

Hello @Patrick, Thanks for your reply .. i will change as u suggested but the problem for me is the result count (res_count) value is coming as 0, where as the same query returns a value outside function.. am i going wrong somewhere in appending quotes for IN operator??
Hi @prasadpv. I cannot give you an answer because you have not posted the structure of the tables features and documents. What strikes me as odd is that you GROUP BY docid, url as this would give you multiple results (1 count for each group) into a scalar value res_count. Perhaps there is a group with 0 hits and this result is returned to you. Difficult to say. Perhaps you can make a new question with this specific problem, showing the table structures.
pls find the structure of documents and features table below..CREATE TABLE my_schema.documents ( docid numeric NOT NULL, url character varying, date character varying, language character varying, CONSTRAINT documents_pkey PRIMARY KEY (docid) )CREATE TABLE my_schema.features ( docid double precision NOT NULL, term character varying NOT NULL, term_freq integer, tf numeric(10,4), doc_freq numeric(10,4), idf numeric(10,4), scores numeric(10,4), idf_okapi numeric(10,4), score_okapi numeric(10,4), d numeric(10,4), CONSTRAINT features_pkey PRIMARY KEY (docid, term) )
the exact problem for me is the select (*) returns 1 row when i execute the query .. but wen i execute the function it is returning 0 rows.. this is quite surprising for me as both are same ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.