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);
}
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;
}
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));
}
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;
}
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);
}
Aggregations