Search in sources :

Example 11 with IdentityStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep in project sqlg by pietermartin.

the class BaseStrategy method handleHasSteps.

protected void handleHasSteps(ListIterator<Step<?, ?>> iterator, int pathCount) {
    // Collect the hasSteps
    int countToGoPrevious = 0;
    while (iterator.hasNext()) {
        Step<?, ?> currentStep = iterator.next();
        countToGoPrevious++;
        String notNullKey;
        String nullKey;
        if (currentStep instanceof HasContainerHolder) {
            HasContainerHolder hasContainerHolder = (HasContainerHolder) currentStep;
            List<HasContainer> hasContainers = hasContainerHolder.getHasContainers();
            List<HasContainer> toRemoveHasContainers = new ArrayList<>();
            if (isNotWithMultipleColumnValue(hasContainerHolder)) {
                toRemoveHasContainers.addAll(optimizeLabelHas(this.currentReplacedStep, hasContainers));
                // important to do optimizeIdHas after optimizeLabelHas as it might add its labels to the previous labelHasContainers labels.
                // i.e. for neq and without 'or' logic
                toRemoveHasContainers.addAll(optimizeIdHas(this.currentReplacedStep, hasContainers));
                toRemoveHasContainers.addAll(optimizeHas(this.currentReplacedStep, hasContainers));
                toRemoveHasContainers.addAll(optimizeWithInOut(this.currentReplacedStep, hasContainers));
                toRemoveHasContainers.addAll(optimizeBetween(this.currentReplacedStep, hasContainers));
                toRemoveHasContainers.addAll(optimizeInside(this.currentReplacedStep, hasContainers));
                toRemoveHasContainers.addAll(optimizeOutside(this.currentReplacedStep, hasContainers));
                toRemoveHasContainers.addAll(optimizeTextContains(this.currentReplacedStep, hasContainers));
                if (toRemoveHasContainers.size() == hasContainers.size()) {
                    if (!currentStep.getLabels().isEmpty()) {
                        final IdentityStep identityStep = new IdentityStep<>(this.traversal);
                        currentStep.getLabels().forEach(label -> this.currentReplacedStep.addLabel(pathCount + BaseStrategy.PATH_LABEL_SUFFIX + label));
                        TraversalHelper.insertAfterStep(identityStep, currentStep, this.traversal);
                    }
                    if (this.traversal.getSteps().contains(currentStep)) {
                        this.traversal.removeStep(currentStep);
                    }
                    iterator.remove();
                    countToGoPrevious--;
                }
            }
        } else if ((notNullKey = isNotNullStep(currentStep)) != null) {
            this.currentReplacedStep.addHasContainer(new HasContainer(notNullKey, new P<>(Existence.NOTNULL, null)));
            if (!currentStep.getLabels().isEmpty()) {
                final IdentityStep identityStep = new IdentityStep<>(this.traversal);
                currentStep.getLabels().forEach(label -> this.currentReplacedStep.addLabel(pathCount + BaseStrategy.PATH_LABEL_SUFFIX + label));
                TraversalHelper.insertAfterStep(identityStep, currentStep, this.traversal);
            }
            if (this.traversal.getSteps().contains(currentStep)) {
                this.traversal.removeStep(currentStep);
            }
            iterator.remove();
            countToGoPrevious--;
        } else if ((nullKey = isNullStep(currentStep)) != null) {
            this.currentReplacedStep.addHasContainer(new HasContainer(nullKey, new P<>(Existence.NULL, null)));
            if (!currentStep.getLabels().isEmpty()) {
                final IdentityStep identityStep = new IdentityStep<>(this.traversal);
                currentStep.getLabels().forEach(label -> this.currentReplacedStep.addLabel(pathCount + BaseStrategy.PATH_LABEL_SUFFIX + label));
                TraversalHelper.insertAfterStep(identityStep, currentStep, this.traversal);
            }
            if (this.traversal.getSteps().contains(currentStep)) {
                this.traversal.removeStep(currentStep);
            }
            iterator.remove();
            countToGoPrevious--;
        } else if (currentStep instanceof IdentityStep) {
        // do nothing
        } else {
            for (int i = 0; i < countToGoPrevious; i++) {
                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) 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) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) HasContainerHolder(org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) AndOrHasContainer(org.umlg.sqlg.sql.parse.AndOrHasContainer)

Example 12 with IdentityStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep in project sqlg by pietermartin.

the class BaseStrategy method unoptimizableChooseStep.

boolean unoptimizableChooseStep(ChooseStep<?, ?, ?> chooseStep) {
    if (!this.chooseStepStack.isEmpty()) {
        return true;
    }
    List<? extends Traversal.Admin<?, ?>> traversalAdmins = chooseStep.getGlobalChildren();
    if (traversalAdmins.size() != 2) {
        return true;
    }
    Traversal.Admin<?, ?> predicate = chooseStep.getLocalChildren().get(0);
    List<Step> predicateSteps = new ArrayList<>(predicate.getSteps());
    if (!(predicate.getSteps().get(predicate.getSteps().size() - 1) instanceof HasNextStep)) {
        return true;
    }
    // Remove the HasNextStep
    predicateSteps.remove(predicate.getSteps().size() - 1);
    Traversal.Admin<?, ?> globalChildOne = chooseStep.getGlobalChildren().get(0);
    List<Step> globalChildOneSteps = new ArrayList<>(globalChildOne.getSteps());
    globalChildOneSteps.remove(globalChildOneSteps.size() - 1);
    Traversal.Admin<?, ?> globalChildTwo = chooseStep.getGlobalChildren().get(1);
    List<Step> globalChildTwoSteps = new ArrayList<>(globalChildTwo.getSteps());
    globalChildTwoSteps.remove(globalChildTwoSteps.size() - 1);
    boolean hasIdentity = globalChildOne.getSteps().stream().anyMatch(s -> s instanceof IdentityStep);
    if (!hasIdentity) {
        hasIdentity = globalChildTwo.getSteps().stream().anyMatch(s -> s instanceof IdentityStep);
        if (hasIdentity) {
            // Identity found check predicate and true are the same
            if (!predicateSteps.equals(globalChildOneSteps)) {
                return true;
            }
        } else {
            // Identity not found
            return true;
        }
    } else {
        // Identity found check predicate and true are the same
        if (!predicateSteps.equals(globalChildTwoSteps)) {
            return true;
        }
    }
    List<Step> localSteps = predicateSteps.stream().filter(p -> p.getClass().equals(LocalStep.class)).collect(Collectors.toList());
    if (!localSteps.isEmpty()) {
        return true;
    }
    List<Step> rangeGlobalSteps = predicateSteps.stream().filter(p -> p.getClass().equals(RangeGlobalStep.class)).collect(Collectors.toList());
    if (rangeGlobalSteps.size() > 1) {
        return true;
    }
    if (rangeGlobalSteps.size() > 0) {
        Step rangeGlobalStep = rangeGlobalSteps.get(0);
        // Only if the rangeGlobalStep is the last step can it be optimized
        if (predicateSteps.get(predicateSteps.size() - 1) != rangeGlobalStep) {
            return true;
        }
    }
    Traversal.Admin<?, ?> trueTraversal;
    Traversal.Admin<?, ?> a = globalChildOne;
    Traversal.Admin<?, ?> b = globalChildTwo;
    if (a.getSteps().stream().anyMatch(s -> s instanceof IdentityStep<?>)) {
        trueTraversal = b;
    } else {
        trueTraversal = a;
    }
    List<Step<?, ?>> trueTraversalSteps = new ArrayList(trueTraversal.getSteps());
    ListIterator<Step<?, ?>> trueTraversalStepsIterator = trueTraversalSteps.listIterator();
    while (trueTraversalStepsIterator.hasNext()) {
        Step internalChooseStep = trueTraversalStepsIterator.next();
        if (!(internalChooseStep instanceof VertexStep || internalChooseStep instanceof EdgeVertexStep || internalChooseStep instanceof EdgeOtherVertexStep || internalChooseStep instanceof ComputerAwareStep.EndStep || internalChooseStep instanceof ChooseStep || internalChooseStep instanceof HasStep || internalChooseStep instanceof OrderGlobalStep || internalChooseStep instanceof RangeGlobalStep)) {
            return true;
        }
    }
    List<Step> chooseSteps = globalChildTwo.getSteps().stream().filter(p -> p.getClass().equals(ChooseStep.class)).collect(Collectors.toList());
    for (Step step : chooseSteps) {
        if (unoptimizableChooseStep((ChooseStep<?, ?, ?>) step)) {
            return true;
        }
    }
    return false;
}
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) 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) ComputerAwareStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.ComputerAwareStep) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep)

