Search in sources :

Example 1 with OrP

use of org.apache.tinkerpop.gremlin.process.traversal.util.OrP in project unipop by unipop-graph.

the class JdbcPredicatesTranslator method handleConnectiveP.

private Condition handleConnectiveP(String key, ConnectiveP predicate) {
    List<P> predicates = predicate.getPredicates();
    List<Condition> queries = predicates.stream().map(p -> {
        if (p instanceof ConnectiveP)
            return handleConnectiveP(key, (ConnectiveP) p);
        Object pValue = p.getValue();
        BiPredicate pBiPredicate = p.getBiPredicate();
        return predicateToQuery(key, pValue, pBiPredicate);
    }).collect(Collectors.toList());
    Condition condition = queries.get(0);
    if (predicate instanceof AndP) {
        for (int i = 1; i < queries.size(); i++) {
            condition = condition.and(queries.get(i));
        }
    } else if (predicate instanceof OrP) {
        for (int i = 1; i < queries.size(); i++) {
            condition = condition.or(queries.get(i));
        }
    } else
        throw new IllegalArgumentException("Connective predicate not supported by unipop");
    return condition;
}
Also used : AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) ExistsP(org.unipop.process.predicate.ExistsP) OrP(org.apache.tinkerpop.gremlin.process.traversal.util.OrP) ConnectiveP(org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP) P(org.apache.tinkerpop.gremlin.process.traversal.P) Condition(org.jooq.Condition) AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) MultiDateFormat(org.unipop.util.MultiDateFormat) java.util(java.util) DSL(org.jooq.impl.DSL) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) ExistsP(org.unipop.process.predicate.ExistsP) DSL.field(org.jooq.impl.DSL.field) Field(org.jooq.Field) Contains(org.apache.tinkerpop.gremlin.process.traversal.Contains) Collectors(java.util.stream.Collectors) Condition(org.jooq.Condition) Compare(org.apache.tinkerpop.gremlin.process.traversal.Compare) BiPredicate(java.util.function.BiPredicate) OrP(org.apache.tinkerpop.gremlin.process.traversal.util.OrP) PredicatesTranslator(org.unipop.common.util.PredicatesTranslator) Date(org.unipop.process.predicate.Date) ConnectiveP(org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP) PredicatesHolder(org.unipop.query.predicates.PredicatesHolder) ParseException(java.text.ParseException) P(org.apache.tinkerpop.gremlin.process.traversal.P) Text(org.unipop.process.predicate.Text) AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) ConnectiveP(org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP) OrP(org.apache.tinkerpop.gremlin.process.traversal.util.OrP) BiPredicate(java.util.function.BiPredicate)

Example 2 with OrP

use of org.apache.tinkerpop.gremlin.process.traversal.util.OrP in project janusgraph by JanusGraph.

the class JanusGraphStepStrategyTest method generateTestParameters.

private static Stream<Arguments> generateTestParameters() {
    final StandardJanusGraph graph = (StandardJanusGraph) StorageSetup.getInMemoryGraph();
    final GraphTraversalSource g = graph.traversal();
    // create a basic schema so that order pushdown can be tested as this optimization requires a JanusGraph
    // transaction registered against a non-EmptyGraph
    final JanusGraphManagement mgmt = graph.openManagement();
    mgmt.makePropertyKey("name").dataType(String.class).make();
    mgmt.makePropertyKey("lang").dataType(String.class).make();
    mgmt.commit();
    return Arrays.stream(new Arguments[] { arguments(g.V().out(), g_V().out(), Collections.emptyList()), arguments(g.V().has("name", "marko").out(), g_V("name", eq("marko")).out(), Collections.emptyList()), arguments(g.V().has("name", "marko").has("age", gt(31).and(lt(10))).out(), g_V("name", eq("marko"), "age", new AndP<Integer>(Arrays.asList(gt(31), lt(10)))).out(), Collections.emptyList()), arguments(g.V().has("name", "marko").has("age", gt(31).or(lt(10))).out(), g_V("name", eq("marko"), "age", new OrP<Integer>(Arrays.asList(gt(31), lt(10)))).out(), Collections.emptyList()), arguments(g.V().has("name", "marko").has("age", gt(31).and(lt(10).or(gt(40)))).out(), g_V("name", eq("marko"), "age", new OrP<Integer>(Arrays.asList(gt(31), new AndP<Integer>(Arrays.asList(lt(10), gt(40)))))).out(), Collections.emptyList()), arguments(g.V().has("name", "marko").or(has("age"), has("age", gt(32))).has("lang", "java"), g_V("name", eq("marko"), "lang", eq("java"), __.or(g_V("age", neq(null)), g_V("age", gt(32)))), Collections.singletonList(FilterRankingStrategy.instance())), arguments(g.V().has("name", "marko").as("a").or(has("age"), has("age", gt(32))).has("lang", "java"), g_V("name", eq("marko"), __.or(g_V("age", neq(null)), g_V("age", gt(32))), "lang", eq("java")).as("a"), Collections.emptyList()), arguments(g.V().has("name", "marko").as("a").or(has("age"), has("age", gt(32))).has("lang", "java"), g_V("name", eq("marko"), "lang", eq("java"), __.or(g_V("age", neq(null)), g_V("age", gt(32)))).as("a"), Collections.singletonList(FilterRankingStrategy.instance())), arguments(g.V().dedup().has("name", "marko").or(has("age"), has("age", gt(32))).has("lang", "java"), g_V("name", eq("marko"), "lang", eq("java"), __.or(g_V("age", neq(null)), g_V("age", gt(32)))).dedup(), Collections.singletonList(FilterRankingStrategy.instance())), arguments(g.V().as("a").dedup().has("name", "marko").or(has("age"), has("age", gt(32))).has("lang", "java"), g_V("name", eq("marko"), "lang", eq("java"), __.or(g_V("age", neq(null)), g_V("age", gt(32)))).dedup().as("a"), Collections.singletonList(FilterRankingStrategy.instance())), arguments(g.V().as("a").has("name", "marko").as("b").or(has("age"), has("age", gt(32))).has("lang", "java"), g_V("name", eq("marko"), "lang", eq("java"), __.or(g_V("age", neq(null)), g_V("age", gt(32)))).as("b", "a"), Collections.singletonList(FilterRankingStrategy.instance())), arguments(g.V().as("a").dedup().has("name", "marko").or(has("age"), has("age", gt(32))).filter(has("name", "bob")).has("lang", "java"), g_V("name", eq("marko"), "lang", eq("java"), "name", eq("bob"), __.or(g_V("age", neq(null)), g_V("age", gt(32)))).dedup().as("a"), Arrays.asList(InlineFilterStrategy.instance(), FilterRankingStrategy.instance())), arguments(g.V().has("name", "marko").or(not(has("age")), has("age", gt(32))).has("name", "bob").has("lang", "java"), g_V("name", eq("marko"), "name", eq("bob"), "lang", eq("java")).or(not(filter(properties("age"))), has("age", gt(32))), TraversalStrategies.GlobalCache.getStrategies(JanusGraph.class).toList()), arguments(g.V().has("name", eq("marko").and(eq("bob").and(eq("stephen")))).out("knows"), g_V("name", new AndP<String>(Arrays.asList(eq("marko"), eq("bob"), eq("stephen")))).out("knows"), Collections.emptyList()), arguments(g.V().hasId(1), g_V(T.id, 1), Collections.emptyList()), arguments(g.V().hasId(within(1, 2)), g_V(T.id, 1, T.id, 2), Collections.emptyList()), arguments(g.V().hasId(1).has("name", "marko"), g_V(T.id, 1, "name", eq("marko")), Collections.emptyList()), arguments(g.V().hasId(1).hasLabel("Person"), g_V(T.id, 1, "~label", eq("Person")), Collections.emptyList()), arguments(g.V().hasLabel("Person").has("lang", "java").order().by("name"), g_V("~label", eq("Person"), "lang", eq("java"), new HasStepFolder.OrderEntry("name", Order.asc)), Collections.emptyList()), arguments(g.V().hasLabel("Person").has("lang", "java").order().by(new ElementValueComparator("name", Order.asc)), g_V("~label", eq("Person"), "lang", eq("java"), new HasStepFolder.OrderEntry("name", Order.asc)), Collections.emptyList()), // same as above, different order
    arguments(g.V().hasLabel("Person").has("lang", "java").order().by("name", Order.desc), g_V("~label", eq("Person"), "lang", eq("java"), new HasStepFolder.OrderEntry("name", Order.desc)), Collections.emptyList()), arguments(g.V().hasLabel("Person").has("lang", "java").order().by(new ElementValueComparator("name", Order.asc)), g_V("~label", eq("Person"), "lang", eq("java"), new HasStepFolder.OrderEntry("name", Order.asc)), Collections.emptyList()), // if multiple order steps are specified in a row, only the last will be folded in because it overrides previous ordering
    arguments(g.V().hasLabel("Person").has("lang", "java").order().by("lang", Order.asc).order().by("name", Order.desc), g_V("~label", eq("Person"), "lang", eq("java"), new HasStepFolder.OrderEntry("name", Order.desc)), Collections.emptyList()), // do not folder in orders that include a nested traversal
    arguments(g.V().hasLabel("Person").order().by(values("age")), g_V("~label", eq("Person")).order().by(values("age")), Collections.emptyList()), // age property is not registered in the schema so the order should not be folded in
    arguments(g.V().hasLabel("Person").has("lang", "java").order().by("age"), g_V("~label", eq("Person"), "lang", eq("java")).order().by("age"), Collections.emptyList()), // into a single within(ids) lookup
    arguments(g.V().hasId(1).hasId(2), g_V(T.id, 1).hasId(2), Collections.emptyList()), arguments(g.V().has("name", "marko").range(10, 20), g_V("name", eq("marko"), __.range(10, 20)), Collections.emptyList()), arguments(g.V().has("name", "marko").or(has("length", lt(160)), has("age", gt(32))).has("lang", "java"), g_V("name", eq("marko"), "lang", eq("java"), __.or(g_V("length", lt(160)), g_V("age", gt(32)))), Collections.emptyList()), arguments(g.V().or(has("length", lt(160)), has("age", gt(32)).range(1, 5)), g_V(__.or(g_V("length", lt(160)), g_V("age", gt(32), __.range(1, 5)))), Collections.emptyList()), arguments(g.V().or(has("length", lt(160)), has("age", gt(32)).range(1, 5)).range(10, 20), g_V(__.or(g_V("length", lt(160)), g_V("age", gt(32), __.range(1, 5))), __.range(10, 20)), Collections.emptyList()), arguments(g.V().or(has("name", "marko"), has("lang", "java").order().by("name", Order.desc)), g_V(__.or(g_V("name", eq("marko")), g_V("lang", eq("java"), new HasStepFolder.OrderEntry("name", Order.desc)))), Collections.emptyList()), arguments(g.V().or(has("name", "marko"), has("lang", "java").order().by("name", Order.desc)).order().by("lang", Order.asc), g_V(__.or(g_V("name", eq("marko")), g_V("lang", eq("java"), new HasStepFolder.OrderEntry("name", Order.desc))), new HasStepFolder.OrderEntry("lang", Order.asc)), Collections.emptyList()), arguments(g.V().or(__.has("name", "marko").has("age", 29), __.has("name", "vadas").has("age", 27)).as("x").select("x"), g_V(__.or(g_V("name", eq("marko"), "age", eq(29)), g_V("name", eq("vadas"), "age", eq(27)))).as("x").select("x"), Collections.emptyList()) });
}
Also used : GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph) JanusGraph(org.janusgraph.core.JanusGraph) OrP(org.apache.tinkerpop.gremlin.process.traversal.util.OrP) ElementValueComparator(org.apache.tinkerpop.gremlin.process.traversal.step.util.ElementValueComparator) HasStepFolder(org.janusgraph.graphdb.tinkerpop.optimize.step.HasStepFolder) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph)

Example 3 with OrP

use of org.apache.tinkerpop.gremlin.process.traversal.util.OrP in project janusgraph by JanusGraph.

the class JanusGraphPSerializer method read.

@Override
public P read(Kryo kryo, Input input, Class<P> aClass) {
    final String predicate = input.readString();
    final boolean isCollection = input.readByte() == (byte) 0;
    final Object value;
    if (isCollection) {
        value = new ArrayList();
        final int size = input.readInt();
        for (int ix = 0; ix < size; ix++) {
            ((List) value).add(kryo.readClassAndObject(input));
        }
    } else {
        value = kryo.readClassAndObject(input);
    }
    try {
        if (value instanceof Collection) {
            switch(predicate) {
                case "and":
                    return new AndP((List<P>) value);
                case "or":
                    return new OrP((List<P>) value);
                case "between":
                    return P.between(((List) value).get(0), ((List) value).get(1));
                case "inside":
                    return P.inside(((List) value).get(0), ((List) value).get(1));
                case "outside":
                    return P.outside(((List) value).get(0), ((List) value).get(1));
                case "within":
                    return P.within((Collection) value);
                case "without":
                    return P.without((Collection) value);
                default:
                    return (P) P.class.getMethod(predicate, Collection.class).invoke(null, (Collection) value);
            }
        } else {
            switch(predicate) {
                case "geoIntersect":
                    return Geo.geoIntersect(value);
                case "geoDisjoint":
                    return Geo.geoDisjoint(value);
                case "geoWithin":
                    return Geo.geoWithin(value);
                case "geoContains":
                    return Geo.geoContains(value);
                case "textContains":
                    return Text.textContains(value);
                case "textContainsFuzzy":
                    return Text.textContainsFuzzy(value);
                case "textContainsPrefix":
                    return Text.textContainsPrefix(value);
                case "textContainsRegex":
                    return Text.textContainsRegex(value);
                case "textFuzzy":
                    return Text.textFuzzy(value);
                case "textPrefix":
                    return Text.textPrefix(value);
                case "textRegex":
                    return Text.textRegex(value);
                default:
                    return (P) P.class.getMethod(predicate, Object.class).invoke(null, value);
            }
        }
    } catch (final Exception e) {
        log.info("Couldn't deserialize class: " + aClass + ", predicate: " + predicate + ", isCollection: " + isCollection + ",value: " + value, e);
        throw new IllegalStateException(e.getMessage(), e);
    }
}
Also used : AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) OrP(org.apache.tinkerpop.gremlin.process.traversal.util.OrP) ConnectiveP(org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP) P(org.apache.tinkerpop.gremlin.process.traversal.P) AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) ArrayList(java.util.ArrayList) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) OrP(org.apache.tinkerpop.gremlin.process.traversal.util.OrP)

