Nested Group By

I am trying to work with a nested GroupBy but having issues finding the right syntax to populate the accumulator and the documentation doesn’t show a clear example of this, any help?

CREATE DISTRIBUTED QUERY vert_stats (
       STRING from_vtype, STRING etype, STRING to_vtype)  
     FOR GRAPH my_graph { 

    //nested GroupBy not working 
    //vertex/edge stats grouped by multiple attributes, i.e. make/model but can be extended to type/source or even make/model/source
    GroupByAccum<STRING mk_type, 
      GroupByAccum<STRING mdl_type, STRING from_vtype,STRING etype,STRING to_vtype , SumAccum<INT> nodeWithEdgeCount, SumAccum<INT> edgeCount, MinAccum<INT> minDegree , MaxAccum<INT> maxDegree, AvgAccum avgDegree>                                   mdl_stats>@@_stat_metrics2;

    Result = SELECT s
             FROM Start:s-(etype:e)->to_vtype:t
             POST-ACCUM
    ERROR>>          @@_stat_metrics2 += (s.make_id-> (s.model_id-> from_vtype, etype, to_vtype, 1,s.outdegree(etype),s.outdegree(etype),s.outdegree(etype),s.outdegree(etype) ));

    FOREACH g IN @@_stat_metrics DO
        PRINT g.x_type , g.from_vtype, g.etype, g.to_vtype, g.nodeWithEdgeCount, g.edgeCount, g.minDegree, g.maxDegree,g.avgDegree;
    END;
}

The error message is:

Incompatible operand types GroupByAccum<string mk_type, GroupByAccum<string mdl_type, string from_vtype, string etype, string to_vtype, SumAccum<int> nodeWithEdgeCount, SumAccum<int> edgeCount, MinAccum<int> minDegree, MaxAccum<int> maxDegree, AvgAccum avgDegree> mdl_stats> and List<int, List<int, string, string, string, int, int, int, int, int>> for the operator +=

1 Like

what type is s.make_id and s.model_id?
You’ve declared them as strings in the group by accum, but the parser is saying they are int’s.

Otherwise your group by syntax looks correct. Well done. It’s possibly the most difficult construct.

Also your PRINT statement looks like it has the wrong fields in it, but I’m sure you’ll get to that :slight_smile:

Yes the error was it needed to be Int not String on the declaration, the error didn’t make that clear referring to the operand.

Another Nested GroupBy issue:

//====================================================================================================
// this part works:
//====================================================================================================

GroupByAccum<string dayOfYear, string airPort, SumAccum<int> flightCount,
    GroupByAccum<string airline, SumAccum<int> landings,
        SumAccum<double> weight, SumAccum<int> paxCount> byAirline> @@byApt;

typedef tuple<string dayOfYear, string airport, int flightCount, string airline, int landings,
    double weight, int paxCount> combinedTuple;

ListAccum<combinedTuple> @@list ;

SELECT s
  FROM flights:s
 ACCUM @@byApt += (s.DayOfYear, s.Apt -> 1, ( s.Airline -> 1, s.Weight, s.Pax ))

PRINT @@byApt;  <-- prints accurately
RETURN @@byApt; <-- GroupByAccum not permitted as RETURN from subquery
//====================================================================================================
// Here is the part that throws an error:
// the line: , byApt.Airline.airline
// is underlined in red and the error balloon reads: no type can be inferred for byApt.Airline.airline
// and the query will not compile
//====================================================================================================

// Since a GroupBy Accum can not be a RETURN value, transfer @@byApt to a list
FOREACH byApt IN @@byApt DO
  FOREACH Airline IN byAirline DO
    @@list += combinedTuple( byApt.dayOfYear
                           , byApt.airPort
                           , byApt.flightCount
                           , byApt.Airline.airline    <-- no type can be inferred for byApt.Airline.airline
                           , byApt.Airline.landings
                           , byApt.Airline.weight
                           , byApt.Airline.paxCount  )
  END;
END;

PRINT @@list;   <-- unable due to error compiling as shown above
RETURN @@list;  <-- needs to be RETURNED to calling query

The issue is the reference…use this instead:

// Since a GroupBy Accum can not be a RETURN value, transfer @@byApt to a list
FOREACH byApt IN @@byApt DO
  FOREACH Airline IN byAirline DO
    @@list += combinedTuple( byApt.dayOfYear
                           , byApt.airPort
                           , byApt.flightCount
                           , Airline.airline    
                           , Airline.landings
                           , Airline.weight
                           , Airline.paxCount  )
  END;
END;