org.umlg.sqlg.test.gremlincompile.TestGremlinCompileWithHas Maven / Gradle / Ivy
package org.umlg.sqlg.test.gremlincompile;
import org.apache.tinkerpop.gremlin.AbstractGremlinTest;
import org.apache.tinkerpop.gremlin.process.traversal.P;
import org.apache.tinkerpop.gremlin.process.traversal.Scope;
import org.apache.tinkerpop.gremlin.process.traversal.Step;
import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
import org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.StartStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.EmptyStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.MapHelper;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.io.GraphReader;
import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoIo;
import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoReader;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Test;
import org.umlg.sqlg.sql.parse.SchemaTableTree;
import org.umlg.sqlg.strategy.SqlgVertexStepCompiled;
import org.umlg.sqlg.structure.RecordId;
import org.umlg.sqlg.structure.SchemaManager;
import org.umlg.sqlg.structure.SchemaTable;
import org.umlg.sqlg.structure.SqlgVertex;
import org.umlg.sqlg.test.BaseTest;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;
/**
* Date: 2015/01/19
* Time: 6:22 AM
*/
public class TestGremlinCompileWithHas extends BaseTest {
@Test
public void testHasIdRecompilation() {
Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
Vertex a2 = this.sqlgGraph.addVertex(T.label, "A");
Vertex a3 = this.sqlgGraph.addVertex(T.label, "A");
this.sqlgGraph.tx().commit();
GraphTraversal gt1 = this.sqlgGraph.traversal().V(a1.id());
GraphTraversal gt2 = this.sqlgGraph.traversal().V().hasId(a1.id());
List vertices1 = gt1.toList();
Assert.assertEquals(1, vertices1.size());
Assert.assertEquals(a1, vertices1.get(0));
List vertices2 = gt2.toList();
Assert.assertEquals(1, vertices2.size());
Assert.assertEquals(a1, vertices2.get(0));
Assert.assertEquals(gt1, gt2);
}
@Test
public void testHasIdIn() {
Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
Vertex a2 = this.sqlgGraph.addVertex(T.label, "A");
Vertex a3 = this.sqlgGraph.addVertex(T.label, "A");
Vertex a4 = this.sqlgGraph.addVertex(T.label, "A");
Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
Vertex b3 = this.sqlgGraph.addVertex(T.label, "B");
Vertex b4 = this.sqlgGraph.addVertex(T.label, "B");
Vertex c1 = this.sqlgGraph.addVertex(T.label, "C");
Vertex c2 = this.sqlgGraph.addVertex(T.label, "C");
Vertex c3 = this.sqlgGraph.addVertex(T.label, "C");
Vertex c4 = this.sqlgGraph.addVertex(T.label, "C");
b1.addEdge("ab", a1);
b2.addEdge("ab", a1);
b3.addEdge("ab", a1);
b4.addEdge("ab", a1);
c1.addEdge("ac", a1);
c2.addEdge("ac", a1);
c3.addEdge("ac", a1);
c4.addEdge("ac", a1);
this.sqlgGraph.tx().commit();
long start = this.sqlgGraph.getSqlDialect().getPrimaryKeyStartValue();
RecordId recordIda1 = RecordId.from(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "A"), start + 0L);
RecordId recordIda2 = RecordId.from(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "A"), start + 1l);
RecordId recordIda3 = RecordId.from(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "A"), start + 2L);
RecordId recordIda4 = RecordId.from(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "A"), start + 3L);
RecordId recordIdb1 = RecordId.from(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "B"), start + 0L);
RecordId recordIdb2 = RecordId.from(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "B"), start + 1L);
RecordId recordIdb3 = RecordId.from(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "B"), start + 2L);
RecordId recordIdb4 = RecordId.from(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "B"), start + 3L);
RecordId recordIdc1 = RecordId.from(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "C"), start + 0L);
RecordId recordIdc2 = RecordId.from(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "C"), start + 1L);
RecordId recordIdc3 = RecordId.from(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "C"), start + 2L);
RecordId recordIdc4 = RecordId.from(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "C"), start + 3L);
List vertices = this.sqlgGraph.traversal().V(recordIda1).hasLabel("A").toList();
Assert.assertEquals(1, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1).has(T.id, P.within(recordIda2, recordIdb1)).toList();
Assert.assertEquals(3, vertices.size());
vertices = this.sqlgGraph.traversal().V().has(T.id, P.within(recordIda1, recordIda2, recordIdb1)).toList();
Assert.assertEquals(3, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1, recordIda2, recordIda3, recordIdb1).toList();
Assert.assertEquals(4, vertices.size());
vertices = this.sqlgGraph.traversal().V().has(T.id, P.within(recordIda1)).toList();
Assert.assertEquals(1, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1).in().hasId(recordIdb1).toList();
Assert.assertEquals(1, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1, recordIda2, recordIda3).in().hasId(recordIdb1, recordIdb2, recordIdb3).toList();
Assert.assertEquals(3, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1).in().hasId(recordIda1).toList();
Assert.assertEquals(0, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1).in().hasId(recordIdb1).toList();
Assert.assertEquals(1, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1).in().hasId(recordIdb1, recordIdb2).toList();
Assert.assertEquals(2, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1).in().hasId(recordIdb1.toString()).toList();
Assert.assertEquals(1, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1).in().hasId(recordIdb1.toString(), recordIdb2.toString()).toList();
Assert.assertEquals(2, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1).in().hasId(recordIdb1.toString(), recordIdc2.toString()).toList();
Assert.assertEquals(2, vertices.size());
}
@Test
public void testHasIdInJoin() {
Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
Vertex a2 = this.sqlgGraph.addVertex(T.label, "A");
Vertex a3 = this.sqlgGraph.addVertex(T.label, "A");
Vertex a4 = this.sqlgGraph.addVertex(T.label, "A");
Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
Vertex b3 = this.sqlgGraph.addVertex(T.label, "B");
Vertex b4 = this.sqlgGraph.addVertex(T.label, "B");
Vertex c1 = this.sqlgGraph.addVertex(T.label, "C");
Vertex c2 = this.sqlgGraph.addVertex(T.label, "C");
Vertex c3 = this.sqlgGraph.addVertex(T.label, "C");
Vertex c4 = this.sqlgGraph.addVertex(T.label, "C");
Vertex d1 = this.sqlgGraph.addVertex(T.label, "D");
Vertex d2 = this.sqlgGraph.addVertex(T.label, "D");
Vertex d3 = this.sqlgGraph.addVertex(T.label, "D");
Vertex d4 = this.sqlgGraph.addVertex(T.label, "D");
b1.addEdge("ab", a1);
b2.addEdge("ab", a2);
b3.addEdge("ab", a3);
b4.addEdge("ab", a4);
c1.addEdge("ac", b1);
c1.addEdge("ac", b2);
c1.addEdge("ac", b3);
c1.addEdge("ac", b4);
d1.addEdge("ac", c1);
d2.addEdge("ac", c2);
d3.addEdge("ac", c3);
d4.addEdge("ac", c4);
this.sqlgGraph.tx().commit();
long start = this.sqlgGraph.getSqlDialect().getPrimaryKeyStartValue();
RecordId recordIda1 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "A"), start + 0L);
RecordId recordIda2 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "A"), start + 1L);
RecordId recordIda3 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "A"), start + 2L);
RecordId recordIda4 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "A"), start + 3L);
RecordId recordIdb1 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "B"), start + 0L);
RecordId recordIdb2 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "B"), start + 1L);
RecordId recordIdb3 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "B"), start + 2L);
RecordId recordIdb4 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "B"), start + 3L);
RecordId recordIdc1 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "C"), start + 0L);
RecordId recordIdc2 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "C"), start + 1L);
RecordId recordIdc3 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "C"), start + 2L);
RecordId recordIdc4 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "C"), start + 3L);
List vertices = this.sqlgGraph.traversal().V(recordIda1, recordIda2, recordIda3, recordIda4).in().hasId(recordIdb1, recordIdb2, recordIdb3).toList();
Assert.assertEquals(3, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1.toString(), recordIda2.toString(), recordIda3.toString(), recordIda4.toString()).in()
.hasId(recordIdb1.toString(), recordIdb2.toString(), recordIdb3.toString()).toList();
Assert.assertEquals(3, vertices.size());
vertices = this.sqlgGraph.traversal().V(a1, a2, a3, a4).in()
.hasId(recordIdb1.toString(), recordIdb2.toString(), recordIdb3.toString()).toList();
Assert.assertEquals(3, vertices.size());
vertices = this.sqlgGraph.traversal().V(a1, a2, a3, a4).in()
.hasId(recordIdb1.toString(), recordIdb2.toString(), recordIdb3.toString()).toList();
Assert.assertEquals(3, vertices.size());
}
@Test
public void testHasIdOutJoin() {
Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
Vertex a2 = this.sqlgGraph.addVertex(T.label, "A");
Vertex a3 = this.sqlgGraph.addVertex(T.label, "A");
Vertex a4 = this.sqlgGraph.addVertex(T.label, "A");
Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
Vertex b3 = this.sqlgGraph.addVertex(T.label, "B");
Vertex b4 = this.sqlgGraph.addVertex(T.label, "B");
Vertex c1 = this.sqlgGraph.addVertex(T.label, "C");
Vertex c2 = this.sqlgGraph.addVertex(T.label, "C");
Vertex c3 = this.sqlgGraph.addVertex(T.label, "C");
Vertex c4 = this.sqlgGraph.addVertex(T.label, "C");
Vertex d1 = this.sqlgGraph.addVertex(T.label, "D");
Vertex d2 = this.sqlgGraph.addVertex(T.label, "D");
Vertex d3 = this.sqlgGraph.addVertex(T.label, "D");
Vertex d4 = this.sqlgGraph.addVertex(T.label, "D");
a1.addEdge("ab", b1);
a2.addEdge("ab", b2);
a3.addEdge("ab", b3);
a4.addEdge("ab", b4);
a1.addEdge("ac", c1);
a1.addEdge("ac", c2);
a1.addEdge("ac", c3);
a1.addEdge("ac", c4);
c1.addEdge("ac", d1);
c2.addEdge("ac", d2);
c3.addEdge("ac", d3);
c4.addEdge("ac", d4);
this.sqlgGraph.tx().commit();
long start = this.sqlgGraph.getSqlDialect().getPrimaryKeyStartValue();
RecordId recordIda1 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "A"), start + 0L);
RecordId recordIda2 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "A"), start + 1L);
RecordId recordIda3 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "A"), start + 2L);
RecordId recordIda4 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "A"), start + 3L);
RecordId recordIdb1 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "B"), start + 0L);
RecordId recordIdb2 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "B"), start + 1L);
RecordId recordIdb3 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "B"), start + 2L);
RecordId recordIdb4 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "B"), start + 3L);
RecordId recordIdc1 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "C"), start + 0L);
RecordId recordIdc2 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "C"), start + 1L);
RecordId recordIdc3 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "C"), start + 2L);
RecordId recordIdc4 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "C"), start + 3L);
List vertices = this.sqlgGraph.traversal().V(recordIda1, recordIda2, recordIda3, recordIda4).out().hasId(recordIdb1, recordIdb2, recordIdb3).toList();
Assert.assertEquals(3, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1.toString(), recordIda2.toString(), recordIda3.toString(), recordIda4.toString()).out()
.hasId(recordIdb1.toString(), recordIdb2.toString(), recordIdb3.toString()).toList();
Assert.assertEquals(3, vertices.size());
vertices = this.sqlgGraph.traversal().V(a1, a2, a3, a4).out()
.hasId(recordIdb1.toString(), recordIdb2.toString(), recordIdb3.toString()).toList();
Assert.assertEquals(3, vertices.size());
vertices = this.sqlgGraph.traversal().V(a1, a2, a3, a4).out()
.hasId(recordIdb1.toString(), recordIdb2.toString(), recordIdb3.toString()).toList();
Assert.assertEquals(3, vertices.size());
}
@Test
public void testHasIdOut() {
Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
Vertex a2 = this.sqlgGraph.addVertex(T.label, "A");
Vertex a3 = this.sqlgGraph.addVertex(T.label, "A");
Vertex a4 = this.sqlgGraph.addVertex(T.label, "A");
Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
Vertex b3 = this.sqlgGraph.addVertex(T.label, "B");
Vertex b4 = this.sqlgGraph.addVertex(T.label, "B");
Vertex c1 = this.sqlgGraph.addVertex(T.label, "C");
Vertex c2 = this.sqlgGraph.addVertex(T.label, "C");
Vertex c3 = this.sqlgGraph.addVertex(T.label, "C");
Vertex c4 = this.sqlgGraph.addVertex(T.label, "C");
a1.addEdge("ab", b1);
a1.addEdge("ab", b2);
a1.addEdge("ab", b3);
a1.addEdge("ab", b4);
a1.addEdge("ac", c1);
a1.addEdge("ac", c2);
a1.addEdge("ac", c3);
a1.addEdge("ac", c4);
this.sqlgGraph.tx().commit();
long start = this.sqlgGraph.getSqlDialect().getPrimaryKeyStartValue();
RecordId recordIda1 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "A"), start + 0L);
RecordId recordIda2 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "A"), start + 1L);
RecordId recordIda3 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "A"), start + 2L);
RecordId recordIda4 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "A"), start + 3L);
RecordId recordIdb1 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "B"), start + 0L);
RecordId recordIdb2 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "B"), start + 1L);
RecordId recordIdb3 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "B"), start + 2L);
RecordId recordIdb4 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "B"), start + 3L);
RecordId recordIdc1 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "C"), start + 0L);
RecordId recordIdc2 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "C"), start + 1L);
RecordId recordIdc3 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "C"), start + 2L);
RecordId recordIdc4 = RecordId.from(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "C"), start + 3L);
List vertices = this.sqlgGraph.traversal().V(recordIda1).hasLabel("A").toList();
Assert.assertEquals(1, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1).has(T.id, P.within(recordIda2, recordIdb1)).toList();
Assert.assertEquals(3, vertices.size());
vertices = this.sqlgGraph.traversal().V().has(T.id, P.within(recordIda1, recordIda2, recordIdb1)).toList();
Assert.assertEquals(3, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1, recordIda2, recordIda3, recordIdb1).toList();
Assert.assertEquals(4, vertices.size());
vertices = this.sqlgGraph.traversal().V().has(T.id, P.within(recordIda1)).toList();
Assert.assertEquals(1, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1).out().hasId(recordIdb1).toList();
Assert.assertEquals(1, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1, recordIda2, recordIda3).out().hasId(recordIdb1, recordIdb2, recordIdb3).toList();
Assert.assertEquals(3, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1).out().hasId(recordIda1).toList();
Assert.assertEquals(0, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1).out().hasId(recordIdb1).toList();
Assert.assertEquals(1, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1).out().hasId(recordIdb1, recordIdb2).toList();
Assert.assertEquals(2, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1).out().hasId(recordIdb1.toString()).toList();
Assert.assertEquals(1, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1).out().hasId(recordIdb1.toString(), recordIdb2.toString()).toList();
Assert.assertEquals(2, vertices.size());
vertices = this.sqlgGraph.traversal().V(recordIda1).out().hasId(recordIdb1.toString(), recordIdc2.toString()).toList();
Assert.assertEquals(2, vertices.size());
}
@Test
public void g_V_asXaX_both_asXbX_dedupXa_bX_byXlabelX_selectXa_bX() throws IOException {
Graph g = this.sqlgGraph;
final GraphReader reader = GryoReader.build()
.mapper(g.io(GryoIo.build()).mapper().create())
.create();
try (final InputStream stream = AbstractGremlinTest.class.getResourceAsStream("/tinkerpop-modern.kryo")) {
reader.readGraph(stream, g);
}
assertModernGraph(g, true, false);
Traversal> t = g.traversal().V().as("a").both().as("b").dedup("a", "b").by(T.label).select("a", "b");
printTraversalForm(t);
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy