Search in sources :

Example 11 with FunctionCallExpression

use of org.apache.atlas.groovy.FunctionCallExpression in project incubator-atlas by apache.

the class Gremlin3ExpressionFactory method generateOrderByExpression.

@Override
public GroovyExpression generateOrderByExpression(GroovyExpression parent, List<GroovyExpression> translatedOrderBy, boolean isAscending) {
    GroovyExpression orderByExpr = translatedOrderBy.get(0);
    GroovyExpression orderByClosure = new ClosureExpression(orderByExpr);
    GroovyExpression orderByClause = new TypeCoersionExpression(orderByClosure, FUNCTION_CLASS);
    GroovyExpression aExpr = new IdentifierExpression("a");
    GroovyExpression bExpr = new IdentifierExpression("b");
    GroovyExpression aCompExpr = new FunctionCallExpression(new FunctionCallExpression(aExpr, TO_STRING_METHOD), TO_LOWER_CASE_METHOD);
    GroovyExpression bCompExpr = new FunctionCallExpression(new FunctionCallExpression(bExpr, TO_STRING_METHOD), TO_LOWER_CASE_METHOD);
    GroovyExpression comparisonExpr = null;
    if (isAscending) {
        comparisonExpr = new ComparisonOperatorExpression(aCompExpr, bCompExpr);
    } else {
        comparisonExpr = new ComparisonOperatorExpression(bCompExpr, aCompExpr);
    }
    ClosureExpression comparisonFunction = new ClosureExpression(comparisonExpr, "a", "b");
    FunctionCallExpression orderCall = new FunctionCallExpression(TraversalStepType.BARRIER, parent, ORDER_METHOD);
    return new FunctionCallExpression(TraversalStepType.SIDE_EFFECT, orderCall, BY_METHOD, orderByClause, comparisonFunction);
}
Also used : TypeCoersionExpression(org.apache.atlas.groovy.TypeCoersionExpression) ComparisonOperatorExpression(org.apache.atlas.groovy.ComparisonOperatorExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) ClosureExpression(org.apache.atlas.groovy.ClosureExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression) IdentifierExpression(org.apache.atlas.groovy.IdentifierExpression)

Example 12 with FunctionCallExpression

use of org.apache.atlas.groovy.FunctionCallExpression in project incubator-atlas by apache.

the class GremlinExpressionFactory method fillVarWithTypeInstances.

private GroovyExpression fillVarWithTypeInstances(GraphPersistenceStrategies s, String typeName, String fillVar) {
    GroovyExpression graphExpr = getAllVerticesExpr();
    GroovyExpression typeAttributeNameExpr = new LiteralExpression(s.typeAttributeName());
    GroovyExpression typeNameExpr = new LiteralExpression(typeName);
    GroovyExpression hasExpr = new FunctionCallExpression(graphExpr, HAS_METHOD, typeAttributeNameExpr, typeNameExpr);
    GroovyExpression fillExpr = new FunctionCallExpression(hasExpr, FILL_METHOD, new IdentifierExpression(fillVar));
    return fillExpr;
}
Also used : LiteralExpression(org.apache.atlas.groovy.LiteralExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression) IdentifierExpression(org.apache.atlas.groovy.IdentifierExpression)

Example 13 with FunctionCallExpression

use of org.apache.atlas.groovy.FunctionCallExpression in project incubator-atlas by apache.

the class GremlinExpressionFactory method fillVarWithSubTypeInstances.

private GroovyExpression fillVarWithSubTypeInstances(GraphPersistenceStrategies s, String typeName, String fillVar) {
    GroovyExpression graphExpr = getAllVerticesExpr();
    GroovyExpression superTypeAttributeNameExpr = new LiteralExpression(s.superTypeAttributeName());
    GroovyExpression typeNameExpr = new LiteralExpression(typeName);
    GroovyExpression hasExpr = new FunctionCallExpression(graphExpr, HAS_METHOD, superTypeAttributeNameExpr, typeNameExpr);
    GroovyExpression fillExpr = new FunctionCallExpression(hasExpr, FILL_METHOD, new IdentifierExpression(fillVar));
    return fillExpr;
}
Also used : LiteralExpression(org.apache.atlas.groovy.LiteralExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression) IdentifierExpression(org.apache.atlas.groovy.IdentifierExpression)

Example 14 with FunctionCallExpression

use of org.apache.atlas.groovy.FunctionCallExpression in project incubator-atlas by apache.

the class ExpandOrsOptimization method expandOrFunction.

