I was writing this cypher query as a function:
CREATE OR REPLACE FUNCTION public.count_friends(name agtype)
RETURNS agtype AS $function$
BEGIN
SELECT * FROM cypher('graph_name', $$
MATCH (v:Person)-[]-(w)
WHERE v.name = name
RETURN COUNT(w) AS friends
$$) AS (friends agtype);
END;
$function$ LANGUAGE plpgsql;
And I was using this select query for testing:
SELECT * FROM cypher('graph_name', $$
MATCH (v:Person)
WHERE public.count_friends(v.name) > 3
RETURN v.name
$$) AS (name agtype);
But, I got an error while executing it:
ERROR: could not find rte for name
LINE 3: WHERE v.name = name
What’s the correct typecast for the variable name in this case? Or, is there something wrong with my code?
Environment: Apache AGE 1.3.0 release, PostgreSQL 13.10, Ubuntu 22.04
2
Answers
The error you are getting might be because the number that is compared in the
WHERE
clause for the testing might be higher than the number of available vertices (WHERE public.count_friends(v.name) > 3
).First, I created a graph to store the
Person
vertices with thename
property.After this, created the function:
And then executed the test query without the
WHERE
clause:It worked fine this way.
There is an issue with
public.count_friends(name agtype)
function.Study this answer to get understand how to return the result from a query inside PostgreSQL function:
https://stackoverflow.com/a/7945958/20972645
Also, the comparison of variables taken as arguments of function with any variable declared or taken by any query within that function gives type miss match errors. Like:
For now, you need to find a way around to achieve this functionality.