DEV Community

Muhammad Farooq
Muhammad Farooq

Posted on

Advanced Tutorial for Cypher (Part 2)

This Tutorial is extension of The tutorial Advanced Tutorial for Cypher Part (1)

Create Stored Procedure (1)

You can create Stored Procedure as follows:

  • The fn_array_uniq function enters an array and returns a unique factor.
  • IMMUTABLE represents the classification of the volatility of the procedure. Has the characteristics of calculating and processing returns as constants.
  • STRICT returns a null value whenever the argument is null.
  • The GENERATE_SERIES function takes** (start, stop, step interval)** as input variables and increments and decreases it to the units set in step interval
  • The ARRAY_LOWER / ARRAY_UPPER function returns the lower/upper limit of the array dimension.
CREATE FUNCTION fn_array_uniq (anyarray)
RETURNS anyarray 
IMMUTABLE STRICT LANGUAGE SQL 
AS 
$$
SELECT ARRAY (
SELECT DISTINCT $1[i] 
FROM GENERATE_SERIES
(ARRAY_LOWER($1,1), ARRAY_UPPER($1,1)) AS g(i));
$$;
Enter fullscreen mode Exit fullscreen mode

Create Stored Procedure (2)

You can create Stored Procedure as follows:

  • Ids is a function that takes id of the unique vertex as a factor and appends it to the array.
  • IF [Conditions] Then [True Execution Code] END IF [False Execution Code] Perform execution code when true or false, depending on the conditions.
  • FOREACH [Execution Code] Performs an execution code with each row as a factor.
  • LOOP [Repeat Execution Code] END LOOP Repeat the execution code in the Loop section until there is no input.
CREATE FUNCTION ids(vertex[]) RETURNS text[] AS $$
DECLARE
v vertex;    vids text[];
BEGIN
IF $1 IS NULL THEN
RETURN ARRAY[]::text[];
END IF;
FOREACH v IN ARRAY $1 
LOOP        vids = array_append(vids,
id(v)::text);
END LOOP;
vids = vids[2:];    RETURN vids;
END;
$$ LANGUAGE plpgsql;
Enter fullscreen mode Exit fullscreen mode

Check the Fraud Ring

Explore the raid ring with Cypher Query using Stored Procedure.

SELECT cy.path
FROM (match path = (person:person)-[*..]-(person) with path,ids(nodes(path)) as node
    return path,length(fn_array_uniq(node)) as dcnt, length(node) as cnt) cy
WHERE cnt = dcnt
Enter fullscreen mode Exit fullscreen mode
  • Use the fn_array_uniq function to query the unique Node ID of the Path to be viewed as an array.
  • Use the Ids function to extract only the Node IDs to consider and count and compare them.
  • GraphPath that meets the previous conditions among the Paths inquired in the Match clause is printed and inquired.

Explore

Top comments (0)