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);
}
}
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);
}
}
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++;
}
}
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());
}
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());
}
Aggregations