My library of UDF's

So, I got challenged by a question elsewhere. How to filter a vertex based on whether a string is a member of vertex attribute which is a list or a set.

Here is my best shot. It is also a useful cheat sheet for getting lists/sets in and out, and iterating them.Not obvious to a C++ noob like myself. It uses the regex string function (defined in my library above), as I thought people might be searching for names in full names, for example.

// List filter, returns a list 
  inline ListAccum <string> regex_filter_list (ListAccum <string>& inBag , string regEx) {
    ListAccum <string> outBag;

for (int i=0; i < inBag.size(); i++){
    if (str_regex_match(inBag.get(i), regEx)) {
        outBag += inBag.get(i);
    }
}
return outBag;
  }

// Set filter, returns a set
  inline SetAccum <string> regex_filter_set (SetAccum <string>& inSet , string regEx) {
SetAccum <string> outSet;

for (auto& it :  inSet.data_) {
  if (str_regex_match(it, regEx)) {
    outSet += it;
  }
}
return outSet;
  }

An example test query:

CREATE QUERY tryFilterBag(string regex) FOR GRAPH MyGraph { 
  /* Write query logic here */ 
	ListAccum <STRING> @outList ;
	SetAccum <STRING> @outSet;
	
	bv = {BagVertex.*}; 
	
	bv = select bvv from bv:bvv
	POST-ACCUM 
	  bvv.@outList = regex_filter_list(bvv.myList, regex),
	  bvv.@outSet = regex_filter_set(bvv.mySet, regex);
      //HAVING bvv.@outList.size() > 0; // uncomment for direct filtration
	  
	cv = select cvv from bv:cvv
	WHERE cvv.@outList.size() > 0 AND cvv.@outSet.size() > 0;

  PRINT bv, cv; 
}
2 Likes