Search in sources :

Example 6 with TypeCoersionExpression

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;
}
Also used : TypeCoersionExpression(org.apache.atlas.groovy.TypeCoersionExpression) LiteralExpression(org.apache.atlas.groovy.LiteralExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) ClosureExpression(org.apache.atlas.groovy.ClosureExpression) CastExpression(org.apache.atlas.groovy.CastExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Aggregations

GroovyExpression (org.apache.atlas.groovy.GroovyExpression)6 TypeCoersionExpression (org.apache.atlas.groovy.TypeCoersionExpression)6 FunctionCallExpression (org.apache.atlas.groovy.FunctionCallExpression)5 ClosureExpression (org.apache.atlas.groovy.ClosureExpression)3 CastExpression (org.apache.atlas.groovy.CastExpression)1 ComparisonOperatorExpression (org.apache.atlas.groovy.ComparisonOperatorExpression)1 IdentifierExpression (org.apache.atlas.groovy.IdentifierExpression)1 ListExpression (org.apache.atlas.groovy.ListExpression)1 LiteralExpression (org.apache.atlas.groovy.LiteralExpression)1 TernaryOperatorExpression (org.apache.atlas.groovy.TernaryOperatorExpression)1 VariableAssignmentExpression (org.apache.atlas.groovy.VariableAssignmentExpression)1