org.umlg.sqlg.test.properties.TestPropertyValues Maven / Gradle / Ivy
package org.umlg.sqlg.test.properties;
import org.apache.tinkerpop.gremlin.process.traversal.Order;
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.DefaultGraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectOneStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.junit.Assert;
import org.junit.Test;
import org.umlg.sqlg.sql.parse.ReplacedStep;
import org.umlg.sqlg.step.SqlgGraphStep;
import org.umlg.sqlg.step.SqlgPropertiesStep;
import org.umlg.sqlg.step.SqlgVertexStep;
import org.umlg.sqlg.structure.SqlgElement;
import org.umlg.sqlg.test.BaseTest;
import java.util.*;
/**
* test behavior on property values
*
* @author JP Moresmau
* @author Pieter Martin (https://github.com/pietermartin)
*/
public class TestPropertyValues extends BaseTest {
@Test
public void testSelectFollowedByValues() {
Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2");
Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "b2");
Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "xxxx", "c1", "yyyy", "y1", "zzzz", "z1");
Vertex c2 = this.sqlgGraph.addVertex(T.label, "C", "xxxx", "c2", "yyyy", "y2", "zzzz", "z2");
Vertex d1 = this.sqlgGraph.addVertex(T.label, "D", "name", "d1");
Vertex d2 = this.sqlgGraph.addVertex(T.label, "D", "name", "d2");
a1.addEdge("ab", b1);
a2.addEdge("ab", b2);
b1.addEdge("bc", c1);
b2.addEdge("bc", c2);
c1.addEdge("cd", d1);
c2.addEdge("cd", d2);
this.sqlgGraph.tx().commit();
DefaultGraphTraversal traversal = (DefaultGraphTraversal)this.sqlgGraph.traversal().V()
.hasLabel("C")
.has("yyyy", "y1")
.as("c")
.in("bc")
.in("ab")
.has("name", "a1")
.as("a")
.select("a", "c")
.select("c")
.values("xxxx");
printTraversalForm(traversal);
Assert.assertEquals(6, 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 IdentityStep);
Assert.assertTrue(traversal.getSteps().get(3) instanceof SelectStep);
Assert.assertTrue(traversal.getSteps().get(4) instanceof SelectOneStep);
Assert.assertTrue(traversal.getSteps().get(5) instanceof SqlgPropertiesStep);
List names = traversal.toList();
Assert.assertEquals(1, names.size());
Assert.assertEquals("c1", names.get(0));
checkRestrictedProperties(SqlgGraphStep.class, traversal, 0, "xxxx");
}
@Test
public void testSelectOneFollowedByValues() {
Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2");
Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "b2");
Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "xxxx", "c1", "yyyy", "y1", "zzzz", "z1");
Vertex c2 = this.sqlgGraph.addVertex(T.label, "C", "xxxx", "c2", "yyyy", "y2", "zzzz", "z2");
Vertex d1 = this.sqlgGraph.addVertex(T.label, "D", "name", "d1");
Vertex d2 = this.sqlgGraph.addVertex(T.label, "D", "name", "d2");
a1.addEdge("ab", b1);
a2.addEdge("ab", b2);
b1.addEdge("bc", c1);
b2.addEdge("bc", c2);
c1.addEdge("cd", d1);
c2.addEdge("cd", d2);
this.sqlgGraph.tx().commit();
DefaultGraphTraversal traversal = (DefaultGraphTraversal)this.sqlgGraph.traversal().V()
.hasLabel("C")
.has("yyyy", "y1")
.as("c")
.in("bc")
.in("ab")
.has("name", "a1")
.select("c")
.values("xxxx");
printTraversalForm(traversal);
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 SelectOneStep);
Assert.assertTrue(traversal.getSteps().get(3) instanceof SqlgPropertiesStep);
List names = traversal.toList();
Assert.assertEquals(1, names.size());
Assert.assertEquals("c1", names.get(0));
checkRestrictedProperties(SqlgGraphStep.class, traversal, 0, "xxxx");
}
@Test
public void testOptimizePastSelect() {
Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2");
Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "b2");
Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "c1");
Vertex c2 = this.sqlgGraph.addVertex(T.label, "C", "name", "c2");
Vertex d1 = this.sqlgGraph.addVertex(T.label, "D", "name", "d1", "surname", "s1");
Vertex d2 = this.sqlgGraph.addVertex(T.label, "D", "name", "d2", "surname", "s2");
a1.addEdge("ab", b1);
a2.addEdge("ab", b2);
b1.addEdge("bc", c1);
b2.addEdge("bc", c2);
c1.addEdge("cd", d1);
c2.addEdge("cd", d2);
this.sqlgGraph.tx().commit();
Traversal traversal = this.sqlgGraph.traversal()
.V().hasLabel("C")
.has("name", "c2")
.as("c")
.in("bc")
.in("ab")
.has("name", "a2")
.select("c")
.out("cd")
.has("name", "d2")
.values("surname");
printTraversalForm(traversal);
List surnames = traversal.toList();
Assert.assertEquals(1, surnames.size());
Assert.assertEquals("s2", surnames.get(0));
checkRestrictedProperties(SqlgVertexStep.class, traversal, 0, "surname");
}
@Test
public void testMultipleSelect() {
Vertex vA = sqlgGraph.addVertex(T.label, "A", "name", "root");
Vertex vI = sqlgGraph.addVertex(T.label, "I", "name", "item1");
vA.addEdge("likes", vI, "howMuch", 5, "who", "Joe");
this.sqlgGraph.tx().commit();
Object id0 = vI.id();
GraphTraversal> gt = sqlgGraph.traversal().V()
.hasLabel("A")
.has("name", "root")
.outE("likes")
.as("e")
.values("howMuch").as("stars")
.select("e")
.values("who").as("user")
.select("e")
.inV()
.id().as("item")
.select("user", "stars", "item");
printTraversalForm(gt);
Assert.assertTrue(gt.hasNext());
Map m = gt.next();
Assert.assertEquals(new Integer(5), m.get("stars"));
Assert.assertEquals("Joe", m.get("user"));
Assert.assertEquals(id0, m.get("item"));
}
/**
* If the order() does not happen on the database, i.e. in java code then the property also needs to be present.
*/
@Test
public void testInMemoryOrderByValues() {
loadModern();
final Traversal traversal = this.sqlgGraph.traversal().V().both().hasLabel("person").order().by("age", Order.decr).limit(5).values("name");
printTraversalForm(traversal);
checkOrderedResults(Arrays.asList("peter", "josh", "josh", "josh", "marko"), traversal);
}
@Test
public void testValueMapOneObject() {
loadModern();
final Traversal> traversal = sqlgGraph.traversal().V().hasLabel("person").valueMap("name");
checkColumnsNotPresent(traversal, "age");
checkRestrictedProperties(SqlgGraphStep.class, traversal, 0, "name");
Set names = new HashSet<>();
while (traversal.hasNext()) {
Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy