How to find internal vertices in variable-length-edges on AgensGraph? - agens-graph

I tried to find internal vertices in variable-length-edges on AgensGraph.
But, It returns error message like following.
Is there problem on CYPHER query on mime?
How can I view internal vertices of VLE?
Attaching sample script follow.
create graph vle;
create vlabel o;
create vlabel l;
create elabel e;
create property index on o ( id );
create property index on l ( id );
create property index on e ( id );
create (:o{id:1})
create (:o{id:2})
create (:o{id:3})
create (:o{id:4})
create (:o{id:5})
create (:o{id:6})
create (:o{id:7})
create (:o{id:8})
create (:o{id:9});
match (o:o) create (:v{id:o.id});
match (n:v) where n.id >= 1 and n.id <= 9
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+1)}]->(:v{id:n.id*10+1})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+2)}]->(:v{id:n.id*10+2})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+3)}]->(:v{id:n.id*10+3})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+4)}]->(:v{id:n.id*10+4})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+5)}]->(:v{id:n.id*10+5})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+6)}]->(:v{id:n.id*10+6})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+7)}]->(:v{id:n.id*10+7})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+8)}]->(:v{id:n.id*10+8})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+9)}]->(:v{id:n.id*10+9});
match (n:v) where n.id >= 11 and n.id <= 99
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+1)}]->(:v{id:n.id*10+1})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+2)}]->(:v{id:n.id*10+2})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+3)}]->(:v{id:n.id*10+3})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+4)}]->(:v{id:n.id*10+4})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+5)}]->(:v{id:n.id*10+5})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+6)}]->(:v{id:n.id*10+6})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+7)}]->(:v{id:n.id*10+7})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+8)}]->(:v{id:n.id*10+8})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+9)}]->(:v{id:n.id*10+9});
match (n:v) where n.id >= 111 and n.id <= 999
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+1)}]->(:v{id:n.id*10+1})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+2)}]->(:v{id:n.id*10+2})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+3)}]->(:v{id:n.id*10+3})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+4)}]->(:v{id:n.id*10+4})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+5)}]->(:v{id:n.id*10+5})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+6)}]->(:v{id:n.id*10+6})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+7)}]->(:v{id:n.id*10+7})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+8)}]->(:v{id:n.id*10+8})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+9)}]->(:v{id:n.id*10+9});
match (n:v) where n.id >= 1111 and n.id <= 9999
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+1)}]->(:v{id:n.id*10+1})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+2)}]->(:v{id:n.id*10+2})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+3)}]->(:v{id:n.id*10+3})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+4)}]->(:v{id:n.id*10+4})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+5)}]->(:v{id:n.id*10+5})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+6)}]->(:v{id:n.id*10+6})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+7)}]->(:v{id:n.id*10+7})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+8)}]->(:v{id:n.id*10+8})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+9)}]->(:v{id:n.id*10+9});
match p = ( (v1:v{id:1})-[:e*]->(v2:v{id:11111}) ) return nodes(p);
ERROR: graph path and variable length edge cannot be used at the same time
LINE 1: match p = ( (v1:v{id:1})-[:e*]->(v2:v{id:11111}) ) return no...
^
Please, help...

It is impossible to get nodes between VLE path on older version of AgensGraph.
Think upgrading version of AgensGraph.
agens (AgensGraph 2.1.0, based on PostgreSQL 10.4)
Type "help" for help.
agens =# match p = ( (v1:v{id:1})-[:e*]->(v2:v{id:11111}) ) return nodes(p);
nodes
----------------------------------------------------------------------------------------------------
[v[6.1]{"id": 1},v[6.10]{"id": 11},v[6.91]{"id": 111},v[6.820]{"id": 1111},v[6.7381]{"id": 11111}]
(1 row)

Related

Cypher - Conditional Create

