How to split string in gsql queries

I am trying in gsql

String p_test ="1,2,3,4";
print split_string(p_test ,",");

its giving error : Error: The tuple name or the function split_string is not defined.

I tried to implement custom C++ function as mentioned here - Split Function and splitting strings inside a query

but unable to compile the function. can someone provide the instruction to compile along with correct function syntax.

@Richard_Henderson is a restart needed after adding custom C++ UDF’s? I don’t think it’s necessary, but was hoping you would remember.

@Gurjit Are you using 2.6 of TigerGraph?

I’m assuming were you following these steps:
https://docs.tigergraph.com/v/2.6/dev/gsql-ref/querying/operators-functions-and-expressions#user-defined-functions

If any code in ExprFunctions.hpp or ExprUtil.hpp causes a compilation error, GSQL cannot install any GSQL query, even if the GSQL query doesn’t call any user-defined function. Therefore, please test each new user-defined expression function after adding it. One way of testing the function is creating a new cpp file test.cpp and compiling it by

g++ test.cpp
./a.out
You might need to remove the include header #include <gle/engine/cpplib/headers.hpp> in ExprFunction.hpp and ExprUtil.hpp in order to compile.

@Jon_Herke yes i tried instructions mentioned in that link and here is the error i am getting while compiling the code

$ g++ test.cpp
In file included from test.cpp:2:0:
ExprFunctions.hpp:67:10: error: ‘ListAccum’ does not name a type
   inline ListAccum string_split(string str, string delimiter) {
          ^
test.cpp:9:8: error: ‘ListAccum’ does not name a type
 inline ListAccum string_split(string str, string delimiter) {
        ^

@Jon_Herke @Richard_Henderson - any updates on how to resolve the error i am getting ?

Re restart, yes a restart is needed.

Gurjit, please post your code.

@rik i just added below lines of code in file /tigergraph/dev/gdk/gsql/src/QueryUdf/ExprFunctions.hpp

but graph queries not compiling after that and giving error. while compiling below i am getting error

“ExprFunctions.hpp:67:10: error: ‘ListAccum’ does not name a type”

   inline ListAccum string_split(string str, string delimiter) {
    ListAccum<string> newList;
    size_t pos = 0;
    std::string token;
    while ((pos = str.find(delimiter)) != std::string::npos) {
      token = str.substr(0, pos);
      newList += token;
      str.erase(0, pos + delimiter.length());
  }
 
  newList += str;
  return newList;
 }

I’m afraid I haven’t had time to try this myself, but I’d guess the problem is you haven’t supplied a type to the returned type in the inital declaration. Try adding or similar to the return type in the declaration i.e.:

inline ListAccum <string>

Hey Richard .Even I am getting Same Error Even After Giving Type
My code looks like this in ExprFunctions.hpp

inline ListAccum <int> sortList (ListAccum <int>& inpList) {
    ListAccum <int> sortedList;
    std::vector<int> arr;

    for (int i=0; i < inpList.size(); i++){
         arr.push_back(inpList.get(i));
    }
    std::sort(arr.begin(),arr.end());
    for(auto x:arr){
        sortedList += x;
    }
    return sortedList;
  }

Error : ./ExprFunctions.hpp:67:10: error: ‘ListAccum’ does not name a type
67 | inline ListAccum sortList (ListAccum & inpList) {
| ^~~~~~~~~

1 Like