/**
     * This method takes an 'or' expression and expands it into multiple expressions.
     *
     * For example:
     *
     * g.V().or(has('x'),has('y')
     *
     * is expanded to:
     *
     * g.V().has('x')
     * g.V().has('y')
     *
     * There are certain cases where it is not safe to move an expression out
     * of the 'or'.  For example, in the expression
     *
     * g.V().or(has('x').out('y'),has('z'))
     *
     * has('x').out('y') cannot be moved out of the 'or', since it changes the value of the traverser.
     *
     * At this time, the ExpandOrsOptimizer is not able to handle this scenario, so we don't remove
     * that expression.  In cases like this, a final expression is created that ors together
     * all of the expressions that could not be extracted.  In this case that would be:
     *
     * g.V().has('z')
     * g.V().or(has('y').out('z'))
     *
     * This processing is done recursively.
     *
     *
     * @param expr
     * @param context
     * @return the expressions that should be unioned together to get the query result
     */
private List<GroovyExpression> expandOrFunction(GroovyExpression expr, OptimizationContext context) {
    FunctionCallExpression functionCall = (FunctionCallExpression) expr;
    GroovyExpression caller = functionCall.getCaller();
    List<GroovyExpression> updatedCallers = null;
    if (caller != null) {
        updatedCallers = expandOrs(caller, context);
    } else {
        updatedCallers = Collections.singletonList(null);
    }
    UpdatedExpressions newArguments = getUpdatedChildren(functionCall.getArguments(), context);
    List<GroovyExpression> allUpdatedArguments = new ArrayList<>();
    for (List<GroovyExpression> exprs : newArguments.getUpdatedChildren()) {
        allUpdatedArguments.addAll(exprs);
    }
    List<AbstractFunctionExpression> extractableArguments = new ArrayList<>();
    List<GroovyExpression> nonExtractableArguments = new ArrayList<>();
    for (GroovyExpression argument : allUpdatedArguments) {
        if (GremlinQueryOptimizer.isExtractable(argument)) {
            extractableArguments.add((AbstractFunctionExpression) argument);
        } else {
            logger_.warn("Found non-extractable argument '{}; in the 'or' expression '{}'", argument.toString(), expr.toString());
            nonExtractableArguments.add(argument);
        }
    }
    List<GroovyExpression> result = new ArrayList<>();
    for (GroovyExpression updatedCaller : updatedCallers) {
        for (AbstractFunctionExpression arg : extractableArguments) {
            GroovyExpression updated = GremlinQueryOptimizer.copyWithNewLeafNode(arg, updatedCaller);
            result.add(updated);
        }
        if (!nonExtractableArguments.isEmpty()) {
            result.add(factory.generateLogicalExpression(updatedCaller, "or", nonExtractableArguments));
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) AbstractFunctionExpression(org.apache.atlas.groovy.AbstractFunctionExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 15 with FunctionCallExpression

use of org.apache.atlas.groovy.FunctionCallExpression in project incubator-atlas by apache.

the class FunctionGenerator method preVisitFunctionCaller.

@Override
public boolean preVisitFunctionCaller(AbstractFunctionExpression expr) {
    depth++;
    if (IsOr.INSTANCE.apply(expr)) {
        FunctionCallExpression functionCall = (FunctionCallExpression) expr;
        scaleFactor *= functionCall.getArguments().size();
    }
    if (newRootExpression == null) {
        newRootExpression = expr;
    }
    return true;
}
Also used : FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Aggregations

FunctionCallExpression (org.apache.atlas.groovy.FunctionCallExpression)42 GroovyExpression (org.apache.atlas.groovy.GroovyExpression)34 LiteralExpression (org.apache.atlas.groovy.LiteralExpression)15 ClosureExpression (org.apache.atlas.groovy.ClosureExpression)14 CastExpression (org.apache.atlas.groovy.CastExpression)5 TypeCoersionExpression (org.apache.atlas.groovy.TypeCoersionExpression)5 AbstractFunctionExpression (org.apache.atlas.groovy.AbstractFunctionExpression)4 ComparisonExpression (org.apache.atlas.groovy.ComparisonExpression)4 FieldExpression (org.apache.atlas.groovy.FieldExpression)4 IdentifierExpression (org.apache.atlas.groovy.IdentifierExpression)4 TernaryOperatorExpression (org.apache.atlas.groovy.TernaryOperatorExpression)4 Test (org.testng.annotations.Test)4 ArrayList (java.util.ArrayList)3 ComparisonOperatorExpression (org.apache.atlas.groovy.ComparisonOperatorExpression)2 LogicalExpression (org.apache.atlas.groovy.LogicalExpression)2 AttributeInfo (org.apache.atlas.typesystem.types.AttributeInfo)2 IDataType (org.apache.atlas.typesystem.types.IDataType)2 VariableDeclaration (org.apache.atlas.groovy.ClosureExpression.VariableDeclaration)1 ListExpression (org.apache.atlas.groovy.ListExpression)1 TraversalStepType (org.apache.atlas.groovy.TraversalStepType)1