Search in sources :

Example 16 with GroovyExpression

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

the class ExpandOrsOptimization method getUpdatedChildren.

private UpdatedExpressions getUpdatedChildren(List<GroovyExpression> children, OptimizationContext context) {
    List<List<GroovyExpression>> updatedChildren = new ArrayList<>();
    boolean changed = false;
    for (GroovyExpression child : children) {
        List<GroovyExpression> childChoices = expandOrs(child, context);
        if (childChoices.size() != 1 || childChoices.iterator().next() != child) {
            changed = true;
        }
        updatedChildren.add(childChoices);
    }
    return new UpdatedExpressions(changed, updatedChildren);
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) GroovyExpression(org.apache.atlas.groovy.GroovyExpression)

Example 17 with GroovyExpression

use of org.apache.atlas.groovy.GroovyExpression 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 18 with GroovyExpression

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

the class Gremlin2ExpressionFactory method generateOrderByExpression.

@Override
public GroovyExpression generateOrderByExpression(GroovyExpression parent, List<GroovyExpression> translatedOrderBy, boolean isAscending) {
    GroovyExpression aPropertyExpr = translatedOrderBy.get(0);
    GroovyExpression bPropertyExpr = translatedOrderBy.get(1);
    GroovyExpression aPropertyNotNull = new ComparisonExpression(aPropertyExpr, ComparisonOperator.NOT_EQUALS, LiteralExpression.NULL);
    GroovyExpression bPropertyNotNull = new ComparisonExpression(bPropertyExpr, ComparisonOperator.NOT_EQUALS, LiteralExpression.NULL);
    GroovyExpression aCondition = new TernaryOperatorExpression(aPropertyNotNull, new FunctionCallExpression(aPropertyExpr, TO_LOWER_CASE_METHOD), aPropertyExpr);
    GroovyExpression bCondition = new TernaryOperatorExpression(bPropertyNotNull, new FunctionCallExpression(bPropertyExpr, TO_LOWER_CASE_METHOD), bPropertyExpr);
    GroovyExpression comparisonFunction = null;
    if (isAscending) {
        comparisonFunction = new ComparisonOperatorExpression(aCondition, bCondition);
    } else {
        comparisonFunction = new ComparisonOperatorExpression(bCondition, aCondition);
    }
    return new FunctionCallExpression(TraversalStepType.BARRIER, parent, ORDER_METHOD, new ClosureExpression(comparisonFunction));
}
Also used : ComparisonExpression(org.apache.atlas.groovy.ComparisonExpression) ComparisonOperatorExpression(org.apache.atlas.groovy.ComparisonOperatorExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) TernaryOperatorExpression(org.apache.atlas.groovy.TernaryOperatorExpression) ClosureExpression(org.apache.atlas.groovy.ClosureExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 19 with GroovyExpression

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

the class Gremlin2ExpressionFactory method generateGetSelectedValueExpression.

@Override
public GroovyExpression generateGetSelectedValueExpression(LiteralExpression key, GroovyExpression rowMap) {
    rowMap = new CastExpression(rowMap, "Row");
    GroovyExpression getExpr = new FunctionCallExpression(rowMap, "getColumn", key);
    return getExpr;
}
Also used : GroovyExpression(org.apache.atlas.groovy.GroovyExpression) CastExpression(org.apache.atlas.groovy.CastExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 20 with GroovyExpression

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

the class Gremlin2ExpressionFactory method typeTestExpression.

@Override
public GroovyExpression typeTestExpression(GraphPersistenceStrategies s, String typeName, GroovyExpression itRef) {
    GroovyExpression superTypeAttrExpr = new FieldExpression(itRef, s.superTypeAttributeName());
    GroovyExpression typeNameExpr = new LiteralExpression(typeName);
    GroovyExpression isSuperTypeExpr = new FunctionCallExpression(superTypeAttrExpr, CONTAINS, typeNameExpr);
    GroovyExpression superTypeMatchesExpr = new TernaryOperatorExpression(superTypeAttrExpr, isSuperTypeExpr, LiteralExpression.FALSE);
    GroovyExpression typeAttrExpr = new FieldExpression(itRef, s.typeAttributeName());
    GroovyExpression typeMatchesExpr = new ComparisonExpression(typeAttrExpr, ComparisonOperator.EQUALS, typeNameExpr);
    return new LogicalExpression(typeMatchesExpr, LogicalOperator.OR, superTypeMatchesExpr);
}
Also used : ComparisonExpression(org.apache.atlas.groovy.ComparisonExpression) LogicalExpression(org.apache.atlas.groovy.LogicalExpression) LiteralExpression(org.apache.atlas.groovy.LiteralExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) TernaryOperatorExpression(org.apache.atlas.groovy.TernaryOperatorExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression) FieldExpression(org.apache.atlas.groovy.FieldExpression)

Aggregations

GroovyExpression (org.apache.atlas.groovy.GroovyExpression)80 FunctionCallExpression (org.apache.atlas.groovy.FunctionCallExpression)34 Test (org.testng.annotations.Test)33 LiteralExpression (org.apache.atlas.groovy.LiteralExpression)17 ClosureExpression (org.apache.atlas.groovy.ClosureExpression)16 AbstractFunctionExpression (org.apache.atlas.groovy.AbstractFunctionExpression)11 ArrayList (java.util.ArrayList)9 IdentifierExpression (org.apache.atlas.groovy.IdentifierExpression)6 TypeCoersionExpression (org.apache.atlas.groovy.TypeCoersionExpression)6 CastExpression (org.apache.atlas.groovy.CastExpression)5 FieldExpression (org.apache.atlas.groovy.FieldExpression)5 ComparisonExpression (org.apache.atlas.groovy.ComparisonExpression)4 TernaryOperatorExpression (org.apache.atlas.groovy.TernaryOperatorExpression)4 ListExpression (org.apache.atlas.groovy.ListExpression)3 RangeFinder (org.apache.atlas.gremlin.optimizer.RangeFinder)2 ComparisonOperatorExpression (org.apache.atlas.groovy.ComparisonOperatorExpression)2 LogicalExpression (org.apache.atlas.groovy.LogicalExpression)2 StatementListExpression (org.apache.atlas.groovy.StatementListExpression)2 AttributeInfo (org.apache.atlas.typesystem.types.AttributeInfo)2 IDataType (org.apache.atlas.typesystem.types.IDataType)2