How Normal Create Works in a Database
In most create circumstances, you don't want to create a new node if one of the unique or primary keys already exist in the database. I also don't want to update the node, as that would be an upsert. I want to do nothing if the key exists in the database. This will work just like a unique constraint.
Create with a Unique Key
If I want only the email field to be unique, I can do this:
MERGE (u:User { email: 'me#you.com' })
ON CREATE SET u.name='Jon Smith'
RETURN u
It won't create the user if there is already an email.
Create with Multiple Unique Keys ?
Let's say I don't want the user to be able to create a user where a username or email is already in the database. You would think this would work:
MERGE (u:User { email: 'me#you.com', username: 'bill' })
ON CREATE SET u.name='Jon Smith'
RETURN u
However, this will still create the node, as only the COMBINATION has to be unique. I want both values separately to be unique... or fail.
Conditional Create
What I want is a conditional create: IF X THEN Y where:
x = email OR username IS NOT IN User
y = CREATE User email="me#you.com", username="bill", role="admin", ...
How can this be done?
J
Please consider the following query:
GRAPH.QUERY g "cypher email='a' username='b'
OPTIONAL MATCH (n)
WHERE n.email = $email OR n.username = $username
WITH n is null as missing
WHERE missing = true
CREATE ({email:$email, username:$username})"
Given an empty graph:
GRAPH.QUERY g "cypher email='a' username='b' OPTIONAL MATCH (n) WHERE n.email = $email OR n.username = $username WITH n is null as missing WHERE missing = true CREATE (n {email:$email, username:$username})"
1) 1) "Nodes created: 1"
2) "Properties set: 2"
3) "Cached execution: 0"
4) "Query internal execution time: 1.469000 milliseconds"
GRAPH.QUERY g "cypher email='a' username='b' OPTIONAL MATCH (n) WHERE n.email = $email OR n.username = $username WITH n is null as missing WHERE missing = true CREATE (n {email:$email, username:$username})"
1) 1) "Cached execution: 1"
2) "Query internal execution time: 0.614000 milliseconds"
Running the exact same query twice, the first execution created the missing node the second one didn't modified the graph.

Snowflake - CREATE table with a date variable in its name

I would like to create a table in snowflake, appending the date to the end of its name. What is the best way to do that?
original table = "DB"."SCHEMA"."CLONEME"
desired new table = "DB"."SCHEMA"."CLONEME_20200812BKP"
Tried setting the date variables, but it didn't work.
First attempt:
set var1= (SELECT TO_CHAR(DATE_TRUNC('DAY',CONVERT_TIMEZONE('UTC', CURRENT_DATE())),'YYYYMMDD'));
set var2 = concat('DB.SCHEMA.CLONEME_',$var1);
create table $var2 clone DB.SCHEMA.CLONEME;
-- and got the following error:
-- SQL compilation error: syntax error line 1 at position 13 unexpected '$var2'.
I'd recommend using the IDENTIFIER function:
https://docs.snowflake.com/en/sql-reference/identifier-literal.html
Example:
CREATE OR REPLACE TABLE CLONEME(
src_string VARCHAR(20));
INSERT INTO CLONEME
VALUES('JKNHJYGHTFGRTYGHJ'), ('ABC123'), (null), ('0123456789');
set var1= (SELECT TO_CHAR(DATE_TRUNC('DAY',CONVERT_TIMEZONE('UTC', CURRENT_DATE())),'YYYYMMDD'));
set var2 = concat('CLONEME_',$var1);
SELECT getvariable('VAR1'), getvariable('VAR2');
--20200812 CLONEME_20200812
create table identifier($var2) clone CLONEME;
--Table CLONEME_20200812 successfully created

How can I increase performance of shortest-path on AgensGraph?

