skip to Main Content

I want to create a customized function that takes multiple parameters, in terms of calling it using my name space here I followed the link here, but still didn’t get the formula to create a schema of the function

I have tried to install langugae cypher using the extension of CREATE EXTENSION plpython3u

2

Answers


  1. You have to follow this Setup for Apache AGE, and post installation you use the following commands:

    CREATE EXTENSION age;
    LOAD 'age';
    SET search_path = ag_catalog, "$user", public;
    

    Then, you can create your function using this example, from here:

    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;
    
    Login or Signup to reply.
  2. Function with arguments:

    CREATE OR REPLACE FUNCTION pg_catalog.is_in_list(list agtype, size agtype, what agtype)
        RETURNS boolean AS $$
        BEGIN
          FOR i IN 0..size-1 LOOP
             IF list->i = what
             THEN
                RETURN TRUE;
             END IF;
          END LOOP;
        RETURN FALSE;
        END;
        $$ LANGUAGE plpgsql;
    

    Calling function:

    SELECT * FROM cypher('muse', $$
    MATCH (record:jazz)
    WHERE pg_catalog.is_in_list(record.tags, size(record.tags), 'mellow')
    RETURN record
    $$) as (record agtype);
    

    Example node:

    SELECT * FROM cypher('muse', $$
    CREATE(r: jazz {
    name: 'Kind of Blue',
    artist: 'Miles Davis',
    date: '17 August 1959',
    sub_genre: ['Modal Jazz', 'Cool Jazz'],
    tags: ['instrumental', 'mellow', 'nocturnal', 'soothing', 'improvisation', 'progressive', 'calm'],
    rating: 4.31
    }) RETURN r
    $$) as (jazz_record agtype);
    

    You can create custom functions without arguments like this:

    CREATE OR REPLACE FUNCTION pg_catalog.current_timestamp_components()
    RETURNS TEXT AS $$
    DECLARE
      ts_text TEXT;
    BEGIN
      SELECT to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS') INTO ts_text;
      RETURN ts_text;
    END;
    $$ LANGUAGE plpgsql;
    

    Calling function:

    SELECT *
    FROM cypher('graph', $$
        CREATE (:Person {name: 'Time', title: 'Developer', time: pg_catalog.current_timestamp_components()})
    $$) as (n agtype);
    

    Helpful link: How to MATCH within a list from vertices using Cypher queries?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search