Search in sources :

Example 1 with ReplacedStep

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

the class BaseStrategy method handleChooseStep.

private void handleChooseStep(int chooseStepNestedCount, ChooseStep<?, ?, ?> chooseStep, Traversal.Admin<?, ?> traversal, MutableInt pathCount) {
    // The currentTreeNode here is the node that will need the left join in the sql generation
    this.chooseStepStack.add(this.currentTreeNodeNode);
    Preconditions.checkState(this.chooseStepStack.size() == chooseStepNestedCount);
    List<? extends Traversal.Admin<?, ?>> globalChildren = chooseStep.getGlobalChildren();
    Preconditions.checkState(globalChildren.size() == 2, "ChooseStep's globalChildren must have size 2, one for true and one for false");
    ReplacedStep<?, ?> previousReplacedStep = this.sqlgStep.getReplacedSteps().get(this.sqlgStep.getReplacedSteps().size() - 1);
    previousReplacedStep.setLeftJoin(true);
    Traversal.Admin<?, ?> trueTraversal;
    Traversal.Admin<?, ?> a = globalChildren.get(0);
    Traversal.Admin<?, ?> b = globalChildren.get(1);
    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) {
            handleVertexStep(trueTraversalStepsIterator, (AbstractStep<?, ?>) internalChooseStep, pathCount);
            // I.e. there was no results for the chooseSteps traversal.
            for (int i = chooseStepNestedCount; i < this.chooseStepStack.size(); i++) {
                ReplacedStepTree.TreeNode treeNode = this.chooseStepStack.get(i);
                this.currentReplacedStep.markAsJoinToLeftJoin();
                treeNode.addReplacedStep(this.currentReplacedStep);
            }
        } else if (internalChooseStep instanceof ChooseStep) {
            handleChooseStep(chooseStepNestedCount + 1, (ChooseStep) internalChooseStep, traversal, pathCount);
        } else if (internalChooseStep instanceof ComputerAwareStep.EndStep) {
            break;
        } else {
            throw new IllegalStateException("Unhandled step nested in ChooseStep " + internalChooseStep.getClass().getName());
        }
    }
    // In that case it will not be a direct step of the traversal.
    if (traversal.getSteps().contains(chooseStep)) {
        traversal.removeStep(chooseStep);
    }
}
Also used : 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) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) ReplacedStepTree(org.umlg.sqlg.sql.parse.ReplacedStepTree)

Example 2 with ReplacedStep

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

the class BaseStrategy method handleOptionalStep.

private void handleOptionalStep(int optionalStepNestedCount, OptionalStep<?> optionalStep, Traversal.Admin<?, ?> traversal, MutableInt pathCount) {
    // The currentTreeNode here is the node that will need the left join in the sql generation
    this.optionalStepStack.add(this.currentTreeNodeNode);
    Preconditions.checkState(this.optionalStepStack.size() == optionalStepNestedCount);
    Traversal.Admin<?, ?> optionalTraversal = optionalStep.getLocalChildren().get(0);
    ReplacedStep<?, ?> previousReplacedStep = this.sqlgStep.getReplacedSteps().get(this.sqlgStep.getReplacedSteps().size() - 1);
    previousReplacedStep.setLeftJoin(true);
    List<Step<?, ?>> optionalTraversalSteps = new ArrayList(optionalTraversal.getSteps());
    ListIterator<Step<?, ?>> optionalStepsIterator = optionalTraversalSteps.listIterator();
    while (optionalStepsIterator.hasNext()) {
        Step internalOptionalStep = optionalStepsIterator.next();
        if (internalOptionalStep instanceof VertexStep || internalOptionalStep instanceof EdgeVertexStep || internalOptionalStep instanceof EdgeOtherVertexStep) {
            handleVertexStep(optionalStepsIterator, (AbstractStep<?, ?>) internalOptionalStep, pathCount);
            // I.e. there was no results for the chooseSteps traversal.
            for (int i = optionalStepNestedCount; i < this.chooseStepStack.size(); i++) {
                ReplacedStepTree.TreeNode treeNode = this.chooseStepStack.get(i);
                this.currentReplacedStep.markAsJoinToLeftJoin();
                treeNode.addReplacedStep(this.currentReplacedStep);
            }
        } else if (internalOptionalStep instanceof OptionalStep) {
            handleOptionalStep(optionalStepNestedCount + 1, (OptionalStep) internalOptionalStep, traversal, pathCount);
        } else if (internalOptionalStep instanceof ComputerAwareStep.EndStep) {
            break;
        } else if (internalOptionalStep instanceof HasStep) {
            handleHasSteps(optionalStepsIterator, pathCount.getValue());
        } else {
            throw new IllegalStateException("Unhandled step nested in OptionalStep " + internalOptionalStep.getClass().getName());
        }
    }
    // In that case it will not be a direct step of the traversal.
    if (traversal.getSteps().contains(optionalStep)) {
        traversal.removeStep(optionalStep);
    }
}
Also used : 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) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) ReplacedStepTree(org.umlg.sqlg.sql.parse.ReplacedStepTree)

Example 3 with ReplacedStep

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

the class SqlgWhereStrategy method apply.

@SuppressWarnings("resource")
@Override
public void apply(Admin<?, ?> traversal) {
    if (!(traversal.getGraph().get() instanceof SqlgGraph)) {
        return;
    }
    SqlgGraph sqlgGraph = (SqlgGraph) traversal.getGraph().get();
    // The query will read from the cache if this is for a cached vertex
    if (sqlgGraph.features().supportsBatchMode() && sqlgGraph.tx().isInNormalBatchMode()) {
        sqlgGraph.tx().flush();
    }
    List<Step<?, ?>> steps = new ArrayList(traversal.asAdmin().getSteps());
    ListIterator<Step<?, ?>> stepIterator = steps.listIterator();
    // get all steps per label
    Map<String, Object> stepsByLabel = new HashMap<>();
    stepIterator = steps.listIterator();
    Step<?, ?> previous = null;
    int idx = 0;
    while (stepIterator.hasNext()) {
        Step<?, ?> step = stepIterator.next();
        captureLabels(step, stepsByLabel);
        if (step instanceof WherePredicateStep<?>) {
            WherePredicateStep<?> wps = (WherePredicateStep<?>) step;
            if (wps.getPredicate().isPresent() && wps.getPredicate().get().getBiPredicate() instanceof FullText) {
                Object referTo = previous;
                if (wps.getStartKey().isPresent()) {
                    referTo = stepsByLabel.get(wps.getStartKey().get());
                }
                if (referTo instanceof SqlgGraphStep<?, ?>) {
                    SqlgGraphStep<?, ?> sgs = (SqlgGraphStep<?, ?>) referTo;
                    if (sgs.getReplacedSteps().size() > 0) {
                        referTo = sgs.getReplacedSteps().get(sgs.getReplacedSteps().size() - 1);
                    }
                }
                if (referTo instanceof ReplacedStep<?, ?>) {
                    ReplacedStep<?, ?> rs = (ReplacedStep<?, ?>) referTo;
                    rs.addHasContainer(new HasContainer("__dummy__", wps.getPredicate().get()));
                    traversal.removeStep(idx);
                }
            }
        }
        previous = step;
        idx++;
    }
}
Also used : WherePredicateStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) WherePredicateStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) FullText(org.umlg.sqlg.predicate.FullText)

Example 4 with ReplacedStep

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

the class TestHasLabelAndId method testConsecutiveEqHasIdAndLabels.

@Test
public void testConsecutiveEqHasIdAndLabels() {
    Vertex a = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b = 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(a).hasLabel("B");
    Assert.assertEquals(0, traversal.toList().size());
    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);
    Assert.assertEquals(2, 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 5 with ReplacedStep

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

the class TestHasLabelAndId method testConsecutiveEqWithinHasLabels2.

@Test
public void testConsecutiveEqWithinHasLabels2() {
    Vertex a = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b = 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").has(T.label, P.within("B"));
    Assert.assertEquals(0, traversal.toList().size());
    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);
    Assert.assertEquals(2, 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)

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