use of org.apache.atlas.groovy.TypeCoersionExpression in project incubator-atlas by apache.
the class Gremlin3ExpressionFactory method generateGroupByExpression.
@Override
public GroovyExpression generateGroupByExpression(GroovyExpression parent, GroovyExpression groupByExpression, GroovyExpression aggregationFunction) {
GroovyExpression result = new FunctionCallExpression(TraversalStepType.BARRIER, parent, "group");
GroovyExpression groupByClosureExpr = new TypeCoersionExpression(new ClosureExpression(groupByExpression), "Function");
result = new FunctionCallExpression(TraversalStepType.SIDE_EFFECT, result, "by", groupByClosureExpr);
result = new FunctionCallExpression(TraversalStepType.END, result, "toList");
GroovyExpression mapValuesClosure = new ClosureExpression(new FunctionCallExpression(new CastExpression(getItVariable(), "Map"), "values"));
result = new FunctionCallExpression(result, "collect", mapValuesClosure);
//when we call Map.values(), we end up with an extra list around the result. We remove this by calling toList().get(0). This
//leaves us with a list of lists containing the vertices that match each group. We then apply the aggregation functions
//specified in the select list to each of these inner lists.
result = new FunctionCallExpression(result, "toList");
result = new FunctionCallExpression(result, "get", new LiteralExpression(0));
GroovyExpression aggregrationFunctionClosure = new ClosureExpression(aggregationFunction);
result = new FunctionCallExpression(result, "collect", aggregrationFunctionClosure);
return result;
}
Aggregations