LOAD 'age';

DROP FUNCTION IF EXISTS ag_catalog.bench_message_content(bigint);

CREATE OR REPLACE FUNCTION ag_catalog.bench_message_content(
    p_mid bigint
) RETURNS TABLE(
    creationDate agtype,
    content agtype
) AS $$
DECLARE
    v_json_params text;
BEGIN
    v_json_params := format('{"mid": %s}', p_mid);

    RETURN QUERY EXECUTE $exec$
        SELECT *
        FROM ag_catalog.cypher('snb_graph', $cypher$
            MATCH (m:Post {id: $mid})
            RETURN m.creationDate, coalesce(m.content, m.imageFile, 'No content')

            UNION ALL

            MATCH (m:Comment {id: $mid})
            RETURN m.creationDate, coalesce(m.content, m.imageFile, 'No content')
        $cypher$, $1) AS (
            creationDate agtype,
            content agtype
        );
$exec$ USING v_json_params::agtype;
END;
$$ LANGUAGE plpgsql;