Example 4 with OrP

use of org.apache.tinkerpop.gremlin.process.traversal.util.OrP in project sqlg by pietermartin.

the class BaseStrategy method handleConnectiveStepInternal.

private Optional<AndOrHasContainer> handleConnectiveStepInternal(ConnectiveStep connectiveStep) {
    AndOrHasContainer.TYPE type = AndOrHasContainer.TYPE.from(connectiveStep);
    AndOrHasContainer outerAndOrHasContainer = new AndOrHasContainer(type);
    List<Traversal.Admin<?, ?>> localTraversals = connectiveStep.getLocalChildren();
    for (Traversal.Admin<?, ?> localTraversal : localTraversals) {
        if (!TraversalHelper.hasAllStepsOfClass(localTraversal, HasStep.class, ConnectiveStep.class)) {
            return Optional.empty();
        }
        AndOrHasContainer andOrHasContainer = new AndOrHasContainer(AndOrHasContainer.TYPE.NONE);
        outerAndOrHasContainer.addAndOrHasContainer(andOrHasContainer);
        for (Step<?, ?> step : localTraversal.getSteps()) {
            if (step instanceof HasStep) {
                HasStep<?> hasStep = (HasStep) step;
                for (HasContainer hasContainer : hasStep.getHasContainers()) {
                    if (hasContainerKeyNotIdOrLabel(hasContainer) && SUPPORTED_BI_PREDICATE.contains(hasContainer.getBiPredicate())) {
                        andOrHasContainer.addHasContainer(hasContainer);
                    } else if (hasContainerKeyNotIdOrLabel(hasContainer) && hasContainer.getPredicate() instanceof AndP) {
                        AndP<?> andP = (AndP) hasContainer.getPredicate();
                        List<? extends P<?>> predicates = andP.getPredicates();
                        if (predicates.size() == 2) {
                            if (predicates.get(0).getBiPredicate() == Compare.gte && predicates.get(1).getBiPredicate() == Compare.lt) {
                                andOrHasContainer.addHasContainer(hasContainer);
                            } else if (predicates.get(0).getBiPredicate() == Compare.gt && predicates.get(1).getBiPredicate() == Compare.lt) {
                                andOrHasContainer.addHasContainer(hasContainer);
                            }
                        }
                    } else if (hasContainerKeyNotIdOrLabel(hasContainer) && hasContainer.getPredicate() instanceof OrP) {
                        OrP<?> orP = (OrP) hasContainer.getPredicate();
                        List<? extends P<?>> predicates = orP.getPredicates();
                        if (predicates.size() == 2) {
                            if (predicates.get(0).getBiPredicate() == Compare.lt && predicates.get(1).getBiPredicate() == Compare.gt) {
                                andOrHasContainer.addHasContainer(hasContainer);
                            }
                        }
                    } else if (hasContainerKeyNotIdOrLabel(hasContainer) && hasContainer.getBiPredicate() instanceof Text || hasContainer.getBiPredicate() instanceof FullText) {
                        andOrHasContainer.addHasContainer(hasContainer);
                    } else {
                        return Optional.empty();
                    }
                }
            } else {
                ConnectiveStep connectiveStepLocalChild = (ConnectiveStep) step;
                Optional<AndOrHasContainer> result = handleConnectiveStepInternal(connectiveStepLocalChild);
                if (result.isPresent()) {
                    andOrHasContainer.addAndOrHasContainer(result.get());
                } else {
                    return Optional.empty();
                }
            }
        }
    }
    return Optional.of(outerAndOrHasContainer);
}
Also used : AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) AndOrHasContainer(org.umlg.sqlg.sql.parse.AndOrHasContainer) LoopTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal) FullText(org.umlg.sqlg.predicate.FullText) Text(org.umlg.sqlg.predicate.Text) OrP(org.apache.tinkerpop.gremlin.process.traversal.util.OrP) FullText(org.umlg.sqlg.predicate.FullText) AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) OrP(org.apache.tinkerpop.gremlin.process.traversal.util.OrP) ConnectiveP(org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP) AndOrHasContainer(org.umlg.sqlg.sql.parse.AndOrHasContainer)

