GSQL how to send extra key value in our custom tuple UDT

Hi team, anyone here who can help me on this

TYPEDEF Tuple <STRING label, STRING id>  feature_score;
ListAccum<feature_score> @childSegments;

I have a local Accumulator childSegments in my GSQL
and in my Schema i have a vertex segment_type which is connected to multiple seperate segment vertex in schema, below image is the sub part of my schema.

image

CREATE QUERY types_of_segments(/* Parameters here */) FOR GRAPH clientSiga syntax  v2{ 
  /* Write query logic here */ 
  
TYPEDEF Tuple <STRING label, STRING id>  custom_defined;
  ListAccum<custom_defined> @childSegments;
  
  start = {segment_types.*};
  results = select s from start:s-(:ee)-:tt ACCUM 
      IF ee.type == "segment_cv_segment" THEN
        s.@childSegments += custom_defined(tt.id, tt.id)
        # Here, particular for this IF CASE I want childSegments to be [{"id":                    "Long Lapsed", "label": "Long Lapsed", "visit_pattern": [], "spend_pattern": []            }, ......]
        # these two spend_pattern and visit_pattern are extra 
        # required only particular for this case
      else
        # Remaining other cases 
        # [{"id":"PurchasesInPast6Months", "label": "Purchases in Past 6 Months"}, .......]
        s.@childSegments += custom_defined(tt.label, tt.id)
      end
  ORDER BY s.label ASC;
  
  PRINT results; 
}
[
  {
    "results": [
      {
        "attributes": {
          "@childSegments": [
            {
              "id": "PurchasesInPast6Months",
              "label": "Purchases in Past 6 Months"
            }
          ],
          "id": "purchaseTransaction",
          "label": "Purchase Transactions",
          "params": {}
        },
        "v_id": "purchaseTransaction",
        "v_type": "segment_types"
      },
      {
        "attributes": {
          "@childSegments": [
            {
              "id": "FocusedShopper",
              "label": "Focused Shopper"
            },
            {
              "id": "researchers",
              "label": "Researchers"
            }
          ],
          "id": "sessionSegments",
          "label": "Session Segments",
          "params": {}
        },
        "v_id": "sessionSegments",
        "v_type": "segment_types"
      },
      {
        "attributes": {
          "@childSegments": [
            {
              "id": "Long Lapsed",
              "label": "Long Lapsed",
              "spend_pattern": [],
              "visit_pattern": []
            },
            {
              "id": "spender",
              "label": "Spender",
              "spend_pattern": [],
              "visit_pattern": []
            }
          ],
          "id": "shortLoyalty",
          "label": "Short Loyalty",
          "params": {}
        },
        "v_id": "shortLoyalty",
        "v_type": "segment_types"
      }
    ]
  }
]

In some case I require extra key value pair, here in this code i require spend_pattern and visit_pattern particular for this specific condition

Thanks,

Hi team anybody here who can help me on this…please

If you just want an optional extra set of attributes, you could change your TYPEDEF TUPLE to have two additional values, then pass defaults when they arent needed. This isnt the most elegant solution, but it would work

TYPEDEF Tuple <STRING label, STRING id, STRING spend, STRING visit>  custom_defined;
  ListAccum<custom_defined> @childSegments;

 start = {segment_types.*};
  results = select s from start:s-(:ee)-:tt ACCUM 
      IF ee.type == "segment_cv_segment" THEN
        s.@childSegments += custom_defined(tt.label,  tt.id, tt.spendPattern, tt.visitPattern)
      else
            s.@childSegments += custom_defined(tt.label, tt.id, "" , "")
      end
  ORDER BY s.label ASC;
1 Like

@markmegerian thanks for your reply
Actually this one is not giving me the required output,
Requirement is something like I need a SetAccum or BagAccum or Array inside Type Def Tuple. but we cant be able to use this one inside Type def.

tuple is a user-defined data structure consisting of a fixed sequence of base type variables

and suppose if we pass defaults when they arent needed
s.@childSegments += custom_defined(tt.label, tt.id, “” , “”)

            {
              "id": "satisficers",
              "label": "Satisficers",
              "spend": "",
              "visit": ""
            },

then this one giving the weird output , Even though these keys, spend and visit is not or never need in this category case.

I want to call a sub query just to get the SetAccum of spend_pattern and SetAccum of visit_pattern for a only particular category

CREATE QUERY types_of_segments(/* Parameters here */) FOR GRAPH clientSiga syntax  v2{ 
  /* Write query logic here */ 
  
TYPEDEF Tuple <STRING label, STRING id>  custom_defined;
  ListAccum<custom_defined> @childSegments;
  
  start = {segment_types.*};
  results = select s from start:s-(:ee)-:tt ACCUM 
      IF ee.type == "segment_cv_segment" THEN
        s.@childSegments += custom_defined(tt.id, tt.id)
        # Requirement just to call the sub_to get spend pattern and visit pattern particular for this specific category
        #  sub_query(tt)
        # Here, particular for this IF CASE I want childSegments to be [{"id":                    "Long Lapsed", "label": "Long Lapsed", "visit_pattern": [], "spend_pattern": []            }, ......]
        # these two spend_pattern and visit_pattern are extra 
        # required only particular for this case
      else
        # Remaining other cases 
        # [{"id":"PurchasesInPast6Months", "label": "Purchases in Past 6 Months"}, .......]
        s.@childSegments += custom_defined(tt.label, tt.id)
      end
  ORDER BY s.label ASC;
  
  PRINT results; 
}
Sub Query

CREATE QUERY sub_query(vertex<cv_segment> p) RETURNS (SetAccum<STRING>, SetAccum<STRING>){
    SetAccum<STRING>@@spend_group;
    SetAccum<STRING>@@visit_group;
    
    start = {p};
    results = select s from start:s, 
     start:s-(spendgroup_cvsegment:spcvg)-spend_group:spg,
     start:s-(visitgroup_cvsegment)-visit_group:vsg  ACCUM @@spend_group += spg.spend_id, @@visit_group += vsg.visit_id;
    RETURN @@spend_group, @@visit_group;
}

Thanks

I think you are making this more complicated by using the subquery. You can just do it with an ACCUM

CREATE QUERY types_of_segments(/* Parameters here /) FOR GRAPH clientSiga syntax v2{
/
Write query logic here */

TYPEDEF Tuple <STRING label, STRING id> custom_defined;
ListAccum<custom_defined> @childSegments;
MapAccum<custom_defined, SetAccum> @childSegmentSpend, @childSegmentVisit;
SetAccum@spend_group;
SetAccum@visit_group;

start = {segment_types.*};

subq1 = select s from start:s -(segment_cv_segment) - :tt -(spendgroup_cvsegment:spcvg)-
spend_group:spg ACCUM tt.@spend_group += spg.spend_id;

subq2 = select s from start:s -(segment_cv_segment) - :tt -(visitgroup_cvsegment)-visit_group:vsg
ACCUM tt.@visit_group += vsg.visit_id;

results = select s from start:s-(:ee)-:tt ACCUM
IF ee.type == “segment_cv_segment” THEN
s.@childSegmentSpend += ( custom_defined(tt.id, tt.id) → tt.@spend_group),
s.@childSegmentVisit += ( custom_defined(tt.id, tt.id) → tt.@visit_group)

  else
    s.@childSegments += custom_defined(tt.label, tt.id)
  end

ORDER BY s.label ASC;

PRINT results;
}

1 Like