Skip to content

Commit

Permalink
Create forEachTest.gsql
Browse files Browse the repository at this point in the history
  • Loading branch information
mingxiw authored Jan 21, 2025
1 parent 20738d2 commit fc29a02
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions demos/guru_scripts/docker/tutorial/4.x/script/forEachTest.gsql
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
USE GRAPH financialGraph
CREATE OR REPLACE QUERY ForeachTest ( ) SYNTAX V3 {

ListAccum<UINT> @@listVar = [1, 2, 3];
SetAccum<UINT> @@setVar = (1, 2, 3);
BagAccum<UINT> @@bagVar = (1, 2, 3);

SetAccum<UINT> @@set1;
SetAccum<UINT> @@set2;
SetAccum<UINT> @@set3;

#FOREACH item in collection accumlators variables
S = SELECT tgt
FROM (s:Account) -[e:Transfer]-> (tgt)
ACCUM
@@listVar += e.amount,
@@setVar += e.amount,
@@bagVar += e.amount;

PRINT @@listVar, @@setVar, @@bagVar;

//loop element in a list
FOREACH i IN @@listVar DO
@@set1 += i;
END;

//loop element in a set
FOREACH i IN @@setVar DO
@@set2 += i;
END;

//loop element in a bag
FOREACH i IN @@bagVar DO
@@set3 += i;
END;

PRINT @@set1, @@set2, @@set3;

//show step of loop var
ListAccum<INT> @@st;
FOREACH k IN RANGE[-1,4].STEP(2) DO
@@st += k;
END;

PRINT @@st;

ListAccum<INT> @@t;

//nested loop:
// outer loop iterates 0, 1, 2.
// inner loop iterates 0 to i
FOREACH i IN RANGE[0, 2] DO
@@t += i;
S = SELECT s
FROM (s:Account)
WHERE s.name =="Scott"
ACCUM
FOREACH j IN RANGE[0, i] DO
@@t += j
END;
END;
PRINT @@t;

MapAccum<STRING,STRING> @@mapVar, @@mapVarResult;
S = SELECT s
FROM (s:Account)
WHERE s.name =="Scott" OR s.name == "Jennie"
ACCUM @@mapVar += (s.name -> s.isBlocked);

//loop (k,v) pairs of a map
FOREACH (keyI, valueJ) IN @@mapVar DO
@@mapVarResult += (keyI -> valueJ);
END;

PRINT @@mapVar, @@mapVarResult;

}

INSTALL QUERY ForeachTest

RUN QUERY ForeachTest()

0 comments on commit fc29a02

Please sign in to comment.