Example 5 with OrP

use of org.apache.tinkerpop.gremlin.process.traversal.util.OrP in project sqlg by pietermartin.

the class BaseStrategy method optimizeOutside.

private List<HasContainer> optimizeOutside(ReplacedStep<?, ?> replacedStep, List<HasContainer> hasContainers) {
    List<HasContainer> result = new ArrayList<>();
    for (HasContainer hasContainer : hasContainers) {
        if (hasContainerKeyNotIdOrLabel(hasContainer) && hasContainer.getPredicate() instanceof OrP) {
            OrP<?> orP = (OrP) hasContainer.getPredicate();
            List<? extends P<?>> predicates = orP.getPredicates();
            if (predicates.size() == 2) {
                if (predicates.get(0).getBiPredicate() == Compare.lt && predicates.get(1).getBiPredicate() == Compare.gt) {
                    replacedStep.addHasContainer(hasContainer);
                    result.add(hasContainer);
                }
            }
        }
    }
    return result;
}
Also used : HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) AndOrHasContainer(org.umlg.sqlg.sql.parse.AndOrHasContainer) OrP(org.apache.tinkerpop.gremlin.process.traversal.util.OrP)

Aggregations

OrP (org.apache.tinkerpop.gremlin.process.traversal.util.OrP)5 AndP (org.apache.tinkerpop.gremlin.process.traversal.util.AndP)4 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)3 ConnectiveP (org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP)3 P (org.apache.tinkerpop.gremlin.process.traversal.P)2 AndOrHasContainer (org.umlg.sqlg.sql.parse.AndOrHasContainer)2 ParseException (java.text.ParseException)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 BiPredicate (java.util.function.BiPredicate)1 Collectors (java.util.stream.Collectors)1 Compare (org.apache.tinkerpop.gremlin.process.traversal.Compare)1 Contains (org.apache.tinkerpop.gremlin.process.traversal.Contains)1 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)1 LoopTraversal (org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal)1 ElementValueComparator (org.apache.tinkerpop.gremlin.process.traversal.step.util.ElementValueComparator)1 JanusGraph (org.janusgraph.core.JanusGraph)1 JanusGraphManagement (org.janusgraph.core.schema.JanusGraphManagement)1