Search in sources :

Example 11 with LiteralExpression

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

the class AbstractGremlinQueryOptimizerTest method testRangeWithNonZeroOffset.

@Test
public void testRangeWithNonZeroOffset() throws Exception {
    // g.V().or(has('__typeName','OMAS_OMRSAsset'),has('__superTypeNames','OMAS_OMRSAsset')).range(5,10).as('inst').select('inst')
    GroovyExpression toOptimize = getVerticesExpression();
    GroovyExpression expr0 = makeHasExpression("__typeName", "OMAS_OMRSAsset");
    GroovyExpression expr1 = makeHasExpression("__superTypeNames", "OMAS_OMRSAsset");
    toOptimize = getFactory().generateLogicalExpression(toOptimize, "or", Arrays.asList(expr0, expr1));
    toOptimize = getFactory().generateRangeExpression(toOptimize, 5, 10);
    toOptimize = getFactory().generateAliasExpression(toOptimize, "inst");
    toOptimize = getFactory().generateSelectExpression(toOptimize, Collections.singletonList(new LiteralExpression("inst")), Collections.<GroovyExpression>emptyList());
    RangeFinder visitor = new RangeFinder(getFactory());
    GremlinQueryOptimizer.visitCallHierarchy(toOptimize, visitor);
    List<AbstractFunctionExpression> rangeExpressions = visitor.getRangeExpressions();
    assertEquals(rangeExpressions.size(), 1);
    int[] rangeParameters = getFactory().getRangeParameters(rangeExpressions.get(0));
    assertNotNull(rangeParameters);
    GroovyExpression optimized = GremlinQueryOptimizer.getInstance().optimize(toOptimize);
    // The range optimization is not supported with a non-zero start index, so the optimizer should not add range expressions
    // to the expanded or's.
    assertEquals(optimized.toString(), getExpectedGremlinForTestRangeWithNonZeroOffset());
}
Also used : LiteralExpression(org.apache.atlas.groovy.LiteralExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) RangeFinder(org.apache.atlas.gremlin.optimizer.RangeFinder) AbstractFunctionExpression(org.apache.atlas.groovy.AbstractFunctionExpression) Test(org.testng.annotations.Test)

Example 12 with LiteralExpression

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

the class AbstractGremlinQueryOptimizerTest method testHasNotMovedToResult.

@Test
public void testHasNotMovedToResult() throws AtlasException {
    GroovyExpression toOptimize = getVerticesExpression();
    GroovyExpression or1Cond1 = makeHasExpression("p1", "e1");
    GroovyExpression or1Cond2 = makeHasExpression("p2", "e2");
    toOptimize = getFactory().generateLogicalExpression(toOptimize, "or", Arrays.asList(or1Cond1, or1Cond2));
    toOptimize = makeHasExpression(toOptimize, "p3", "e3");
    toOptimize = getFactory().generateAliasExpression(toOptimize, "_src");
    toOptimize = getFactory().generateSelectExpression(toOptimize, Collections.singletonList(new LiteralExpression("src1")), Collections.<GroovyExpression>singletonList(new IdentifierExpression("it")));
    GroovyExpression optimized = GremlinQueryOptimizer.getInstance().optimize(toOptimize);
    assertEquals(optimized.toString(), getExpectedGremlinForTestHasNotMovedToResult());
}
Also used : LiteralExpression(org.apache.atlas.groovy.LiteralExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) IdentifierExpression(org.apache.atlas.groovy.IdentifierExpression) Test(org.testng.annotations.Test)

Example 13 with LiteralExpression

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

the class Gremlin3ExpressionFactory method typeTestExpression.

@Override
public GroovyExpression typeTestExpression(GraphPersistenceStrategies s, String typeName, GroovyExpression itRef) {
    LiteralExpression superTypeAttrExpr = new LiteralExpression(s.superTypeAttributeName());
    LiteralExpression typeNameExpr = new LiteralExpression(typeName);
    LiteralExpression typeAttrExpr = new LiteralExpression(s.typeAttributeName());
    FunctionCallExpression result = new FunctionCallExpression(TraversalStepType.FILTER, HAS_METHOD, typeAttrExpr, new FunctionCallExpression(EQ_METHOD, typeNameExpr));
    result = new FunctionCallExpression(TraversalStepType.FILTER, result, "or");
    result = new FunctionCallExpression(TraversalStepType.FILTER, result, HAS_METHOD, superTypeAttrExpr, new FunctionCallExpression(EQ_METHOD, typeNameExpr));
    return result;
}
Also used : LiteralExpression(org.apache.atlas.groovy.LiteralExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 14 with LiteralExpression

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

the class Gremlin2ExpressionFactory method generateHasExpression.

@Override
public GroovyExpression generateHasExpression(GraphPersistenceStrategies s, GroovyExpression parent, String propertyName, String symbol, GroovyExpression requiredValue, FieldInfo fInfo) throws AtlasException {
    GroovyExpression op = gremlin2CompOp(symbol);
    GroovyExpression propertyNameExpr = new LiteralExpression(propertyName);
    return new FunctionCallExpression(TraversalStepType.FILTER, parent, HAS_METHOD, propertyNameExpr, op, requiredValue);
}
Also used : LiteralExpression(org.apache.atlas.groovy.LiteralExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 15 with LiteralExpression

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

the class Gremlin2ExpressionFactory method escapePropertyValue.

private GroovyExpression escapePropertyValue(GroovyExpression propertyValue) {
    GroovyExpression ret = propertyValue;
    if (propertyValue instanceof LiteralExpression) {
        LiteralExpression exp = (LiteralExpression) propertyValue;
        if (exp != null && exp.getValue() instanceof String) {
            String stringValue = (String) exp.getValue();
            // replace '*' with ".*", replace '?' with '.'
            stringValue = stringValue.replaceAll("\\*", ".*").replaceAll("\\?", ".");
            ret = new LiteralExpression(stringValue);
        }
    }
    return ret;
}
Also used : LiteralExpression(org.apache.atlas.groovy.LiteralExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression)

Aggregations

LiteralExpression (org.apache.atlas.groovy.LiteralExpression)22 GroovyExpression (org.apache.atlas.groovy.GroovyExpression)17 FunctionCallExpression (org.apache.atlas.groovy.FunctionCallExpression)15 ClosureExpression (org.apache.atlas.groovy.ClosureExpression)5 ComparisonExpression (org.apache.atlas.groovy.ComparisonExpression)3 IdentifierExpression (org.apache.atlas.groovy.IdentifierExpression)3 ArrayList (java.util.ArrayList)2 AbstractFunctionExpression (org.apache.atlas.groovy.AbstractFunctionExpression)2 CastExpression (org.apache.atlas.groovy.CastExpression)2 FieldExpression (org.apache.atlas.groovy.FieldExpression)2 LogicalExpression (org.apache.atlas.groovy.LogicalExpression)2 TernaryOperatorExpression (org.apache.atlas.groovy.TernaryOperatorExpression)2 AttributeInfo (org.apache.atlas.typesystem.types.AttributeInfo)2 IDataType (org.apache.atlas.typesystem.types.IDataType)2 Test (org.testng.annotations.Test)2 HashMap (java.util.HashMap)1 RangeFinder (org.apache.atlas.gremlin.optimizer.RangeFinder)1 ListExpression (org.apache.atlas.groovy.ListExpression)1 TypeCoersionExpression (org.apache.atlas.groovy.TypeCoersionExpression)1 AtlasGraph (org.apache.atlas.repository.graphdb.AtlasGraph)1