Skip to content

Commit

Permalink
fix: gremlin wrong path optimization with count
Browse files Browse the repository at this point in the history
Fixed issue #1674
  • Loading branch information
lvca committed Jul 28, 2024
1 parent 73cae4c commit 8e7b013
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public void apply(final Traversal.Admin<?, ?> traversal) {
((HasStep<?>) step).removeHasContainer(c);

if (totalLabels == 1 && typeNameToMatch != null) {

// LOOKING FOR INDEX LOOKUP
final ArcadeGraph graph = (ArcadeGraph) traversal.getGraph().get();

Expand Down Expand Up @@ -124,7 +123,8 @@ else if (c.getBiPredicate().equals(Compare.lte))

final Step replaceWith;
if (indexCursors.isEmpty()) {
if (i + 1 < steps.size() && steps.get(i + 1) instanceof CountGlobalStep) {
if (((HasStep<?>) step).getHasContainers().isEmpty() &&
i + 1 < steps.size() && steps.get(i + 1) instanceof CountGlobalStep) {
traversal.removeStep(i - 1);
traversal.removeStep(i - 1);
replaceWith = new ArcadeCountGlobalStep(step.getTraversal(), prevStepGraph.getReturnClass(), typeNameToMatch);
Expand All @@ -136,9 +136,11 @@ else if (c.getBiPredicate().equals(Compare.lte))
replaceWith = new ArcadeFilterByIndexStep(prevStepGraph.getTraversal(), prevStepGraph.getReturnClass(),
prevStepGraph.isStartStep(), indexCursors);

//traversal.removeStep(i); // IF THE HAS-LABEL STEP IS REMOVED, FOR SOME REASON DOES NOT WORK
traversal.removeStep(i - 1);
traversal.addStep(i - 1, replaceWith);
if (replaceWith != null) {
//traversal.removeStep(i); // IF THE HAS-LABEL STEP IS REMOVED, FOR SOME REASON DOES NOT WORK
traversal.removeStep(i - 1);
traversal.addStep(i - 1, replaceWith);
}

if (replacedWithFilterByType)
break;
Expand Down
47 changes: 35 additions & 12 deletions gremlin/src/test/java/com/arcadedb/gremlin/GremlinTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public void testGremlinFromDatabase() {

@Test
public void testCypherSyntaxError() {
final ArcadeGraph graph = ArcadeGraph.open("./target/testcypher");
final ArcadeGraph graph = ArcadeGraph.open("./target/testgremlin");
try {

graph.getDatabase().getSchema().createVertexType("Person");
Expand All @@ -293,7 +293,7 @@ public void testCypherSyntaxError() {

@Test
public void testGremlinParse() {
final ArcadeGraph graph = ArcadeGraph.open("./target/testcypher");
final ArcadeGraph graph = ArcadeGraph.open("./target/testgremlin");
try {

final ArcadeGremlin gremlinReadOnly = graph.gremlin(
Expand All @@ -314,7 +314,7 @@ public void testGremlinParse() {

@Test
public void testGremlinLists() {
final ArcadeGraph graph = ArcadeGraph.open("./target/testlist");
final ArcadeGraph graph = ArcadeGraph.open("./target/testgremlin");
try {
final ResultSet result = graph.gremlin("g.addV('Person').property( 'list', ['a', 'b'] )").execute();

Expand All @@ -333,7 +333,7 @@ public void testGremlinLists() {

@Test
public void testUseIndex() {
final ArcadeGraph graph = ArcadeGraph.open("./target/testcypher");
final ArcadeGraph graph = ArcadeGraph.open("./target/testgremlin");
try {
graph.getDatabase().getSchema().getOrCreateVertexType("Person").getOrCreateProperty("id", Type.STRING)
.getOrCreateIndex(Schema.INDEX_TYPE.LSM_TREE, true);
Expand All @@ -353,7 +353,7 @@ public void testUseIndex() {

@Test
public void labelExists() {
final ArcadeGraph graph = ArcadeGraph.open("./target/testLabel");
final ArcadeGraph graph = ArcadeGraph.open("./target/testgremlin");
try {
graph.traversal().V().hasLabel("Car").forEachRemaining(System.out::println);
} finally {
Expand All @@ -365,7 +365,7 @@ public void labelExists() {
@Disabled
@Test
public void infinityValue() {
final ArcadeGraph graph = ArcadeGraph.open("./target/testInfinite");
final ArcadeGraph graph = ArcadeGraph.open("./target/testgremlin");
try {
final Vertex alice = graph.addVertex("person");
alice.property("hair", Double.POSITIVE_INFINITY);
Expand All @@ -386,7 +386,7 @@ public void infinityValue() {
// ISSUE: https://github.com/ArcadeData/arcadedb/issues/690
@Test
public void testVertexConstraints() {
final ArcadeGraph graph = ArcadeGraph.open("./target/testConstraints");
final ArcadeGraph graph = ArcadeGraph.open("./target/testgremlin");
try {
final VertexType type = graph.getDatabase().getSchema().getOrCreateVertexType("ChipID");
type.getOrCreateProperty("name", Type.STRING).setMandatory(true).setNotNull(true).setReadonly(true);
Expand All @@ -405,7 +405,7 @@ public void testVertexConstraints() {
// ISSUE: https://github.com/ArcadeData/arcadedb/issues/290
@Test
public void sort() {
final ArcadeGraph graph = ArcadeGraph.open("./target/testOrder");
final ArcadeGraph graph = ArcadeGraph.open("./target/testgremlin");
try {
graph.getDatabase().getSchema().getOrCreateVertexType("Person");
graph.getDatabase().getSchema().getOrCreateEdgeType("FriendOf");
Expand All @@ -431,7 +431,7 @@ public void sort() {
// ISSUE: https://github.com/ArcadeData/arcadedb/issues/911
@Test
public void testLongOverflow() {
final ArcadeGraph graph = ArcadeGraph.open("./target/testLongOverflow");
final ArcadeGraph graph = ArcadeGraph.open("./target/testgremlin");
try {
Result value = graph.gremlin("g.inject(Long.MAX_VALUE, 0).sum()").execute().nextIfAvailable();
Assertions.assertEquals(Long.MAX_VALUE, (long) value.getProperty("result"));
Expand All @@ -450,7 +450,7 @@ public void testLongOverflow() {
// ISSUE: https://github.com/ArcadeData/arcadedb/issues/912
@Test
public void testNumberConversion() {
final ArcadeGraph graph = ArcadeGraph.open("./target/testNumberConversion");
final ArcadeGraph graph = ArcadeGraph.open("./target/testgremlin");
try {
Result value = graph.gremlin("g.inject(1).size()").execute().nextIfAvailable();
Assertions.assertEquals(1, (int) value.getProperty("result"));
Expand All @@ -461,7 +461,7 @@ public void testNumberConversion() {

@Test
public void testGroupBy() {
final ArcadeGraph graph = ArcadeGraph.open("./target/testGroupBy");
final ArcadeGraph graph = ArcadeGraph.open("./target/testgremlin");
try {
graph.getDatabase().getSchema().getOrCreateVertexType("Person");
graph.getDatabase().getSchema().getOrCreateEdgeType("FriendOf");
Expand All @@ -487,7 +487,7 @@ public void testGroupBy() {
// Issue https://github.com/ArcadeData/arcadedb/issues/1301
@Test
public void testMerge() {
final ArcadeGraph graph = ArcadeGraph.open("./target/testMerge");
final ArcadeGraph graph = ArcadeGraph.open("./target/testgremlin");
try {
graph.database.command("sqlscript",//
"CREATE VERTEX TYPE TestMerge;" + //
Expand All @@ -502,6 +502,29 @@ public void testMerge() {
}
}

// https://github.com/ArcadeData/arcadedb/issues/1674
@Test
public void testBooleanProperties() {
final ArcadeGraph graph = ArcadeGraph.open("./target/testgremlin");
try {
graph.database.command("sqlscript",//
"CREATE VERTEX TYPE A;" + //
"CREATE PROPERTY A.b BOOLEAN;");

graph.gremlin("g.addV('A').property('b', true)").execute().nextIfAvailable();
graph.gremlin("g.addV('A').property('b', true)").execute().nextIfAvailable();
graph.gremlin("g.addV('A').property('b', false)").execute().nextIfAvailable();
graph.gremlin("g.addV('A')").execute().nextIfAvailable();
Assertions.assertEquals(4, graph.gremlin("g.V().hasLabel('A')").execute().toVertices().size());
Assertions.assertEquals(2, graph.gremlin("g.V().hasLabel('A').has('b',true)").execute().toVertices().size());
Assertions.assertEquals(2,
(Long) graph.gremlin("g.V().hasLabel('A').has('b',true).count()").execute().nextIfAvailable().getProperty("result"));

} finally {
graph.drop();
}
}

@BeforeEach
@AfterEach
public void clean() {
Expand Down

0 comments on commit 8e7b013

Please sign in to comment.