I tried to use shortest-path on AgensGraph.
But, It is quietly slower than other graph database.
How can I increase performance of shortest-path on AgensGraph?
I want to some tips or configuration parameters.
Attaching sample script follow.
create graph shortestpath;
create vlabel o;
create vlabel l;
create elabel e;
create property index on o ( id );
create property index on l ( id );
create property index on e ( id );
create (:o{id:1})
create (:o{id:2})
create (:o{id:3})
create (:o{id:4})
create (:o{id:5})
create (:o{id:6})
create (:o{id:7})
create (:o{id:8})
create (:o{id:9});
match (o:o) create (:l{id:o.id});
match (n:l) where n.id >= 1 and n.id <= 9
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+1)}]->(:l{id:n.id*10+1})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+2)}]->(:l{id:n.id*10+2})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+3)}]->(:l{id:n.id*10+3})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+4)}]->(:l{id:n.id*10+4})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+5)}]->(:l{id:n.id*10+5})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+6)}]->(:l{id:n.id*10+6})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+7)}]->(:l{id:n.id*10+7})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+8)}]->(:l{id:n.id*10+8})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+9)}]->(:l{id:n.id*10+9});
match (n:l) where n.id >= 11 and n.id <= 99
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+1)}]->(:l{id:n.id*10+1})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+2)}]->(:l{id:n.id*10+2})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+3)}]->(:l{id:n.id*10+3})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+4)}]->(:l{id:n.id*10+4})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+5)}]->(:l{id:n.id*10+5})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+6)}]->(:l{id:n.id*10+6})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+7)}]->(:l{id:n.id*10+7})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+8)}]->(:l{id:n.id*10+8})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+9)}]->(:l{id:n.id*10+9});
match (n:l) where n.id >= 111 and n.id <= 999
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+1)}]->(:l{id:n.id*10+1})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+2)}]->(:l{id:n.id*10+2})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+3)}]->(:l{id:n.id*10+3})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+4)}]->(:l{id:n.id*10+4})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+5)}]->(:l{id:n.id*10+5})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+6)}]->(:l{id:n.id*10+6})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+7)}]->(:l{id:n.id*10+7})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+8)}]->(:l{id:n.id*10+8})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+9)}]->(:l{id:n.id*10+9});
match (n:l) where n.id >= 1111 and n.id <= 9999
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+1)}]->(:l{id:n.id*10+1})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+2)}]->(:l{id:n.id*10+2})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+3)}]->(:l{id:n.id*10+3})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+4)}]->(:l{id:n.id*10+4})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+5)}]->(:l{id:n.id*10+5})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+6)}]->(:l{id:n.id*10+6})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+7)}]->(:l{id:n.id*10+7})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+8)}]->(:l{id:n.id*10+8})
create (n)-[:e{id:'l:'+n.id+'->l:'+(n.id*10+9)}]->(:l{id:n.id*10+9});
\timing
match p = allshortestpaths( (l1:l)-[:e*]->(l2:l) ) where l1.id = 1 and l2.id = 11111 return l1.id as l1id, l2.id as l2id, count(p) order by l1id, l2id;
l1id | l2id | count
------+-------+-------
1 | 11111 | 1
(1 row)
Time: 133.547 ms
Is it possible that improve to under 10ms.
There is an improvement on Shortest-Path on "AgensGraph Version 2.1".
Algorithm is changed to "Bidirectional BFS" from "BFS".
agens (AgensGraph 2.1.0, based on PostgreSQL 10.4)
Type "help" for help.
match p = allshortestpaths( (l1:l)-[:e*]->(l2:l) ) where l1.id = 1 and l2.id = 11111 return l1.id as l1id, l2.id as l2id, count(p) order by l1id, l2id;
l1id | l2id | count
------+-------+-------
1 | 11111 | 1
(1 row)
Time: 1.776 ms
If you use "Version 1.3" or "Version 2.0", It is better choice upgrade to "Version 2.1"

How to create node on AgensGraph?

After connected to AgensGraph.
I try to create node on AgensGraph.
But, there is a error on action.
kurt=# create (:v{id:1});
ERROR: graph_path is NULL
HINT: Use SET graph_path
How to set graph_path?
You must create graph, before create vertex(node).
assam=# create graph new_graph;
CREATE GRAPH
After creating graph, set graph_path.
assam=# set graph_path = new_graph;
SET
Finally, You can create a vertex(node).
assam=# create (:v{id:1});
GRAPH WRITE (INSERT VERTEX 1, INSERT EDGE 0)
assam=# match (n:v) return n;
n
-----------------
v[3.1]{"id": 1}
(1 row)

List menu hierarchy using SQL Server hierarchyid

I try to build a menu using the datatype hierarchyid.
I have the root node and the current selected node. now I want to list of all elements that are related wetween root and selected node AND there siblings.
I get all related elements with following sql query
DECLARE #rootNode hierarchyid, #selectedNode hierarchyid
SELECT #rootNode = MenuNode FROM CMS_Menu WHERE MenuItemID = 3;
SELECT #selectedNode = MenuNode FROM CMS_Menu WHERE MenuItemID =15;
SELECT CMS_Menu.MenuNode
FROM CMS_Menu
WHERE #selectedNode.IsDescendantOf(MenuNode) = 1 /*all related elements*/
AND MenuNode.GetLevel() >= #rootNode.GetLevel() /*nothing below root*/
Now I have to do something like MenuNode.GetAncestor(1) = result for each row in the query above.
Does anyone have an idea how to get this in a sql query?
Thanks : )
Not entirely sure I understand the question but could you not do something like the following with the WHERE clause:
WHERE #selectedNode.IsDescendantOf(MenuNode.GetAncestor(1)) = 1
Tom

Resources