Using SQL in Cypher

AGE does not support embedding SQL statements directly within Cypher queries. However, you can define SQL functions and invoke them from within Cypher commands using user-defined functions.

Note!
Only VOID and scalar-returning functions are supported. Functions returning sets (i.e., SETOF or TABLE) are not currently supported.

Function Creation

CREATE OR REPLACE FUNCTION public.get_event_year(name agtype) RETURNS agtype AS $$
        SELECT year::agtype
        FROM history AS h
        WHERE h.event_name = name::text
        LIMIT 1;
$$ LANGUAGE sql;

Query Execution

SELECT * FROM cypher('graph_name', $$
        MATCH (e:event)
        WHERE e.year < public.get_event_year(e.name)
        RETURN e.name
$$) AS (n agtype);

Result

name
"Apache Con 2021"
(1 row)