I am trying to access a map attribute of a Vertex type, could someone tell how to access in GSQL ? The version of GSQL I am using is 3.11
Hi @tiger_boi ,
For Map attribute, you should be able to access it just like any other attributes.
E.g. suppose I have the following schema:
I can have the query seen here - where you can access functions like containsKey
and get
from a Map attribute:
CREATE OR REPLACE DISTRIBUTED QUERY print_test_map_attr(STRING key_to_check = "k1") {
MapAccum<STRING, STRING> @map_attr_string_acc;
SumAccum<STRING> @val_at_key;
all_test_map = {Test_Map.*};
all_test_map = SELECT s FROM all_test_map:s
POST-ACCUM s.@map_attr_string_acc += s.map_attr_string
;
all_test_map_with_attr_filter = SELECT s FROM all_test_map:s
WHERE s.map_attr_string.containsKey(key_to_check)
POST-ACCUM s.@val_at_key = s.map_attr_string.get(key_to_check)
;
PRINT all_test_map;
PRINT all_test_map_with_attr_filter;
}
Note: if you want to “modify” the map attribute, you’ll have to first assign it to a MapAccum with corresponding type. Then, you can set it. Note that the updates will be performed AFTER the query finished running, so the results from the query wasn’t updated yet.
CREATE OR REPLACE DISTRIBUTED QUERY print_test_map_attr_remove_key(STRING key_to_check = "k1") {
MapAccum<STRING, STRING> @map_attr_string_acc;
SumAccum<STRING> @val_at_key;
all_test_map = {Test_Map.*};
all_test_map = SELECT s FROM all_test_map:s
POST-ACCUM s.@map_attr_string_acc += s.map_attr_string
;
all_test_map_with_attr_filter = SELECT s FROM all_test_map:s
WHERE s.map_attr_string.containsKey(key_to_check)
POST-ACCUM
s.@val_at_key = s.map_attr_string.get(key_to_check),
s.@map_attr_string_acc.remove(key_to_check),
s.map_attr_string = s.@map_attr_string_acc
;
PRINT all_test_map;
PRINT all_test_map_with_attr_filter;
}
I hope this helps!
Best,
Supawish Limprasert (Jim)
Solution Engineer, TigerGraph