Example 13 with IdentityStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep in project sqlg by pietermartin.

the class TestLocalVertexStepOptionalWithOrder method testOptionalWithOrder2.

@Test
public void testOptionalWithOrder2() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "order", 13);
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "order", 12);
    Vertex a3 = this.sqlgGraph.addVertex(T.label, "A", "order", 11);
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "order", 3);
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "order", 2);
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "order", 1);
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A").as("a").local(__.optional(__.outE().as("e").otherV().as("v"))).order().by("order");
    Assert.assertEquals(4, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(4, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
    Assert.assertTrue(traversal.getSteps().get(1) instanceof IdentityStep);
    Assert.assertTrue(traversal.getSteps().get(2) instanceof SqlgLocalStepBarrier);
    Assert.assertTrue(traversal.getSteps().get(3) instanceof OrderGlobalStep);
    SqlgLocalStepBarrier<?, ?> localStep = (SqlgLocalStepBarrier<?, ?>) traversal.getSteps().get(2);
    List<SqlgVertexStep> sqlgVertexSteps = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
    Assert.assertEquals(1, sqlgVertexSteps.size());
    Assert.assertEquals(5, vertices.size());
    Assert.assertEquals(b3, vertices.get(0));
    Assert.assertEquals(b2, vertices.get(1));
    Assert.assertEquals(b1, vertices.get(2));
    Assert.assertEquals(a3, vertices.get(3));
    Assert.assertEquals(a2, vertices.get(4));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) SqlgLocalStepBarrier(org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier) OrderGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Aggregations

IdentityStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep)13 ArrayList (java.util.ArrayList)5 HasStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep)5 OrderGlobalStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep)5 HasContainerHolder (org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder)4 NoOpBarrierStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.NoOpBarrierStep)4 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)4 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)4 SqlgGraphStep (org.umlg.sqlg.step.SqlgGraphStep)4 SqlgVertexStep (org.umlg.sqlg.step.SqlgVertexStep)4 Comparator (java.util.Comparator)3 List (java.util.List)3 LoopTraversal (org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal)3 ChooseStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep)3 LocalStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep)3 OptionalStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.OptionalStep)3 RepeatStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep)3 SackValueStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.SackValueStep)3 TreeSideEffectStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.TreeSideEffectStep)3 AbstractStep (org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep)3