Search in sources :

Example 31 with ReplacedStep

use of org.umlg.sqlg.sql.parse.ReplacedStep in project sqlg by pietermartin.

the class TestHasLabelAndId method testConsecutiveEqHasWithoutIdAndLabels.

@Test
public void testConsecutiveEqHasWithoutIdAndLabels() {
    Vertex a = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b = this.sqlgGraph.addVertex(T.label, "B");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex c = this.sqlgGraph.addVertex(T.label, "C");
    Vertex d = this.sqlgGraph.addVertex(T.label, "D");
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A", "B", "C", "D").has(T.id, P.without(b.id(), b2.id()));
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(3, vertices.size());
    Assert.assertTrue(vertices.contains(a) && vertices.contains(c) && vertices.contains(d));
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
    SqlgGraphStep sqlgGraphStep = (SqlgGraphStep) traversal.getSteps().get(0);
    Assert.assertEquals(1, sqlgGraphStep.getReplacedSteps().size());
    ReplacedStep replacedStep = (ReplacedStep) sqlgGraphStep.getReplacedSteps().get(0);
    // without's labels are merged into the previous labelHasContainers
    // this is because without has 'or' logic rather than 'and'
    Assert.assertEquals(1, replacedStep.getLabelHasContainers().size());
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 32 with ReplacedStep

use of org.umlg.sqlg.sql.parse.ReplacedStep in project sqlg by pietermartin.

the class TestHasLabelAndId method testInWithHasLabelAndHasWithoutId2.

@Test
public void testInWithHasLabelAndHasWithoutId2() {
    Vertex a = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b = this.sqlgGraph.addVertex(T.label, "B");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex c = this.sqlgGraph.addVertex(T.label, "C");
    Vertex d = this.sqlgGraph.addVertex(T.label, "D");
    b.addEdge("ab", a);
    b2.addEdge("ab", a);
    c.addEdge("ac", a);
    d.addEdge("ad", a);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A").in().hasLabel("B", "C", "D").has(T.id, P.without(b.id(), c.id()));
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(2, vertices.size());
    Assert.assertTrue(vertices.contains(b2) && vertices.contains(d));
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
    SqlgGraphStep sqlgGraphStep = (SqlgGraphStep) traversal.getSteps().get(0);
    Assert.assertEquals(2, sqlgGraphStep.getReplacedSteps().size());
    ReplacedStep replacedStep = (ReplacedStep) sqlgGraphStep.getReplacedSteps().get(0);
    Assert.assertEquals(1, replacedStep.getLabelHasContainers().size());
    replacedStep = (ReplacedStep) sqlgGraphStep.getReplacedSteps().get(1);
    Assert.assertEquals(1, replacedStep.getLabelHasContainers().size());
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 33 with ReplacedStep

use of org.umlg.sqlg.sql.parse.ReplacedStep in project sqlg by pietermartin.

the class BaseStrategy method handleOrderGlobalSteps.

protected void handleOrderGlobalSteps(ListIterator<Step<?, ?>> iterator, MutableInt pathCount) {
    // Collect the OrderGlobalSteps
    while (iterator.hasNext()) {
        Step<?, ?> step = iterator.next();
        if (step instanceof OrderGlobalStep) {
            if (optimizable((OrderGlobalStep) step)) {
                // add the label if any
                for (String label : step.getLabels()) {
                    this.currentReplacedStep.addLabel(pathCount.getValue() + BaseStrategy.PATH_LABEL_SUFFIX + label);
                }
                // The step might not be here. For instance if it was nested in a chooseStep where the chooseStep logic already removed the step.
                if (this.traversal.getSteps().contains(step)) {
                    this.traversal.removeStep(step);
                }
                iterator.previous();
                Step previousStep = iterator.previous();
                if (previousStep instanceof SelectOneStep) {
                    SelectOneStep selectOneStep = (SelectOneStep) previousStep;
                    String key = (String) selectOneStep.getScopeKeys().iterator().next();
                    this.currentReplacedStep.getSqlgComparatorHolder().setPrecedingSelectOneLabel(key);
                    List<Pair<Traversal.Admin<?, ?>, Comparator<?>>> comparators = ((OrderGlobalStep) step).getComparators();
                    // get the step for the label
                    Optional<ReplacedStep<?, ?>> labeledReplacedStep = this.sqlgStep.getReplacedSteps().stream().filter(r -> {
                        Set<String> labels = r.getLabels();
                        for (String label : labels) {
                            String stepLabel = SqlgUtil.originalLabel(label);
                            if (stepLabel.equals(key)) {
                                return true;
                            } else {
                                return false;
                            }
                        }
                        return false;
                    }).findAny();
                    Preconditions.checkState(labeledReplacedStep.isPresent());
                    ReplacedStep<?, ?> replacedStep = labeledReplacedStep.get();
                    replacedStep.getSqlgComparatorHolder().setComparators(comparators);
                    // add a label if the step does not yet have one and is not a leaf node
                    if (replacedStep.getLabels().isEmpty()) {
                        replacedStep.addLabel(pathCount.getValue() + BaseStrategy.PATH_LABEL_SUFFIX + BaseStrategy.SQLG_PATH_ORDER_RANGE_LABEL);
                    }
                } else if (previousStep instanceof OptionalStep) {
                    throw new RuntimeException("not yet implemented");
                } else if (previousStep instanceof ChooseStep) {
                    // The order applies to the current replaced step and the previous ChooseStep
                    List<Pair<Traversal.Admin<?, ?>, Comparator<?>>> comparators = ((OrderGlobalStep) step).getComparators();
                    this.currentReplacedStep.getSqlgComparatorHolder().setComparators(comparators);
                    // add a label if the step does not yet have one and is not a leaf node
                    if (this.currentReplacedStep.getLabels().isEmpty()) {
                        this.currentReplacedStep.addLabel(pathCount.getValue() + BaseStrategy.PATH_LABEL_SUFFIX + BaseStrategy.SQLG_PATH_ORDER_RANGE_LABEL);
                    }
                } else {
                    List<Pair<Traversal.Admin<?, ?>, Comparator<?>>> comparators = ((OrderGlobalStep) step).getComparators();
                    this.currentReplacedStep.getSqlgComparatorHolder().setComparators(comparators);
                    // add a label if the step does not yet have one and is not a leaf node
                    if (this.currentReplacedStep.getLabels().isEmpty()) {
                        this.currentReplacedStep.addLabel(pathCount.getValue() + BaseStrategy.PATH_LABEL_SUFFIX + BaseStrategy.SQLG_PATH_ORDER_RANGE_LABEL);
                    }
                }
                iterator.next();
                iterator.next();
            } else {
                return;
            }
        } else {
            // break on the first step that is not a OrderGlobalStep
            iterator.previous();
            break;
        }
    }
}
Also used : org.apache.tinkerpop.gremlin.process.traversal(org.apache.tinkerpop.gremlin.process.traversal) AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) java.util(java.util) MutableInt(org.apache.commons.lang3.mutable.MutableInt) SqlgUtil(org.umlg.sqlg.util.SqlgUtil) Graph(org.apache.tinkerpop.gremlin.structure.Graph) FullText(org.umlg.sqlg.predicate.FullText) ZonedDateTime(java.time.ZonedDateTime) Text(org.umlg.sqlg.predicate.Text) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) LoopTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) SqlgStep(org.umlg.sqlg.step.SqlgStep) SackValueStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.SackValueStep) BiPredicate(java.util.function.BiPredicate) ChooseStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep) OrP(org.apache.tinkerpop.gremlin.process.traversal.util.OrP) SqlgTraversalUtil(org.umlg.sqlg.util.SqlgTraversalUtil) Duration(java.time.Duration) OptionalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.OptionalStep) ComputerAwareStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.ComputerAwareStep) HasContainerHolder(org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder) Range(org.apache.commons.lang3.Range) EventStrategy(org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.EventStrategy) Period(java.time.Period) org.apache.tinkerpop.gremlin.process.traversal.step.map(org.apache.tinkerpop.gremlin.process.traversal.step.map) TraversalHelper(org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) ReplacedStepTree(org.umlg.sqlg.sql.parse.ReplacedStepTree) org.apache.tinkerpop.gremlin.process.traversal.step.filter(org.apache.tinkerpop.gremlin.process.traversal.step.filter) Predicate(java.util.function.Predicate) RepeatStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep) T(org.apache.tinkerpop.gremlin.structure.T) Collectors(java.util.stream.Collectors) Pair(org.javatuples.Pair) SqlgLocalStepBarrier(org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) AbstractStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep) TreeSideEffectStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.TreeSideEffectStep) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) AndOrHasContainer(org.umlg.sqlg.sql.parse.AndOrHasContainer) Existence(org.umlg.sqlg.predicate.Existence) Preconditions(com.google.common.base.Preconditions) ConnectiveP(org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) OptionalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.OptionalStep) LoopTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) SqlgStep(org.umlg.sqlg.step.SqlgStep) SackValueStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.SackValueStep) ChooseStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep) OptionalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.OptionalStep) ComputerAwareStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.ComputerAwareStep) RepeatStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) AbstractStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep) TreeSideEffectStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.TreeSideEffectStep) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) ChooseStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep) Pair(org.javatuples.Pair)

Example 34 with ReplacedStep

use of org.umlg.sqlg.sql.parse.ReplacedStep in project sqlg by pietermartin.

the class BaseStrategy method handleRepeatStep.

private void handleRepeatStep(RepeatStep<?> repeatStep, MutableInt pathCount) {
    List<? extends Traversal.Admin<?, ?>> repeatTraversals = repeatStep.getGlobalChildren();
    Traversal.Admin admin = repeatTraversals.get(0);
    LoopTraversal loopTraversal;
    long numberOfLoops;
    loopTraversal = (LoopTraversal) repeatStep.getUntilTraversal();
    numberOfLoops = loopTraversal.getMaxLoops();
    for (int i = 0; i < numberOfLoops; i++) {
        ListIterator<Step<?, ?>> repeatStepIterator = admin.getSteps().listIterator();
        while (repeatStepIterator.hasNext()) {
            Step internalRepeatStep = repeatStepIterator.next();
            if (internalRepeatStep instanceof RepeatStep.RepeatEndStep) {
                break;
            } else if (internalRepeatStep instanceof VertexStep || internalRepeatStep instanceof EdgeVertexStep || internalRepeatStep instanceof EdgeOtherVertexStep) {
                ReplacedStep<?, ?> replacedStepToEmit;
                // i.e. the currentReplacedStep before running handleVertexStep needs to be emitted.
                if (repeatStep.emitFirst) {
                    replacedStepToEmit = this.currentReplacedStep;
                    pathCount.decrement();
                    // noinspection ConstantConditions
                    replacedStepToEmit.setEmit(repeatStep.getEmitTraversal() != null);
                    replacedStepToEmit.setUntilFirst(repeatStep.untilFirst);
                    if (repeatStep.getLabels().isEmpty()) {
                        replacedStepToEmit.addLabel(pathCount + BaseStrategy.EMIT_LABEL_SUFFIX + BaseStrategy.SQLG_PATH_FAKE_LABEL);
                    } else {
                        for (String label : repeatStep.getLabels()) {
                            replacedStepToEmit.addLabel(pathCount + BaseStrategy.EMIT_LABEL_SUFFIX + label);
                        }
                    }
                    pathCount.increment();
                }
                handleVertexStep(repeatStepIterator, (AbstractStep<?, ?>) internalRepeatStep, pathCount);
                pathCount.decrement();
                if (!repeatStep.emitFirst) {
                    replacedStepToEmit = this.currentReplacedStep;
                    // noinspection ConstantConditions
                    replacedStepToEmit.setEmit(repeatStep.getEmitTraversal() != null);
                    replacedStepToEmit.setUntilFirst(repeatStep.untilFirst);
                    if (repeatStep.getLabels().isEmpty()) {
                        replacedStepToEmit.addLabel(pathCount + BaseStrategy.EMIT_LABEL_SUFFIX + BaseStrategy.SQLG_PATH_FAKE_LABEL);
                    } else {
                        for (String label : repeatStep.getLabels()) {
                            replacedStepToEmit.addLabel(pathCount + BaseStrategy.EMIT_LABEL_SUFFIX + label);
                        }
                    }
                }
                pathCount.increment();
                // If there is an emit we can not continue the optimization.
                this.reset = repeatStep.getEmitTraversal() != null;
            } else {
                throw new IllegalStateException("Unhandled step nested in RepeatStep " + internalRepeatStep.getClass().getName());
            }
        }
    }
    this.traversal.removeStep(repeatStep);
}
Also used : ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) LoopTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) SqlgStep(org.umlg.sqlg.step.SqlgStep) SackValueStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.SackValueStep) ChooseStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep) OptionalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.OptionalStep) ComputerAwareStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.ComputerAwareStep) RepeatStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) AbstractStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep) TreeSideEffectStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.TreeSideEffectStep) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) AbstractStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) LoopTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal)

Aggregations

ReplacedStep (org.umlg.sqlg.sql.parse.ReplacedStep)34 SqlgGraphStep (org.umlg.sqlg.step.SqlgGraphStep)34 DefaultGraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal)29 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)29 Test (org.junit.Test)29 BaseTest (org.umlg.sqlg.test.BaseTest)29 LoopTraversal (org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal)4 ChooseStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep)4 LocalStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep)4 OptionalStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.OptionalStep)4 RepeatStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep)4 IdentityStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep)4 SackValueStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.SackValueStep)4 TreeSideEffectStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.TreeSideEffectStep)4 AbstractStep (org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep)4 ComputerAwareStep (org.apache.tinkerpop.gremlin.process.traversal.step.util.ComputerAwareStep)4 SqlgStep (org.umlg.sqlg.step.SqlgStep)4 SqlgVertexStep (org.umlg.sqlg.step.SqlgVertexStep)4 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)2 ReplacedStepTree (org.umlg.sqlg.sql.parse.ReplacedStepTree)2